PluginProbe
MaxButtons – Create buttons / 6.23
MaxButtons – Create buttons v6.23
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.23, at js/maxmodal.js

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