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

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