summaryrefslogtreecommitdiff
path: root/assets/js/components/image-slideshow.js
blob: 9db72bb9e839db2c7f62cfcee4891738b777b2e0 (plain)
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
KB.component('image-slideshow', function (containerElement, options) {
    var currentImage;

    function onKeyDown(e) {
        switch (KB.utils.getKey(e)) {
            case 'Escape':
                destroySlide();
                break;
            case 'ArrowRight':
                renderNextSlide();
                break;
            case 'ArrowLeft':
                renderPreviousSlide();
                break;
        }
    }

    function onOverlayClick(element) {
        if (element.matches('.slideshow-next-icon')) {
            renderNextSlide();
        } else if (element.matches('.slideshow-previous-icon')) {
            renderPreviousSlide();
        } else if (element.matches('.slideshow-download-icon')) {
            window.location.href = element.href;
        } else {
            destroySlide();
        }
    }

    function onClick(element) {
        var imageId = KB.dom(element).data('imageId');
        var image = getImage(imageId);

        renderSlide(image);
    }

    function renderNextSlide() {
        destroySlide();

        for (var i = 0; i < options.images.length; i++) {
            if (options.images[i].id === currentImage.id) {
                var index = i + 1;

                if (index >= options.images.length) {
                    index = 0;
                }

                currentImage = options.images[index];
                break;
            }
        }

        renderSlide();
    }

    function renderPreviousSlide() {
        destroySlide();

        for (var i = 0; i < options.images.length; i++) {
            if (options.images[i].id === currentImage.id) {
                var index = i - 1;

                if (index < 0) {
                    index = options.images.length - 1;
                }

                currentImage = options.images[index];
                break;
            }
        }

        renderSlide();
    }

    function renderSlide() {
        var closeElement = KB.dom('div')
            .attr('class', 'fa fa-window-close slideshow-icon slideshow-close-icon')
            .build();

        var downloadElement = KB.dom('a')
            .attr('class', 'fa fa-download slideshow-icon slideshow-download-icon')
            .attr('href', getUrl(currentImage, 'download'))
            .build();

        var previousElement = KB.dom('div')
            .attr('class', 'fa fa-chevron-circle-left slideshow-icon slideshow-previous-icon')
            .build();

        var nextElement = KB.dom('div')
            .attr('class', 'fa fa-chevron-circle-right slideshow-icon slideshow-next-icon')
            .build();

        var imageElement = KB.dom('img')
            .attr('src', getUrl(currentImage, 'image'))
            .attr('alt', currentImage.name)
            .attr('title', currentImage.name)
            .style('maxHeight', (window.innerHeight - 50) + 'px')
            .build();

        var captionElement = KB.dom('figcaption')
            .text(currentImage.name)
            .build();

        var figureElement = KB.dom('figure')
            .add(imageElement)
            .add(captionElement)
            .build();

        var overlayElement = KB.dom('div')
            .addClass('image-slideshow-overlay')
            .add(closeElement)
            .add(downloadElement)
            .add(previousElement)
            .add(nextElement)
            .add(figureElement)
            .click(onOverlayClick)
            .build();

        document.body.appendChild(overlayElement);
        document.addEventListener('keydown', onKeyDown, false);
    }

    function destroySlide() {
        var overlayElement = KB.find('.image-slideshow-overlay');

        if (overlayElement !== null) {
            document.removeEventListener('keydown', onKeyDown, false);
            overlayElement.remove();
        }
    }

    function getImage(imageId) {
        for (var i = 0; i < options.images.length; i++) {
            if (options.images[i].id === imageId) {
                return options.images[i];
            }
        }

        return null;
    }

    function getUrl(image, type) {
        var regex = new RegExp(options.regex, 'g');
        return options.url[type].replace(regex, image.id);
    }

    function buildThumbnailElement(image) {
        return KB.dom('img')
            .attr('src', getUrl(image, 'thumbnail'))
            .attr('alt', image.name)
            .attr('title', image.name)
            .data('imageId', image.id)
            .click(onClick)
            .build();
    }

    this.render = function () {
        currentImage = options.image;
        containerElement.appendChild(buildThumbnailElement(currentImage));
    };
});