aliases-entity-select.directive.js
5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* 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.
*/
import './aliases-entity-select.scss';
import $ from 'jquery';
/* eslint-disable import/no-unresolved, import/default */
import aliasesEntitySelectButtonTemplate from './aliases-entity-select-button.tpl.html';
import aliasesEntitySelectPanelTemplate from './aliases-entity-select-panel.tpl.html';
/* eslint-enable import/no-unresolved, import/default */
/* eslint-disable angular/angularelement */
/*@ngInject*/
export default function AliasesEntitySelectDirective($compile, $templateCache, $mdMedia, types, $mdPanel, $document, $translate) {
var linker = function (scope, element, attrs) {
/* tbAliasesEntitySelect (ng-model)
* {
* "aliasId": {
* alias: alias,
* entityType: entityType,
* entityId: entityId
* }
* }
*/
var template = $templateCache.get(aliasesEntitySelectButtonTemplate);
scope.tooltipDirection = angular.isDefined(attrs.tooltipDirection) ? attrs.tooltipDirection : 'top';
element.html(template);
scope.openEditMode = function (event) {
if (scope.disabled) {
return;
}
var position;
var panelHeight = $mdMedia('min-height: 350px') ? 250 : 150;
var panelWidth = 300;
var offset = element[0].getBoundingClientRect();
var bottomY = offset.bottom - $(window).scrollTop(); //eslint-disable-line
var leftX = offset.left - $(window).scrollLeft(); //eslint-disable-line
var yPosition;
var xPosition;
if (bottomY + panelHeight > $( window ).height()) { //eslint-disable-line
yPosition = $mdPanel.yPosition.ABOVE;
} else {
yPosition = $mdPanel.yPosition.BELOW;
}
if (leftX + panelWidth > $( window ).width()) { //eslint-disable-line
xPosition = $mdPanel.xPosition.CENTER;
} else {
xPosition = $mdPanel.xPosition.ALIGN_START;
}
position = $mdPanel.newPanelPosition()
.relativeTo(element)
.addPanelPosition(xPosition, yPosition);
var config = {
attachTo: angular.element($document[0].body),
controller: 'AliasesEntitySelectPanelController',
controllerAs: 'vm',
templateUrl: aliasesEntitySelectPanelTemplate,
panelClass: 'tb-aliases-entity-select-panel',
position: position,
fullscreen: false,
locals: {
'aliasController': scope.aliasController,
'onEntityAliasesUpdate': function () {
scope.updateView();
}
},
openFrom: event,
clickOutsideToClose: true,
escapeToClose: true,
focusOnOpen: false
};
$mdPanel.open(config);
}
scope.$on('entityAliasesChanged', function() {
scope.updateView();
});
scope.$on('entityAliasResolved', function() {
scope.updateView();
});
scope.updateView = function () {
updateDisplayValue();
}
function updateDisplayValue() {
var displayValue;
var singleValue = true;
var currentAliasId;
var entityAliases = scope.aliasController.getEntityAliases();
for (var aliasId in entityAliases) {
var entityAlias = entityAliases[aliasId];
if (!entityAlias.filter.resolveMultiple) {
var resolvedAlias = scope.aliasController.getInstantAliasInfo(aliasId);
if (resolvedAlias && resolvedAlias.currentEntity) {
if (!currentAliasId) {
currentAliasId = aliasId;
} else {
singleValue = false;
break;
}
}
}
}
if (singleValue && currentAliasId) {
var aliasInfo = scope.aliasController.getInstantAliasInfo(currentAliasId);
displayValue = aliasInfo.currentEntity.name;
} else {
displayValue = $translate.instant('entity.entities');
}
scope.displayValue = displayValue;
}
$compile(element.contents())(scope);
}
return {
restrict: "E",
scope: {
aliasController:'='
},
link: linker
};
}
/* eslint-enable angular/angularelement */