diff --git a/package-lock.json b/package-lock.json index e9deab93..280dcb97 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,14 +5,14 @@ "packages": { "": { "devDependencies": { - "prettier": "^3.1.0", + "prettier": "^3.2.4", "prettier-plugin-go-template": "^0.0.15" } }, "node_modules/prettier": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.1.0.tgz", - "integrity": "sha512-TQLvXjq5IAibjh8EpBIkNKxO749UEWABoiIZehEPiY4GNpVdhaFKqSTu+QrlU6D2dPAfubRmtJTi4K4YkQ5eXw==", + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.2.4.tgz", + "integrity": "sha512-FWu1oLHKCrtpO1ypU6J0SbK2d9Ckwysq6bHj/uaCP26DxrPpppCLQRGVuqAxSTvhF00AcvDRyYrLNW7ocBhFFQ==", "dev": true, "bin": { "prettier": "bin/prettier.cjs" diff --git a/package.json b/package.json index ab453ab0..63e73def 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "devDependencies": { - "prettier": "^3.1.0", + "prettier": "^3.2.4", "prettier-plugin-go-template": "^0.0.15" } } diff --git a/ui/charts.go b/ui/charts.go new file mode 100644 index 00000000..065556ec --- /dev/null +++ b/ui/charts.go @@ -0,0 +1,85 @@ +// Copyright (c) Abstract Machines +// SPDX-License-Identifier: Apache-2.0 + +package ui + +type Item struct { + Id int `json:"id"` + Title string `json:"title"` + Content string `json:"content"` + Image string `json:"image"` + Widget string `json:"widget"` +} + +func CreateItem() []Item { + items := []Item{ + { + Title: "Time Series Line Chart", + Content: "This is simple cartesian axis time series line chart", + Image: "https://www.jqueryscript.net/images/Simple-Canvas-Based-Line-Chart-Plugin-For-jQuery-Topup.jpg", + Widget: "lineChart", + }, + { + Title: "Time Series Bar Chart", + Content: "This is simple cartesian axis time series bar chart", + Image: "https://sigma-docs-screenshots.s3.us-west-2.amazonaws.com/Workbooks/Visualizations/Build+a+Bar+Chart/bar-chart.png", + Widget: "barChart", + }, + { + Title: "Simple Analogue Gauge", + Content: "This is a radial analogue gauge", + Image: "https://www.infragistics.com/products/indigo-design/help/images/radial_gauge_inside-2@2x.png", + Widget: "gauge", + }, + { + Title: "Pie Chart", + Content: "This is a pie chart", + Image: "https://earthly.dev/blog/generated/assets/images/stop-using-pie-charts/pie-chart-800-a835136f5.png", + Widget: "pieChart", + }, + { + Title: "Donut Chart", + Content: "This is a donut chart", + Image: "https://media.geeksforgeeks.org/wp-content/uploads/20230322231450/Rplot07.png", + Widget: "donut", + }, + { + Title: "Speed Gauge", + Content: "This is an analogue speed gauge", + Image: "https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/5/b/5b953b24910ffe51767d97af48d8d6c9125039a8.png", + Widget: "speedGauge", + }, + { + Title: "Range Chart", + Content: "This is a range chart", + Image: "https://storage.googleapis.com/studio_v_0_0_2/HVUMTI2S/_desktop_preview_1655159666002.png", + Widget: "stackedLineChart", + }, + { + Title: "Area Line Chart", + Content: "This is a stacked area line chart", + Image: "https://i.stack.imgur.com/vTgJy.gif", + Widget: "areaLineChart", + }, + { + Title: "Temperature Gauge", + Content: "This is an analogue temperature gauge", + Image: "https://storage.googleapis.com/support-forums-api/attachment/thread-7015968-5764838897040940014.png", + Widget: "tempGauge", + }, + { + Title: "Bar and Line Chart", + Content: "This is a dynamic data bar and line chart", + Image: "https://i.stack.imgur.com/tYgjo.png", + Widget: "dynamicDataChart", + }, + { + Title: "Horizontal Bar Chart", + Content: "This is a horizontal bar chart", + Image: "https://datavizproject.com/wp-content/uploads/types/Bar-Chart-Horizontal.png", + Widget: "horizontalBarChart", + }, + } + + return items +} diff --git a/ui/jsonObject.go b/ui/jsonObject.go deleted file mode 100644 index 2a700e22..00000000 --- a/ui/jsonObject.go +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) Abstract Machines -// SPDX-License-Identifier: Apache-2.0 - -// To be Deleted. This is simulating the charts that we are going to get from the backend. -package ui - -type Item struct { - Id int `json:"id"` - Title string `json:"title"` - Content string `json:"content"` - Image string `json:"image"` - Widget string `json:"widget"` -} - -func CreateItem() []Item { - items := []Item{ - { - Title: "Line Chart", - Content: "This is a small sentence about a line chart", - Image: "https://example.com/image1.jpg", - Widget: "lineChart", - }, - { - Title: "Line Chart", - Content: "This is a small sentence about a line chart", - Image: "https://example.com/image2.jpg", - Widget: "lineChart", - }, - { - Title: "Gauge", - Content: "This is a small sentence about a gauge", - Image: "https://example.com/image3.jpg", - Widget: "gauge", - }, - { - Title: "Line Chart", - Content: "This is a small sentence about a line chart", - Image: "https://example.com/image4.jpg", - Widget: "lineChart", - }, - { - Title: "Line Chart", - Content: "This is a small sentence about a line chart", - Image: "https://example.com/image5.jpg", - Widget: "lineChart", - }, - { - Title: "Gauge", - Content: "This is a small sentence about a gauge", - Image: "https://example.com/image6.jpg", - Widget: "gauge", - }, - { - Title: "Line Chart", - Content: "This is a small sentence about a line chart", - Image: "https://example.com/image7.jpg", - Widget: "lineChart", - }, - { - Title: "Line Chart", - Content: "This is a small sentence about a line chart", - Image: "https://example.com/image8.jpg", - Widget: "lineChart", - }, - { - Title: "Gauge", - Content: "This is a small sentence about a line chart", - Image: "https://example.com/image9.jpg", - Widget: "gauge", - }, - { - Title: "Line Chart", - Content: "This is a small sentence about a line chart", - Image: "https://example.com/image10.jpg", - Widget: "lineChart", - }, - } - - return items -} diff --git a/ui/service.go b/ui/service.go index 6e97a0fe..655a42a7 100644 --- a/ui/service.go +++ b/ui/service.go @@ -28,7 +28,8 @@ import ( ) const ( - templateDir = "ui/web/templates" + templatesDir = "ui/web/templates" + chartTemplatesDir = "ui/web/templates/charts" enabled = "enabled" statePending = "pending" statusAll = "all" @@ -125,11 +126,22 @@ var ( "invitations", "dashboard", + } - // Modals + chartTemplates = []string{ "linechartmodal", "gaugemodal", + "barchartmodal", + "piechartmodal", + "donutmodal", + "speedgaugemodal", + "tempgaugemodal", + "stackedlinechartmodal", + "arealinechartmodal", + "horizontalbarchartmodal", + "dynamicdatachartmodal", } + ErrToken = errors.New("failed to create token") ErrTokenRefresh = errors.New("failed to refresh token") ErrFailedCreate = errors.New("failed to create entity") @@ -2433,7 +2445,10 @@ func parseTemplates(mfsdk sdk.SDK, templates []string) (tpl *template.Template, var tmplFiles []string for _, value := range templates { - tmplFiles = append(tmplFiles, templateDir+"/"+value+".html") + tmplFiles = append(tmplFiles, templatesDir+"/"+value+".html") + } + for _, value := range chartTemplates { + tmplFiles = append(tmplFiles, chartTemplatesDir+"/"+value+".html") } tpl, err = tpl.ParseFiles(tmplFiles...) if err != nil { diff --git a/ui/web/static/js/dashboards.js b/ui/web/static/js/dashboards.js index 0f735a16..f6632ba5 100644 --- a/ui/web/static/js/dashboards.js +++ b/ui/web/static/js/dashboards.js @@ -20,7 +20,9 @@ function initGrid() { function saveLayout() { const itemData = grid.getItems().map((item) => ({ - innerHTML: item.getElement().innerHTML, + innerHTML: item._element.innerHTML, + widgetID: item._element.children[1].children[0].id, + widgetScript: item._element.children[2].innerHTML, })); const gridState = { @@ -63,6 +65,11 @@ function loadLayout(savedLayout) { const newItem = document.createElement("div"); newItem.className = "item"; newItem.innerHTML = itemData.innerHTML.trim(); + var scriptTag = document.createElement("script"); + scriptTag.type = "text/javascript"; + scriptTag.defer = true; + scriptTag.innerHTML = itemData.widgetScript; + newItem.appendChild(scriptTag); const item = grid.add(newItem); }); @@ -92,6 +99,11 @@ function editGrid(grid) { const newItem = document.createElement("div"); newItem.className = "item"; newItem.innerHTML = itemData.innerHTML.trim(); + var scriptTag = document.createElement("script"); + scriptTag.type = "text/javascript"; + scriptTag.defer = true; + scriptTag.innerHTML = itemData.widgetScript; + newItem.appendChild(scriptTag); const item = grid.add(newItem); }); grid.layout(gridState.layout); diff --git a/ui/web/templates/charts/arealinechartmodal.html b/ui/web/templates/charts/arealinechartmodal.html new file mode 100644 index 00000000..18dc5650 --- /dev/null +++ b/ui/web/templates/charts/arealinechartmodal.html @@ -0,0 +1,185 @@ + + +{{ define "arealinechartmodal" }} + + + +{{ end }} diff --git a/ui/web/templates/charts/barchartmodal.html b/ui/web/templates/charts/barchartmodal.html new file mode 100644 index 00000000..e1c2be93 --- /dev/null +++ b/ui/web/templates/charts/barchartmodal.html @@ -0,0 +1,126 @@ + + +{{ define "barchartmodal" }} + + + +{{ end }} diff --git a/ui/web/templates/charts/donutmodal.html b/ui/web/templates/charts/donutmodal.html new file mode 100644 index 00000000..5517e73f --- /dev/null +++ b/ui/web/templates/charts/donutmodal.html @@ -0,0 +1,149 @@ + + +{{ define "donutmodal" }} + + + +{{ end }} diff --git a/ui/web/templates/charts/dynamicdatachartmodal.html b/ui/web/templates/charts/dynamicdatachartmodal.html new file mode 100644 index 00000000..b45e302c --- /dev/null +++ b/ui/web/templates/charts/dynamicdatachartmodal.html @@ -0,0 +1,252 @@ + + +{{ define "dynamicdatachartmodal" }} + + + +{{ end }} diff --git a/ui/web/templates/gaugemodal.html b/ui/web/templates/charts/gaugemodal.html similarity index 79% rename from ui/web/templates/gaugemodal.html rename to ui/web/templates/charts/gaugemodal.html index 81c37c47..750f9ae0 100644 --- a/ui/web/templates/gaugemodal.html +++ b/ui/web/templates/charts/gaugemodal.html @@ -95,7 +95,38 @@ } } form.reset(); - addWidget(JSON.stringify(formData, "gauge")); + + var widgetID = "gauge-" + Date.now(); + var widgetScript = ` + var gaugeChart = echarts.init(document.getElementById("${widgetID}"), null, { + width: 300, + height: 300, + }); + var option = { + tooltip: { + formatter: "{a}
{b} : {c}%", + }, + series: [ + { + name: "", + type: "gauge", + progress: { + show: true, + }, + detail: { valueAnimation: true, formatter: "{value}" }, + data: [ + { + value: 55, + name: "Pressure", + }, + ], + }, + ], + }; + + gaugeChart.setOption(option);`; + + addWidget(widgetID, widgetScript); }); {{ end }} diff --git a/ui/web/templates/charts/horizontalbarchartmodal.html b/ui/web/templates/charts/horizontalbarchartmodal.html new file mode 100644 index 00000000..09664d38 --- /dev/null +++ b/ui/web/templates/charts/horizontalbarchartmodal.html @@ -0,0 +1,150 @@ + + +{{ define "horizontalbarchartmodal" }} + + + +{{ end }} diff --git a/ui/web/templates/linechartmodal.html b/ui/web/templates/charts/linechartmodal.html similarity index 82% rename from ui/web/templates/linechartmodal.html rename to ui/web/templates/charts/linechartmodal.html index dfcdda3a..9ba8fbcb 100644 --- a/ui/web/templates/linechartmodal.html +++ b/ui/web/templates/charts/linechartmodal.html @@ -95,7 +95,32 @@ } } form.reset(); - addWidget(JSON.stringify(formData)); + + var widgetID = "linechart-" + Date.now(); + var widgetScript = ` + var lineChart = echarts.init(document.getElementById("${widgetID}"), null, { + width: 500, + height: 200, + }); + var option = { + xAxis: { + type: 'category', + data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] + }, + yAxis: { + type: 'value' + }, + series: [ + { + data: [150, 230, 224, 218, 135, 147, 260], + type: 'line' + } + ] + }; + + lineChart.setOption(option);`; + + addWidget(widgetID, widgetScript); }); {{ end }} diff --git a/ui/web/templates/charts/piechartmodal.html b/ui/web/templates/charts/piechartmodal.html new file mode 100644 index 00000000..3a160c2c --- /dev/null +++ b/ui/web/templates/charts/piechartmodal.html @@ -0,0 +1,146 @@ + + +{{ define "piechartmodal" }} + + + +{{ end }} diff --git a/ui/web/templates/charts/speedgaugemodal.html b/ui/web/templates/charts/speedgaugemodal.html new file mode 100644 index 00000000..05fd0b1c --- /dev/null +++ b/ui/web/templates/charts/speedgaugemodal.html @@ -0,0 +1,177 @@ + + +{{ define "speedgaugemodal" }} + + + +{{ end }} diff --git a/ui/web/templates/charts/stackedlinechartmodal.html b/ui/web/templates/charts/stackedlinechartmodal.html new file mode 100644 index 00000000..451d796b --- /dev/null +++ b/ui/web/templates/charts/stackedlinechartmodal.html @@ -0,0 +1,158 @@ + + +{{ define "stackedlinechartmodal" }} + + + +{{ end }} diff --git a/ui/web/templates/charts/tempgaugemodal.html b/ui/web/templates/charts/tempgaugemodal.html new file mode 100644 index 00000000..06323d28 --- /dev/null +++ b/ui/web/templates/charts/tempgaugemodal.html @@ -0,0 +1,241 @@ + + +{{ define "tempgaugemodal" }} + + + +{{ end }} diff --git a/ui/web/templates/dashboard.html b/ui/web/templates/dashboard.html index 70d43472..d4e5d5a8 100644 --- a/ui/web/templates/dashboard.html +++ b/ui/web/templates/dashboard.html @@ -8,6 +8,12 @@ {{ template "header" }} Dashboard + {{ template "navbar" . }} @@ -82,8 +88,17 @@
Widgets
- {{ template "gaugemodal" . }} - {{ template "linechartmodal" . }} + {{ template "gaugemodal" }} + {{ template "linechartmodal" }} + {{ template "barchartmodal" }} + {{ template "piechartmodal" }} + {{ template "donutmodal" }} + {{ template "speedgaugemodal" }} + {{ template "tempgaugemodal" }} + {{ template "stackedlinechartmodal" }} + {{ template "arealinechartmodal" }} + {{ template "horizontalbarchartmodal" }} + {{ template "dynamicdatachartmodal" }} @@ -94,7 +109,7 @@
Widgets