Browse Source

Code review and refactoring 2

pull/2436/head
deaflynx 6 years ago
parent
commit
d7f8c765a9
  1. 8
      ui/src/app/dashboard/add-dashboards-to-edge.controller.js
  2. 9
      ui/src/app/dashboard/dashboards.controller.js
  3. 113
      ui/src/app/edge/edge.routes.js
  4. 6
      ui/src/app/event/event-row-edge-event.tpl.html
  5. 377
      ui/src/app/event/event-row.directive.js
  6. 2
      ui/src/app/rulechain/add-rulechains-to-edge.controller.js
  7. 57
      ui/src/app/rulechain/rulechain.routes.js
  8. 2
      ui/src/app/rulechain/rulechains.controller.js

8
ui/src/app/dashboard/add-dashboards-to-edge.controller.js

@ -53,13 +53,7 @@ export default function AddDashboardsToEdgeController(dashboardService, types, $
fetchMoreItems_: function () {
if (vm.dashboards.hasNext && !vm.dashboards.pending) {
vm.dashboards.pending = true;
var fetchDashboardsPromise;
if (edgeCustomerId === vm.types.id.nullUid) {
fetchDashboardsPromise = dashboardService.getTenantDashboards(vm.dashboards.nextPageLink);
} else {
fetchDashboardsPromise = dashboardService.getCustomerDashboards(edgeCustomerId, vm.dashboards.nextPageLink);
}
fetchDashboardsPromise.then(
dashboardService.getTenantDashboards(vm.dashboards.nextPageLink).then(
function success(dashboards) {
vm.dashboards.data = vm.dashboards.data.concat(dashboards.data);
vm.dashboards.nextPageLink = dashboards.nextPageLink;

9
ui/src/app/dashboard/dashboards.controller.js

@ -708,14 +708,7 @@ export function DashboardsController(userService, dashboardService, customerServ
$event.stopPropagation();
}
var pageSize = 10;
var fetchDashboardsPromise;
if (vm.edgeCustomerId.id === vm.types.id.nullUid) {
fetchDashboardsPromise = dashboardService.getTenantDashboards({limit: pageSize, textSearch: ''});
} else {
fetchDashboardsPromise = dashboardService.getCustomerDashboards(vm.edgeCustomerId.id, {limit: pageSize, textSearch: ''});
}
fetchDashboardsPromise.then(
dashboardService.getTenantDashboards({limit: pageSize, textSearch: ''}).then(
function success(_dashboards) {
var dashboards = {
pageSize: pageSize,

113
ui/src/app/edge/edge.routes.js

@ -21,6 +21,8 @@ import devicesTemplate from "../device/devices.tpl.html";
import assetsTemplate from "../asset/assets.tpl.html";
import dashboardsTemplate from "../dashboard/dashboards.tpl.html";
import dashboardTemplate from "../dashboard/dashboard.tpl.html";
import ruleChainsTemplate from "../rulechain/rulechains.tpl.html";
import ruleChainTemplate from "../rulechain/rulechain.tpl.html";
/* eslint-enable import/no-unresolved, import/default */
@ -49,8 +51,7 @@ export default function EdgeRoutes($stateProvider, types) {
ncyBreadcrumb: {
label: '{"icon": "transform", "label": "edge.edges"}'
}
})
.state('home.edges.entityViews', {
}).state('home.edges.entityViews', {
url: '/:edgeId/entityViews',
params: {'topIndex': 0},
module: 'private',
@ -72,8 +73,7 @@ export default function EdgeRoutes($stateProvider, types) {
ncyBreadcrumb: {
label: '{"icon": "view_quilt", "label": "edge.entity-views"}'
}
})
.state('home.edges.devices', {
}).state('home.edges.devices', {
url: '/:edgeId/devices',
params: {'topIndex': 0},
module: 'private',
@ -95,8 +95,7 @@ export default function EdgeRoutes($stateProvider, types) {
ncyBreadcrumb: {
label: '{"icon": "devices_other", "label": "edge.devices"}'
}
})
.state('home.edges.assets', {
}).state('home.edges.assets', {
url: '/:edgeId/assets',
params: {'topIndex': 0},
module: 'private',
@ -118,29 +117,27 @@ export default function EdgeRoutes($stateProvider, types) {
ncyBreadcrumb: {
label: '{"icon": "domain", "label": "edge.assets"}'
}
})
.state('home.edges.dashboards', {
url: '/:edgeId/dashboards',
params: {'topIndex': 0},
module: 'private',
auth: ['TENANT_ADMIN'],
views: {
"content@home": {
templateUrl: dashboardsTemplate,
controllerAs: 'vm',
controller: 'DashboardsController'
}).state('home.edges.dashboards', {
url: '/:edgeId/dashboards',
params: {'topIndex': 0},
module: 'private',
auth: ['TENANT_ADMIN'],
views: {
"content@home": {
templateUrl: dashboardsTemplate,
controllerAs: 'vm',
controller: 'DashboardsController'
}
},
data: {
dashboardsType: 'edge',
searchEnabled: true,
pageTitle: 'edge.dashboards'
},
ncyBreadcrumb: {
label: '{"icon": "dashboard", "label": "edge.dashboards"}'
}
},
data: {
dashboardsType: 'edge',
searchEnabled: true,
pageTitle: 'edge.dashboards'
},
ncyBreadcrumb: {
label: '{"icon": "dashboard", "label": "edge.dashboards"}'
}
})
.state('home.edges.dashboards.dashboard', {
}).state('home.edges.dashboards.dashboard', {
url: '/:dashboardId?state',
reloadOnSearch: false,
module: 'private',
@ -161,8 +158,7 @@ export default function EdgeRoutes($stateProvider, types) {
ncyBreadcrumb: {
label: '{"icon": "dashboard", "label": "{{ vm.dashboard.title }}", "translate": "false"}'
}
})
.state('home.customers.edges', {
}).state('home.customers.edges', {
url: '/:customerId/edges',
params: {'topIndex': 0},
module: 'private',
@ -184,5 +180,62 @@ export default function EdgeRoutes($stateProvider, types) {
ncyBreadcrumb: {
label: '{"icon": "router", "label": "{{ vm.customerEdgesTitle }}", "translate": "false"}'
}
}).state('home.edges.ruleChains', {
url: '/:edgeId/ruleChains',
params: {'topIndex': 0},
module: 'private',
auth: ['TENANT_ADMIN'],
views: {
"content@home": {
templateUrl: ruleChainsTemplate,
controllerAs: 'vm',
controller: 'RuleChainsController'
}
},
data: {
searchEnabled: true,
pageTitle: 'edge.rulechains',
ruleChainsType: 'edge'
},
ncyBreadcrumb: {
label: '{"icon": "settings_ethernet", "label": "rulechain.edge-rulechains"}'
}
}).state('home.edges.ruleChains.ruleChain', {
url: '/:ruleChainId',
reloadOnSearch: false,
module: 'private',
auth: ['SYS_ADMIN', 'TENANT_ADMIN'],
views: {
"content@home": {
templateUrl: ruleChainTemplate,
controller: 'RuleChainController',
controllerAs: 'vm'
}
},
resolve: {
ruleChain:
/*@ngInject*/
function($stateParams, ruleChainService) {
return ruleChainService.getRuleChain($stateParams.ruleChainId);
},
ruleChainMetaData:
/*@ngInject*/
function($stateParams, ruleChainService) {
return ruleChainService.getRuleChainMetaData($stateParams.ruleChainId);
},
ruleNodeComponents:
/*@ngInject*/
function($stateParams, ruleChainService) {
return ruleChainService.getRuleNodeComponents(types.ruleChainType.edge);
}
},
data: {
import: false,
searchEnabled: false,
pageTitle: 'edge.rulechain'
},
ncyBreadcrumb: {
label: '{"icon": "settings_ethernet", "label": "{{ vm.ruleChain.name }}", "translate": "false"}'
}
});
}

6
ui/src/app/event/event-row-edge-event.tpl.html

@ -16,12 +16,12 @@
-->
<div class="tb-cell" flex="20">{{ event.createdTime | date : 'yyyy-MM-dd HH:mm:ss' }}</div>
<div class="tb-cell" flex="20">{{ event.edgeEventType }}</div>
<div class="tb-cell" flex="40">{{ event.edgeEventAction }}</div>
<div class="tb-cell" flex="20">{{ event.type }}</div>
<div class="tb-cell" flex="40">{{ event.action }}</div>
<div class="tb-cell" flex="20">{{ event.entityId }}</div>
<div class="tb-cell" flex="15" ng-style="isPending ? {'color': 'rgba(0, 0, 0, .38)'} : {'color': '#000'}">{{ updateStatus(event.createdTime) | translate }}</div>
<div class="tb-cell" flex="10">
<md-button class="md-icon-button md-primary"
<md-button ng-if="checkEdgeEventType(event.type)" class="md-icon-button md-primary"
ng-click="showEdgeEntityContent($event, 'edge.entity-info', 'JSON')"
aria-label="{{ 'action.view' | translate }}">
<md-tooltip md-direction="top">

377
ui/src/app/event/event-row.directive.js

@ -1,184 +1,193 @@
/*
* Copyright © 2016-2020 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable import/no-unresolved, import/default */
import eventErrorDialogTemplate from './event-content-dialog.tpl.html';
import eventRowLcEventTemplate from './event-row-lc-event.tpl.html';
import eventRowStatsTemplate from './event-row-stats.tpl.html';
import eventRowErrorTemplate from './event-row-error.tpl.html';
import eventRowDebugRuleNodeTemplate from './event-row-debug-rulenode.tpl.html';
import eventRowEdgeEventTemplate from './event-row-edge-event.tpl.html';
/* eslint-enable import/no-unresolved, import/default */
/*@ngInject*/
export default function EventRowDirective($compile, $templateCache, $mdDialog, $document, $translate,
types, utils, toast, entityService, ruleChainService) {
var linker = function (scope, element, attrs) {
var getTemplate = function(eventType) {
var template = '';
switch(eventType) {
case types.eventType.lcEvent.value:
template = eventRowLcEventTemplate;
break;
case types.eventType.stats.value:
template = eventRowStatsTemplate;
break;
case types.eventType.error.value:
template = eventRowErrorTemplate;
break;
case types.debugEventType.debugRuleNode.value:
template = eventRowDebugRuleNodeTemplate;
break;
case types.debugEventType.debugRuleChain.value:
template = eventRowDebugRuleNodeTemplate;
break;
case types.eventType.edgeEvent.value:
template = eventRowEdgeEventTemplate;
break;
}
return $templateCache.get(template);
}
scope.loadTemplate = function() {
element.html(getTemplate(attrs.eventType));
$compile(element.contents())(scope);
}
attrs.$observe('eventType', function() {
scope.loadTemplate();
});
scope.types = types;
scope.event = attrs.event;
scope.showContent = function($event, content, title, contentType) {
var onShowingCallback = {
onShowing: function(){}
}
if (!contentType) {
contentType = null;
}
var sortedContent;
try {
sortedContent = angular.toJson(utils.sortObjectKeys(angular.fromJson(content)));
}
catch(err) {
sortedContent = content;
}
$mdDialog.show({
controller: 'EventContentDialogController',
controllerAs: 'vm',
templateUrl: eventErrorDialogTemplate,
locals: {content: sortedContent, title: title, contentType: contentType, showingCallback: onShowingCallback},
parent: angular.element($document[0].body),
fullscreen: true,
targetEvent: $event,
multiple: true,
onShowing: function(scope, element) {
onShowingCallback.onShowing(scope, element);
}
});
}
scope.showEdgeEntityContent = function($event, title, contentType) {
var onShowingCallback = {
onShowing: function(){}
}
if (!contentType) {
contentType = null;
}
var content = '';
switch(scope.event.edgeEventType) {
case types.edgeEventType.relation:
content = angular.toJson(scope.event.entityBody);
showDialog();
break;
case types.edgeEventType.ruleChainMetaData:
content = ruleChainService.getRuleChainMetaData(scope.event.entityId, {ignoreErrors: true}).then(
function success(info) {
showDialog();
return angular.toJson(info);
}, function fail() {
showError();
});
break;
default:
content = entityService.getEntity(scope.event.edgeEventType, scope.event.entityId, {ignoreErrors: true}).then(
function success(info) {
showDialog();
return angular.toJson(info);
}, function fail() {
showError();
});
break;
}
function showDialog() {
$mdDialog.show({
controller: 'EventContentDialogController',
controllerAs: 'vm',
templateUrl: eventErrorDialogTemplate,
locals: {content: content, title: title, contentType: contentType, showingCallback: onShowingCallback},
parent: angular.element($document[0].body),
fullscreen: true,
targetEvent: $event,
multiple: true,
onShowing: function(scope, element) {
onShowingCallback.onShowing(scope, element);
}
});
}
function showError() {
toast.showError($translate.instant('edge.load-entity-error'));
}
}
scope.checkTooltip = function($event) {
var el = $event.target;
var $el = angular.element(el);
if(el.offsetWidth < el.scrollWidth && !$el.attr('title')){
$el.attr('title', $el.text());
}
}
$compile(element.contents())(scope);
scope.updateStatus = function(eventCreatedTime) {
if (scope.queueStartTs) {
var status;
if (eventCreatedTime < scope.queueStartTs) {
status = $translate.instant('edge.success');
scope.isPending = false;
} else {
status = $translate.instant('edge.failed');
scope.isPending = true;
}
return status;
}
}
}
return {
restrict: "A",
replace: false,
link: linker,
scope: false
};
}
/*
* Copyright © 2016-2020 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable import/no-unresolved, import/default */
import eventErrorDialogTemplate from './event-content-dialog.tpl.html';
import eventRowLcEventTemplate from './event-row-lc-event.tpl.html';
import eventRowStatsTemplate from './event-row-stats.tpl.html';
import eventRowErrorTemplate from './event-row-error.tpl.html';
import eventRowDebugRuleNodeTemplate from './event-row-debug-rulenode.tpl.html';
import eventRowEdgeEventTemplate from './event-row-edge-event.tpl.html';
/* eslint-enable import/no-unresolved, import/default */
/*@ngInject*/
export default function EventRowDirective($compile, $templateCache, $mdDialog, $document, $translate,
types, utils, toast, entityService, ruleChainService) {
var linker = function (scope, element, attrs) {
var getTemplate = function(eventType) {
var template = '';
switch(eventType) {
case types.eventType.lcEvent.value:
template = eventRowLcEventTemplate;
break;
case types.eventType.stats.value:
template = eventRowStatsTemplate;
break;
case types.eventType.error.value:
template = eventRowErrorTemplate;
break;
case types.debugEventType.debugRuleNode.value:
template = eventRowDebugRuleNodeTemplate;
break;
case types.debugEventType.debugRuleChain.value:
template = eventRowDebugRuleNodeTemplate;
break;
case types.eventType.edgeEvent.value:
template = eventRowEdgeEventTemplate;
break;
}
return $templateCache.get(template);
}
scope.loadTemplate = function() {
element.html(getTemplate(attrs.eventType));
$compile(element.contents())(scope);
}
attrs.$observe('eventType', function() {
scope.loadTemplate();
});
scope.types = types;
scope.event = attrs.event;
scope.showContent = function($event, content, title, contentType) {
var onShowingCallback = {
onShowing: function(){}
}
if (!contentType) {
contentType = null;
}
var sortedContent;
try {
sortedContent = angular.toJson(utils.sortObjectKeys(angular.fromJson(content)));
}
catch(err) {
sortedContent = content;
}
$mdDialog.show({
controller: 'EventContentDialogController',
controllerAs: 'vm',
templateUrl: eventErrorDialogTemplate,
locals: {content: sortedContent, title: title, contentType: contentType, showingCallback: onShowingCallback},
parent: angular.element($document[0].body),
fullscreen: true,
targetEvent: $event,
multiple: true,
onShowing: function(scope, element) {
onShowingCallback.onShowing(scope, element);
}
});
}
scope.showEdgeEntityContent = function($event, title, contentType) {
var onShowingCallback = {
onShowing: function(){}
}
if (!contentType) {
contentType = null;
}
var content = '';
switch(scope.event.type) {
case types.edgeEventType.relation:
case types.edgeEventType.whiteLabeling:
case types.edgeEventType.loginWhiteLabeling:
case types.edgeEventType.customTranslation:
content = angular.toJson(scope.event.body);
showDialog();
break;
case types.edgeEventType.ruleChainMetaData:
content = ruleChainService.getRuleChainMetaData(scope.event.entityId, {ignoreErrors: true}).then(
function success(info) {
showDialog();
return angular.toJson(info);
}, function fail() {
showError();
});
break;
default:
content = entityService.getEntity(scope.event.type, scope.event.entityId, {ignoreErrors: true}).then(
function success(info) {
showDialog();
return angular.toJson(info);
}, function fail() {
showError();
});
break;
}
function showDialog() {
$mdDialog.show({
controller: 'EventContentDialogController',
controllerAs: 'vm',
templateUrl: eventErrorDialogTemplate,
locals: {content: content, title: title, contentType: contentType, showingCallback: onShowingCallback},
parent: angular.element($document[0].body),
fullscreen: true,
targetEvent: $event,
multiple: true,
onShowing: function(scope, element) {
onShowingCallback.onShowing(scope, element);
}
});
}
function showError() {
toast.showError($translate.instant('edge.load-entity-error'));
}
}
scope.checkEdgeEventType = function (edgeEventType) {
return !(edgeEventType === types.edgeEventType.widgetType ||
edgeEventType === types.edgeEventType.adminSettings ||
edgeEventType === types.edgeEventType.widgetsBundle );
}
scope.checkTooltip = function($event) {
var el = $event.target;
var $el = angular.element(el);
if(el.offsetWidth < el.scrollWidth && !$el.attr('title')){
$el.attr('title', $el.text());
}
}
$compile(element.contents())(scope);
scope.updateStatus = function(eventCreatedTime) {
if (scope.queueStartTs) {
var status;
if (eventCreatedTime < scope.queueStartTs) {
status = $translate.instant('edge.success');
scope.isPending = false;
} else {
status = $translate.instant('edge.failed');
scope.isPending = true;
}
return status;
}
}
}
return {
restrict: "A",
replace: false,
link: linker,
scope: false
};
}

2
ui/src/app/rulechain/add-rulechains-to-edge.controller.js

@ -54,7 +54,7 @@ export default function AddRuleChainsToEdgeController(ruleChainService, $mdDialo
vm.ruleChains.pending = true;
ruleChainService.getEdgesRuleChains(vm.ruleChains.nextPageLink).then(
function success(ruleChains) {
vm.ruleChains.data = ruleChains.data;
vm.ruleChains.data = vm.ruleChains.data.concat(ruleChains.data);
vm.ruleChains.nextPageLink = ruleChains.nextPageLink;
vm.ruleChains.hasNext = ruleChains.hasNext;
if (vm.ruleChains.hasNext) {

57
ui/src/app/rulechain/rulechain.routes.js

@ -191,62 +191,5 @@ export default function RuleChainRoutes($stateProvider, NodeTemplatePathProvider
ncyBreadcrumb: {
label: '{"icon": "settings_ethernet", "label": "{{ vm.ruleChain.name }}", "translate": "false"}'
}
}).state('home.edges.ruleChains', {
url: '/:edgeId/ruleChains',
params: {'topIndex': 0},
module: 'private',
auth: ['TENANT_ADMIN'],
views: {
"content@home": {
templateUrl: ruleChainsTemplate,
controllerAs: 'vm',
controller: 'RuleChainsController'
}
},
data: {
searchEnabled: true,
pageTitle: 'edge.rulechains',
ruleChainsType: 'edge'
},
ncyBreadcrumb: {
label: '{"icon": "settings_ethernet", "label": "rulechain.edge-rulechains"}'
}
}).state('home.edges.ruleChains.ruleChain', {
url: '/:ruleChainId',
reloadOnSearch: false,
module: 'private',
auth: ['SYS_ADMIN', 'TENANT_ADMIN'],
views: {
"content@home": {
templateUrl: ruleChainTemplate,
controller: 'RuleChainController',
controllerAs: 'vm'
}
},
resolve: {
ruleChain:
/*@ngInject*/
function($stateParams, ruleChainService) {
return ruleChainService.getRuleChain($stateParams.ruleChainId);
},
ruleChainMetaData:
/*@ngInject*/
function($stateParams, ruleChainService) {
return ruleChainService.getRuleChainMetaData($stateParams.ruleChainId);
},
ruleNodeComponents:
/*@ngInject*/
function($stateParams, ruleChainService) {
return ruleChainService.getRuleNodeComponents(types.ruleChainType.edge);
}
},
data: {
import: false,
searchEnabled: false,
pageTitle: 'edge.rulechain'
},
ncyBreadcrumb: {
label: '{"icon": "settings_ethernet", "label": "{{ vm.ruleChain.name }}", "translate": "false"}'
}
});
}

2
ui/src/app/rulechain/rulechains.controller.js

@ -397,7 +397,7 @@ export default function RuleChainsController(ruleChainService, userService, impo
}
var ruleChainParams = {ruleChainId: ruleChain.id.id};
if (vm.ruleChainsScope === 'edge') {
$state.go('home.edges.ruleChains.ruleChain', {...ruleChainParams, edgeId: vm.edge.id.id});
$state.go('home.edges.ruleChains.ruleChain', Object.assign(ruleChainParams, edgeId = vm.edge.id.id));
} else if (vm.ruleChainsScope === 'edges') {
$state.go('home.ruleChains.edge.ruleChain', ruleChainParams);
} else {

Loading…
Cancel
Save