-
Notifications
You must be signed in to change notification settings - Fork 332
/
paper-toast.js
134 lines (110 loc) · 3.72 KB
/
paper-toast.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
/**
* @module ember-paper
*/
import { inject as service } from '@ember/service';
import { or } from '@ember/object/computed';
import Component from '@ember/component';
import { computed } from '@ember/object';
import { run } from '@ember/runloop';
import { guidFor } from '@ember/object/internals';
import { getOwner } from '@ember/application';
import layout from '../templates/components/paper-toast';
import { invokeAction } from 'ember-invoke-action';
/**
* @class PaperToast
* @extends Ember.Component
*/
export default Component.extend({
layout,
tagName: '',
escapeToClose: false,
swipeToClose: true,
capsule: false,
duration: 3000,
position: 'bottom left',
left: computed('position', function() {
let [, x] = this.get('position').split(' ');
return x === 'left';
}),
top: computed('position', function() {
let [y] = this.get('position').split(' ');
return y === 'top';
}),
// Calculate a default that is always valid for the parent of the backdrop.
wormholeSelector: '#paper-toast-fab-wormhole',
defaultedParent: or('parent', 'wormholeSelector'),
// Calculate the id of the wormhole destination, setting it if need be. The
// id is that of the 'parent', if provided, or 'paper-wormhole' if not.
destinationId: computed('defaultedParent', function() {
let config = getOwner(this).resolveRegistration('config:environment');
if (config.environment === 'test' && !this.get('parent')) {
return '#ember-testing';
}
let parent = this.get('defaultedParent');
let parentEle = typeof parent === 'string'
? document.querySelector(parent)
: parent;
// If the parent isn't found, assume that it is an id, but that the DOM doesn't
// exist yet. This only happens during integration tests or if entire application
// route is a dialog.
if (typeof parent === 'string' && parent.charAt(0) === '#') {
return `#${parent.substring(1)}`;
} else {
let { id } = parentEle;
if (!id) {
id = `${this.uniqueId}-parent`;
parentEle.id = id;
}
return `#${id}`;
}
}),
// Find the element referenced by destinationId
destinationEl: computed('destinationId', function() {
return document.querySelector(this.get('destinationId'));
}),
constants: service(),
_destroyMessage() {
if (!this.isDestroyed) {
invokeAction(this, 'onClose');
}
},
init() {
this._super(...arguments);
this.uniqueId = guidFor(this);
},
willInsertElement() {
this._super(...arguments);
document.querySelector(this.get('destinationId')).classList.add('md-toast-animating');
},
didInsertElement() {
this._super(...arguments);
if (this.get('duration') !== false) {
run.later(this, '_destroyMessage', this.get('duration'));
}
if (this.get('escapeToClose')) {
// Adding Listener to body tag, FIXME
this._escapeToClose = run.bind(this, (e) => {
if (e.keyCode === this.get('constants.KEYCODE.ESCAPE') && this.get('onClose')) {
this._destroyMessage();
}
});
document.body.addEventListener('keydown', this._escapeToClose);
}
let y = this.get('top') ? 'top' : 'bottom';
document.querySelector(this.get('destinationId')).classList.add(`md-toast-open-${y}`);
},
willDestroyElement() {
this._super(...arguments);
if (this.get('escapeToClose')) {
document.body.removeEventListener('keydown', this._escapeToClose);
this._escapeToClose = null;
}
let y = this.get('top') ? 'top' : 'bottom';
document.querySelector(this.get('destinationId')).classList.remove(`md-toast-open-${y}`, 'md-toast-animating');
},
swipeAction() {
if (this.get('swipeToClose')) {
invokeAction(this, 'onClose');
}
}
});