legend-config.directive.js
Home
/
ui /
src /
app /
components /
legend-config.directive.js
import './legend-config.scss';
import $ from 'jquery';
import legendConfigButtonTemplate from './legend-config-button.tpl.html';
import legendConfigPanelTemplate from './legend-config-panel.tpl.html';
import LegendConfigPanelController from './legend-config-panel.controller';
export default angular.module('thingsboard.directives.legendConfig', [])
.controller('LegendConfigPanelController', LegendConfigPanelController)
.directive('tbLegendConfig', LegendConfig)
.name;
function LegendConfig($compile, $templateCache, types, $mdPanel, $document) {
var linker = function (scope, element, attrs, ngModelCtrl) {
var template = $templateCache.get(legendConfigButtonTemplate);
element.html(template);
scope.openEditMode = function (event) {
if (scope.disabled) {
return;
}
var position;
var panelHeight = 220;
var panelWidth = 220;
var offset = element[0].getBoundingClientRect();
var bottomY = offset.bottom - $(window).scrollTop();
var leftX = offset.left - $(window).scrollLeft();
var yPosition;
var xPosition;
if (bottomY + panelHeight > $( window ).height()) {
yPosition = $mdPanel.yPosition.ABOVE;
} else {
yPosition = $mdPanel.yPosition.BELOW;
}
if (leftX + panelWidth > $( window ).width()) {
xPosition = $mdPanel.xPosition.ALIGN_END;
} else {
xPosition = $mdPanel.xPosition.ALIGN_START;
}
position = $mdPanel.newPanelPosition()
.relativeTo(element)
.addPanelPosition(xPosition, yPosition);
var config = {
attachTo: angular.element($document[0].body),
controller: 'LegendConfigPanelController',
controllerAs: 'vm',
templateUrl: legendConfigPanelTemplate,
panelClass: 'tb-legend-config-panel',
position: position,
fullscreen: false,
locals: {
'legendConfig': angular.copy(scope.model),
'onLegendConfigUpdate': function (legendConfig) {
scope.model = legendConfig;
scope.updateView();
}
},
openFrom: event,
clickOutsideToClose: true,
escapeToClose: true,
focusOnOpen: false
};
$mdPanel.open(config);
}
scope.updateView = function () {
var value = {};
var model = scope.model;
value.position = model.position;
value.showMin = model.showMin;
value.showMax = model.showMax;
value.showAvg = model.showAvg;
value.showTotal = model.showTotal;
ngModelCtrl.$setViewValue(value);
}
ngModelCtrl.$render = function () {
if (ngModelCtrl.$viewValue) {
var value = ngModelCtrl.$viewValue;
if (!scope.model) {
scope.model = {};
}
var model = scope.model;
model.position = value.position || types.position.bottom.value;
model.showMin = angular.isDefined(value.showMin) ? value.showMin : false;
model.showMax = angular.isDefined(value.showMax) ? value.showMax : false;
model.showAvg = angular.isDefined(value.showAvg) ? value.showAvg : true;
model.showTotal = angular.isDefined(value.showTotal) ? value.showTotal : false;
} else {
scope.model = {
position: types.position.bottom.value,
showMin: false,
showMax: false,
showAvg: true,
showTotal: false
}
}
}
$compile(element.contents())(scope);
}
return {
restrict: "E",
require: "^ngModel",
scope: {
disabled:'=ngDisabled'
},
link: linker
};
}