-
Notifications
You must be signed in to change notification settings - Fork 141
/
tether-tooltip-and-popover.js
492 lines (371 loc) · 11.5 KB
/
tether-tooltip-and-popover.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import Ember from 'ember';
import EmberTetherComponent from 'ember-tether/components/ember-tether';
const { $, computed, observer, run, on } = Ember;
const defaultPosition = 'center';
let tooltipOrPopoverCounterId = 0;
function cleanNumber(stringOrNumber) {
let cleanNumber;
if (stringOrNumber && typeof stringOrNumber === 'string') {
cleanNumber = parseInt(stringOrNumber, 10);
/* Remove invalid parseInt results */
if (isNaN(cleanNumber) || !isFinite(cleanNumber)) {
cleanNumber = 0;
}
} else {
cleanNumber = stringOrNumber;
}
return cleanNumber;
}
export default EmberTetherComponent.extend({
passedPropertiesObject: null,
setPropertiesWithPassedPropertiesObject: on('didReceiveAttrs', function() {
this._super(...arguments);
let passedPropertiesObject = this.get('passedPropertiesObject');
if (passedPropertiesObject) {
this.setProperties(passedPropertiesObject);
}
}),
/* Options */
delay: 0,
delayOnChange: true,
duration: 0,
effect: 'slide', // fade, slide, none
event: 'hover', // hover, click, focus, none
hideOn: null,
role: 'tooltip',
side: 'top',
showOn: null,
spacing: 10,
tabindex: '0', // A positive integer (to enable) or -1 (to disable)
isShown: false,
tooltipIsVisible: computed.deprecatingAlias('isShown', {
id: 'tooltip-and-popover.tooltipIsVisible',
until: '3.0.0',
}),
keepInWindow: true,
/*
When this property changes it repositions the tooltip.
This isn't actually used in the code anywhere - it just
needs to be passed as an attribute so didUpdate is
triggered.
@property updateFor
*/
updateFor: null,
/* Actions */
onDestroy: null,
onHide: null,
onRender: null,
onShow: null,
onTooltipDestroy: computed.deprecatingAlias('onDestroy', {
id: 'tooltip-and-popover.onTooltipDestroy',
until: '3.0.0',
}),
onTooltipHide: computed.deprecatingAlias('onHide', {
id: 'tooltip-and-popover.onTooltipHide',
until: '3.0.0',
}),
onTooltipRender: computed.deprecatingAlias('onRender', {
id: 'tooltip-and-popover.onTooltipRender',
until: '3.0.0',
}),
onTooltipShow: computed.deprecatingAlias('onShow', {
id: 'tooltip-and-popover.onTooltipShow',
until: '3.0.0',
}),
/* Properties */
attributeBindings: ['role', 'tabindex'],
classNameBindings: ['effectClass'],
classPrefix: 'ember-tooltip-or-popover',
_didUpdateTimeoutLength: 1000, // 1000 ms or 0 ms, depending whether in test mode
_hideTimer: null,
_showTimer: null,
_isTetherEnabled: true,
/* CPs */
// attributeBindings are handled asynchronously http://stackoverflow.com/a/18731021/3304337
// this observer makes it sync, which makes testing more consistent
dataTetherEnabledHandler: observer('_isTetherEnabled', function() {
let tetherEnabledString = this.get('_isTetherEnabled') ? 'true' : 'false';
let $element = this.$();
if ($element && $element.attr) {
$element.attr('data-tether-enabled', tetherEnabledString);
}
}),
ariaHiddenHandler: observer('isShown', function() {
let ariaHiddenString = this.get('isShown') ? 'false' : 'true';
let $element = this.$();
if ($element && $element.attr) {
$element.attr('aria-hidden', ariaHiddenString);
}
}),
attachment: computed(function() {
const side = this.get('side');
let horizontalPosition, verticalPosition;
switch(side) {
case 'top':
horizontalPosition = defaultPosition;
verticalPosition = 'bottom';
break;
case 'right':
horizontalPosition = 'left';
verticalPosition = defaultPosition;
break;
case 'bottom':
horizontalPosition = defaultPosition;
verticalPosition = 'top';
break;
case 'left':
horizontalPosition = 'right';
verticalPosition = defaultPosition;
break;
}
return `${verticalPosition} ${horizontalPosition}`;
}),
constraints: computed('keepInWindow', function() {
let constraints;
if (this.get('keepInWindow')) {
constraints = [
{
to: 'window',
attachment: 'together'
}
];
}
return constraints;
}),
effectClass: computed(function() {
return `${this.get('classPrefix')}-${this.get('effect')}`;
}),
positionClass: computed(function() {
const targetAttachment = this.get('targetAttachment');
const classPrefix = this.get('classPrefix');
return `${classPrefix}-${targetAttachment.replace(' ', ` ${classPrefix}-`)}`;
}),
sideIsVertical: computed(function() {
const side = this.get('side');
return side === 'top' || side === 'bottom';
}),
target: computed(function() {
const $element = this.$();
// it's not possible to access the DOM when running in Fastboot
if (!$element) {
return null;
}
const parentElement = $element.parent();
let parentElementId = parentElement && parentElement.attr('id');
if (!parentElementId) {
parentElementId = `target-for-tooltip-or-popover-${tooltipOrPopoverCounterId}`;
tooltipOrPopoverCounterId++;
parentElement.attr('id', parentElementId);
}
return `#${parentElementId}`;
}),
targetAttachment: computed(function() {
const side = this.get('side');
if (!this.get('sideIsVertical')) {
return `center ${side}`;
} else {
return `${side} center`; // top and bottom
}
}),
typeClass: computed(function() {
const type = this.get('type');
const classPrefix = this.get('classPrefix');
return type ? `${classPrefix}-${type}` : null;
}),
/* Private CPs */
_hideOn: computed(function() {
let hideOn = this.get('hideOn');
if (!hideOn) {
const event = this.get('event');
switch (event) {
case 'hover': hideOn = 'mouseleave'; break;
case 'focus': hideOn = 'blur'; break;
case 'ready': hideOn = null; break;
default: hideOn = event; break;
}
}
return hideOn;
}),
_showOn: computed(function() {
let showOn = this.get('showOn');
if (!showOn) {
const event = this.get('event');
switch (event) {
case 'hover': showOn = 'mouseenter'; break;
default: showOn = event; break;
}
}
return showOn;
}),
/* Methods */
hide() {
if (this.get('isDestroying')) {
return;
}
/* If the tooltip is about to be showed by
a delay, stop is being shown. */
run.cancel(this.get('_showTimer'));
this.set('isShown', false);
this.sendAction('onHide', this);
this.stopTether();
},
didInsertElement() {
this._super(...arguments);
if (this.get('_shouldShowOnRender')) {
this.show();
}
const target = this.get('target');
if (!target || target.indexOf('#') === -1) {
Ember.assert('You must specify a target attribute in the format target="#element-id" for the tooltip component');
}
const $target = $(this.get('target'));
const _tether = this.get('_tether');
const $_tether = $(_tether.element);
this.sendAction('onRender', this);
$target.attr({
'aria-describedby': `${this.get('elementId')}`,
tabindex: $target.attr('tabindex') || this.get('tabindex'),
});
// needed so that these 'aria-hidden' and
// 'data-tether-enabled' don't init to undefined
this.ariaHiddenHandler();
this.dataTetherEnabledHandler();
/* When this component has rendered we need
to check if Tether moved its position to keep the
element in bounds */
let renderedSide;
['top', 'right', 'bottom', 'left'].forEach((side) => {
if ($_tether.hasClass(`${this.get('classPrefix')}-target-attached-${side}`)) {
renderedSide = side;
}
});
/* We then use the side the tooltip was *actually*
rendered on to set the correct offset from
the target element */
const spacing = this.get('spacing');
let offset;
switch(renderedSide) {
case 'top':
offset = `${spacing}px 0`;
break;
case 'right':
offset = `0 -${spacing}px`;
break;
case 'bottom':
offset = `-${spacing}px 0`;
break;
case 'left':
offset = `0 ${spacing}px`;
break;
}
this.set('offset', offset);
if (!this.get('isShown')) {
this.stopTether();
}
},
/*
Repositions the tooltip if new attributes or content are
passed to the tooltip.
@method didUpdate
*/
didUpdate() {
this._super(...arguments);
run.later(() => {
this.positionTether();
this.sendAction('onRender', this);
}, this.get('_didUpdateTimeoutLength'));
},
/*
We use an observer so the user can set isShown
as a attribute.
@method setTimer
*/
setTimer: observer('isShown', function() {
const isShown = this.get('isShown');
if (isShown) {
this.startTether();
const duration = cleanNumber(this.get('duration'));
run.cancel(this.get('_hideTimer'));
if (duration) {
/* Hide tooltip after specified duration */
const hideTimer = run.later(this, this.hide, duration);
/* Save timer ID for canceling should an event
hide the tooltip before the duration */
this.set('_hideTimer', hideTimer);
}
} else {
this.stopTether();
}
}),
show() {
// this.positionTether() fixes the issues raised in
// https://github.com/sir-dunxalot/ember-tooltips/issues/75
this.positionTether();
if (this.get('isDestroying')) {
return;
}
const _showTimer = this.get('_showTimer');
let delay = cleanNumber(this.get('delay'));
run.cancel(_showTimer);
if (delay) {
if (!this.get('delayOnChange')) {
/* If the `delayOnChange` property is set to false, we
don't want to delay opening this tooltip/popover if there is
already a tooltip/popover shown in the DOM. Check that here
and adjust the delay as needed. */
let shownTooltipsOrPopovers = Ember.$(`.${this.get('classPrefix')}-element[aria-hidden="false"]`).length;
if (shownTooltipsOrPopovers) {
delay = 0;
}
}
const _showTimer = run.later(this, () => {
if (!this.get('destroying') && !this.get('isDestroyed')) {
this.startTether();
this.set('isShown', true);
}
}, delay);
this.set('_showTimer', _showTimer);
} else {
/* If there is no delay, show the tooltip immediately */
this.startTether();
this.set('isShown', true);
}
this.sendAction('onShow', this);
},
toggle() {
/* We don't use toggleProperty because we centralize
logic for showing and hiding in the show() and hide()
methods. */
if (this.get('isShown')) {
this.hide();
} else {
this.show();
}
},
willDestroy() {
// there's no jQuery when running in Fastboot
const $target = $ && $(this.get('target'));
this.set('effect', null);
this.hide();
if ($target) {
$target.removeAttr('aria-describedby');
$target.off();
}
this._super(...arguments); // Removes tether
this.sendAction('onDestroy', this);
},
startTether() {
// can't depend on `_tether.enabled` because it's not an
// Ember property (so won't trigger cp update when changed)
this.set('_isTetherEnabled', true);
this.get('_tether').enable();
},
stopTether() {
run.schedule('afterRender', () => {
// can't depend on `_tether.enabled` because it's not an
// Ember property (so won't trigger cp update when changed)
this.set('_isTetherEnabled', false);
this.get('_tether').disable();
});
},
});