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

319 lines 7.0 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 this.currentModal.show();
54
55 if (this.setWidth)
56 {
57 this.currentModal.width(this.setWidth);
58 }
59 if (this.setHeight)
60 {
61 this.currentModal.height(this.setHeight);
62 }
63
64 $m = this.currentModal;
65
66 var headerHeight = $m.find('.modal_header').outerHeight();
67
68 var contentHeight = $m.find('.modal_content').outerHeight();
69 var contentWidth = $m.find('.modal_content').width();
70
71 var controlsHeight = $m.find('.modal_controls').outerHeight();
72
73 var modalHeight = headerHeight + contentHeight + controlsHeight; //this.currentModal.height();
74 var modalWidth = contentWidth; //this.currentModal.width();
75 var top = (this.windowHeight - modalHeight) / 2;
76 var left = (this.windowWidth - modalWidth) / 2;
77
78 if (top < 30)
79 {
80 top = 30; // top + admin bar
81 }
82 if (left < 0)
83 {
84 left: 0;
85 }
86
87 if (modalHeight > this.windowHeight) // if height is higher than screen supports
88 {
89 newHeight = this.windowHeight - top - 5;
90 this.currentModal.height(newHeight);
91
92 var newContentH = newHeight - headerHeight - controlsHeight;
93 $m.find('.modal_content').height(newContentH);
94
95 }
96 this.currentModal.css('left', left + 'px');
97 this.currentModal.css('top', top + 'px');
98 this.currentModal.css('height', modalHeight);
99
100
101 $('.maxmodal_overlay').show();
102 $('body').addClass('max-modal-active');
103
104 $(document).off('keydown', $.proxy(this.keyPressHandler, this));
105 $(document).on('keydown', $.proxy(this.keyPressHandler, this));
106
107 this.currentModal.focus();
108 }
109
110 maxModal.prototype.keyPressHandler = function (e)
111 {
112 if (e.keyCode === 27)
113 this.close();
114 }
115
116 maxModal.prototype.checkResize = function ()
117 {
118 this.windowHeight = $(window).height();
119 this.windowWidth = $(window).width();
120
121 if (this.currentModal === null)
122 return;
123
124 this.currentModal.removeAttr('style');
125 this.currentModal.find('.modal_content').removeAttr('style');
126 this.currentModal.removeAttr('style');
127
128 // redo sizes, repaint.
129
130 this.show();
131 }
132
133 maxModal.prototype.close = function()
134 {
135 this.currentModal.trigger('modal_close', [this]);
136 this.currentModal.remove();
137 this.currentModal = null;
138 $('.maxmodal_overlay').remove();
139 $('body').removeClass('max-modal-active');
140 $(document).off('keydown', $.proxy(this.keyPressHandler, this));
141
142 }
143
144 maxModal.prototype.fadeOut = function (timeOut)
145 {
146 if (typeof timeOut == undefined)
147 timeOut = 600;
148
149 var self = this;
150 this.currentModal.fadeOut(timeOut, function() { self.close(); } );
151
152 }
153
154 maxModal.prototype.setTitle = function(title)
155 {
156 this.currentModal.find('.modal_title').text(title);
157 }
158
159 maxModal.prototype.resetControls = function()
160 {
161 this.controls = [];
162 }
163
164 maxModal.prototype.setControls = function(controls)
165 {
166 var content = this.currentModal.find('.modal_content');
167 var controldiv = $('<div class="modal_controls controls">');
168
169 for(i =0; i < this.controls.length; i++)
170 controldiv.append(this.controls[i]);
171
172 if (typeof controls !== 'undefined')
173 controldiv.append(controls);
174
175 content.append(controldiv);
176
177 // general close button
178 $(this.currentModal).find('.modal_close').off('click');
179 $(this.currentModal).find('.modal_close').on('click', $.proxy(this.close, this));
180 }
181
182 maxModal.prototype.addControl = function (type, data, handler)
183 {
184 var text = '';
185
186 switch(type)
187 {
188 case 'yes':
189 text = modaltext.yes;
190 break;
191 case 'ok':
192 text = modaltext.ok;
193 break;
194 case 'no':
195 text = modaltext.no;
196 break;
197 case 'cancel':
198 text = modaltext.cancel;
199 break;
200
201 default:
202 text = type; // if nothing else
203 type = 'custom';
204 break;
205 }
206
207 var control = $('<a class="button-primary ' + type + '">' + text + '</a>');
208 control.on('click', data, handler );
209 this.controls.push(control);
210
211 }
212
213
214 /* Set the modal content
215
216 Sets the content of the modal. Do not run this function after adding controls.
217 @param string HTML,text content of the modal
218 */
219 maxModal.prototype.setContent = function(content)
220 {
221 this.currentModal.find('.modal_content').html(content);
222 }
223
224 /* Builds modal from hidden data
225
226 Builds modal from an formatted data object in DOM. Triggered on Click
227
228 */
229 maxModal.prototype.buildModal = function(e)
230 {
231 e.preventDefault();
232
233 var target = $(e.target);
234 if (typeof target.data('modal') == 'undefined')
235 target = target.parents('.maxmodal');
236
237 this.target = target;
238 var id = target.data('modal');
239 var data = $('#' + id);
240
241 // options
242 if (typeof data.data('width') !== 'undefined')
243 this.setWidth = data.data('width');
244 else
245 this.setWidth = false;
246
247 if (typeof data.data('height') !== 'undefined')
248 this.setHeight = data.data('height');
249 else
250 this.setHeight = false;
251
252 var title = $(data).find('.title').text();
253 var controls = $(data).find('.controls').html();
254 var content = $(data).find('.content').html();
255
256 this.newModal(id);
257 this.setTitle(title);
258 this.setContent(content);
259 this.setControls(controls);
260
261 // callback on init
262 if (typeof $(data).data('load') !== 'undefined')
263 {
264
265 // default call
266 var funcName = data.data('load') + '(modal)';
267 var callFunc = new Function ('modal', funcName);
268
269 try
270 {
271 callFunc(this);
272 }
273 catch(err)
274 {
275 console.error('MB Modal Callback Error: ' + err.message);
276 console.error('MB Modal tried calling: ' + funcName);
277 }
278 }
279
280 this.show();
281 }
282
283 maxModal.prototype.newModal = function(id)
284 {
285 if (this.currentModal !== null)
286 this.close();
287
288 var modal = $('<div class="max-modal ' + id + '" > \
289 <div class="modal_header"> \
290 <div class="modal_close dashicons dashicons-no"></div><h3 class="modal_title"></h3> \
291 </div> \
292 <div class="inner modal_content"></div>\
293 </div>');
294 if ($(this.parent).length > 0)
295 $(this.parent).append(modal);
296 else
297 $('body').append(modal); // fallback in case of interrupting page builders
298
299 $(modal).draggable({
300 handle: '.modal_header'
301 });
302
303 this.modals.push(modal);
304 this.currentModal = modal;
305 this.controls = [];
306 return this;
307
308 }
309
310 maxModal.prototype.writeOverlay = function()
311 {
312
313 $(this.parent).append('<div class="maxmodal_overlay"></div>');
314 $('.maxmodal_overlay').on('click', $.proxy(this.close, this));
315
316 }
317
318 }); // jquery
319