-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
228 lines (186 loc) · 4.31 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
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
'use strict';
/**
* Module dependencies.
*/
var Emitter = require('events').EventEmitter;
var util = require('util');
var inherits = util.inherits;
var extend = util._extend;
/**
* Modal template.
*/
var template = require('./template.jade');
/**
* jQuery or Zepto
*/
var $ = window.Zepto || window.jQuery;
/**
* DOM elements
*/
var $body = $('body');
var $document = $(window.document);
/**
* Defaults
*/
var defaults = {
'closable': true
};
/**
* Creates a new Modal.
* @constructor
* @augments Emitter
* @param {Object} [options] - Options to customize a modal.
* @param {(String | HTMLElement)} [options.content] - The content to be shown into a modal.
* @param {Boolean} [options.closable] - Enables or disables if the user can close a modal.
* @returns {modal} - Returns a new instance of Modal.
* @example var modal = new Modal(options);
*/
function Modal(options) {
options = options || {};
// Clone defaults options.
this._options = extend({}, defaults);
extend(this._options, options);
/**
* Modal dom element.
* @type {dom}
* @public
*/
this.$modal = $(template());
/**
* Content dom element.
* @type {dom}
* @public
*/
this.$content = $('.modal-content', this.$modal);
if (this._options.content) {
this.$content.html(this._options.content);
}
this.closable(this._options.closable);
return this;
};
/**
* Inherits from Emitter.
*/
inherits(Modal, Emitter);
/**
* Enables or disables if the user can close a modal.
* @function
* @param {Boolean} [closable] - If the value is false, the user can't close a modal.
* @returns {modal} - Returns an instance of Modal.
* @example
* modal.closable();
* @example
* modal.closable(false);
*/
Modal.prototype.closable = function(closable) {
closable = closable === undefined ? true : closable;
this._options.closable = closable;
this._closable(closable);
return this;
};
/**
* Enables or disables if the user can close a modal.
* @function
* @private
* @param {Boolean} [closable] - If the value is false, the user can't close a modal.
* @returns {modal} - Returns an instance of Modal.
*/
Modal.prototype._closable = function (closable) {
var self = this;
if (closable === false) {
this.$modal.off('click');
$document.off('keydown');
return this;
}
// Close button and overlay
this.$modal.on('click', function(eve) {
var classname = eve.target.className;
if (classname !== 'modal-container' && classname !== 'modal-close') { return; }
self.hide();
});
// Esc
$document.on('keydown', function(eve) {
if (eve.keyCode !== 27 || document.activeElement !== document.body) { return; }
self.hide();
});
}
/**
* Shows a modal.
* @function
* @returns {modal} - Returns an instance of Modal.
* @example
* modal.show();
*/
Modal.prototype.show = function() {
if (this._shown) {
return this;
}
this.emit('beforeshow');
// Append a modal element
$body.append(this.$modal);
this.$modal.removeClass('modal-hide');
if (this._options.closable) {
this._closable();
}
this._shown = true;
this.emit('show');
return this;
};
/**
* Hides a modal.
* @function
* @returns {modal} - Returns an instance of Modal.
* @example
* modal.hide();
*/
Modal.prototype.hide = function() {
if (!this._shown) {
return this;
}
this.emit('beforehide');
this.$modal.addClass('modal-hide');
this.$modal.remove();
if (this._options.closable) {
this._closable(false);
}
this._shown = false;
this.emit('hide');
return this;
};
/**
* Sets a new content for a modal.
* @function
* @param {(String | HTMLElement)} [content] - The content to be shown into a modal.
* @returns {modal} - Returns an instance of Modal.
* @example
* modal.setContent();
*/
Modal.prototype.setContent = function(content) {
this.$content.html(content);
this.emit('setcontent');
return this;
};
/**
* Get content from a modal.
* @function
* @returns {String}
* @example
* modal.getContent();
*/
Modal.prototype.getContent = function() {
return this.$content.html();
};
/**
* Returns a boolean specifying if a modal is shown or hidden.
* @function
* @returns {modal} - Returns an instance of Modal.
* @example
* modal.isShown();
*/
Modal.prototype.isShown = function () {
return this._shown;
};
/**
* Expose Modal
*/
module.exports = Modal;