-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
201 lines (160 loc) · 5.9 KB
/
index.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
'use strict';
var lightbox_base_element;
var lightbox_background;
var lightbox_img_element;
var text_element_title;
var text_element_text;
var buttons_container;
var button_confirm;
var button_cancel;
var button_confirm_p;
var button_cancel_p;
var settings = {
closeOnBGClick : true,
showImage : false,
imageURL : '',
bgOpacity : 0.6,
fadeOut : true,
globalPadding : '100px 70px 100px 70px',
};
module.exports = {
createlightbox: function(options)
{
settings = extend(settings, options); // vanilla extend
var w=window,
d=document,
e=d.documentElement,
g=d.getElementsByTagName('body')[0],
x=w.innerWidth||e.clientWidth||g.clientWidth,
y=w.innerHeight||e.clientHeight||g.clientHeight;
var window_width = x;
var window_height = y;
var window_width_px = String(window_width) + 'px';
var window_height_px = String(window_height) + 'px';
lightbox_background = document.createElement('DIV');
lightbox_base_element = document.createElement('DIV');
lightbox_img_element = document.createElement('IMG');
lightbox_base_element.classList.add('ui-lightbox-image');
lightbox_background.classList.add('ui-lightbox-image-bg');
lightbox_img_element.classList.add('ui-lightbox-image-img');
lightbox_base_element.setAttribute('id', 'ui-lightbox-image');
lightbox_background.setAttribute('id', 'ui-lightbox-image-bg');
lightbox_img_element.setAttribute('id', 'ui-lightbox-image-img');
lightbox_img_element.setAttribute('src', settings.imageURL);
lightbox_img_element.onload = handleImageLoaded;
lightbox_base_element.style.position = 'fixed';
lightbox_base_element.style.top = '0';
lightbox_base_element.style.left = '0';
lightbox_base_element.style.width = window_width_px;
lightbox_base_element.style.height = window_height_px;
lightbox_base_element.style.zIndex = '1000';
lightbox_base_element.style.opacity = '0';
lightbox_background.style.position = 'fixed';
lightbox_background.style.top = '0';
lightbox_background.style.left = '0';
lightbox_background.style.width = window_width_px;
lightbox_background.style.height = window_height_px;
lightbox_background.style.backgroundColor = 'black';
lightbox_background.style.display = 'block';
lightbox_background.style.opacity = String(settings.bgOpacity);
lightbox_img_element.style.position = 'fixed';
lightbox_img_element.style.borderRadius = '2px';
lightbox_img_element.style.boxSizing = 'border-box';
lightbox_img_element.style.padding = settings.globalPadding;
lightbox_img_element.style.webkitTransition = 'all 0.5s';
lightbox_img_element.style.mozTransition = 'all 0.5s';
lightbox_img_element.style.msTransition = 'all 0.5s';
lightbox_img_element.style.oTransition = 'all 0.5s';
lightbox_background.style.webkitTransition = 'all 0.5s';
lightbox_background.style.mozTransition = 'all 0.5s';
lightbox_background.style.msTransition = 'all 0.5s';
lightbox_background.style.oTransition = 'all 0.5s';
lightbox_base_element.style.webkitTransition = 'all 0.5s';
lightbox_base_element.style.mozTransition = 'all 0.5s';
lightbox_base_element.style.msTransition = 'all 0.5s';
lightbox_base_element.style.oTransition = 'all 0.5s';
lightbox_base_element.appendChild(lightbox_background);
lightbox_base_element.appendChild(lightbox_img_element);
document.body.appendChild(lightbox_base_element);
lightbox_base_element.addEventListener('click', closelightbox);
addViewportListeners();
adjustViewPortLightbox();
setTimeout(function()
{
lightbox_base_element.style.opacity = '1';
}, 100)
},
}
var closelightbox = function()
{
removeViewportListeners();
var removeElement = function() {
document.body.removeChild(document.getElementById('ui-lightbox-image'));
}
if (settings.fadeOut)
{
lightbox_base_element.style.opacity = '0';
setTimeout(function() { removeElement(); }, 500)
}
else { removeElement(); }
}
var handleImageLoaded = function()
{
adjustViewPortLightbox();
}
var addViewportListeners = function()
{
if(window.attachEvent) { window.attachEvent('onresize', adjustViewPortLightbox);
} else if(window.addEventListener) { window.addEventListener('resize', adjustViewPortLightbox, true);
} else { //The browser does not support Javascript event binding
}
}
var removeViewportListeners = function()
{
if(window.detachEvent) { window.detachEvent('onresize', adjustViewPortLightbox);
} else if(window.removeEventListener) { window.removeEventListener('resize', adjustViewPortLightbox);
} else { //The browser does not support Javascript event binding
}
}
var handleConfirmCallback = function()
{
settings.confirmCallback();
closelightbox();
}
var adjustViewPortLightbox = function()
{
var w=window,
d=document,
e=d.documentElement,
g=d.getElementsByTagName('body')[0],
x=w.innerWidth||e.clientWidth||g.clientWidth,
y=w.innerHeight||e.clientHeight||g.clientHeight;
var lightbox_instance = document.getElementById('ui-lightbox-image-img');
if (lightbox_instance) {
lightbox_instance.style.maxWidth = (x - (x / 14)) + 'px';
lightbox_instance.style.maxHeight = (y - (y / 14)) + 'px';
lightbox_background.style.width = String(x) + 'px';
lightbox_background.style.height = String(y) + 'px';
lightbox_instance.style.left = String((x / 2) - (lightbox_instance.offsetWidth / 2)) + 'px';
lightbox_instance.style.top = String((y / 2) - (lightbox_instance.offsetHeight / 2)) + 'px';
};
}
var extend = function ( defaults, options )
{
var extended = {};
var prop;
for (prop in defaults)
{
if (Object.prototype.hasOwnProperty.call(defaults, prop)) {
extended[prop] = defaults[prop];
}
}
for (prop in options)
{
if (Object.prototype.hasOwnProperty.call(options, prop))
{
extended[prop] = options[prop];
}
}
return extended;
};