PluginProbe
MaxButtons – Create buttons / 7.1.3
MaxButtons – Create buttons v7.1.3
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 7.1.3, at js/maxmodal.js

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