Browse code

Readded jbox files in public folder

Benjamin Roth authored on22/02/2023 21:56:38
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,358 @@
1
+/**
2
+ * jBox Image plugin: Adds a lightbox to your images
3
+ *
4
+ * Author: Stephan Wagner <stephanwagner.me@gmail.com> (https://stephanwagner.me)
5
+ *
6
+ * License: MIT (https://opensource.org/licenses/MIT)
7
+ *
8
+ * Requires: jBox (https://cdn.jsdelivr.net/gh/StephanWagner/jBox@latest/dist/jBox.min.js)
9
+ */
10
+
11
+function jBoxImageWrapper(jBox, jQuery) {
12
+
13
+  new jBox.plugin('Image', {
14
+
15
+
16
+    // Options (https://stephanwagner.me/jBox/options#options-image)
17
+
18
+    src: 'href',                 // The attribute where jBox gets the image source from, e.g. href="/path_to_image/image.jpg"
19
+    gallery: 'data-jbox-image',  // The attribute to set the galleries, e.g. data-jbox-image="gallery1"
20
+    imageLabel: 'title',         // The attribute where jBox gets the image label from, e.g. title="My label"
21
+    imageFade: 360,              // The fade duration for images in ms
22
+    imageSize: 'contain',        // How to display the images. Use CSS background-position values, e.g. 'cover', 'contain', 'auto', 'initial', '50% 50%'
23
+    imageCounter: false,         // Set to true to add an image counter, e.g. 4/20
24
+    imageCounterSeparator: '/',  // HTML to separate the current image number from all image numbers, e.g. '/' or ' of '
25
+    downloadButton: false,       // Adds a download button
26
+    downloadButtonText: null,    // Text for the download button
27
+    downloadButtonUrl: null,     // The attribute at the source element where to find the image to download, e.g. data-download="/path_to_image/image.jpg". If none provided, the currently active image will be downloaded
28
+    mobileImageAttr: null,       // The attribute to look for an mobile version of the image
29
+    mobileImageBreakpoint: null, // The upper breakpoint to load the mobile image
30
+    preloadFirstImage: false,    // Preload the first image when page is loaded
31
+    target: window,
32
+    attach: '[data-jbox-image]',
33
+    fixed: true,
34
+    blockScroll: true,
35
+    closeOnEsc: true,
36
+    closeOnClick: 'button',
37
+    closeButton: true,
38
+    overlay: true,
39
+    animation: 'zoomIn',
40
+    preventDefault: true,
41
+    width: '100%',
42
+    height: '100%',
43
+    adjustDistance: {
44
+      top: 40,
45
+      right: 0,
46
+      bottom: 40,
47
+      left: 0
48
+    },
49
+
50
+
51
+    // Triggered when jBox is initialized
52
+
53
+    _onInit: function ()
54
+    {
55
+      // Initial images and z-index
56
+      this.images = this.currentImage = {};
57
+      this.imageZIndex = 1;
58
+
59
+      this.initImage = function (item) {
60
+        item = jQuery(item);
61
+
62
+        // Abort if the item was added to a gallery already
63
+        if (item.data('jBox-image-gallery')) {
64
+          return;
65
+        }
66
+
67
+        // Get the image src
68
+        var src = item.attr(this.options.src);
69
+
70
+        // Update responsive image src
71
+        if (this.options.mobileImageAttr && this.options.mobileImageBreakpoint && item.attr(this.options.mobileImageAttr)) {
72
+          if (jQuery(window).width() <= this.options.mobileImageBreakpoint) {
73
+            src = item.attr(this.options.mobileImageAttr);
74
+          }
75
+        }
76
+
77
+        // Add item to a gallery
78
+        var gallery = item.attr(this.options.gallery) || 'default';
79
+        !this.images[gallery] && (this.images[gallery] = []);
80
+        this.images[gallery].push({
81
+          src: src,
82
+          label: (item.attr(this.options.imageLabel) || ''),
83
+          downloadUrl: this.options.downloadButtonUrl && item.attr(this.options.downloadButtonUrl) ? item.attr(this.options.downloadButtonUrl) : null
84
+        });
85
+
86
+        // Remove the title attribute so it won't show the browsers tooltip
87
+        this.options.imageLabel == 'title' && item.removeAttr('title');
88
+
89
+        // Store data in source element for easy access
90
+        item.data('jBox-image-gallery', gallery);
91
+        item.data('jBox-image-id', (this.images[gallery].length - 1));
92
+      }.bind(this);
93
+
94
+      // Loop through images, sort and save in global variable
95
+      this.attachedElements && this.attachedElements.length && jQuery.each(this.attachedElements, function (index, item) {
96
+        this.initImage(item);
97
+      }.bind(this));
98
+
99
+      // Helper to inject the image into content area
100
+      var appendImage = function (gallery, id, show, instant) {
101
+        // Abort if image was appended already
102
+        if (jQuery('#jBox-image-' + gallery + '-' + id).length) {
103
+          return;
104
+        }
105
+
106
+        // Create image container
107
+        var image = jQuery('<div/>', {
108
+          id: 'jBox-image-' + gallery + '-' + id,
109
+          'class': 'jBox-image-container' + (show ? ' jBox-image-' + gallery + '-current' : '')
110
+        }).css({
111
+          backgroundSize: this.options.imageSize,
112
+          opacity: (instant ? 1 : 0),
113
+          zIndex: (show ? this.imageZIndex++ : 0)
114
+        }).appendTo(this.content);
115
+
116
+        // Add swipe events
117
+        this.swipeDetector(image)
118
+          .on("swipeLeft.sd swipeRight.sd", function (event) {
119
+            if (event.type === "swipeLeft") {
120
+              this.showImage('next');
121
+            } else if (event.type === "swipeRight") {
122
+              this.showImage('prev');
123
+            }
124
+          }.bind(this));
125
+
126
+        // Create labels
127
+        jQuery('<div/>', {
128
+          id: 'jBox-image-label-' + gallery + '-' + id,
129
+          'class': 'jBox-image-label' + (show ? ' active' : '')
130
+        })
131
+        .html(this.images[gallery][id].label)
132
+        .on('click tap', function () {
133
+          jQuery(this).toggleClass('expanded');
134
+        })
135
+        .appendTo(this.imageLabelContainer);
136
+
137
+        // Show image
138
+        show && image.animate({opacity: 1}, instant ? 0 : this.options.imageFade);
139
+
140
+        return image;
141
+      }.bind(this);
142
+
143
+      // Function to download an image
144
+      this.downloadImage = function (imageUrl) {
145
+        var link = document.createElement('a');
146
+        link.href = imageUrl;
147
+        link.setAttribute('download', imageUrl.substring(imageUrl.lastIndexOf('/')+1));
148
+        document.body.appendChild(link);
149
+        link.click();
150
+      };
151
+
152
+      // Helper to show new image label
153
+      var showLabel = function (gallery, id) {
154
+        jQuery('.jBox-image-label.active').removeClass('active expanded');
155
+        jQuery('#jBox-image-label-' + gallery + '-' + id).addClass('active');
156
+      };
157
+
158
+      // Helper to load image
159
+      var loadImage = function (gallery, id, show, instant) {
160
+        var imageContainer = appendImage(gallery, id, show, instant);
161
+        imageContainer.addClass('jBox-image-loading');
162
+
163
+        jQuery('<img src="' + this.images[gallery][id].src + '" />').each(function () {
164
+          var tmpImg = new Image();
165
+          tmpImg.onload = function () {
166
+            imageContainer.removeClass('jBox-image-loading');
167
+            imageContainer.css({backgroundImage: 'url("' + this.images[gallery][id].src + '")'});
168
+          }.bind(this);
169
+
170
+          tmpImg.onerror = function () {
171
+            imageContainer.removeClass('jBox-image-loading');
172
+            imageContainer.addClass('jBox-image-not-found');
173
+          }.bind(this);
174
+
175
+          tmpImg.src = this.images[gallery][id].src;
176
+        }.bind(this));
177
+      }.bind(this);
178
+
179
+      // Show images when they are loaded or load them if not
180
+      this.showImage = function (img) {
181
+        // Get the gallery and the image id from the next or the previous image
182
+        var gallery;
183
+        var id;
184
+
185
+        if (img != 'open') {
186
+          gallery = this.currentImage.gallery;
187
+          id = this.currentImage.id + (1 * (img == 'prev') ? -1 : 1);
188
+          id = id > (this.images[gallery].length - 1) ? 0 : (id < 0 ? (this.images[gallery].length - 1) : id);
189
+
190
+        // Or get image data from source element
191
+        } else {
192
+          // Get gallery and image id from source element
193
+          if (this.source) {
194
+            gallery = this.source.data('jBox-image-gallery');
195
+            id = this.source.data('jBox-image-id');
196
+
197
+          // Get gallery and image id attached elements
198
+          } else if (this.attachedElements && this.attachedElements.length) {
199
+            gallery = jQuery(this.attachedElements[0]).data('jBox-image-gallery');
200
+            id = jQuery(this.attachedElements[0]).data('jBox-image-id');
201
+          } else {
202
+            return;
203
+          }
204
+
205
+          // Remove or show the next and prev buttons
206
+          if (this.images && this.images[gallery]) {
207
+            jQuery('.jBox-image-pointer-prev, .jBox-image-pointer-next').css({display: (this.images[gallery].length > 1 ? 'block' : 'none')});
208
+          }
209
+        }
210
+
211
+        // If there is a current image already shown, hide it
212
+        if (jQuery('.jBox-image-' + gallery + '-current').length) {
213
+          jQuery('.jBox-image-' + gallery + '-current').removeClass('jBox-image-' + gallery + '-current').animate({opacity: 0}, (img == 'open') ? 0 : this.options.imageFade);
214
+        }
215
+
216
+        // Set new current image
217
+        this.currentImage = {gallery: gallery, id: id};
218
+
219
+        // Show image if it already exists
220
+        if (jQuery('#jBox-image-' + gallery + '-' + id).length) {
221
+          jQuery('#jBox-image-' + gallery + '-' + id).addClass('jBox-image-' + gallery + '-current').css({zIndex: this.imageZIndex++, opacity: 0}).animate({opacity: 1}, (img == 'open') ? 0 : this.options.imageFade);
222
+
223
+        // Load image
224
+        } else {
225
+          loadImage(gallery, id, true, (img === 'open'));
226
+        }
227
+
228
+        // Show label
229
+        showLabel(gallery, id);
230
+
231
+        // Update the image counter numbers
232
+        if (this.imageCounter) {
233
+          if (this.images[gallery] && this.images[gallery].length > 1) {
234
+            this.wrapper.addClass('jBox-image-has-counter');
235
+            this.imageCounter.find('.jBox-image-counter-all').html(this.images[gallery].length);
236
+            this.imageCounter.find('.jBox-image-counter-current').html(id + 1);
237
+          } else {
238
+            this.wrapper.removeClass('jBox-image-has-counter');
239
+          }
240
+        }
241
+
242
+        // Preload next image
243
+        if (this.images[gallery] && this.images[gallery].length - 1) {
244
+	        var next_id = id + 1;
245
+	        next_id = next_id > (this.images[gallery].length - 1) ? 0 : (next_id < 0 ? (this.images[gallery].length - 1) : next_id);
246
+
247
+	        if (!jQuery('#jBox-image-' + gallery + '-' + next_id).length) {
248
+            loadImage(gallery, next_id, false, false);
249
+          }
250
+	      }
251
+      };
252
+
253
+      // Preload image
254
+      if (this.options.preloadFirstImage) {
255
+        jQuery(window).on('load', function() {
256
+          this.showImage('open');
257
+        }.bind(this));
258
+      }
259
+    },
260
+
261
+
262
+    // Triggered when jBox attaches a new element
263
+
264
+    _onAttach: function (item) {
265
+      this.initImage && this.initImage(item);
266
+    },
267
+
268
+
269
+    // Triggered when jBox was created
270
+
271
+    _onCreated: function ()
272
+    {
273
+      // Create image label and navigation buttons
274
+      this.imageLabelWrapper = jQuery('<div class="jBox-image-label-wrapper"/>').appendTo(this.wrapper);
275
+
276
+      this.imagePrevButton = jQuery('<div class="jBox-image-pointer-prev"/>')
277
+        .on('click tap', function () {
278
+          this.showImage('prev');
279
+        }.bind(this));
280
+
281
+      this.imageNextButton = jQuery('<div class="jBox-image-pointer-next"/>')
282
+        .on('click tap', function () {
283
+          this.showImage('next');
284
+        }.bind(this));
285
+
286
+      this.imageLabelContainer = jQuery('<div class="jBox-image-label-container"/>');
287
+
288
+      this.imageLabelWrapper
289
+        .append(this.imagePrevButton)
290
+        .append(this.imageLabelContainer)
291
+        .append(this.imageNextButton);
292
+
293
+      // Append the download button
294
+      if (this.options.downloadButton) {
295
+        this.downloadButton = jQuery('<div/>', {'class': 'jBox-image-download-button-wrapper'})
296
+          .appendTo(this.wrapper)
297
+          .append(
298
+            this.options.downloadButtonText ? jQuery('<div/>', {'class': 'jBox-image-download-button-text'}).html(this.options.downloadButtonText) : null
299
+          )
300
+          .append(
301
+            jQuery('<div/>', {'class': 'jBox-image-download-button-icon'})
302
+          ).on('click tap', function () {
303
+            if (this.images[this.currentImage.gallery][this.currentImage.id].downloadUrl) {
304
+              var currentImageUrl = this.images[this.currentImage.gallery][this.currentImage.id].downloadUrl;
305
+            } else {
306
+              var currentImage = this.wrapper.find('.jBox-image-' + this.currentImage.gallery + '-current');
307
+              var currentImageStyle = currentImage[0].style.backgroundImage;
308
+              var currentImageUrl = currentImageStyle.slice(4, -1).replace(/["']/g, '');
309
+            }
310
+            this.downloadImage(currentImageUrl);
311
+          }.bind(this));
312
+      }
313
+
314
+      // Creating the image counter containers
315
+      if (this.options.imageCounter) {
316
+        this.imageCounter = jQuery('<div/>', {'class': 'jBox-image-counter-container'}).insertAfter(this.imageLabelContainer);
317
+        this.imageCounter.append(jQuery('<span/>', {'class': 'jBox-image-counter-current'})).append(jQuery('<span/>').html(this.options.imageCounterSeparator)).append(jQuery('<span/>', {'class': 'jBox-image-counter-all'}));
318
+      }
319
+    },
320
+
321
+
322
+    // Triggered when jBox opens
323
+
324
+    _onOpen: function ()
325
+    {
326
+      // Add key events
327
+      jQuery(document).on('keyup.jBox-Image-' + this.id, function (ev) {
328
+        (ev.keyCode == 37) && this.showImage('prev');
329
+        (ev.keyCode == 39) && this.showImage('next');
330
+      }.bind(this));
331
+
332
+      // Load the image from the attached element
333
+      this.showImage('open');
334
+    },
335
+
336
+
337
+    // Triggered when jBox closes
338
+
339
+    _onClose: function ()
340
+    {
341
+      // Remove key events
342
+      jQuery(document).off('keyup.jBox-Image-' + this.id);
343
+    },
344
+
345
+
346
+    // Triggered when jBox finished closing
347
+
348
+    _onCloseComplete: function ()
349
+    {
350
+      // Hide all image containers
351
+      this.wrapper.find('.jBox-image-container').css('opacity', 0);
352
+    }
353
+
354
+  });
355
+
356
+};
357
+
358
+//# sourceMappingURL=jBox.Image.js.map
Browse code

Add platform independent npm postinstall script

Benjamin Roth authored on22/02/2023 21:54:52
Showing1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,358 +0,0 @@
1
-/**
2
- * jBox Image plugin: Adds a lightbox to your images
3
- *
4
- * Author: Stephan Wagner <stephanwagner.me@gmail.com> (https://stephanwagner.me)
5
- *
6
- * License: MIT (https://opensource.org/licenses/MIT)
7
- *
8
- * Requires: jBox (https://cdn.jsdelivr.net/gh/StephanWagner/jBox@latest/dist/jBox.min.js)
9
- */
10
-
11
-function jBoxImageWrapper(jBox, jQuery) {
12
-
13
-  new jBox.plugin('Image', {
14
-
15
-
16
-    // Options (https://stephanwagner.me/jBox/options#options-image)
17
-
18
-    src: 'href',                 // The attribute where jBox gets the image source from, e.g. href="/path_to_image/image.jpg"
19
-    gallery: 'data-jbox-image',  // The attribute to set the galleries, e.g. data-jbox-image="gallery1"
20
-    imageLabel: 'title',         // The attribute where jBox gets the image label from, e.g. title="My label"
21
-    imageFade: 360,              // The fade duration for images in ms
22
-    imageSize: 'contain',        // How to display the images. Use CSS background-position values, e.g. 'cover', 'contain', 'auto', 'initial', '50% 50%'
23
-    imageCounter: false,         // Set to true to add an image counter, e.g. 4/20
24
-    imageCounterSeparator: '/',  // HTML to separate the current image number from all image numbers, e.g. '/' or ' of '
25
-    downloadButton: false,       // Adds a download button
26
-    downloadButtonText: null,    // Text for the download button
27
-    downloadButtonUrl: null,     // The attribute at the source element where to find the image to download, e.g. data-download="/path_to_image/image.jpg". If none provided, the currently active image will be downloaded
28
-    mobileImageAttr: null,       // The attribute to look for an mobile version of the image
29
-    mobileImageBreakpoint: null, // The upper breakpoint to load the mobile image
30
-    preloadFirstImage: false,    // Preload the first image when page is loaded
31
-    target: window,
32
-    attach: '[data-jbox-image]',
33
-    fixed: true,
34
-    blockScroll: true,
35
-    closeOnEsc: true,
36
-    closeOnClick: 'button',
37
-    closeButton: true,
38
-    overlay: true,
39
-    animation: 'zoomIn',
40
-    preventDefault: true,
41
-    width: '100%',
42
-    height: '100%',
43
-    adjustDistance: {
44
-      top: 40,
45
-      right: 0,
46
-      bottom: 40,
47
-      left: 0
48
-    },
49
-
50
-
51
-    // Triggered when jBox is initialized
52
-
53
-    _onInit: function ()
54
-    {
55
-      // Initial images and z-index
56
-      this.images = this.currentImage = {};
57
-      this.imageZIndex = 1;
58
-
59
-      this.initImage = function (item) {
60
-        item = jQuery(item);
61
-
62
-        // Abort if the item was added to a gallery already
63
-        if (item.data('jBox-image-gallery')) {
64
-          return;
65
-        }
66
-
67
-        // Get the image src
68
-        var src = item.attr(this.options.src);
69
-
70
-        // Update responsive image src
71
-        if (this.options.mobileImageAttr && this.options.mobileImageBreakpoint && item.attr(this.options.mobileImageAttr)) {
72
-          if (jQuery(window).width() <= this.options.mobileImageBreakpoint) {
73
-            src = item.attr(this.options.mobileImageAttr);
74
-          }
75
-        }
76
-
77
-        // Add item to a gallery
78
-        var gallery = item.attr(this.options.gallery) || 'default';
79
-        !this.images[gallery] && (this.images[gallery] = []);
80
-        this.images[gallery].push({
81
-          src: src,
82
-          label: (item.attr(this.options.imageLabel) || ''),
83
-          downloadUrl: this.options.downloadButtonUrl && item.attr(this.options.downloadButtonUrl) ? item.attr(this.options.downloadButtonUrl) : null
84
-        });
85
-
86
-        // Remove the title attribute so it won't show the browsers tooltip
87
-        this.options.imageLabel == 'title' && item.removeAttr('title');
88
-
89
-        // Store data in source element for easy access
90
-        item.data('jBox-image-gallery', gallery);
91
-        item.data('jBox-image-id', (this.images[gallery].length - 1));
92
-      }.bind(this);
93
-
94
-      // Loop through images, sort and save in global variable
95
-      this.attachedElements && this.attachedElements.length && jQuery.each(this.attachedElements, function (index, item) {
96
-        this.initImage(item);
97
-      }.bind(this));
98
-
99
-      // Helper to inject the image into content area
100
-      var appendImage = function (gallery, id, show, instant) {
101
-        // Abort if image was appended already
102
-        if (jQuery('#jBox-image-' + gallery + '-' + id).length) {
103
-          return;
104
-        }
105
-
106
-        // Create image container
107
-        var image = jQuery('<div/>', {
108
-          id: 'jBox-image-' + gallery + '-' + id,
109
-          'class': 'jBox-image-container' + (show ? ' jBox-image-' + gallery + '-current' : '')
110
-        }).css({
111
-          backgroundSize: this.options.imageSize,
112
-          opacity: (instant ? 1 : 0),
113
-          zIndex: (show ? this.imageZIndex++ : 0)
114
-        }).appendTo(this.content);
115
-
116
-        // Add swipe events
117
-        this.swipeDetector(image)
118
-          .on("swipeLeft.sd swipeRight.sd", function (event) {
119
-            if (event.type === "swipeLeft") {
120
-              this.showImage('next');
121
-            } else if (event.type === "swipeRight") {
122
-              this.showImage('prev');
123
-            }
124
-          }.bind(this));
125
-
126
-        // Create labels
127
-        jQuery('<div/>', {
128
-          id: 'jBox-image-label-' + gallery + '-' + id,
129
-          'class': 'jBox-image-label' + (show ? ' active' : '')
130
-        })
131
-        .html(this.images[gallery][id].label)
132
-        .on('click tap', function () {
133
-          jQuery(this).toggleClass('expanded');
134
-        })
135
-        .appendTo(this.imageLabelContainer);
136
-
137
-        // Show image
138
-        show && image.animate({opacity: 1}, instant ? 0 : this.options.imageFade);
139
-
140
-        return image;
141
-      }.bind(this);
142
-
143
-      // Function to download an image
144
-      this.downloadImage = function (imageUrl) {
145
-        var link = document.createElement('a');
146
-        link.href = imageUrl;
147
-        link.setAttribute('download', imageUrl.substring(imageUrl.lastIndexOf('/')+1));
148
-        document.body.appendChild(link);
149
-        link.click();
150
-      };
151
-
152
-      // Helper to show new image label
153
-      var showLabel = function (gallery, id) {
154
-        jQuery('.jBox-image-label.active').removeClass('active expanded');
155
-        jQuery('#jBox-image-label-' + gallery + '-' + id).addClass('active');
156
-      };
157
-
158
-      // Helper to load image
159
-      var loadImage = function (gallery, id, show, instant) {
160
-        var imageContainer = appendImage(gallery, id, show, instant);
161
-        imageContainer.addClass('jBox-image-loading');
162
-
163
-        jQuery('<img src="' + this.images[gallery][id].src + '" />').each(function () {
164
-          var tmpImg = new Image();
165
-          tmpImg.onload = function () {
166
-            imageContainer.removeClass('jBox-image-loading');
167
-            imageContainer.css({backgroundImage: 'url("' + this.images[gallery][id].src + '")'});
168
-          }.bind(this);
169
-
170
-          tmpImg.onerror = function () {
171
-            imageContainer.removeClass('jBox-image-loading');
172
-            imageContainer.addClass('jBox-image-not-found');
173
-          }.bind(this);
174
-
175
-          tmpImg.src = this.images[gallery][id].src;
176
-        }.bind(this));
177
-      }.bind(this);
178
-
179
-      // Show images when they are loaded or load them if not
180
-      this.showImage = function (img) {
181
-        // Get the gallery and the image id from the next or the previous image
182
-        var gallery;
183
-        var id;
184
-
185
-        if (img != 'open') {
186
-          gallery = this.currentImage.gallery;
187
-          id = this.currentImage.id + (1 * (img == 'prev') ? -1 : 1);
188
-          id = id > (this.images[gallery].length - 1) ? 0 : (id < 0 ? (this.images[gallery].length - 1) : id);
189
-
190
-        // Or get image data from source element
191
-        } else {
192
-          // Get gallery and image id from source element
193
-          if (this.source) {
194
-            gallery = this.source.data('jBox-image-gallery');
195
-            id = this.source.data('jBox-image-id');
196
-
197
-          // Get gallery and image id attached elements
198
-          } else if (this.attachedElements && this.attachedElements.length) {
199
-            gallery = jQuery(this.attachedElements[0]).data('jBox-image-gallery');
200
-            id = jQuery(this.attachedElements[0]).data('jBox-image-id');
201
-          } else {
202
-            return;
203
-          }
204
-
205
-          // Remove or show the next and prev buttons
206
-          if (this.images && this.images[gallery]) {
207
-            jQuery('.jBox-image-pointer-prev, .jBox-image-pointer-next').css({display: (this.images[gallery].length > 1 ? 'block' : 'none')});
208
-          }
209
-        }
210
-
211
-        // If there is a current image already shown, hide it
212
-        if (jQuery('.jBox-image-' + gallery + '-current').length) {
213
-          jQuery('.jBox-image-' + gallery + '-current').removeClass('jBox-image-' + gallery + '-current').animate({opacity: 0}, (img == 'open') ? 0 : this.options.imageFade);
214
-        }
215
-
216
-        // Set new current image
217
-        this.currentImage = {gallery: gallery, id: id};
218
-
219
-        // Show image if it already exists
220
-        if (jQuery('#jBox-image-' + gallery + '-' + id).length) {
221
-          jQuery('#jBox-image-' + gallery + '-' + id).addClass('jBox-image-' + gallery + '-current').css({zIndex: this.imageZIndex++, opacity: 0}).animate({opacity: 1}, (img == 'open') ? 0 : this.options.imageFade);
222
-
223
-        // Load image
224
-        } else {
225
-          loadImage(gallery, id, true, (img === 'open'));
226
-        }
227
-
228
-        // Show label
229
-        showLabel(gallery, id);
230
-
231
-        // Update the image counter numbers
232
-        if (this.imageCounter) {
233
-          if (this.images[gallery] && this.images[gallery].length > 1) {
234
-            this.wrapper.addClass('jBox-image-has-counter');
235
-            this.imageCounter.find('.jBox-image-counter-all').html(this.images[gallery].length);
236
-            this.imageCounter.find('.jBox-image-counter-current').html(id + 1);
237
-          } else {
238
-            this.wrapper.removeClass('jBox-image-has-counter');
239
-          }
240
-        }
241
-
242
-        // Preload next image
243
-        if (this.images[gallery] && this.images[gallery].length - 1) {
244
-	        var next_id = id + 1;
245
-	        next_id = next_id > (this.images[gallery].length - 1) ? 0 : (next_id < 0 ? (this.images[gallery].length - 1) : next_id);
246
-
247
-	        if (!jQuery('#jBox-image-' + gallery + '-' + next_id).length) {
248
-            loadImage(gallery, next_id, false, false);
249
-          }
250
-	      }
251
-      };
252
-
253
-      // Preload image
254
-      if (this.options.preloadFirstImage) {
255
-        jQuery(window).on('load', function() {
256
-          this.showImage('open');
257
-        }.bind(this));
258
-      }
259
-    },
260
-
261
-
262
-    // Triggered when jBox attaches a new element
263
-
264
-    _onAttach: function (item) {
265
-      this.initImage && this.initImage(item);
266
-    },
267
-
268
-
269
-    // Triggered when jBox was created
270
-
271
-    _onCreated: function ()
272
-    {
273
-      // Create image label and navigation buttons
274
-      this.imageLabelWrapper = jQuery('<div class="jBox-image-label-wrapper"/>').appendTo(this.wrapper);
275
-
276
-      this.imagePrevButton = jQuery('<div class="jBox-image-pointer-prev"/>')
277
-        .on('click tap', function () {
278
-          this.showImage('prev');
279
-        }.bind(this));
280
-
281
-      this.imageNextButton = jQuery('<div class="jBox-image-pointer-next"/>')
282
-        .on('click tap', function () {
283
-          this.showImage('next');
284
-        }.bind(this));
285
-
286
-      this.imageLabelContainer = jQuery('<div class="jBox-image-label-container"/>');
287
-
288
-      this.imageLabelWrapper
289
-        .append(this.imagePrevButton)
290
-        .append(this.imageLabelContainer)
291
-        .append(this.imageNextButton);
292
-
293
-      // Append the download button
294
-      if (this.options.downloadButton) {
295
-        this.downloadButton = jQuery('<div/>', {'class': 'jBox-image-download-button-wrapper'})
296
-          .appendTo(this.wrapper)
297
-          .append(
298
-            this.options.downloadButtonText ? jQuery('<div/>', {'class': 'jBox-image-download-button-text'}).html(this.options.downloadButtonText) : null
299
-          )
300
-          .append(
301
-            jQuery('<div/>', {'class': 'jBox-image-download-button-icon'})
302
-          ).on('click tap', function () {
303
-            if (this.images[this.currentImage.gallery][this.currentImage.id].downloadUrl) {
304
-              var currentImageUrl = this.images[this.currentImage.gallery][this.currentImage.id].downloadUrl;
305
-            } else {
306
-              var currentImage = this.wrapper.find('.jBox-image-' + this.currentImage.gallery + '-current');
307
-              var currentImageStyle = currentImage[0].style.backgroundImage;
308
-              var currentImageUrl = currentImageStyle.slice(4, -1).replace(/["']/g, '');
309
-            }
310
-            this.downloadImage(currentImageUrl);
311
-          }.bind(this));
312
-      }
313
-
314
-      // Creating the image counter containers
315
-      if (this.options.imageCounter) {
316
-        this.imageCounter = jQuery('<div/>', {'class': 'jBox-image-counter-container'}).insertAfter(this.imageLabelContainer);
317
-        this.imageCounter.append(jQuery('<span/>', {'class': 'jBox-image-counter-current'})).append(jQuery('<span/>').html(this.options.imageCounterSeparator)).append(jQuery('<span/>', {'class': 'jBox-image-counter-all'}));
318
-      }
319
-    },
320
-
321
-
322
-    // Triggered when jBox opens
323
-
324
-    _onOpen: function ()
325
-    {
326
-      // Add key events
327
-      jQuery(document).on('keyup.jBox-Image-' + this.id, function (ev) {
328
-        (ev.keyCode == 37) && this.showImage('prev');
329
-        (ev.keyCode == 39) && this.showImage('next');
330
-      }.bind(this));
331
-
332
-      // Load the image from the attached element
333
-      this.showImage('open');
334
-    },
335
-
336
-
337
-    // Triggered when jBox closes
338
-
339
-    _onClose: function ()
340
-    {
341
-      // Remove key events
342
-      jQuery(document).off('keyup.jBox-Image-' + this.id);
343
-    },
344
-
345
-
346
-    // Triggered when jBox finished closing
347
-
348
-    _onCloseComplete: function ()
349
-    {
350
-      // Hide all image containers
351
-      this.wrapper.find('.jBox-image-container').css('opacity', 0);
352
-    }
353
-
354
-  });
355
-
356
-};
357
-
358
-//# sourceMappingURL=jBox.Image.js.map
Browse code

Initial commit

Benjamin Roth authored on12/01/2023 00:53:30
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,358 @@
1
+/**
2
+ * jBox Image plugin: Adds a lightbox to your images
3
+ *
4
+ * Author: Stephan Wagner <stephanwagner.me@gmail.com> (https://stephanwagner.me)
5
+ *
6
+ * License: MIT (https://opensource.org/licenses/MIT)
7
+ *
8
+ * Requires: jBox (https://cdn.jsdelivr.net/gh/StephanWagner/jBox@latest/dist/jBox.min.js)
9
+ */
10
+
11
+function jBoxImageWrapper(jBox, jQuery) {
12
+
13
+  new jBox.plugin('Image', {
14
+
15
+
16
+    // Options (https://stephanwagner.me/jBox/options#options-image)
17
+
18
+    src: 'href',                 // The attribute where jBox gets the image source from, e.g. href="/path_to_image/image.jpg"
19
+    gallery: 'data-jbox-image',  // The attribute to set the galleries, e.g. data-jbox-image="gallery1"
20
+    imageLabel: 'title',         // The attribute where jBox gets the image label from, e.g. title="My label"
21
+    imageFade: 360,              // The fade duration for images in ms
22
+    imageSize: 'contain',        // How to display the images. Use CSS background-position values, e.g. 'cover', 'contain', 'auto', 'initial', '50% 50%'
23
+    imageCounter: false,         // Set to true to add an image counter, e.g. 4/20
24
+    imageCounterSeparator: '/',  // HTML to separate the current image number from all image numbers, e.g. '/' or ' of '
25
+    downloadButton: false,       // Adds a download button
26
+    downloadButtonText: null,    // Text for the download button
27
+    downloadButtonUrl: null,     // The attribute at the source element where to find the image to download, e.g. data-download="/path_to_image/image.jpg". If none provided, the currently active image will be downloaded
28
+    mobileImageAttr: null,       // The attribute to look for an mobile version of the image
29
+    mobileImageBreakpoint: null, // The upper breakpoint to load the mobile image
30
+    preloadFirstImage: false,    // Preload the first image when page is loaded
31
+    target: window,
32
+    attach: '[data-jbox-image]',
33
+    fixed: true,
34
+    blockScroll: true,
35
+    closeOnEsc: true,
36
+    closeOnClick: 'button',
37
+    closeButton: true,
38
+    overlay: true,
39
+    animation: 'zoomIn',
40
+    preventDefault: true,
41
+    width: '100%',
42
+    height: '100%',
43
+    adjustDistance: {
44
+      top: 40,
45
+      right: 0,
46
+      bottom: 40,
47
+      left: 0
48
+    },
49
+
50
+
51
+    // Triggered when jBox is initialized
52
+
53
+    _onInit: function ()
54
+    {
55
+      // Initial images and z-index
56
+      this.images = this.currentImage = {};
57
+      this.imageZIndex = 1;
58
+
59
+      this.initImage = function (item) {
60
+        item = jQuery(item);
61
+
62
+        // Abort if the item was added to a gallery already
63
+        if (item.data('jBox-image-gallery')) {
64
+          return;
65
+        }
66
+
67
+        // Get the image src
68
+        var src = item.attr(this.options.src);
69
+
70
+        // Update responsive image src
71
+        if (this.options.mobileImageAttr && this.options.mobileImageBreakpoint && item.attr(this.options.mobileImageAttr)) {
72
+          if (jQuery(window).width() <= this.options.mobileImageBreakpoint) {
73
+            src = item.attr(this.options.mobileImageAttr);
74
+          }
75
+        }
76
+
77
+        // Add item to a gallery
78
+        var gallery = item.attr(this.options.gallery) || 'default';
79
+        !this.images[gallery] && (this.images[gallery] = []);
80
+        this.images[gallery].push({
81
+          src: src,
82
+          label: (item.attr(this.options.imageLabel) || ''),
83
+          downloadUrl: this.options.downloadButtonUrl && item.attr(this.options.downloadButtonUrl) ? item.attr(this.options.downloadButtonUrl) : null
84
+        });
85
+
86
+        // Remove the title attribute so it won't show the browsers tooltip
87
+        this.options.imageLabel == 'title' && item.removeAttr('title');
88
+
89
+        // Store data in source element for easy access
90
+        item.data('jBox-image-gallery', gallery);
91
+        item.data('jBox-image-id', (this.images[gallery].length - 1));
92
+      }.bind(this);
93
+
94
+      // Loop through images, sort and save in global variable
95
+      this.attachedElements && this.attachedElements.length && jQuery.each(this.attachedElements, function (index, item) {
96
+        this.initImage(item);
97
+      }.bind(this));
98
+
99
+      // Helper to inject the image into content area
100
+      var appendImage = function (gallery, id, show, instant) {
101
+        // Abort if image was appended already
102
+        if (jQuery('#jBox-image-' + gallery + '-' + id).length) {
103
+          return;
104
+        }
105
+
106
+        // Create image container
107
+        var image = jQuery('<div/>', {
108
+          id: 'jBox-image-' + gallery + '-' + id,
109
+          'class': 'jBox-image-container' + (show ? ' jBox-image-' + gallery + '-current' : '')
110
+        }).css({
111
+          backgroundSize: this.options.imageSize,
112
+          opacity: (instant ? 1 : 0),
113
+          zIndex: (show ? this.imageZIndex++ : 0)
114
+        }).appendTo(this.content);
115
+
116
+        // Add swipe events
117
+        this.swipeDetector(image)
118
+          .on("swipeLeft.sd swipeRight.sd", function (event) {
119
+            if (event.type === "swipeLeft") {
120
+              this.showImage('next');
121
+            } else if (event.type === "swipeRight") {
122
+              this.showImage('prev');
123
+            }
124
+          }.bind(this));
125
+
126
+        // Create labels
127
+        jQuery('<div/>', {
128
+          id: 'jBox-image-label-' + gallery + '-' + id,
129
+          'class': 'jBox-image-label' + (show ? ' active' : '')
130
+        })
131
+        .html(this.images[gallery][id].label)
132
+        .on('click tap', function () {
133
+          jQuery(this).toggleClass('expanded');
134
+        })
135
+        .appendTo(this.imageLabelContainer);
136
+
137
+        // Show image
138
+        show && image.animate({opacity: 1}, instant ? 0 : this.options.imageFade);
139
+
140
+        return image;
141
+      }.bind(this);
142
+
143
+      // Function to download an image
144
+      this.downloadImage = function (imageUrl) {
145
+        var link = document.createElement('a');
146
+        link.href = imageUrl;
147
+        link.setAttribute('download', imageUrl.substring(imageUrl.lastIndexOf('/')+1));
148
+        document.body.appendChild(link);
149
+        link.click();
150
+      };
151
+
152
+      // Helper to show new image label
153
+      var showLabel = function (gallery, id) {
154
+        jQuery('.jBox-image-label.active').removeClass('active expanded');
155
+        jQuery('#jBox-image-label-' + gallery + '-' + id).addClass('active');
156
+      };
157
+
158
+      // Helper to load image
159
+      var loadImage = function (gallery, id, show, instant) {
160
+        var imageContainer = appendImage(gallery, id, show, instant);
161
+        imageContainer.addClass('jBox-image-loading');
162
+
163
+        jQuery('<img src="' + this.images[gallery][id].src + '" />').each(function () {
164
+          var tmpImg = new Image();
165
+          tmpImg.onload = function () {
166
+            imageContainer.removeClass('jBox-image-loading');
167
+            imageContainer.css({backgroundImage: 'url("' + this.images[gallery][id].src + '")'});
168
+          }.bind(this);
169
+
170
+          tmpImg.onerror = function () {
171
+            imageContainer.removeClass('jBox-image-loading');
172
+            imageContainer.addClass('jBox-image-not-found');
173
+          }.bind(this);
174
+
175
+          tmpImg.src = this.images[gallery][id].src;
176
+        }.bind(this));
177
+      }.bind(this);
178
+
179
+      // Show images when they are loaded or load them if not
180
+      this.showImage = function (img) {
181
+        // Get the gallery and the image id from the next or the previous image
182
+        var gallery;
183
+        var id;
184
+
185
+        if (img != 'open') {
186
+          gallery = this.currentImage.gallery;
187
+          id = this.currentImage.id + (1 * (img == 'prev') ? -1 : 1);
188
+          id = id > (this.images[gallery].length - 1) ? 0 : (id < 0 ? (this.images[gallery].length - 1) : id);
189
+
190
+        // Or get image data from source element
191
+        } else {
192
+          // Get gallery and image id from source element
193
+          if (this.source) {
194
+            gallery = this.source.data('jBox-image-gallery');
195
+            id = this.source.data('jBox-image-id');
196
+
197
+          // Get gallery and image id attached elements
198
+          } else if (this.attachedElements && this.attachedElements.length) {
199
+            gallery = jQuery(this.attachedElements[0]).data('jBox-image-gallery');
200
+            id = jQuery(this.attachedElements[0]).data('jBox-image-id');
201
+          } else {
202
+            return;
203
+          }
204
+
205
+          // Remove or show the next and prev buttons
206
+          if (this.images && this.images[gallery]) {
207
+            jQuery('.jBox-image-pointer-prev, .jBox-image-pointer-next').css({display: (this.images[gallery].length > 1 ? 'block' : 'none')});
208
+          }
209
+        }
210
+
211
+        // If there is a current image already shown, hide it
212
+        if (jQuery('.jBox-image-' + gallery + '-current').length) {
213
+          jQuery('.jBox-image-' + gallery + '-current').removeClass('jBox-image-' + gallery + '-current').animate({opacity: 0}, (img == 'open') ? 0 : this.options.imageFade);
214
+        }
215
+
216
+        // Set new current image
217
+        this.currentImage = {gallery: gallery, id: id};
218
+
219
+        // Show image if it already exists
220
+        if (jQuery('#jBox-image-' + gallery + '-' + id).length) {
221
+          jQuery('#jBox-image-' + gallery + '-' + id).addClass('jBox-image-' + gallery + '-current').css({zIndex: this.imageZIndex++, opacity: 0}).animate({opacity: 1}, (img == 'open') ? 0 : this.options.imageFade);
222
+
223
+        // Load image
224
+        } else {
225
+          loadImage(gallery, id, true, (img === 'open'));
226
+        }
227
+
228
+        // Show label
229
+        showLabel(gallery, id);
230
+
231
+        // Update the image counter numbers
232
+        if (this.imageCounter) {
233
+          if (this.images[gallery] && this.images[gallery].length > 1) {
234
+            this.wrapper.addClass('jBox-image-has-counter');
235
+            this.imageCounter.find('.jBox-image-counter-all').html(this.images[gallery].length);
236
+            this.imageCounter.find('.jBox-image-counter-current').html(id + 1);
237
+          } else {
238
+            this.wrapper.removeClass('jBox-image-has-counter');
239
+          }
240
+        }
241
+
242
+        // Preload next image
243
+        if (this.images[gallery] && this.images[gallery].length - 1) {
244
+	        var next_id = id + 1;
245
+	        next_id = next_id > (this.images[gallery].length - 1) ? 0 : (next_id < 0 ? (this.images[gallery].length - 1) : next_id);
246
+
247
+	        if (!jQuery('#jBox-image-' + gallery + '-' + next_id).length) {
248
+            loadImage(gallery, next_id, false, false);
249
+          }
250
+	      }
251
+      };
252
+
253
+      // Preload image
254
+      if (this.options.preloadFirstImage) {
255
+        jQuery(window).on('load', function() {
256
+          this.showImage('open');
257
+        }.bind(this));
258
+      }
259
+    },
260
+
261
+
262
+    // Triggered when jBox attaches a new element
263
+
264
+    _onAttach: function (item) {
265
+      this.initImage && this.initImage(item);
266
+    },
267
+
268
+
269
+    // Triggered when jBox was created
270
+
271
+    _onCreated: function ()
272
+    {
273
+      // Create image label and navigation buttons
274
+      this.imageLabelWrapper = jQuery('<div class="jBox-image-label-wrapper"/>').appendTo(this.wrapper);
275
+
276
+      this.imagePrevButton = jQuery('<div class="jBox-image-pointer-prev"/>')
277
+        .on('click tap', function () {
278
+          this.showImage('prev');
279
+        }.bind(this));
280
+
281
+      this.imageNextButton = jQuery('<div class="jBox-image-pointer-next"/>')
282
+        .on('click tap', function () {
283
+          this.showImage('next');
284
+        }.bind(this));
285
+
286
+      this.imageLabelContainer = jQuery('<div class="jBox-image-label-container"/>');
287
+
288
+      this.imageLabelWrapper
289
+        .append(this.imagePrevButton)
290
+        .append(this.imageLabelContainer)
291
+        .append(this.imageNextButton);
292
+
293
+      // Append the download button
294
+      if (this.options.downloadButton) {
295
+        this.downloadButton = jQuery('<div/>', {'class': 'jBox-image-download-button-wrapper'})
296
+          .appendTo(this.wrapper)
297
+          .append(
298
+            this.options.downloadButtonText ? jQuery('<div/>', {'class': 'jBox-image-download-button-text'}).html(this.options.downloadButtonText) : null
299
+          )
300
+          .append(
301
+            jQuery('<div/>', {'class': 'jBox-image-download-button-icon'})
302
+          ).on('click tap', function () {
303
+            if (this.images[this.currentImage.gallery][this.currentImage.id].downloadUrl) {
304
+              var currentImageUrl = this.images[this.currentImage.gallery][this.currentImage.id].downloadUrl;
305
+            } else {
306
+              var currentImage = this.wrapper.find('.jBox-image-' + this.currentImage.gallery + '-current');
307
+              var currentImageStyle = currentImage[0].style.backgroundImage;
308
+              var currentImageUrl = currentImageStyle.slice(4, -1).replace(/["']/g, '');
309
+            }
310
+            this.downloadImage(currentImageUrl);
311
+          }.bind(this));
312
+      }
313
+
314
+      // Creating the image counter containers
315
+      if (this.options.imageCounter) {
316
+        this.imageCounter = jQuery('<div/>', {'class': 'jBox-image-counter-container'}).insertAfter(this.imageLabelContainer);
317
+        this.imageCounter.append(jQuery('<span/>', {'class': 'jBox-image-counter-current'})).append(jQuery('<span/>').html(this.options.imageCounterSeparator)).append(jQuery('<span/>', {'class': 'jBox-image-counter-all'}));
318
+      }
319
+    },
320
+
321
+
322
+    // Triggered when jBox opens
323
+
324
+    _onOpen: function ()
325
+    {
326
+      // Add key events
327
+      jQuery(document).on('keyup.jBox-Image-' + this.id, function (ev) {
328
+        (ev.keyCode == 37) && this.showImage('prev');
329
+        (ev.keyCode == 39) && this.showImage('next');
330
+      }.bind(this));
331
+
332
+      // Load the image from the attached element
333
+      this.showImage('open');
334
+    },
335
+
336
+
337
+    // Triggered when jBox closes
338
+
339
+    _onClose: function ()
340
+    {
341
+      // Remove key events
342
+      jQuery(document).off('keyup.jBox-Image-' + this.id);
343
+    },
344
+
345
+
346
+    // Triggered when jBox finished closing
347
+
348
+    _onCloseComplete: function ()
349
+    {
350
+      // Hide all image containers
351
+      this.wrapper.find('.jBox-image-container').css('opacity', 0);
352
+    }
353
+
354
+  });
355
+
356
+};
357
+
358
+//# sourceMappingURL=jBox.Image.js.map