forked from enguyen/io-menu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathio-menu-option.html
315 lines (304 loc) · 8.46 KB
/
io-menu-option.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<!-- TODO: add deps? -->
<link rel="import" href="../io-tooltip/io-tooltip.html">
<link rel="import" href="../io-help/io-help.html">
<link rel="import" href="io-menu-group.html">
<dom-module id="io-menu-option">
<style>
:host {
position: relative;
padding: 0 0.4em 0.125em 0.4em;
display: -webkit-flex;
display: flex;
}
:host([opened]) {
background: rgba(63, 127, 255, 0.5);
color: #fff;
}
:host(:focus) {
outline: 0;
}
:host([opened]:focus) {
background: rgba(63, 127, 255, 1.0);
color: #fff;
}
:host([inactive]),
:host([disabled]) {
opacity: 0.5;
}
:host([type=separator]) {
pointer-events: none;
height: 0px;
border-top: 1px solid rgba(0, 0, 0, 0.2);
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
margin: 0.125em 0;
}
:host([type=separator]) > * {
display: none;
}
iron-icon {
top: 0.1em;
width: 1em !important;
height: 1em !important;
}
.expandicon {
top: 0.05em !important;
}
.label,
.info {
white-space: nowrap;
cursor: default;
margin: 0 0.25em;
-webkit-flex: 1;
flex: 1;
}
.info {
text-align: right;
}
</style>
<template>
<iron-icon class="icon" icon="{{icon}}"></iron-icon>
<span class="label">{{label}}</span>
<span class="info">{{info}}</span>
<template is="dom-if" if="{{options}}">
<iron-icon class="expandicon" icon="icons:chevron-right"></iron-icon>
</template>
<template is="dom-if" if="{{tooltip}}" restamp>
<io-tooltip message="{{tooltip}}"></io-tooltip>
</template>
<template is="dom-if" if="{{help}}" restamp>
<io-help message="{{help}}"></io-help>
</template>
</template>
</dom-module>
<script>
(function() {
var x = 0, y = 0, v = 0;
var previousOption;
var previousParent;
var timeoutOpen;
var timeoutReset;
var WAIT_TIME = 1000;
Polymer({
is: 'io-menu-option',
properties: {
opened: {
type: Boolean,
observer: '_openedChanged',
reflectToAttribute: true
},
/**
* Array of item objects.
*/
options: {
type: Array
},
/**
* Label to be displayed with the option
*/
label: {
type: String,
},
/**
* Secondary label/information to be displayed below the label.
*/
info: {
type: String
},
/**
* Icon to be displayed with the option
*/
icon: {
type: String,
notify: true
},
/**
* Function to be fired when option is selected
*/
action: {
type: Function
},
/**
* value to be passed to action when invoked
*/
value: {
type: Array
},
/**
* option is disabled if true
*/
disabled: {
type: Boolean,
reflectToAttribute: true
},
/**
* Tooltip to be displayed with the option
*/
tooltip: {
value: '',
type: String
},
/**
* option is disabled if true
*/
type: {
value: '',
type: String,
reflectToAttribute: true
},
/**
* option is inactive is no action, options or value is set
*/
inactive: {
type: Boolean,
reflectToAttribute: true
},
/**
* Reference to a button element in case the action requires button to be clicked.
*/
// TODO
button: {
type: HTMLElement
},
//
_menuGroup: {
type: HTMLElement
}
},
hostAttributes: {
tabindex: 1
},
listeners: {
'mousemove': '_onMousemove',
'touchmove': '_onMousemove',
'mousedown': '_onMousedown',
'keydown': '_onKeydown',
'click': '_onClick'
},
created: function() {
this._menuGroup = document.createElement('io-menu-group');
this._menuGroup._parent = this;
},
ready: function() {
this.inactive = !this.action && !this.value && (!this.options || !this.options.length);
},
_onMousemove: function(event) {
if (event.changedTouches) event = event.changedTouches[0];
event.clientX = event.clientX;
event.clientY = event.clientY;
v = Math.abs(event.clientY - y) - Math.abs(event.clientX - x);
x = event.clientX;
y = event.clientY;
if (this !== previousOption) {
clearTimeout(timeoutOpen);
clearTimeout(timeoutReset);
if (v > 0 || this.parentNode !== previousParent) {
previousOption = this;
this._openGroup();
} else {
timeoutOpen = setTimeout(function() {
previousOption = this;
this._openGroup();
}.bind(this), WAIT_TIME);
}
previousParent = this.parentNode;
timeoutReset = setTimeout(function() {
previousOption = null;
previousParent = null;
}.bind(this), WAIT_TIME + 1);
}
},
_onMousedown: function(event) {
this._openGroup();
},
_onKeydown: function(event) {
var siblings = Array.prototype.slice.call(this.parentNode.querySelectorAll(
'io-menu-option:not([type=separator]):not([disabled]):not([inactive])'));
var index = siblings.indexOf(this);
if (event.which == 13) {
if (this.disabled) return;
event.preventDefault();
this._onClick(event); // TODO: test
} else if (event.which == 37) { // LEFT
event.preventDefault();
if (this.parentNode._parent) {
this.parentNode._parent._openGroup();
}
} else if (event.which == 38) { // UP
event.preventDefault();
if (this.parentNode.search && index === 0) {
this.parentNode.$$('input').focus();
// TODO: make work when first is inactive
} else {
siblings[(index + siblings.length - 1) % (siblings.length)]._openGroup();
}
} else if (event.which == 39) { // RIGHT
event.preventDefault();
if (this.options && this.options.length) {
this._menuGroup.querySelector('io-menu-option')._openGroup();
// TODO: if #1 is inactive/disabled, find first available option.
}
} else if (event.which == 40) { // DOWN
event.preventDefault();
if (this.parentNode.search && index === siblings.length - 1) {
this.parentNode.$$('input').focus();
// TODO: make work when last is inactive
} else {
siblings[(index + 1) % (siblings.length)]._openGroup();
}
} else if (event.which == 9) { // TAB
event.preventDefault();
if (this.options && this.options.length) {
this._menuGroup.querySelector('io-menu-option')._openGroup();
} else if (index < siblings.length - 1) {
siblings[(index + 1)]._openGroup();
} else if (this.parentNode._parent) {
//TODO: find first available option in sequence.
}
} else if (event.which == 27) { // ESC
event.preventDefault();
this.fire('io-menu-option-clicked', this);
}
},
_onClick: function(event) {
if (this.disabled) return;
if (this.button) {
// TODO: test
this.button.click();
}
if (typeof this.action === 'function') {
this.action.apply(null, [this.value]);
this.fire('io-menu-option-clicked', this);
}
if (typeof this.action === 'function' || this.value || this.button) {
// TODO: doc
this.fire('io-menu-option-clicked', this);
}
},
_openGroup: function() {
this.parentNode._closeSubmenus();
if (!this.disabled && this.options && this.options.length && this.parentNode.opened) {
var rect = this.getBoundingClientRect();
this._menuGroup.x = rect.right + 1;
this._menuGroup.y = rect.top - 4;
this._menuGroup.options = this.options;
this.options._menuGroup = this._menuGroup;
this.fire('io-menu-open-group', this._menuGroup);
}
if (!this.disabled && !this.inactive) {
this.opened = true;
this.focus();
}
},
_openedChanged: function() {
if (this.opened) {
this.setAttribute('tabindex', 1);
} else {
this.removeAttribute('tabindex');
}
}
});
}());
</script>