-
Notifications
You must be signed in to change notification settings - Fork 4
/
cosmoz-treenode-button-view.js
445 lines (425 loc) · 10.2 KB
/
cosmoz-treenode-button-view.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import '@polymer/paper-icon-button/paper-icon-button';
import '@polymer/paper-button/paper-button';
import '@polymer/iron-icons/iron-icons';
import { Debouncer } from '@polymer/polymer/lib/utils/debounce';
import { timeOut } from '@polymer/polymer/lib/utils/async';
import { PolymerElement } from '@polymer/polymer/polymer-element';
import { html } from '@polymer/polymer/lib/utils/html-tag';
import '@neovici/cosmoz-dialog';
import { Tree } from '@neovici/cosmoz-tree';
import { translatable } from '@neovici/cosmoz-i18next';
import './cosmoz-treenode-navigator';
import { getNode, getTreePathParts } from './helpers';
/**
`cosmoz-treenode-navigator`
Navigator through object with treelike datastructure.
@appliesMixin translatable
*/
class CosmozTreenodeButtonView extends translatable(PolymerElement) {
/* eslint-disable-next-line max-lines-per-function */
static get template() {
return html`
<style>
.actions {
display: flex;
}
.open {
flex: auto;
}
.pathToNode {
max-width: 320px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
direction: rtl;
text-align: left;
}
/* Safari only css fix */
_:matches(x),
_:lang(x) + _:-webkit-full-screen-document,
.pathToNode span {
display: inline-block;
}
#chips {
display: flex;
flex-wrap: wrap;
max-width: 90%;
padding: 20px;
overflow-y: auto;
max-height: 30vh;
}
.chip {
border-radius: 500px;
background-color: #e0e0e0;
margin: 1px 4px 1px 0;
white-space: nowrap;
overflow: hidden;
display: flex;
align-items: center;
}
.chip > span {
color: #424242;
margin-left: 10px;
font-size: 13px;
overflow: hidden;
text-overflow: ellipsis;
}
.chip iron-icon {
margin: 2px 4px;
color: #cdcdcd;
background-color: #a6a6a6;
border-radius: 500px;
cursor: pointer;
min-width: 16px;
width: 16px;
min-height: 16px;
height: 16px;
}
#dialogTree {
min-width: 250px !important;
width: 450px;
}
</style>
<div class="actions">
<paper-button
part="button"
class="open"
raised
on-click="openDialogTree"
title="[[ buttonText ]]"
>
<div class="pathToNode">‎<span>[[ buttonText ]]</span></div>
</paper-button>
<paper-icon-button
part="clear"
icon="clear"
on-click="reset"
hidden$="[[ !_enableReset(nodePath, noReset) ]]"
></paper-icon-button>
</div>
<template
is="dom-if"
if="[[ _showSelectedNodes(multiSelection, selectedNodes.length) ]]"
>
<div id="chips" class="row">
<template is="dom-repeat" items="[[ selectedNodes ]]">
<div class="chip">
<span>[[ _getChipText(item, tree) ]]</span
><iron-icon
icon="clear"
on-click="_clearItemSelection"
></iron-icon>
</div>
</template>
</div>
</template>
<cosmoz-dialog
id="dialogTree"
class="treeDialog"
on-iron-overlay-opened="onOpened"
on-iron-overlay-closed="onClosed"
modal
prerender
>
<template>
<h2>[[ dialogText ]]</h2>
<cosmoz-treenode-navigator
id="treeNavigator"
class="no-padding"
tree="[[ tree ]]"
selected-node="{{ selectedNode }}"
on-data-plane-changed="refit"
highlighted-node-path="{{ highlightedNodePath }}"
search-placeholder="[[ searchPlaceholder ]]"
search-global-placeholder="[[ searchGlobalPlaceholder ]]"
search-min-length="[[ searchMinLength ]]"
node-path="{{ nodePath }}"
nodes-on-node-path="{{ nodesOnNodePath }}"
on-node-dblclicked="_selectNodeAndCloseDialog"
on-select-node="selectNode"
opened="[[ opened ]]"
>
<slot></slot>
</cosmoz-treenode-navigator>
<div class="buttons">
<paper-button
disabled="[[!highlightedNodePath]]"
autofocus
on-click="selectNode"
>[[ _('Select', t) ]]</paper-button
>
<paper-button dialog-dismiss>[[ _('Cancel', t) ]]</paper-button>
</div>
</template>
</cosmoz-dialog>
`;
}
static get is() {
return 'cosmoz-treenode-button-view';
}
/* eslint-disable-next-line max-lines-per-function */
static get properties() {
return {
multiSelection: {
type: Boolean,
value: false,
},
/*
* The main node structure
*/
tree: {
type: Tree,
},
/*
* Currently selected node object
*/
selectedNode: {
type: Object,
value() {
return {};
},
notify: true,
},
/**
* Selected nodes
*/
selectedNodes: {
type: Array,
notify: true,
value: () => [],
},
/**
* If true, reset button gets hidden
*/
noReset: {
type: Boolean,
value: false,
},
/**
* If true opened
*/
opened: {
type: Boolean,
value: false,
},
/*
* Placeholder for the search field
*/
searchPlaceholder: {
type: String,
},
/*
* Placeholder for button text
*/
buttonTextPlaceholder: {
type: String,
computed: 'getButtonTextPlaceholder(multiSelection)',
},
buttonText: {
type: String,
computed: '_getButtonLabel(nodesOnNodePath, buttonTextPlaceholder)',
},
/*
* The path of the selected node
*/
nodePath: {
type: String,
notify: true,
},
/*
* The nodes on the path of the selected node
*/
nodesOnNodePath: {
type: Array,
},
/*
* Text displayed when local search has finished
* to suggest a search on the entire tree
*/
searchGlobalPlaceholder: {
type: String,
value: 'Click to search again but globally.',
},
/*
* Settable text for dialog title.
*/
dialogText: {
type: String,
value: 'Search or navigate to chosen destination',
},
/*
* Minimum length before an search
* starts.
*/
searchMinLength: {
type: Number,
value: 1,
},
/*
* Path string of highlighted (focused) node
*/
highlightedNodePath: {
type: String,
},
};
}
/**
* Event handler for node chip removal button, removes a node chip.
* @param {object} event Polymer event object.
* @returns {void}
*/
_clearItemSelection(event) {
const item = event.model.item,
selectedIndex = this.selectedNodes.indexOf(item);
// This will remove from the DOM the source element of the processed event ...
this.splice('selectedNodes', selectedIndex, 1);
// ... so we must prevent further propagation of this event, because its source is now invalid.
// (This has caused troubles in app-drawer-layout click event handler).
if (!this.selectedNodes.length) {
this.reset();
}
event.preventDefault();
event.stopPropagation();
}
/**
* Get a text label for the node selection button.
* @param {boolean} multiSelection Multi selection setting.
* @returns {string} Text label.
*/
getButtonTextPlaceholder(multiSelection) {
return multiSelection
? this._('Select a node')
: this._('No selection made');
}
/**
* Whether the reset button should be enabled or not.
* @param {string} nodePath Node path to check.
* @param {boolean} noReset Bypass to force disabled.
* @returns {boolean} Whether the button should be enabled or not.
*/
_enableReset(nodePath, noReset) {
if (noReset) {
return false;
}
return !!nodePath;
}
/**
* Get a button label based on path parts or a placeholder.
* @param {array} pathParts Nodes on the node path.
* @param {string} placeholder Replacement placeholder if no nodes are available.
* @returns {string} Button label.
*/
_getButtonLabel(pathParts, placeholder) {
if (!Array.isArray(pathParts) || pathParts.length === 0) {
return placeholder;
}
return pathParts
.filter((n) => n)
.map((part) => part[this.tree.searchProperty])
.join(' / ');
}
/**
* Get text from a node to set on a node chip.
* @param {object} node Node to get text from.
* @returns {string} Chip text.
*/
_getChipText(node) {
return node.name;
}
/**
* Open the treenode navigator dialog.
* @returns {void}
*/
openDialogTree() {
this.$.dialogTree.open();
}
/**
* Focus on the treenode navigator in the treenode navigator dialog.
* @returns {void}
*/
focusSearch() {
this.$.dialogTree.paperDialog
.querySelector('#treeNavigator')
.shadowRoot.querySelector('cosmoz-input')
.focus();
}
/**
* Reset the component to make it ready for reuse
* @returns {void}
*/
reset() {
this.nodePath = '';
this.selectedNode = {};
this.nodesOnNodePath = [];
this.selectedNodes = [];
this.highlightedNodePath = '';
}
/**
* Select the node in the treenode navigator.
* @returns {void}
*/
selectNode() {
// nodePath selects the node, without it no selectedNode
this.selectedNode = getNode(
this.highlightedNodePath || this.nodePath,
this.tree
);
if (this.highlightedNodePath) {
this.nodePath = this.highlightedNodePath;
}
this.nodesOnNodePath = getTreePathParts(
this.highlightedNodePath || this.nodePath,
this.tree
);
if (this.multiSelection) {
if (
!this.selectedNodes.some(
(node) => node.pathLocator === this.highlightedNodePath
)
) {
this.push('selectedNodes', this.selectedNode);
}
this.nodePath = '';
this.selectedNode = {};
}
this.$.dialogTree.close();
}
/**
* Selects node and closes the dialog
* @returns {void}
*/
_selectNodeAndCloseDialog() {
this.selectNode();
}
/**
* Determine if selected nodes container should be visible or not.
* @param {boolean} multiSelection Multi selection setting.
* @param {number} selectedNodesLength Selected nodes quantity.
* @returns {boolean} Whether the selected nodes container should be visible or not.
*/
_showSelectedNodes(multiSelection, selectedNodesLength) {
return multiSelection && selectedNodesLength > 0;
}
/**
* Callback event handler to refit the treenode navigator dialog when
* data plane has changed.
* @returns {void}
*/
refit() {
this._debouncer = Debouncer.debounce(
this._debouncer,
timeOut.after(50), // 5 was enough during test
() => {
this.$.dialogTree.fit();
}
);
}
onOpened() {
this.opened = true;
this.focusSearch();
}
onClosed() {
this.opened = false;
}
}
customElements.define(CosmozTreenodeButtonView.is, CosmozTreenodeButtonView);