blob: bc23129b8d04ffd4fb8990b6d6a9542d65807581 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001'use strict';
2
3var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
4
5var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
6
7var _isInteger = require('babel-runtime/core-js/number/is-integer');
8
9var _isInteger2 = _interopRequireDefault(_isInteger);
10
11var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
12
13var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
14
15var _entries = require('babel-runtime/core-js/object/entries');
16
17var _entries2 = _interopRequireDefault(_entries);
18
19var _getIterator2 = require('babel-runtime/core-js/get-iterator');
20
21var _getIterator3 = _interopRequireDefault(_getIterator2);
22
23var _fullThrottle = require('../utils/full-throttle');
24
25var _fullThrottle2 = _interopRequireDefault(_fullThrottle);
26
27var _constants = require('../utils/constants');
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
31/**
32 * @license
33 * Copyright 2016 Leif Olsen. All Rights Reserved.
34 *
35 * Licensed under the Apache License, Version 2.0 (the "License");
36 * you may not use this file except in compliance with the License.
37 * You may obtain a copy of the License at
38 *
39 * http://www.apache.org/licenses/LICENSE-2.0
40 *
41 * Unless required by applicable law or agreed to in writing, software
42 * distributed under the License is distributed on an "AS IS" BASIS,
43 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44 * See the License for the specific language governing permissions and
45 * limitations under the License.
46 *
47 * This code is built with Google Material Design Lite,
48 * which is Licensed under the Apache License, Version 2.0
49 */
50
51/**
52 * Responsive Lightbox
53 */
54
55(function () {
56 'use strict';
57
58 var LIGHTBOX = 'mdlext-lightbox';
59 var LIGHTBOX_SLIDER = 'mdlext-lightbox__slider';
60 var LIGHTBOX_SLIDER_SLIDE = 'mdlext-lightbox__slider__slide';
61 var STICKY_FOOTER = 'mdlext-lightbox--sticky-footer';
62 var BUTTON = 'mdl-button';
63
64 /**
65 * https://github.com/google/material-design-lite/issues/4205
66 * @constructor
67 * @param {Element} element The element that will be upgraded.
68 */
69 var MaterialExtLightbox = function MaterialExtLightbox(element) {
70 // Stores the element.
71 this.element_ = element;
72
73 // Initialize instance.
74 this.init();
75 };
76 window['MaterialExtLightbox'] = MaterialExtLightbox;
77
78 /**
79 * Handle keypress
80 * @param event
81 * @private
82 */
83 MaterialExtLightbox.prototype.keyDownHandler_ = function (event) {
84
85 if (event) {
86 if (event.keyCode === _constants.VK_ESC || event.keyCode === _constants.VK_SPACE || event.keyCode === _constants.VK_END || event.keyCode === _constants.VK_HOME || event.keyCode === _constants.VK_ARROW_UP || event.keyCode === _constants.VK_ARROW_LEFT || event.keyCode === _constants.VK_ARROW_DOWN || event.keyCode === _constants.VK_ARROW_RIGHT) {
87
88 if (event.keyCode !== _constants.VK_ESC) {
89 event.preventDefault();
90 event.stopPropagation();
91 }
92
93 var action = 'first';
94 if (event.keyCode === _constants.VK_END) {
95 action = 'last';
96 } else if (event.keyCode === _constants.VK_ARROW_UP || event.keyCode === _constants.VK_ARROW_LEFT) {
97 action = 'prev';
98 } else if (event.keyCode === _constants.VK_ARROW_DOWN || event.keyCode === _constants.VK_ARROW_RIGHT) {
99 action = 'next';
100 } else if (event.keyCode === _constants.VK_SPACE) {
101 action = 'select';
102 } else if (event.keyCode === _constants.VK_ESC) {
103 action = 'cancel';
104 }
105
106 dispatchAction_(action, this);
107 }
108 }
109 };
110
111 /**
112 * Handle button clicks
113 * @param event
114 * @private
115 */
116 MaterialExtLightbox.prototype.buttonClickHandler_ = function (event) {
117
118 if (event) {
119 event.preventDefault();
120 event.stopPropagation();
121
122 dispatchAction_(this.getAttribute('data-action') || '', this);
123
124 var n = this.closest('.' + LIGHTBOX);
125 if (n) {
126 n.focus();
127 }
128 }
129 };
130
131 /**
132 * Dispatches an action custom event
133 * @param action
134 * @param source
135 * @param target
136 * @private
137 */
138 var dispatchAction_ = function dispatchAction_(action, source) {
139 var target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : source;
140
141
142 target.dispatchEvent(new CustomEvent('action', {
143 bubbles: true,
144 cancelable: true,
145 detail: {
146 action: action || '',
147 source: source
148 }
149 }));
150 };
151
152 /**
153 * Reposition dialog if component parent element is "DIALOG"
154 * @param lightboxElement
155 * @private
156 */
157 var repositionDialog_ = function repositionDialog_(lightboxElement) {
158 var footerHeight = function footerHeight(footer, isSticky) {
159 return isSticky && footer ? footer.offsetHeight : 0;
160 };
161
162 var reposition = function reposition(dialog, fh) {
163 if (window.getComputedStyle(dialog).position === 'absolute') {
164 var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
165 var topValue = scrollTop + (window.innerHeight - dialog.offsetHeight - fh) / 2;
166 dialog.style.top = Math.max(scrollTop, topValue) + 'px';
167 }
168 };
169
170 var p = lightboxElement.parentNode;
171 var dialog = p && p.nodeName === 'DIALOG' ? p : null;
172
173 if (dialog && dialog.hasAttribute('open')) {
174 lightboxElement.style.width = 'auto';
175 lightboxElement.style.maxWidth = '100%';
176 var img = lightboxElement.querySelector('img');
177 if (img) {
178 lightboxElement.style.maxWidth = img.naturalWidth !== undefined ? img.naturalWidth + 'px' : img.width + 'px' || '100%';
179 }
180
181 var fh = footerHeight(lightboxElement.querySelector('footer'), lightboxElement.classList.contains(STICKY_FOOTER));
182 var vh = Math.max(document.documentElement.clientHeight, window.innerHeight || 0) - fh;
183 if (dialog.offsetHeight > vh) {
184 var n = 0;
185 while (dialog.offsetHeight > vh && ++n < 4) {
186 lightboxElement.style.width = lightboxElement.offsetWidth * vh / lightboxElement.offsetHeight + 'px';
187 }
188 }
189 reposition(dialog, fh);
190 }
191 };
192
193 /**
194 * Handle image load
195 * @param event
196 * @private
197 */
198
199 MaterialExtLightbox.prototype.imgLoadHandler_ = function () /*event*/{
200 repositionDialog_(this);
201 };
202
203 /**
204 * Handle image drag
205 * @param event
206 * @private
207 */
208 MaterialExtLightbox.prototype.imgDragHandler_ = function (event) {
209
210 var setStyles = function setStyles(element, properties) {
211 //noinspection JSAnnotator
212 var _iteratorNormalCompletion = true;
213 var _didIteratorError = false;
214 var _iteratorError = undefined;
215
216 try {
217 for (var _iterator = (0, _getIterator3.default)((0, _entries2.default)(properties)), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
218 var _step$value = (0, _slicedToArray3.default)(_step.value, 2),
219 key = _step$value[0],
220 value = _step$value[1];
221
222 element.style[key] = value;
223 }
224 // ... or:
225 //for (const key in properties) {
226 // element.style[key] = properties[key];
227 //}
228 } catch (err) {
229 _didIteratorError = true;
230 _iteratorError = err;
231 } finally {
232 try {
233 if (!_iteratorNormalCompletion && _iterator.return) {
234 _iterator.return();
235 }
236 } finally {
237 if (_didIteratorError) {
238 throw _iteratorError;
239 }
240 }
241 }
242 };
243
244 event.preventDefault();
245 //event.stopPropagation();
246
247 var x = event.clientX || (event.touches !== undefined ? event.touches[0].clientX : 0);
248
249 var img = this;
250 img.style.opacity = '0.2';
251
252 var slider = document.createElement('div');
253 slider.classList.add(LIGHTBOX_SLIDER);
254 setStyles(slider, { 'width': img.offsetWidth + 'px', 'height': img.offsetHeight + 'px' });
255
256 var slide = document.createElement('div');
257 slide.classList.add(LIGHTBOX_SLIDER_SLIDE);
258 slide.textContent = '>';
259 setStyles(slide, {
260 'width': img.offsetWidth + 'px',
261 'height': img.offsetHeight + 'px',
262 'line-height': img.offsetHeight + 'px',
263 'font-size': img.offsetHeight / 4 + 'px',
264 'text-align': 'right',
265 'background-image': 'url("' + (img.getAttribute('data-img-url-prev') || '') + '")'
266 });
267 slider.appendChild(slide);
268
269 slide = document.createElement('div');
270 slide.classList.add(LIGHTBOX_SLIDER_SLIDE);
271 setStyles(slide, {
272 'width': img.offsetWidth + 'px',
273 'height': img.offsetHeight + 'px',
274 'background-image': 'url("' + img.src + '")'
275 });
276 slider.appendChild(slide);
277
278 slide = document.createElement('div');
279 slide.classList.add(LIGHTBOX_SLIDER_SLIDE);
280 slide.textContent = '<';
281 setStyles(slide, {
282 'width': img.offsetWidth + 'px',
283 'height': img.offsetHeight + 'px',
284 'line-height': img.offsetHeight + 'px',
285 'font-size': img.offsetHeight / 4 + 'px',
286 'text-align': 'left',
287 'background-image': 'url("' + (img.getAttribute('data-img-url-next') || '') + '")'
288 });
289 slider.appendChild(slide);
290
291 img.parentNode.appendChild(slider);
292
293 // drag handler
294 var drag = function drag(e) {
295 e.preventDefault();
296 var dx = (e.clientX || (e.touches !== undefined ? e.touches[0].clientX : 0)) - x; // TODO: maybe rewrite to improve performance
297
298 if (slider.offsetWidth - Math.abs(dx) > 19) {
299 slider.style.left = dx + 'px';
300 }
301 };
302
303 // end drag handler
304 var endDrag = function endDrag(e) {
305 e.preventDefault();
306 //e.stopPropagation();
307
308 window.removeEventListener('mousemove', drag);
309 window.removeEventListener('touchmove', drag);
310 window.removeEventListener('mouseup', endDrag);
311 window.removeEventListener('touchend', endDrag);
312
313 var dx = slider.offsetLeft;
314 img.parentNode.removeChild(slider);
315 img.style.opacity = '1.0';
316
317 if (Math.abs(dx) > 19) {
318 dispatchAction_(dx > 0 ? 'prev' : 'next', img);
319 }
320 };
321
322 window.addEventListener('mousemove', drag);
323 window.addEventListener('touchmove', drag);
324 window.addEventListener('mouseup', endDrag);
325 window.addEventListener('touchend', endDrag);
326 };
327
328 /**
329 * Initialize component
330 */
331 MaterialExtLightbox.prototype.init = function () {
332 var _this = this;
333
334 if (this.element_) {
335 // Do the init required for this component to work
336 this.element_.addEventListener('keydown', this.keyDownHandler_.bind(this.element_), true);
337
338 if (!(0, _isInteger2.default)(this.element_.getAttribute('tabindex'))) {
339 this.element_.setAttribute('tabindex', 1);
340 }
341
342 [].concat((0, _toConsumableArray3.default)(this.element_.querySelectorAll('.' + BUTTON))).forEach(function (button) {
343 return button.addEventListener('click', _this.buttonClickHandler_.bind(button), false);
344 });
345
346 var figcaption = this.element_.querySelector('figcaption');
347 if (figcaption) {
348 figcaption.addEventListener('click', this.buttonClickHandler_.bind(figcaption), false);
349 }
350
351 var footer = this.element_.querySelector('footer');
352 if (footer) {
353 footer.addEventListener('click', this.buttonClickHandler_.bind(footer), false);
354 }
355
356 var img = this.element_.querySelector('img');
357 if (img) {
358 img.addEventListener('load', this.imgLoadHandler_.bind(this.element_), false);
359 img.addEventListener('click', function (e) {
360 return e.preventDefault();
361 }, true);
362 img.addEventListener('mousedown', this.imgDragHandler_.bind(img), true);
363 img.addEventListener('touchstart', this.imgDragHandler_.bind(img), true);
364 }
365 window.addEventListener('resize', (0, _fullThrottle2.default)(function () {
366 return repositionDialog_(_this.element_);
367 }));
368 window.addEventListener('orientationchange', function () {
369 return repositionDialog_(_this.element_);
370 });
371
372 // Set upgraded flag
373 this.element_.classList.add(_constants.IS_UPGRADED);
374 }
375 };
376
377 /*
378 * Downgrade component
379 * E.g remove listeners and clean up resources
380 *
381 * Nothing to downgrade
382 *
383 MaterialExtLightbox.prototype.mdlDowngrade_ = function() {
384 };
385 */
386
387 /**
388 * The component registers itself. It can assume componentHandler is available in the global scope.
389 */
390 /* jshint undef:false */
391 componentHandler.register({
392 constructor: MaterialExtLightbox,
393 classAsString: 'MaterialExtLightbox',
394 cssClass: 'mdlext-js-lightbox'
395 });
396})();