Skip to content
This repository was archived by the owner on May 22, 2020. It is now read-only.

Commit 82119a2

Browse files
author
Chase Peeler
committed
Added new configuration options that allow specifying which attributes are used for the data source, footer, and title.
Options can be the name of attributes or callbacks. Added a method called _getAttr which will use the .data method if the attribute starts with data-, otherwise the .attr method.
1 parent 496f727 commit 82119a2

File tree

5 files changed

+55
-26
lines changed

5 files changed

+55
-26
lines changed

dist/ekko-lightbox.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/*!
2-
* Lightbox for Bootstrap by @ashleydw
3-
* https://github.com/ashleydw/lightbox
4-
*
5-
* License: https://github.com/ashleydw/lightbox/blob/master/LICENSE
6-
*/
7-
+function ($) {
8-
1+
/*!
2+
* Lightbox for Bootstrap by @ashleydw
3+
* https://github.com/ashleydw/lightbox
4+
*
5+
* License: https://github.com/ashleydw/lightbox/blob/master/LICENSE
6+
*/
7+
+function ($) {
8+
99
'use strict';
1010

1111
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
@@ -34,6 +34,9 @@ var Lightbox = (function ($) {
3434
fail: 'Failed to load image:',
3535
type: 'Could not detect remote target type. Force the type using data-type'
3636
},
37+
dataSrc: 'data-remote',
38+
titleSrc: 'data-title',
39+
footerSrc: 'data-footer',
3740
doc: document, // if in an iframe can specify top.document
3841
onShow: function onShow() {},
3942
onShown: function onShown() {},
@@ -261,6 +264,17 @@ var Lightbox = (function ($) {
261264

262265
return type;
263266
}
267+
}, {
268+
key: '_getAttr',
269+
value: function _getAttr(element, attr) {
270+
if (typeof attr === 'function') {
271+
return attr.call(element);
272+
} else if (0 === attr.toString().indexOf("data-")) {
273+
return $(element).data(attr.replace("data-", ""));
274+
} else {
275+
return $(element).attr(attr);
276+
}
277+
}
264278
}, {
265279
key: '_isImage',
266280
value: function _isImage(string) {
@@ -296,8 +310,9 @@ var Lightbox = (function ($) {
296310
var $toUse = this._containerToUse();
297311
this._updateTitleAndFooter();
298312

299-
var currentRemote = this._$element.attr('data-remote') || this._$element.attr('href');
300-
var currentType = this._detectRemoteType(currentRemote, this._$element.attr('data-type') || false);
313+
var currentRemote = this._getAttr(this._$element, this._config.dataSrc) || this._getAttr(this._$element, 'href');
314+
315+
var currentType = this._detectRemoteType(currentRemote, this._getAttr(this._$element, 'data-type') || false);
301316

302317
if (['image', 'youtube', 'vimeo', 'instagram', 'video', 'url'].indexOf(currentType) < 0) return this._error(this._config.strings.type);
303318

@@ -388,8 +403,8 @@ var Lightbox = (function ($) {
388403
}, {
389404
key: '_updateTitleAndFooter',
390405
value: function _updateTitleAndFooter() {
391-
var title = this._$element.data('title') || "";
392-
var caption = this._$element.data('footer') || "";
406+
var title = this._getAttr(this._$element, this._config.titleSrc) || "";
407+
var caption = this._getAttr(this._$element, this._config.footerSrc) || "";
393408

394409
this._titleIsShown = false;
395410
if (title || this._config.alwaysShowClose) {
@@ -519,8 +534,8 @@ var Lightbox = (function ($) {
519534
var next = $(this._$galleryItems.get(startIndex), false);
520535
if (typeof next == 'undefined') return;
521536

522-
var src = next.attr('data-remote') || next.attr('href');
523-
if (next.attr('data-type') === 'image' || this._isImage(src)) this._preloadImage(src, false);
537+
var src = this._getAttr(next, this._config.dataSrc) || this._getAttr(next, 'href');
538+
if (this._getAttr(next, 'data-type') === 'image' || this._isImage(src)) this._preloadImage(src, false);
524539

525540
if (numberOfTimes > 0) return this._preloadImageByIndex(startIndex + 1, numberOfTimes - 1);
526541
}
@@ -663,6 +678,6 @@ var Lightbox = (function ($) {
663678

664679
return Lightbox;
665680
})(jQuery);
666-
//# sourceMappingURL=ekko-lightbox.js.map
667-
668-
}(jQuery);
681+
//# sourceMappingURL=ekko-lightbox.js.map
682+
683+
}(jQuery);

dist/ekko-lightbox.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ekko-lightbox.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ekko-lightbox.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ekko-lightbox.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const Lightbox = (($) => {
2020
fail: 'Failed to load image:',
2121
type: 'Could not detect remote target type. Force the type using data-type',
2222
},
23+
dataSrc: 'data-remote',
24+
titleSrc: 'data-title',
25+
footerSrc: 'data-footer',
2326
doc: document, // if in an iframe can specify top.document
2427
onShow() {},
2528
onShown() {},
@@ -268,6 +271,16 @@ const Lightbox = (($) => {
268271
return type;
269272
}
270273

274+
_getAttr(element,attr){
275+
if(typeof attr === 'function') {
276+
return attr.call(element);
277+
} else if(0 === attr.toString().indexOf("data-")) {
278+
return $(element).data(attr.replace("data-", ""));
279+
} else {
280+
return $(element).attr(attr);
281+
}
282+
}
283+
271284
_isImage(string) {
272285
return string && string.match(/(^data:image\/.*,)|(\.(jp(e|g|eg)|gif|png|bmp|webp|svg)((\?|#).*)?$)/i)
273286
}
@@ -299,8 +312,9 @@ const Lightbox = (($) => {
299312
let $toUse = this._containerToUse()
300313
this._updateTitleAndFooter()
301314

302-
let currentRemote = this._$element.attr('data-remote') || this._$element.attr('href')
303-
let currentType = this._detectRemoteType(currentRemote, this._$element.attr('data-type') || false)
315+
let currentRemote = this._getAttr(this._$element,this._config.dataSrc) || this._getAttr(this._$element,'href')
316+
317+
let currentType = this._detectRemoteType(currentRemote, this._getAttr(this._$element,'data-type') || false)
304318

305319
if(['image', 'youtube', 'vimeo', 'instagram', 'video', 'url'].indexOf(currentType) < 0)
306320
return this._error(this._config.strings.type)
@@ -386,8 +400,8 @@ const Lightbox = (($) => {
386400
}
387401

388402
_updateTitleAndFooter() {
389-
let title = this._$element.data('title') || ""
390-
let caption = this._$element.data('footer') || ""
403+
let title = this._getAttr(this._$element,this._config.titleSrc) || ""
404+
let caption = this._getAttr(this._$element,this._config.footerSrc) || ""
391405

392406
this._titleIsShown = false
393407
if (title || this._config.alwaysShowClose) {
@@ -520,8 +534,8 @@ const Lightbox = (($) => {
520534
if(typeof next == 'undefined')
521535
return
522536

523-
let src = next.attr('data-remote') || next.attr('href')
524-
if (next.attr('data-type') === 'image' || this._isImage(src))
537+
let src = this._getAttr(next,this._config.dataSrc) || this._getAttr(next,'href')
538+
if (this._getAttr(next,'data-type') === 'image' || this._isImage(src))
525539
this._preloadImage(src, false)
526540

527541
if(numberOfTimes > 0)

0 commit comments

Comments
 (0)