2017-05-24 10:39:33 +03:00
|
|
|
/*
|
2019-02-01 16:39:33 +02:00
|
|
|
* Copyright © 2016-2019 The Thingsboard Authors
|
2017-05-24 10:39:33 +03:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
import './datasource-entity.scss';
|
|
|
|
|
|
|
|
|
|
import 'md-color-picker';
|
|
|
|
|
import tinycolor from 'tinycolor2';
|
|
|
|
|
import $ from 'jquery';
|
|
|
|
|
import thingsboardTypes from '../common/types.constant';
|
|
|
|
|
import thingsboardDatakeyConfigDialog from './datakey-config-dialog.controller';
|
|
|
|
|
import thingsboardTruncate from './truncate.filter';
|
|
|
|
|
|
|
|
|
|
/* eslint-disable import/no-unresolved, import/default */
|
|
|
|
|
|
|
|
|
|
import datasourceEntityTemplate from './datasource-entity.tpl.html';
|
|
|
|
|
import datakeyConfigDialogTemplate from './datakey-config-dialog.tpl.html';
|
|
|
|
|
|
|
|
|
|
/* eslint-enable import/no-unresolved, import/default */
|
|
|
|
|
|
|
|
|
|
/* eslint-disable angular/angularelement */
|
|
|
|
|
|
|
|
|
|
export default angular.module('thingsboard.directives.datasourceEntity', [thingsboardTruncate, thingsboardTypes, thingsboardDatakeyConfigDialog])
|
|
|
|
|
.directive('tbDatasourceEntity', DatasourceEntity)
|
|
|
|
|
.name;
|
|
|
|
|
|
|
|
|
|
/*@ngInject*/
|
|
|
|
|
function DatasourceEntity($compile, $templateCache, $q, $mdDialog, $window, $document, $mdColorPicker, $mdConstant, types) {
|
|
|
|
|
|
|
|
|
|
var linker = function (scope, element, attrs, ngModelCtrl) {
|
|
|
|
|
var template = $templateCache.get(datasourceEntityTemplate);
|
|
|
|
|
element.html(template);
|
|
|
|
|
|
|
|
|
|
scope.ngModelCtrl = ngModelCtrl;
|
|
|
|
|
scope.types = types;
|
|
|
|
|
|
2017-06-14 13:43:18 +03:00
|
|
|
scope.alarmFields = [];
|
|
|
|
|
for (var alarmField in types.alarmFields) {
|
|
|
|
|
scope.alarmFields.push(alarmField);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 12:08:02 +02:00
|
|
|
scope.selectedDataKey = null;
|
|
|
|
|
scope.dataKeySearchText = null;
|
2017-05-24 10:39:33 +03:00
|
|
|
|
2017-06-14 13:43:18 +03:00
|
|
|
scope.selectedAlarmDataKey = null;
|
|
|
|
|
scope.alarmDataKeySearchText = null;
|
|
|
|
|
|
2017-05-24 10:39:33 +03:00
|
|
|
scope.updateValidity = function () {
|
|
|
|
|
if (ngModelCtrl.$viewValue) {
|
|
|
|
|
var value = ngModelCtrl.$viewValue;
|
|
|
|
|
var dataValid = angular.isDefined(value) && value != null;
|
|
|
|
|
ngModelCtrl.$setValidity('entityData', dataValid);
|
|
|
|
|
if (dataValid) {
|
|
|
|
|
ngModelCtrl.$setValidity('entityAlias',
|
|
|
|
|
angular.isDefined(value.entityAliasId) &&
|
|
|
|
|
value.entityAliasId != null);
|
2017-07-12 19:28:53 +03:00
|
|
|
if (scope.optDataKeys) {
|
|
|
|
|
ngModelCtrl.$setValidity('entityKeys', true);
|
|
|
|
|
} else {
|
|
|
|
|
ngModelCtrl.$setValidity('entityKeys',
|
|
|
|
|
angular.isDefined(value.dataKeys) &&
|
|
|
|
|
value.dataKeys != null &&
|
|
|
|
|
value.dataKeys.length > 0);
|
|
|
|
|
}
|
2017-05-24 10:39:33 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
scope.$watch('entityAlias', function () {
|
|
|
|
|
if (ngModelCtrl.$viewValue) {
|
|
|
|
|
if (scope.entityAlias) {
|
|
|
|
|
ngModelCtrl.$viewValue.entityAliasId = scope.entityAlias.id;
|
|
|
|
|
} else {
|
|
|
|
|
ngModelCtrl.$viewValue.entityAliasId = null;
|
|
|
|
|
}
|
|
|
|
|
scope.updateValidity();
|
|
|
|
|
scope.selectedEntityAliasChange();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-05-10 12:08:02 +02:00
|
|
|
scope.$watch('dataKeys', function () {
|
2017-06-14 13:43:18 +03:00
|
|
|
updateDataKeys();
|
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
|
|
scope.$watch('alarmDataKeys', function () {
|
|
|
|
|
updateDataKeys();
|
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
|
|
function updateDataKeys() {
|
2017-05-24 10:39:33 +03:00
|
|
|
if (ngModelCtrl.$viewValue) {
|
|
|
|
|
var dataKeys = [];
|
2019-05-10 12:08:02 +02:00
|
|
|
dataKeys = dataKeys.concat(scope.dataKeys);
|
2017-06-14 13:43:18 +03:00
|
|
|
dataKeys = dataKeys.concat(scope.alarmDataKeys);
|
2019-05-10 12:08:02 +02:00
|
|
|
if (!angular.equals(ngModelCtrl.$viewValue.dataKeys, dataKeys))
|
2019-05-09 14:56:49 +02:00
|
|
|
{
|
|
|
|
|
ngModelCtrl.$setDirty();
|
|
|
|
|
ngModelCtrl.$viewValue.dataKeys = dataKeys;
|
|
|
|
|
}
|
2017-05-24 10:39:33 +03:00
|
|
|
scope.updateValidity();
|
|
|
|
|
}
|
2017-06-14 13:43:18 +03:00
|
|
|
}
|
2017-05-24 10:39:33 +03:00
|
|
|
|
|
|
|
|
ngModelCtrl.$render = function () {
|
|
|
|
|
if (ngModelCtrl.$viewValue) {
|
|
|
|
|
var entityAliasId = ngModelCtrl.$viewValue.entityAliasId;
|
2017-06-07 17:09:04 +03:00
|
|
|
var entityAliases = scope.aliasController.getEntityAliases();
|
|
|
|
|
if (entityAliases[entityAliasId]) {
|
|
|
|
|
scope.entityAlias = entityAliases[entityAliasId];
|
2017-05-24 10:39:33 +03:00
|
|
|
} else {
|
|
|
|
|
scope.entityAlias = null;
|
|
|
|
|
}
|
2019-05-10 12:08:02 +02:00
|
|
|
var dataKeys = [];
|
2017-06-14 13:43:18 +03:00
|
|
|
var alarmDataKeys = [];
|
2017-05-24 10:39:33 +03:00
|
|
|
for (var d in ngModelCtrl.$viewValue.dataKeys) {
|
|
|
|
|
var dataKey = ngModelCtrl.$viewValue.dataKeys[d];
|
2019-05-10 12:08:02 +02:00
|
|
|
if ((dataKey.type === types.dataKeyType.timeseries) || (dataKey.type === types.dataKeyType.attribute)) {
|
|
|
|
|
dataKeys.push(dataKey);
|
2017-06-14 13:43:18 +03:00
|
|
|
} else if (dataKey.type === types.dataKeyType.alarm) {
|
|
|
|
|
alarmDataKeys.push(dataKey);
|
2017-05-24 10:39:33 +03:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-10 12:08:02 +02:00
|
|
|
scope.dataKeys = dataKeys;
|
2017-06-14 13:43:18 +03:00
|
|
|
scope.alarmDataKeys = alarmDataKeys;
|
2017-05-24 10:39:33 +03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
scope.textIsNotEmpty = function(text) {
|
|
|
|
|
return (text && text != null && text.length > 0) ? true : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope.selectedEntityAliasChange = function () {
|
2019-05-10 12:08:02 +02:00
|
|
|
if (!scope.dataKeySearchText || scope.dataKeySearchText === '') {
|
|
|
|
|
scope.dataKeySearchText = scope.dataKeySearchText === '' ? null : '';
|
2017-05-24 10:39:33 +03:00
|
|
|
}
|
2017-06-14 13:43:18 +03:00
|
|
|
if (!scope.alarmDataKeySearchText || scope.alarmDataKeySearchText === '') {
|
|
|
|
|
scope.alarmDataKeySearchText = scope.alarmDataKeySearchText === '' ? null : '';
|
|
|
|
|
}
|
2017-05-24 10:39:33 +03:00
|
|
|
};
|
|
|
|
|
|
2019-05-10 12:08:02 +02:00
|
|
|
scope.transformDataKeyChip = function (chip) {
|
2017-06-19 19:22:48 +03:00
|
|
|
if (scope.maxDataKeys > 0 && ngModelCtrl.$viewValue.dataKeys.length >= scope.maxDataKeys ) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
2019-06-25 20:41:53 +03:00
|
|
|
if (chip.type) {
|
|
|
|
|
return scope.generateDataKey({chip: chip.name, type: chip.type});
|
|
|
|
|
} else {
|
|
|
|
|
if (scope.widgetType != types.widgetType.latest.value) {
|
|
|
|
|
return scope.generateDataKey({chip: chip, type: types.dataKeyType.timeseries});
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-19 19:22:48 +03:00
|
|
|
}
|
2017-05-24 10:39:33 +03:00
|
|
|
};
|
|
|
|
|
|
2017-06-14 13:43:18 +03:00
|
|
|
scope.transformAlarmDataKeyChip = function (chip) {
|
2019-11-08 14:21:55 +02:00
|
|
|
if (chip.type) {
|
|
|
|
|
return scope.generateDataKey({chip: chip.name, type: chip.type});
|
|
|
|
|
} else {
|
|
|
|
|
return scope.generateDataKey({chip: chip, type: types.dataKeyType.alarm});
|
|
|
|
|
}
|
2017-06-14 13:43:18 +03:00
|
|
|
};
|
|
|
|
|
|
2017-05-24 10:39:33 +03:00
|
|
|
scope.showColorPicker = function (event, dataKey) {
|
|
|
|
|
$mdColorPicker.show({
|
|
|
|
|
value: dataKey.color,
|
|
|
|
|
defaultValue: '#fff',
|
|
|
|
|
random: tinycolor.random(),
|
|
|
|
|
clickOutsideToClose: false,
|
|
|
|
|
hasBackdrop: false,
|
2018-10-29 18:28:15 +02:00
|
|
|
multiple: true,
|
2017-05-24 10:39:33 +03:00
|
|
|
preserveScope: false,
|
|
|
|
|
|
|
|
|
|
mdColorAlphaChannel: true,
|
|
|
|
|
mdColorSpectrum: true,
|
|
|
|
|
mdColorSliders: true,
|
|
|
|
|
mdColorGenericPalette: false,
|
|
|
|
|
mdColorMaterialPalette: true,
|
|
|
|
|
mdColorHistory: false,
|
|
|
|
|
mdColorDefaultTab: 2,
|
|
|
|
|
|
|
|
|
|
$event: event
|
|
|
|
|
|
|
|
|
|
}).then(function (color) {
|
|
|
|
|
dataKey.color = color;
|
|
|
|
|
ngModelCtrl.$setDirty();
|
|
|
|
|
});
|
2019-08-09 14:35:20 +03:00
|
|
|
};
|
2017-05-24 10:39:33 +03:00
|
|
|
|
2019-08-09 14:35:20 +03:00
|
|
|
scope.editDataKey = function (event, dataKey) {
|
2017-05-24 10:39:33 +03:00
|
|
|
|
|
|
|
|
$mdDialog.show({
|
|
|
|
|
controller: 'DatakeyConfigDialogController',
|
|
|
|
|
controllerAs: 'vm',
|
|
|
|
|
templateUrl: datakeyConfigDialogTemplate,
|
|
|
|
|
locals: {
|
|
|
|
|
dataKey: angular.copy(dataKey),
|
|
|
|
|
dataKeySettingsSchema: scope.datakeySettingsSchema,
|
|
|
|
|
entityAlias: scope.entityAlias,
|
2017-06-07 17:09:04 +03:00
|
|
|
aliasController: scope.aliasController
|
2017-05-24 10:39:33 +03:00
|
|
|
},
|
|
|
|
|
parent: angular.element($document[0].body),
|
|
|
|
|
fullscreen: true,
|
|
|
|
|
targetEvent: event,
|
2018-10-29 18:28:15 +02:00
|
|
|
multiple: true,
|
2017-05-24 10:39:33 +03:00
|
|
|
onComplete: function () {
|
|
|
|
|
var w = angular.element($window);
|
|
|
|
|
w.triggerHandler('resize');
|
|
|
|
|
}
|
2019-08-09 14:35:20 +03:00
|
|
|
}).then(function (newDataKey) {
|
|
|
|
|
if ((newDataKey.type === types.dataKeyType.timeseries) || (newDataKey.type === types.dataKeyType.attribute)) {
|
2019-09-09 11:57:39 +03:00
|
|
|
let index = scope.dataKeys.indexOf(dataKey);
|
2019-08-09 14:35:20 +03:00
|
|
|
scope.dataKeys[index] = newDataKey;
|
|
|
|
|
} else if (newDataKey.type === types.dataKeyType.alarm) {
|
2019-09-09 11:57:39 +03:00
|
|
|
let index = scope.alarmDataKeys.indexOf(dataKey);
|
2019-08-09 14:35:20 +03:00
|
|
|
scope.alarmDataKeys[index] = newDataKey;
|
2017-05-24 10:39:33 +03:00
|
|
|
}
|
|
|
|
|
ngModelCtrl.$setDirty();
|
|
|
|
|
}, function () {
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-10 12:08:02 +02:00
|
|
|
scope.dataKeysSearch = function (searchText) {
|
2017-06-14 13:43:18 +03:00
|
|
|
if (scope.widgetType == types.widgetType.alarm.value) {
|
|
|
|
|
var dataKeys = searchText ? scope.alarmFields.filter(
|
|
|
|
|
scope.createFilterForDataKey(searchText)) : scope.alarmFields;
|
|
|
|
|
return dataKeys;
|
2017-05-24 10:39:33 +03:00
|
|
|
} else {
|
2017-06-14 13:43:18 +03:00
|
|
|
if (scope.entityAlias) {
|
|
|
|
|
var deferred = $q.defer();
|
2019-05-10 12:08:02 +02:00
|
|
|
scope.fetchEntityKeys({entityAliasId: scope.entityAlias.id, query: searchText, type: types.dataKeyType.timeseries})
|
2017-06-14 13:43:18 +03:00
|
|
|
.then(function (dataKeys) {
|
2019-05-10 12:08:02 +02:00
|
|
|
var items = [];
|
|
|
|
|
for (var i = 0; i < dataKeys.length; i++) {
|
|
|
|
|
items.push({ name: dataKeys[i], type: types.dataKeyType.timeseries });
|
|
|
|
|
}
|
|
|
|
|
if (scope.widgetType == types.widgetType.latest.value) {
|
|
|
|
|
scope.fetchEntityKeys({entityAliasId: scope.entityAlias.id, query: searchText, type: types.dataKeyType.attribute})
|
|
|
|
|
.then(function (dataKeys) {
|
|
|
|
|
for (var i = 0; i < dataKeys.length; i++) {
|
|
|
|
|
items.push({ name: dataKeys[i], type: types.dataKeyType.attribute });
|
|
|
|
|
}
|
|
|
|
|
deferred.resolve(items);
|
|
|
|
|
}, function (e) {
|
|
|
|
|
deferred.reject(e);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
deferred.resolve(items);
|
2017-06-14 13:43:18 +03:00
|
|
|
}, function (e) {
|
|
|
|
|
deferred.reject(e);
|
|
|
|
|
});
|
|
|
|
|
return deferred.promise;
|
|
|
|
|
} else {
|
|
|
|
|
return $q.when([]);
|
|
|
|
|
}
|
2017-05-24 10:39:33 +03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-14 13:43:18 +03:00
|
|
|
scope.createFilterForDataKey = function (query) {
|
|
|
|
|
var lowercaseQuery = angular.lowercase(query);
|
|
|
|
|
return function filterFn(dataKey) {
|
|
|
|
|
return (angular.lowercase(dataKey).indexOf(lowercaseQuery) === 0);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-10 12:08:02 +02:00
|
|
|
scope.createKey = function (event, type, chipsId) {
|
2017-05-24 10:39:33 +03:00
|
|
|
var chipsChild = $(chipsId, element)[0].firstElementChild;
|
|
|
|
|
var el = angular.element(chipsChild);
|
|
|
|
|
var chipBuffer = el.scope().$mdChipsCtrl.getChipBuffer();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
2019-05-10 12:08:02 +02:00
|
|
|
el.scope().$mdChipsCtrl.appendChip({ name: chipBuffer.trim(), type: type});
|
2017-05-24 10:39:33 +03:00
|
|
|
el.scope().$mdChipsCtrl.resetChipBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$compile(element.contents())(scope);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
restrict: "E",
|
|
|
|
|
require: "^ngModel",
|
|
|
|
|
scope: {
|
|
|
|
|
widgetType: '=',
|
2017-06-19 19:22:48 +03:00
|
|
|
maxDataKeys: '=',
|
2017-07-12 19:28:53 +03:00
|
|
|
optDataKeys: '=',
|
2017-06-07 17:09:04 +03:00
|
|
|
aliasController: '=',
|
2017-05-24 10:39:33 +03:00
|
|
|
datakeySettingsSchema: '=',
|
|
|
|
|
generateDataKey: '&',
|
|
|
|
|
fetchEntityKeys: '&',
|
|
|
|
|
onCreateEntityAlias: '&'
|
|
|
|
|
},
|
|
|
|
|
link: linker
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* eslint-enable angular/angularelement */
|