-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjModal.js
More file actions
165 lines (129 loc) · 4.78 KB
/
Copy pathjModal.js
File metadata and controls
165 lines (129 loc) · 4.78 KB
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
;(function (window, console, document, $) {
'use strict';
if ($.fn.jModal) {
return console.log('jModal already included');
}
/**
*
* @param jElement {Object}
* @param actions {Object}
* @constructor
*/
var Modal = function Modal(jElement, actions) {
var bPassedOptions = actions && !$.isEmptyObject(actions),
sClose;
this.hParam = bPassedOptions ? $.extend({}, $.fn.jModal.defaults, actions) : $.fn.jModal.defaults;
sClose = typeof this.hParam.closeText === 'string' ? this.hParam.closeText : $.fn.jModal.defaults.closeText;
this.hElements = {
jElement: jElement,
jDocument: $(document),
jBody: $('body'),
jModal: $(this.hParam.modal).addClass(this.hParam.classes.modal),
jClose: $(this.hParam.close).addClass(this.hParam.classes.close).text(sClose)
};
this.hElements.jContent = this.hElements.jModal.children();
this.bOpen = false;
this._attachEvents();
};
$.extend(Modal.prototype, {
set: function append(html) {
this.hParam.html = html;
this.hElements.jContent
.addClass(this.hParam.classes.inner)
.html(this.hParam.prepare(this.hParam.html))
.append(this.hElements.jClose);
// TODO: move it to attachEvents method
this.hElements.jClose.on(this.hParam.events.close + '.jmodal', $.proxy(this.closeModal, this));
return this.hElements.jElement;
},
_attachEvents: function _attachEvents() {
this.hElements.jElement.on(this.hParam.events.open + '.jmodal', $.proxy(this.openModal, this));
this.hElements.jDocument.on('keyup.jmodal', $.proxy(this._keyClose, this));
this.hElements.jModal.on(this.hParam.events.close + '.jmodal', $.proxy(this.closeModal, this));
this.hElements.jContent.on(this.hParam.events.close + '.jmodal', function(event) {
event.stopPropagation();
});
},
openModal: function openModal(event) {
var _this = this;
if (event) {
event.preventDefault();
}
$.when($.isFunction(_this.hParam.promise) ? _this.hParam.promise() : _this.hParam.promise)
.done(function (data) {
_this.set(data || _this.hParam.html);
_this._showModal();
});
return _this.hElements.jElement;
},
_showModal: function _showModal() {
this.hElements.jBody.append(this.hElements.jModal);
this.bOpen = true;
this.hParam.onOpen(this);
},
closeModal: function closeModal(event) {
if (event) {
event.preventDefault();
}
if (this.bOpen) {
this.hElements.jModal.detach();
this.bOpen = false;
this.hParam.onClose(this);
}
return this.hElements.jElement;
},
// Handle 'esc' key
_keyClose: function _keyClose(event) {
if (event && event.keyCode === 27) {
this.closeModal();
}
},
destroy: function destroy() {
this.hElements.jElement.off('.jmodal').data('jModal', null);
this.hElements.jModal.remove();
}
});
$.fn.jModal = function (action, params) {
var args = arguments;
return this.each(function () {
var jModal = $(this);
if (!$.data(this, 'jModal')) {
$.data(this, 'jModal', new Modal(jModal, action));
} else {
if (args.length) {
if (typeof jModal.data('jModal')[action] === 'function') {
// TODO: extend to getters
jModal.data('jModal')[action](params);
} else {
$.error('No such method in jModal plugin')
}
}
return jModal;
}
return this;
});
};
$.fn.jModal.defaults = {
classes: {
modal: 'g-modal',
inner: 'g-modal__inner',
close: 'g-modal__inner__close'
},
events: {
open: 'click',
close: 'click'
},
modal: '<div><div></div></div>',
close: '<a href="#"/>',
closeText: 'Close',
beforeSet: $.noop,
prepare: function(html) {
return html;
},
onOpen: $.noop,
onClose: $.noop
};
$.fn.jModal.setDefaults = function(hDefaults) {
$.extend($.fn.jModal.defaults, hDefaults);
};
})(window, window.console, document, window.jQuery);