Completed
Push — master ( c9442a...a0c18c )
by D
02:25
created

Lightbox.constructor   B

Complexity

Conditions 8

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
c 0
b 0
f 0
rs 7.3333
cc 8
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
const globals_1 = require("../globals/globals");
4
(function (root, factory) {
5
    if (typeof globals_1.define === 'function' && globals_1.define.amd) {
6
        globals_1.define([], factory);
7
    }
8
    else if (typeof module === 'object' && module.exports) {
9
        module.exports = factory();
10
    }
11
    else {
12
        root.Lightbox = factory();
13
    }
14
}(typeof self !== 'undefined' ? self : this, function () {
0 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
15
    class Lightbox {
16
        constructor(options) {
17
            if (!options.targets) {
18
                throw new Error('No targets');
19
            }
20
            this.options = options;
21
            this.options.targets = typeof options.targets === 'string' ? document.querySelectorAll(options.targets) : options.targets;
22
            this.initClasses();
23
            this.initClone();
24
            this.initClose();
25
        }
26
        initClasses() {
27
            this.options.targets.forEach(function (target) {
28
                target.classList.add('lightbox');
29
            });
30
        }
31
        initHTML(target, position) {
32
            let wrapper = document.createElement('div');
33
            wrapper.classList.add('lightbox-clone-wrapper');
34
            setTimeout(() => {
35
                wrapper.classList.add('active');
36
            }, 100);
37
            let clone = document.createElement('div');
38
            clone.classList.add('lightbox-clone');
39
            clone.style.top = position.top + 'px';
40
            clone.style.left = position.left + 'px';
41
            clone.style.width = target.naturalWidth + 'px';
42
            clone.style.height = target.naturalHeight + 'px';
43
            setTimeout(() => {
44
                clone.classList.add('centered');
45
            }, 100);
46
            let cloneImg = document.createElement('img');
47
            cloneImg.src = target.src;
48
            clone.appendChild(cloneImg);
49
            wrapper.appendChild(clone);
50
            document.body.appendChild(wrapper);
51
        }
52
        initClone() {
53
            let that = this;
54
            this.options.targets.forEach(function (target) {
55
                target.addEventListener('click', function (e) {
56
                    that.initHTML(e.target, Lightbox.getTargetPosition(target));
57
                });
58
            });
59
        }
60
        static getTargetPosition(target) {
61
            let targetPosition = target.getBoundingClientRect();
62
            return {
63
                top: targetPosition.top,
64
                left: targetPosition.left
65
            };
66
        }
67
        initClose() {
68
            this.bodyClose();
69
            this.scrollClose();
70
        }
71
        static close() {
72
            let wrapper = document.querySelector('.lightbox-clone-wrapper');
73
            let clone = wrapper.querySelector('.lightbox-clone');
74
            clone.classList.remove('centered');
75
            setTimeout(() => {
76
                wrapper.remove();
77
            }, 500);
78
        }
79
        bodyClose() {
80
            let that = this;
0 ignored issues
show
Unused Code introduced by
The variable that seems to be never used. Consider removing it.
Loading history...
81
            document.addEventListener('click', function (e) {
82
                e.preventDefault();
83
                if (e.target.closest('.lightbox-clone-wrapper')) {
84
                    Lightbox.close();
85
                }
86
            });
87
        }
88
        scrollClose() {
89
            let that = this;
0 ignored issues
show
Unused Code introduced by
The variable that seems to be never used. Consider removing it.
Loading history...
90
            document.addEventListener('scroll', function () {
91
                Lightbox.close();
92
            });
93
        }
94
    }
95
    return Lightbox;
96
}));
97