-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
138 lines (103 loc) · 2.6 KB
/
index.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
/**
* Confirmation Module
*
* Super simple confirmation dialogue following in the footsteps
* of https://github.com/component/dialog but without jquery.
*
* I'm generally of the opinion that a dialogue should always have a confirmation
* button of some sort, so I've wrapped dialogue & confirmation into one module.
*/
var Emitter = require('emitter');
module.exports = function(attributes, options){
return new Confirmation(attributes, options);
}
var active;
// Constructor
var Confirmation = function (attributes, options) {
this.attributes = attributes || {};
options = options || {};
this.template = options.template || require('./template');
this.isShown = false;
this.el = this._render();
if (active && active.isShown)
active.hide();
active = this;
if (attributes.okay) this.okay();
if (attributes.cancel) this.cancel();
}
Confirmation.prototype = new Emitter();
// Functions
Confirmation.prototype._render = function(){
var self = this
, el = document.createElement('div')
, html = self.template(self.attributes);
el.className = 'confirmation hide';
el.innerHTML = html;
el.setAttribute('tabindex', '-1');
setTimeout(function(){
el.className = el.className.replace( /(?:^|\s)hide(?!\S)/g , '' );
}, 0);
return el;
};
Confirmation.prototype.show = function(){
if (this.isShown)
return
var el = this.el;
document.querySelector('body').appendChild(el);
this.isShown = true;
this.emit('show');
el.focus();
return this;
};
Confirmation.prototype.hide = function(){
var self = this
, el = self.el;
if (!this.isShown)
return
if (self.animate) {
el.className += ' hide';
clearTimeout(this.timer);
this.timer = setTimeout(function(){
self.remove();
}, 400);
} else {
self.remove();
}
self.isShown = false;
self.emit('hide');
};
Confirmation.prototype.remove = function(){
var self = this
, el = self.el;
el.parentNode.removeChild(self.el);
self.emit('hidden');
};
Confirmation.prototype.okay = function(callback){
var self = this
, el = self.el;
el.querySelector('.ok').onclick = function(e){
if (callback) callback(e);
self.emit('okay');
self.hide();
}
return this;
};
Confirmation.prototype.cancel = function(callback){
var self = this
, el = self.el;
el.querySelector('.cancel').onclick = function(e){
if (callback) callback(e);
self.emit('cancel');
self.hide();
}
return this;
};
Confirmation.prototype.addClass = function(name){
this.el.className += ' '+name;
return this;
};
Confirmation.prototype.effect = function(type){
this.animate = type;
this.el.className += ' '+type;
return this;
}