PluginProbe
MaxButtons – Create buttons / 6.25
MaxButtons – Create buttons v6.25
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
maxbuttons / js / maxmodal.js

maxmodal.js in MaxButtons – Create buttons 6.25, at js/maxmodal.js

310 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var maxModal;
2
3
4 jQuery(document).ready(function($) {
5 maxModal = function () {
6
7 }
8
9 maxModal.prototype = {
10 currentModal: null,
11 modals: [],
12 controls: [],
13 parent: '#maxbuttons', // modal will be written to this element.
14 multiple: false,
15 windowHeight: false,
16 windowWidth: false,
17 setWidth: false,
18 setHeight: false,
19 target: false,
20
21 }
22
23 maxModal.prototype.init = function()
24 {
25
26 this.windowHeight = $(window).height();
27 this.windowWidth = $(window).width();
28
29 $(document).on('click', '.maxmodal', $.proxy(this.buildModal, this));
30 $(window).on('resize', $.proxy(this.checkResize, this));
31
32 }
33
34 maxModal.prototype.focus = function()
35 {
36 this.currentModal.show();
37
38 }
39
40 maxModal.prototype.get = function()
41 {
42 return this.currentModal;
43 }
44
45
46 maxModal.prototype.show = function()
47 {
48 $('.maxmodal_overlay').remove();
49 $('body').removeClass('max-modal-active');
50 this.writeOverlay();
51
52 if (this.setWidth)
53 {
54 this.currentModal.width(this.setWidth);
55 }
56 if (this.setHeight)
57 {
58 this.currentModal.height(this.setHeight);
59 }
60
61 var modalHeight = this.currentModal.height();
62 var modalWidth = this.currentModal.width();
63
64 var top = (this.windowHeight - modalHeight) / 2;
65 var left = (this.windowWidth - modalWidth) / 2;
66
67 if (top < 30)
68 {
69 top = 30; // top + admin bar
70 }
71 if (left < 0)
72 {
73 left: 0;
74 }
75
76 if (modalHeight > this.windowHeight)
77 this.currentModal.height(this.windowHeight - top - 5 + 'px');
78
79 this.currentModal.css('left', left + 'px');
80 this.currentModal.css('top', top + 'px');
81 this.currentModal.css('height', modalHeight);
82
83 this.currentModal.show();
84
85 $('.maxmodal_overlay').show();
86 $('body').addClass('max-modal-active');
87
88 $(document).off('keydown', $.proxy(this.keyPressHandler, this));
89 $(document).on('keydown', $.proxy(this.keyPressHandler, this));
90
91 this.currentModal.focus();
92 }
93
94 maxModal.prototype.keyPressHandler = function (e)
95 {
96 if (e.keyCode === 27)
97 this.close();
98 }
99
100 maxModal.prototype.checkResize = function ()
101 {
102 this.windowHeight = $(window).height();
103 this.windowWidth = $(window).width();
104
105 if (this.currentModal === null)
106 return;
107
108 this.currentModal.removeAttr('style');
109 this.currentModal.find('.modal_content').removeAttr('style');
110 // redo sizes, repaint.
111
112 this.show();
113 }
114
115 maxModal.prototype.close = function()
116 {
117 this.currentModal.trigger('modal_close', [this]);
118 this.currentModal.remove();
119 this.currentModal = null;
120 $('.maxmodal_overlay').remove();
121 $('body').removeClass('max-modal-active');
122 $(document).off('keydown', $.proxy(this.keyPressHandler, this));
123
124 }
125
126 maxModal.prototype.fadeOut = function (timeOut)
127 {
128 if (typeof timeOut == undefined)
129 timeOut = 600;
130
131 var self = this;
132 this.currentModal.fadeOut(timeOut, function() { self.close(); } );
133
134 }
135
136 maxModal.prototype.setTitle = function(title)
137 {
138 this.currentModal.find('.modal_title').text(title);
139 }
140
141 maxModal.prototype.setControls = function(controls)
142 {
143 var content = this.currentModal.find('.modal_content');
144 var controldiv = $('<div class="controls">');
145
146 for(i =0; i < this.controls.length; i++)
147 controldiv.append(this.controls[i]);
148
149 if (typeof controls !== 'undefined')
150 controldiv.append(controls);
151
152 content.append(controldiv);
153
154 // general close button
155 $(this.currentModal).find('.modal_close').off('click');
156 $(this.currentModal).find('.modal_close').on('click', $.proxy(this.close, this));
157 }
158
159 maxModal.prototype.addControl = function (type, data, handler)
160 {
161 var text = '';
162
163 switch(type)
164 {
165 case 'yes':
166 text = modaltext.yes;
167 break;
168 case 'ok':
169 text = modaltext.ok;
170 break;
171 case 'no':
172 text = modaltext.no;
173 break;
174 case 'cancel':
175 text = modaltext.cancel;
176 break;
177 case 'insert':
178 text = mbtrans.insert; // used for mediabutton
179 break;
180 }
181
182 var control = $('<a class="button-primary ' + type + '">' + text + '</a>');
183 control.on('click', data, handler );
184 this.controls.push(control);
185
186 }
187
188
189 /* Set the modal content
190
191 Sets the content of the modal. Do not run this function after adding controls.
192 @param string HTML,text content of the modal
193 */
194 maxModal.prototype.setContent = function(content)
195 {
196 this.currentModal.find('.modal_content').html(content);
197 }
198
199 /* Builds modal from hidden data
200
201 Builds modal from an formatted data object in DOM. Triggered on Click
202
203 */
204 maxModal.prototype.buildModal = function(e)
205 {
206 e.preventDefault();
207
208 var target = $(e.target);
209 if (typeof target.data('modal') == 'undefined')
210 target = target.parents('.maxmodal');
211
212 this.target = target;
213 var id = target.data('modal');
214 var data = $('#' + id);
215
216 // options
217 if (typeof data.data('width') !== 'undefined')
218 this.setWidth = data.data('width');
219 else
220 this.setWidth = false;
221
222 if (typeof data.data('height') !== 'undefined')
223 this.setHeight = data.data('height');
224 else
225 this.setHeight = false;
226
227
228 var title = $(data).find('.title').text();
229 var controls = $(data).find('.controls').html();
230 var content = $(data).find('.content').html();
231
232 this.newModal(id);
233 this.setTitle(title)
234 this.setContent(content);
235 this.setControls(controls);
236
237 // callback on init
238 if (typeof $(data).data('load') !== 'undefined')
239 {
240
241 // default call
242 var funcName = data.data('load') + '(modal)';
243 var callFunc = new Function ('modal', funcName);
244
245
246 /* Args coming!
247 if (typeof(data.data('load-args') !== 'undefined')
248 {
249 var args = data.data('load-args').split(',');
250
251 for(i=0; i< args.length; i++)
252 {
253
254 }
255 }
256 */
257
258 try
259 {
260 callFunc(this);
261 }
262 catch(err)
263 {
264
265 console.log('MB Modal Callback Error: ' + err.message);
266 console.log('MB Mobdal tried calling: ' + funcName);
267 }
268 }
269
270 this.show();
271 }
272
273 maxModal.prototype.newModal = function(id)
274 {
275
276 if (this.currentModal !== null)
277 this.close();
278
279 var modal = $('<div class="max-modal ' + id + '" > \
280 <div class="modal_header"> \
281 <div class="modal_close dashicons dashicons-no"></div><h3 class="modal_title"></h3> \
282 </div> \
283 <div class="inner modal_content"></div>\
284 </div>');
285 if ($(this.parent).length > 0)
286 $(this.parent).append(modal);
287 else
288 $('body').append(modal); // fallback in case of interrupting page builders
289
290 $(modal).draggable({
291 handle: '.modal_header'
292 });
293
294 this.modals.push(modal);
295 this.currentModal = modal;
296 this.controls = [];
297 return this;
298
299 }
300
301 maxModal.prototype.writeOverlay = function()
302 {
303
304 $(this.parent).append('<div class="maxmodal_overlay"></div>');
305 $('.maxmodal_overlay').on('click', $.proxy(this.close, this));
306
307 }
308
309 });
310