-
Notifications
You must be signed in to change notification settings - Fork 0
/
modal.js
524 lines (502 loc) · 21.1 KB
/
modal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
Copyright (c) 2010, Ian Wilson
SEE LICENSE for the licensing information.
This modal is very simplistic and provides to help for specific types of
content.
Controls
- Controls should be added manually by having them call the hide method,
or having them call loadContent with new content.
Ajax
- In order to use this modal with ajax you need to create a request and
on success call the loadContent method.
Images
- In order to emulate lightbox a modal instance should be created and the
loadContent method should be called when thumbnails are clicked.
*/
(function () {
var Modal = new Class({
Implements: [Options, Events],
options: {
overlayOpacity: 0.5,
overlayZIndex: 49,
overlayBackgroundColor: '#000000',
overlayCssClass: null,
panelZIndex: 50,
panelWidth: 600,
panelHeight: 600,
panelBackgroundColor: '#ffffff',
panelCssClass: null,
panelXMargin: 10,
panelYMargin: 10
},
initialize: function (options) {
/* Initialize the modal's properties and options.
Must be called before attach. */
this.setOptions(options);
this.panelEl = null;
this.overlayEl = null;
// True if panel and overlay are showing.
this.showing = false;
// Used to detect if
this.newContent = null;
// True if the user customized the size.
this.sizeCustomized = false;
// These options are only used for one set of content.
this.contentOptions = null;
// This is the computed size and margin of the panel.
this.computedPanelDimensions = null;
this.panelParentEvents = {
keydown: this.panelParentKeydown.bind(this)
};
this.overlayEvents = {
click: this.overlayClick.bind(this)
};
this.windowEvents = {
resize: this.windowResize.bind(this),
scroll: this.windowScroll.bind(this)
};
this.ourEvents = {
onContentChanged: this.contentChanged.bind(this)
};
},
attach: function () {
/* Attach the modal to DOM.
Must be called before loadContent. */
var panelParentEl = $(document.body);
this.overlayEl = this.buildOverlay();
this.panelEl = this.buildPanel();
panelParentEl.adopt([this.overlayEl, this.panelEl]);
},
destroy: function () {
/* Destroy this modal, it cannot be used again. */
this.hide();
this.detachEvents();
this.overlayEl.destroy();
this.panelEl.destroy();
},
buildOverlay: function () {
/* Build the overlay element and set styles. */
var overlayEl = new Element('div');
overlayEl.setStyles({
display: 'none',
top: 0 + 'px',
left: 0 + 'px',
zIndex: this.options.overlayZIndex,
margin: 0,
padding: 0,
position: 'absolute',
opacity: this.options.overlayOpacity,
backgroundColor: this.options.overlayBackgroundColor
});
if (this.options.overlayCssClass !== null) {
overlayEl.addClass(this.options.overlayCssClass);
}
return overlayEl;
},
buildPanel: function () {
/* Build the panel element and set styles. */
var panelEl = new Element('div');
panelEl.setStyles({
display: 'none',
zIndex: this.options.panelZIndex,
margin: 0,
padding: 0,
position: 'absolute',
backgroundColor: this.options.panelBackgroundColor
});
if (this.options.panelCssClass !== null) {
panelEl.addClass(this.options.panelCssClass);
}
return panelEl;
},
loadContent: function (contentEl, contentOptions) {
/* Load content into the modal.
The panel size must be provided either in contentOptions with the
panelWidth and panelHeight properties or in the options for this
modal instance. Note that contentOptions is not optional and must
be at minimum an empty object. */
// Make sure we hide the modal before loading new content.
if (this.showing) {
this.hide();
}
this.contentOptions = contentOptions;
this.panelEl.empty();
this.panelEl.adopt(contentEl);
this.show();
},
loadContentAutoSize: function (contentEl, contentOptions) {
/* Try to compute the content size and then load content into the
modal.
This is just a wrapper around loadContent. */
var self;
// We need this in order to reposition and resize the modal.
contentOptions.autosize = true;
// Pass modal instance into closure.
self = this;
function onSizeComputed(computedSize) {
contentOptions.panelWidth = computedSize.totalWidth;
contentOptions.panelHeight = computedSize.totalHeight;
self.loadContent(contentEl, contentOptions);
}
this.measureContentComputedSize(contentEl, onSizeComputed);
},
measureContentComputedSize: function (contentEl, onComplete) {
/* Measure computed size of contentEl and call onComplete with the
size. */
var imagePairs, images, counter;
imagePairs = contentEl.getElements('img').map(
function (imageEl) {
var pair = {
el: imageEl,
src: imageEl.get('src')
};
// Clear this so we can set it later to trigger onload.
imageEl.set('src', '');
return pair;
});
function onSizeComplete(onComplete) {
/* Put the content in a table because it seems to be the only
reliable way to measure the width and height with shrink
wrapping. The table is going to add all kinds of margins,
padding, and border complications so I would like to use
another way. */
var tdEl, trEl, tbodyEl, tableEl, computedSize;
tdEl = new Element('td').adopt(contentEl);
trEl = new Element('tr').adopt(tdEl);
tbodyEl = new Element('tbody').adopt(trEl);
tableEl = new Element('table').adopt(tbodyEl);
// Make sure the table is not shown.
tableEl.setStyle('display', 'none');
// Some styles might depend on being in the modal, so fake it.
if (this.options.panelCssClass !== null) {
tableEl.addClass(this.options.panelCssClass);
}
$(document.body).adopt(tableEl);
computedSize = tableEl.measure(function () {
return tableEl.getComputedSize({
styles: ['padding', 'border', 'margin']
});
});
[tdEl, trEl, tbodyEl, tableEl].each(function (el) {
el.dispose();
});
onComplete(computedSize);
}
if (imagePairs.length > 0) {
counter = 0;
Array.each(imagePairs, function (pair) {
var events, self;
self = this;
events = {
load: function () {
counter = counter + 1;
if (counter === imagePairs.length) {
onSizeComplete.apply(self, [onComplete]);
}
pair.el.removeEvents(events);
},
error: function () {
counter = counter + 1;
if (counter === imagePairs.length) {
onSizeComplete.apply(self, [onComplete]);
}
pair.el.removeEvents(events);
}
};
pair.el.addEvents(events);
pair.el.set('src', pair.src);
// Check for cached images.
if (typeof pair.el.complete !== typeof undefined &&
pair.el.complete) {
// Call asyncronously.
events.load.delay(1);
}
}, this);
} else {
onSizeComplete.apply(this, [onComplete]);
}
},
attachEvents: function () {
/* Attach events for modal to function. */
var panelParentEl = $(document.body);
panelParentEl.addEvents(this.panelParentEvents);
this.overlayEl.addEvents(this.overlayEvents);
window.addEvents(this.windowEvents);
this.addEvents(this.ourEvents);
},
detachEvents: function () {
/* Detach events for modal to function. */
var panelParentEl = $(document.body);
this.overlayEl.removeEvents(this.overlayEvents);
panelParentEl.removeEvents(this.panelParentEvents);
window.removeEvents(this.windowEvents);
this.removeEvents(this.ourEvents);
},
show: function () {
/* Make the modal visible. */
this.recomputePanelDimensions();
this.showing = true;
this.attachEvents();
this.overlayEl.setStyles(Object.merge(
this.getOverlaySize(), {
display: 'block'
}));
this.panelEl.setStyles(Object.merge(
this.getComputedPanelSize(),
this.getComputedPanelPosition(),
this.getOverflowStyles(), {
display: 'block'
}));
this.fireEvent('panelShown');
},
hide: function () {
/* Hide the modal so it is not visible. */
this.showing = false;
this.contentOptions = null;
this.detachEvents();
this.panelEl.setStyle('display', 'none');
this.overlayEl.setStyle('display', 'none');
// Clear out panel so old ids/classes aren't sitting around.
this.panelEl.empty();
this.fireEvent('panelHidden');
},
getPanelWidth: function () {
/* Get the panel width from contentOptions and options. */
if (this.contentOptions !== null &&
this.contentOptions.hasOwnProperty('panelWidth')) {
return this.contentOptions.panelWidth;
} else {
return this.options.panelWidth;
}
},
getPanelHeight: function () {
/* Get the panel height from contentOptions and options. */
if (this.contentOptions !== null &&
this.contentOptions.hasOwnProperty('panelHeight')) {
return this.contentOptions.panelHeight;
} else {
return this.options.panelHeight;
}
},
getOverlaySize: function () {
/* Get the position for the overlay element. */
var windowScrollSize = window.getScrollSize();
return {
width: windowScrollSize.x + 'px',
height: windowScrollSize.y + 'px'
};
},
updateContentSize: function (sizeOptions, onComplete) {
/* Update content size if necessary.
The argument onComplete will be called when the content size
has been updated. */
var contentCloneEl, onSizeComputed, rootEl;
if (this.contentOptions.hasOwnProperty('autosize') &&
this.contentOptions.autosize) {
// TODO: We should clean this up.
rootEl = this.panelEl.getChildren()[0];
// Do not use mootools clone because IE crashes.
contentCloneEl = $(rootEl.cloneNode(true));
onSizeComputed = (function (computedSize) {
if (sizeOptions.hasOwnProperty('canShrink') &&
sizeOptions.canShrink) {
this.contentOptions.panelWidth =
computedSize.totalWidth;
this.contentOptions.panelHeight =
computedSize.totalHeight;
} else {
if (this.contentOptions.panelWidth <
computedSize.totalWidth) {
this.contentOptions.panelWidth =
computedSize.totalWidth;
}
if (this.contentOptions.panelHeight <
computedSize.totalHeight) {
this.contentOptions.panelHeight =
computedSize.totalHeight;
}
}
onComplete();
}).bind(this);
this.measureContentComputedSize(contentCloneEl,
onSizeComputed);
} else {
onComplete();
}
},
recomputePanelDimensions: function () {
/* Get the position and size for the panel element. */
var windowSize, panelXMargin, panelYMargin, panelWidth,
panelHeight, windowScroll, actualHeight, actualWidth;
windowSize = window.getSize();
actualWidth = panelWidth = this.getPanelWidth();
if (windowSize.x >= (panelWidth + this.options.panelXMargin * 2)) {
panelXMargin = (windowSize.x - panelWidth) / 2;
} else {
if (windowSize.x <= this.options.panelXMargin * 2) {
panelXMargin = 0;
panelWidth = windowSize.x;
} else {
panelXMargin = this.options.panelXMargin;
panelWidth = windowSize.x - (this.options.panelXMargin * 2);
}
}
actualHeight = panelHeight = this.getPanelHeight();
if (windowSize.y >=
(panelHeight + this.options.panelYMargin * 2)) {
panelYMargin = (windowSize.y - panelHeight) / 2;
} else {
if (windowSize.y <= this.options.panelYMargin * 2) {
panelYMargin = 0;
panelHeight = windowSize.y;
} else {
panelYMargin = this.options.panelYMargin;
panelHeight = windowSize.y -
(this.options.panelYMargin * 2);
}
}
// Save these so they don't have to be recomputed.
this.computedPanelDimensions = {
xMargin: panelXMargin,
yMargin: panelYMargin,
width: panelWidth,
height: panelHeight,
actualWidth: actualWidth,
actualHeight: actualHeight
};
},
getComputedPanelPosition: function () {
/* Get the position of where the panel *should* be.
Note that the panelXMargin/panelYMargin should only be
re-calculated when loading new content or on resize of the window.
*/
var left, top, windowScroll, windowScrollSize, position;
windowScroll = window.getScroll();
windowScrollSize = window.getScrollSize();
position = {};
if (this.computedPanelDimensions.actualWidth +
(this.computedPanelDimensions.xMargin * 2) +
windowScroll.x <= windowScrollSize.x) {
position.left = this.computedPanelDimensions.xMargin +
windowScroll.x + 'px';
}
if (this.computedPanelDimensions.actualHeight +
(this.computedPanelDimensions.yMargin * 2) +
windowScroll.y <= windowScrollSize.y) {
position.top = this.computedPanelDimensions.yMargin +
windowScroll.y + 'px';
}
return position;
},
getComputedPanelSize: function () {
return {
width: this.computedPanelDimensions.width + 'px',
height: this.computedPanelDimensions.height + 'px'
};
},
getOverflowStyles: function () {
/* Sets the overflow when the modal physically is larger than the
window.
*/
var overflowStyles = {};
if (this.computedPanelDimensions.width <
this.computedPanelDimensions.actualWidth ||
this.computedPanelDimensions.height <
this.computedPanelDimensions.actualHeight) {
overflowStyles.overflow = 'scroll';
} else {
overflowStyles.overflow = 'auto';
}
return overflowStyles;
},
panelParentKeydown: function (e) {
/* Handles key events in the panel parent.
Close the modal when esc is pressed. */
if (e.key === 'esc') {
if (this.showing) {
this.hide();
}
}
},
overlayClick: function (e) {
/* Handles a click on the overlay element.
Clicking on overlay should close the modal. */
if (this.showing) {
this.hide();
}
},
windowResize: function (e) {
/* Handles window resize.
Re-positions and re-sizes the overlay and panel.
*/
var onComplete, panelStyles;
if (this.showing) {
onComplete = (function () {
this.recomputePanelDimensions();
this.overlayEl.setStyles(this.getOverlaySize());
panelStyles = Object.merge(this.getComputedPanelSize(),
this.getComputedPanelPosition(),
this.getOverflowStyles());
this.panelEl.setStyles(panelStyles);
}).bind(this);
if (this.contentOptions.hasOwnProperty('autosize') &&
this.contentOptions.autosize) {
this.updateContentSize({
canShrink: true
}, onComplete);
} else {
onComplete();
}
}
},
windowScroll: function (e) {
/* When the user scrolls up and down in the window.
Re-positions the overlay.
*/
var targetEl;
if (this.showing) {
// TODO: Find a way to test if incompliant browsers
// actually do this. Scroll should not bubble up.
// Only update the coords if the user
// is not scrolling inside the modal.
targetEl = null;
if (typeof e !== typeof undefined) {
targetEl = $(e.target);
}
if (targetEl === null || !this.panelEl.contains(targetEl)) {
this.panelEl.setStyles(this.getComputedPanelPosition());
}
}
},
contentChanged: function () {
/* When the application changes the content of the panel.
This should not decrease the size, only increase it.
*/
var onComplete, panelStyles;
if (this.showing) {
onComplete = (function () {
this.recomputePanelDimensions();
this.overlayEl.setStyles(this.getOverlaySize());
panelStyles = Object.merge(this.getComputedPanelSize(),
this.getComputedPanelPosition(),
this.getOverflowStyles());
this.panelEl.setStyles(panelStyles);
}).bind(this);
if (this.contentOptions.hasOwnProperty('autosize') &&
this.contentOptions.autosize) {
this.updateContentSize({
canShrink: false
}, onComplete);
} else {
onComplete();
}
}
},
inPanel: function (el) {
/* Check if the given element is in the panel. */
return this.panelEl.contains(el);
}
});
// Until a better namespacing mechanism exists I guess this is it.
window.Modal = Modal;
})();