Commit 5efe90e853b641c01845f50b486841d754f5fad7

Authored by Igor Kulikov
Committed by GitHub
2 parents ec2bbbc2 abc16e6b

Merge pull request #310 from Terny22/master

Issue 304
@@ -17,7 +17,14 @@ export default angular.module('thingsboard.api.dashboard', []) @@ -17,7 +17,14 @@ export default angular.module('thingsboard.api.dashboard', [])
17 .factory('dashboardService', DashboardService).name; 17 .factory('dashboardService', DashboardService).name;
18 18
19 /*@ngInject*/ 19 /*@ngInject*/
20 -function DashboardService($http, $q, $location, customerService) { 20 +function DashboardService($rootScope, $http, $q, $location, customerService) {
  21 +
  22 + var stDiffPromise;
  23 +
  24 + $rootScope.dadshboardServiceStateChangeStartHandle = $rootScope.$on('$stateChangeStart', function () {
  25 + stDiffPromise = undefined;
  26 + });
  27 +
21 28
22 var service = { 29 var service = {
23 assignDashboardToCustomer: assignDashboardToCustomer, 30 assignDashboardToCustomer: assignDashboardToCustomer,
@@ -113,18 +120,23 @@ function DashboardService($http, $q, $location, customerService) { @@ -113,18 +120,23 @@ function DashboardService($http, $q, $location, customerService) {
113 } 120 }
114 121
115 function getServerTimeDiff() { 122 function getServerTimeDiff() {
116 - var deferred = $q.defer();  
117 - var url = '/api/dashboard/serverTime';  
118 - var ct1 = Date.now();  
119 - $http.get(url, { ignoreLoading: true }).then(function success(response) {  
120 - var ct2 = Date.now();  
121 - var st = response.data;  
122 - var stDiff = Math.ceil(st - (ct1+ct2)/2);  
123 - deferred.resolve(stDiff);  
124 - }, function fail() {  
125 - deferred.reject();  
126 - });  
127 - return deferred.promise; 123 + if (stDiffPromise) {
  124 + return stDiffPromise;
  125 + } else {
  126 + var deferred = $q.defer();
  127 + stDiffPromise = deferred.promise;
  128 + var url = '/api/dashboard/serverTime';
  129 + var ct1 = Date.now();
  130 + $http.get(url, {ignoreLoading: true}).then(function success(response) {
  131 + var ct2 = Date.now();
  132 + var st = response.data;
  133 + var stDiff = Math.ceil(st - (ct1 + ct2) / 2);
  134 + deferred.resolve(stDiff);
  135 + }, function fail() {
  136 + deferred.reject();
  137 + });
  138 + }
  139 + return stDiffPromise;
128 } 140 }
129 141
130 function getDashboard(dashboardId) { 142 function getDashboard(dashboardId) {
@@ -80,9 +80,7 @@ export default class Subscription { @@ -80,9 +80,7 @@ export default class Subscription {
80 this.alarms = []; 80 this.alarms = [];
81 81
82 this.originalTimewindow = null; 82 this.originalTimewindow = null;
83 - this.timeWindow = {  
84 - stDiff: this.ctx.stDiff  
85 - } 83 + this.timeWindow = {};
86 this.useDashboardTimewindow = options.useDashboardTimewindow; 84 this.useDashboardTimewindow = options.useDashboardTimewindow;
87 85
88 if (this.useDashboardTimewindow) { 86 if (this.useDashboardTimewindow) {
@@ -124,9 +122,7 @@ export default class Subscription { @@ -124,9 +122,7 @@ export default class Subscription {
124 this.data = []; 122 this.data = [];
125 this.hiddenData = []; 123 this.hiddenData = [];
126 this.originalTimewindow = null; 124 this.originalTimewindow = null;
127 - this.timeWindow = {  
128 - stDiff: this.ctx.stDiff  
129 - } 125 + this.timeWindow = {};
130 this.useDashboardTimewindow = options.useDashboardTimewindow; 126 this.useDashboardTimewindow = options.useDashboardTimewindow;
131 this.stateData = options.stateData; 127 this.stateData = options.stateData;
132 if (this.useDashboardTimewindow) { 128 if (this.useDashboardTimewindow) {
@@ -213,27 +209,51 @@ export default class Subscription { @@ -213,27 +209,51 @@ export default class Subscription {
213 } 209 }
214 } 210 }
215 211
216 - initAlarmSubscription() { 212 + loadStDiff() {
217 var deferred = this.ctx.$q.defer(); 213 var deferred = this.ctx.$q.defer();
218 - if (!this.ctx.aliasController) {  
219 - this.configureAlarmsData();  
220 - deferred.resolve();  
221 - } else {  
222 - var subscription = this;  
223 - this.ctx.aliasController.resolveAlarmSource(this.alarmSource).then(  
224 - function success(alarmSource) {  
225 - subscription.alarmSource = alarmSource;  
226 - subscription.configureAlarmsData(); 214 + if (this.ctx.getStDiff && this.timeWindow) {
  215 + this.ctx.getStDiff().then(
  216 + (stDiff) => {
  217 + this.timeWindow.stDiff = stDiff;
227 deferred.resolve(); 218 deferred.resolve();
228 }, 219 },
229 - function fail() {  
230 - deferred.reject(); 220 + () => {
  221 + this.timeWindow.stDiff = 0;
  222 + deferred.resolve();
231 } 223 }
232 ); 224 );
  225 + } else {
  226 + if (this.timeWindow) {
  227 + this.timeWindow.stDiff = 0;
  228 + }
  229 + deferred.resolve();
233 } 230 }
234 return deferred.promise; 231 return deferred.promise;
235 } 232 }
236 233
  234 + initAlarmSubscription() {
  235 + var deferred = this.ctx.$q.defer();
  236 + var subscription = this;
  237 + this.loadStDiff().then(() => {
  238 + if (!subscription.ctx.aliasController) {
  239 + subscription.configureAlarmsData();
  240 + deferred.resolve();
  241 + } else {
  242 + subscription.ctx.aliasController.resolveAlarmSource(subscription.alarmSource).then(
  243 + function success(alarmSource) {
  244 + subscription.alarmSource = alarmSource;
  245 + subscription.configureAlarmsData();
  246 + deferred.resolve();
  247 + },
  248 + function fail() {
  249 + deferred.reject();
  250 + }
  251 + );
  252 + }
  253 + });
  254 + return deferred.promise;
  255 + }
  256 +
237 configureAlarmsData() { 257 configureAlarmsData() {
238 var subscription = this; 258 var subscription = this;
239 var registration; 259 var registration;
@@ -252,22 +272,24 @@ export default class Subscription { @@ -252,22 +272,24 @@ export default class Subscription {
252 272
253 initDataSubscription() { 273 initDataSubscription() {
254 var deferred = this.ctx.$q.defer(); 274 var deferred = this.ctx.$q.defer();
255 - if (!this.ctx.aliasController) {  
256 - this.configureData();  
257 - deferred.resolve();  
258 - } else {  
259 - var subscription = this;  
260 - this.ctx.aliasController.resolveDatasources(this.datasources).then(  
261 - function success(datasources) {  
262 - subscription.datasources = datasources;  
263 - subscription.configureData();  
264 - deferred.resolve();  
265 - },  
266 - function fail() {  
267 - deferred.reject();  
268 - }  
269 - );  
270 - } 275 + var subscription = this;
  276 + this.loadStDiff().then(() => {
  277 + if (!subscription.ctx.aliasController) {
  278 + subscription.configureData();
  279 + deferred.resolve();
  280 + } else {
  281 + subscription.ctx.aliasController.resolveDatasources(subscription.datasources).then(
  282 + function success(datasources) {
  283 + subscription.datasources = datasources;
  284 + subscription.configureData();
  285 + deferred.resolve();
  286 + },
  287 + function fail() {
  288 + deferred.reject();
  289 + }
  290 + );
  291 + }
  292 + });
271 return deferred.promise; 293 return deferred.promise;
272 } 294 }
273 295
@@ -75,7 +75,6 @@ function Dashboard() { @@ -75,7 +75,6 @@ function Dashboard() {
75 prepareDashboardContextMenu: '&?', 75 prepareDashboardContextMenu: '&?',
76 prepareWidgetContextMenu: '&?', 76 prepareWidgetContextMenu: '&?',
77 loadWidgets: '&?', 77 loadWidgets: '&?',
78 - getStDiff: '&?',  
79 onInit: '&?', 78 onInit: '&?',
80 onInitFailed: '&?', 79 onInitFailed: '&?',
81 dashboardStyle: '=?', 80 dashboardStyle: '=?',
@@ -102,8 +101,6 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -102,8 +101,6 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
102 101
103 vm.gridster = null; 102 vm.gridster = null;
104 103
105 - vm.stDiff = 0;  
106 -  
107 vm.isMobileDisabled = angular.isDefined(vm.isMobileDisabled) ? vm.isMobileDisabled : false; 104 vm.isMobileDisabled = angular.isDefined(vm.isMobileDisabled) ? vm.isMobileDisabled : false;
108 105
109 vm.isMobileSize = false; 106 vm.isMobileSize = false;
@@ -500,7 +497,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -500,7 +497,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
500 sortWidgets(); 497 sortWidgets();
501 }); 498 });
502 499
503 - loadStDiff(); 500 + loadDashboard();
504 501
505 function sortWidgets() { 502 function sortWidgets() {
506 vm.widgets.sort(function (widget1, widget2) { 503 vm.widgets.sort(function (widget1, widget2) {
@@ -515,28 +512,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -515,28 +512,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
515 } 512 }
516 513
517 function reload() { 514 function reload() {
518 - loadStDiff();  
519 - }  
520 -  
521 - function loadStDiff() {  
522 - if (vm.getStDiff) {  
523 - var promise = vm.getStDiff();  
524 - if (promise) {  
525 - promise.then(function (stDiff) {  
526 - vm.stDiff = stDiff;  
527 - loadDashboard();  
528 - }, function () {  
529 - vm.stDiff = 0;  
530 - loadDashboard();  
531 - });  
532 - } else {  
533 - vm.stDiff = 0;  
534 - loadDashboard();  
535 - }  
536 - } else {  
537 - vm.stDiff = 0;  
538 - loadDashboard();  
539 - } 515 + loadDashboard();
540 } 516 }
541 517
542 function loadDashboard() { 518 function loadDashboard() {
@@ -113,7 +113,6 @@ @@ -113,7 +113,6 @@
113 stateController: vm.stateController, 113 stateController: vm.stateController,
114 isEdit: vm.isEdit, 114 isEdit: vm.isEdit,
115 isMobile: vm.isMobileSize, 115 isMobile: vm.isMobileSize,
116 - stDiff: vm.stDiff,  
117 dashboardTimewindow: vm.dashboardTimewindow, 116 dashboardTimewindow: vm.dashboardTimewindow,
118 dashboardTimewindowApi: vm.dashboardTimewindowApi }"> 117 dashboardTimewindowApi: vm.dashboardTimewindowApi }">
119 </div> 118 </div>
@@ -21,7 +21,7 @@ import Subscription from '../../api/subscription'; @@ -21,7 +21,7 @@ import Subscription from '../../api/subscription';
21 21
22 /*@ngInject*/ 22 /*@ngInject*/
23 export default function WidgetController($scope, $state, $timeout, $window, $element, $q, $log, $injector, $filter, $compile, tbRaf, types, utils, timeService, 23 export default function WidgetController($scope, $state, $timeout, $window, $element, $q, $log, $injector, $filter, $compile, tbRaf, types, utils, timeService,
24 - datasourceService, alarmService, entityService, deviceService, visibleRect, isEdit, isMobile, stDiff, dashboardTimewindow, 24 + datasourceService, alarmService, entityService, dashboardService, deviceService, visibleRect, isEdit, isMobile, dashboardTimewindow,
25 dashboardTimewindowApi, widget, aliasController, stateController, widgetInfo, widgetType) { 25 dashboardTimewindowApi, widget, aliasController, stateController, widgetInfo, widgetType) {
26 26
27 var vm = this; 27 var vm = this;
@@ -159,7 +159,7 @@ export default function WidgetController($scope, $state, $timeout, $window, $ele @@ -159,7 +159,7 @@ export default function WidgetController($scope, $state, $timeout, $window, $ele
159 widgetUtils: widgetContext.utils, 159 widgetUtils: widgetContext.utils,
160 dashboardTimewindowApi: dashboardTimewindowApi, 160 dashboardTimewindowApi: dashboardTimewindowApi,
161 types: types, 161 types: types,
162 - stDiff: stDiff, 162 + getStDiff: dashboardService.getServerTimeDiff,
163 aliasController: aliasController 163 aliasController: aliasController
164 }; 164 };
165 165
@@ -170,7 +170,6 @@ export default function DashboardController(types, utils, dashboardUtils, widget @@ -170,7 +170,6 @@ export default function DashboardController(types, utils, dashboardUtils, widget
170 } 170 }
171 } 171 }
172 172
173 - vm.getServerTimeDiff = getServerTimeDiff;  
174 vm.addWidget = addWidget; 173 vm.addWidget = addWidget;
175 vm.addWidgetFromType = addWidgetFromType; 174 vm.addWidgetFromType = addWidgetFromType;
176 vm.exportDashboard = exportDashboard; 175 vm.exportDashboard = exportDashboard;
@@ -334,10 +333,6 @@ export default function DashboardController(types, utils, dashboardUtils, widget @@ -334,10 +333,6 @@ export default function DashboardController(types, utils, dashboardUtils, widget
334 } 333 }
335 } 334 }
336 335
337 - function getServerTimeDiff() {  
338 - return dashboardService.getServerTimeDiff();  
339 - }  
340 -  
341 function loadDashboard() { 336 function loadDashboard() {
342 if (vm.widgetEditMode) { 337 if (vm.widgetEditMode) {
343 var widget = { 338 var widget = {
@@ -137,8 +137,7 @@ @@ -137,8 +137,7 @@
137 dashboard-ctx="vm.dashboardCtx" 137 dashboard-ctx="vm.dashboardCtx"
138 is-edit="vm.isEdit" 138 is-edit="vm.isEdit"
139 is-mobile="vm.forceDashboardMobileMode" 139 is-mobile="vm.forceDashboardMobileMode"
140 - widget-edit-mode="vm.widgetEditMode"  
141 - get-st-diff="vm.getServerTimeDiff()"> 140 + widget-edit-mode="vm.widgetEditMode">
142 </tb-dashboard-layout> 141 </tb-dashboard-layout>
143 </div> 142 </div>
144 <md-sidenav ng-if="vm.layouts.right.show" 143 <md-sidenav ng-if="vm.layouts.right.show"
@@ -157,8 +156,7 @@ @@ -157,8 +156,7 @@
157 dashboard-ctx="vm.dashboardCtx" 156 dashboard-ctx="vm.dashboardCtx"
158 is-edit="vm.isEdit" 157 is-edit="vm.isEdit"
159 is-mobile="vm.forceDashboardMobileMode" 158 is-mobile="vm.forceDashboardMobileMode"
160 - widget-edit-mode="vm.widgetEditMode"  
161 - get-st-diff="vm.getServerTimeDiff()"> 159 + widget-edit-mode="vm.widgetEditMode">
162 </tb-dashboard-layout> 160 </tb-dashboard-layout>
163 </md-sidenav> 161 </md-sidenav>
164 </div> 162 </div>
@@ -29,8 +29,7 @@ export default function DashboardLayout() { @@ -29,8 +29,7 @@ export default function DashboardLayout() {
29 dashboardCtx: '=', 29 dashboardCtx: '=',
30 isEdit: '=', 30 isEdit: '=',
31 isMobile: '=', 31 isMobile: '=',
32 - widgetEditMode: '=',  
33 - getStDiff: '&?' 32 + widgetEditMode: '='
34 }, 33 },
35 controller: DashboardLayoutController, 34 controller: DashboardLayoutController,
36 controllerAs: 'vm', 35 controllerAs: 'vm',
@@ -64,7 +64,6 @@ @@ -64,7 +64,6 @@
64 prepare-dashboard-context-menu="vm.prepareDashboardContextMenu()" 64 prepare-dashboard-context-menu="vm.prepareDashboardContextMenu()"
65 prepare-widget-context-menu="vm.prepareWidgetContextMenu(widget)" 65 prepare-widget-context-menu="vm.prepareWidgetContextMenu(widget)"
66 on-remove-widget="vm.removeWidget(event, widget)" 66 on-remove-widget="vm.removeWidget(event, widget)"
67 - get-st-diff="vm.getStDiff()"  
68 on-init="vm.dashboardInited(dashboard)" 67 on-init="vm.dashboardInited(dashboard)"
69 on-init-failed="vm.dashboardInitFailed(e)" 68 on-init-failed="vm.dashboardInitFailed(e)"
70 ignore-loading="vm.layoutCtx.ignoreLoading"> 69 ignore-loading="vm.layoutCtx.ignoreLoading">
@@ -31,7 +31,7 @@ import AliasController from '../../api/alias-controller'; @@ -31,7 +31,7 @@ import AliasController from '../../api/alias-controller';
31 /*@ngInject*/ 31 /*@ngInject*/
32 export default function AttributeTableDirective($compile, $templateCache, $rootScope, $q, $mdEditDialog, $mdDialog, 32 export default function AttributeTableDirective($compile, $templateCache, $rootScope, $q, $mdEditDialog, $mdDialog,
33 $mdUtil, $document, $translate, $filter, utils, types, dashboardUtils, 33 $mdUtil, $document, $translate, $filter, utils, types, dashboardUtils,
34 - dashboardService, entityService, attributeService, widgetService) { 34 + entityService, attributeService, widgetService) {
35 35
36 var linker = function (scope, element, attrs) { 36 var linker = function (scope, element, attrs) {
37 37
@@ -407,10 +407,6 @@ export default function AttributeTableDirective($compile, $templateCache, $rootS @@ -407,10 +407,6 @@ export default function AttributeTableDirective($compile, $templateCache, $rootS
407 scope.getEntityAttributes(true); 407 scope.getEntityAttributes(true);
408 } 408 }
409 409
410 - scope.getServerTimeDiff = function() {  
411 - return dashboardService.getServerTimeDiff();  
412 - }  
413 -  
414 scope.addWidgetToDashboard = function($event) { 410 scope.addWidgetToDashboard = function($event) {
415 if (scope.mode === 'widget' && scope.widgetsListCache.length > 0) { 411 if (scope.mode === 'widget' && scope.widgetsListCache.length > 0) {
416 var widget = scope.widgetsListCache[scope.widgetsCarousel.index][0]; 412 var widget = scope.widgetsListCache[scope.widgetsCarousel.index][0];
@@ -158,7 +158,6 @@ @@ -158,7 +158,6 @@
158 <tb-dashboard 158 <tb-dashboard
159 alias-controller="aliasController" 159 alias-controller="aliasController"
160 widgets="widgets" 160 widgets="widgets"
161 - get-st-diff="getServerTimeDiff()"  
162 columns="20" 161 columns="20"
163 is-edit="false" 162 is-edit="false"
164 is-mobile-disabled="true" 163 is-mobile-disabled="true"
@@ -30,7 +30,7 @@ @@ -30,7 +30,7 @@
30 <md-input-container class="md-block"> 30 <md-input-container class="md-block">
31 <label translate>user.email</label> 31 <label translate>user.email</label>
32 <input required name="email" 32 <input required name="email"
33 - ng-pattern="/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/" 33 + ng-pattern='/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/'
34 ng-model="user.email"> 34 ng-model="user.email">
35 <div ng-messages="theForm.email.$error"> 35 <div ng-messages="theForm.email.$error">
36 <div translate ng-message="required">user.email-required</div> 36 <div translate ng-message="required">user.email-required</div>