/*! uikit 2.25.0 | http://www.getuikit.com | (c) 2014 yootheme | mit license */
(function(addon) {
var component;
if (window.uikit) {
component = addon(uikit);
}
if (typeof define == "function" && define.amd) { // amd
define("uikit-lightbox", ["uikit"], function(){
return component || addon(uikit);
});
}
})(function(ui){
"use strict";
var modal, cache = {};
ui.component('lightbox', {
defaults: {
"group" : false,
"duration" : 400,
"keyboard" : true
},
index : 0,
items : false,
boot: function() {
ui.$html.on('click', '[data-uk-lightbox]', function(e){
e.preventdefault();
var link = ui.$(this);
if (!link.data("lightbox")) {
ui.lightbox(link, ui.utils.options(link.attr("data-uk-lightbox")));
}
link.data("lightbox").show(link);
});
// keyboard navigation
ui.$doc.on('keyup', function(e) {
if (modal && modal.is(':visible') && modal.lightbox.options.keyboard) {
e.preventdefault();
switch(e.keycode) {
case 37:
modal.lightbox.previous();
break;
case 39:
modal.lightbox.next();
break;
}
}
});
},
init: function() {
var siblings = [];
this.index = 0;
this.siblings = [];
if (this.element && this.element.length) {
var domsiblings = this.options.group ? ui.$([
'[data-uk-lightbox*="'+this.options.group+'"]',
"[data-uk-lightbox*='"+this.options.group+"']"
].join(',')) : this.element;
domsiblings.each(function() {
var ele = ui.$(this);
siblings.push({
'source': ele.attr('href'),
'title' : ele.attr('data-title') || ele.attr('title'),
'type' : ele.attr("data-lightbox-type") || 'auto',
'link' : ele
});
});
this.index = domsiblings.index(this.element);
this.siblings = siblings;
} else if (this.options.group && this.options.group.length) {
this.siblings = this.options.group;
}
this.trigger('lightbox-init', [this]);
},
show: function(index) {
this.modal = getmodal(this);
// stop previous animation
this.modal.dialog.stop();
this.modal.content.stop();
var $this = this, promise = ui.$.deferred(), data, item;
index = index || 0;
// index is a jquery object or dom element
if (typeof(index) == 'object') {
this.siblings.foreach(function(s, idx){
if (index[0] === s.link[0]) {
index = idx;
}
});
}
// fix index if needed
if ( index < 0 ) {
index = this.siblings.length - index;
} else if (!this.siblings[index]) {
index = 0;
}
item = this.siblings[index];
data = {
"lightbox" : $this,
"source" : item.source,
"type" : item.type,
"index" : index,
"promise" : promise,
"title" : item.title,
"item" : item,
"meta" : {
"content" : '',
"width" : null,
"height" : null
}
};
this.index = index;
this.modal.content.empty();
if (!this.modal.is(':visible')) {
this.modal.content.css({width:'', height:''}).empty();
this.modal.modal.show();
}
this.modal.loader.removeclass('uk-hidden');
promise.promise().done(function() {
$this.data = data;
$this.fitsize(data);
}).fail(function(){
data.meta.content = '
loading resource failed!
';
data.meta.width = 400;
data.meta.height = 300;
$this.data = data;
$this.fitsize(data);
});
$this.trigger('showitem.uk.lightbox', [data]);
},
fitsize: function() {
var $this = this,
data = this.data,
pad = this.modal.dialog.outerwidth() - this.modal.dialog.width(),
dpadtop = parseint(this.modal.dialog.css('margin-top'), 10),
dpadbot = parseint(this.modal.dialog.css('margin-bottom'), 10),
dpad = dpadtop + dpadbot,
content = data.meta.content,
duration = $this.options.duration;
if (this.siblings.length > 1) {
content = [
content,
'',
''
].join('');
}
// calculate width
var tmp = ui.$('
').css({
'opacity' : 0,
'position' : 'absolute',
'top' : 0,
'left' : 0,
'width' : '100%',
'max-width' : $this.modal.dialog.css('max-width'),
'padding' : $this.modal.dialog.css('padding'),
'margin' : $this.modal.dialog.css('margin')
}), maxwidth, maxheight, w = data.meta.width, h = data.meta.height;
tmp.appendto('body').width();
maxwidth = tmp.width();
maxheight = window.innerheight - dpad;
tmp.remove();
this.modal.dialog.find('.uk-modal-caption').remove();
if (data.title) {
this.modal.dialog.append(''+data.title+'
');
maxheight -= this.modal.dialog.find('.uk-modal-caption').outerheight();
}
if (maxwidth < data.meta.width) {
h = math.floor( h * (maxwidth / w) );
w = maxwidth;
}
if (maxheight < h) {
h = math.floor(maxheight);
w = math.ceil(data.meta.width * (maxheight/data.meta.height));
}
this.modal.content.css('opacity', 0).width(w).html(content);
if (data.type == 'iframe') {
this.modal.content.find('iframe:first').height(h);
}
var dh = h + pad,
t = math.floor(window.innerheight/2 - dh/2) - dpad;
if (t < 0) { t = 0; }
this.modal.closer.addclass('uk-hidden');
if ($this.modal.data('mwidth') == w && $this.modal.data('mheight') == h) {
duration = 0;
}
this.modal.dialog.animate({width: w + pad, height: h + pad, top: t }, duration, 'swing', function() {
$this.modal.loader.addclass('uk-hidden');
$this.modal.content.css({width:''}).animate({'opacity': 1}, function() {
$this.modal.closer.removeclass('uk-hidden');
});
$this.modal.data({'mwidth': w, 'mheight': h});
});
},
next: function() {
this.show(this.siblings[(this.index+1)] ? (this.index+1) : 0);
},
previous: function() {
this.show(this.siblings[(this.index-1)] ? (this.index-1) : this.siblings.length-1);
}
});
// plugins
ui.plugin('lightbox', 'image', {
init: function(lightbox) {
lightbox.on("showitem.uk.lightbox", function(e, data){
if (data.type == 'image' || data.source && data.source.match(/\.(jpg|jpeg|png|gif|svg)$/i)) {
var resolve = function(source, width, height) {
data.meta = {
"content" : '',
"width" : width,
"height" : height
};
data.type = 'image';
data.promise.resolve();
};
if (!cache[data.source]) {
var img = new image();
img.onerror = function(){
data.promise.reject('loading image failed');
};
img.onload = function(){
cache[data.source] = {width: img.width, height: img.height};
resolve(data.source, cache[data.source].width, cache[data.source].height);
};
img.src = data.source;
} else {
resolve(data.source, cache[data.source].width, cache[data.source].height);
}
}
});
}
});
ui.plugin("lightbox", "youtube", {
init: function(lightbox) {
var youtuberegexp = /(\/\/.*?youtube\.[a-z]+)\/watch\?v=([^&]+)&?(.*)/,
youtuberegexpshort = /youtu\.be\/(.*)/;
lightbox.on("showitem.uk.lightbox", function(e, data){
var id, matches, resolve = function(id, width, height) {
data.meta = {
'content': '',
'width': width,
'height': height
};
data.type = 'iframe';
data.promise.resolve();
};
if (matches = data.source.match(youtuberegexp)) {
id = matches[2];
}
if (matches = data.source.match(youtuberegexpshort)) {
id = matches[1];
}
if (id) {
if(!cache[id]) {
var img = new image(), lowres = false;
img.onerror = function(){
cache[id] = {width:640, height:320};
resolve(id, cache[id].width, cache[id].height);
};
img.onload = function(){
//youtube default 404 thumb, fall back to lowres
if (img.width == 120 && img.height == 90) {
if (!lowres) {
lowres = true;
img.src = '//img.youtube.com/vi/' + id + '/0.jpg';
} else {
cache[id] = {width: 640, height: 320};
resolve(id, cache[id].width, cache[id].height);
}
} else {
cache[id] = {width: img.width, height: img.height};
resolve(id, img.width, img.height);
}
};
img.src = '//img.youtube.com/vi/'+id+'/maxresdefault.jpg';
} else {
resolve(id, cache[id].width, cache[id].height);
}
e.stopimmediatepropagation();
}
});
}
});
ui.plugin("lightbox", "vimeo", {
init: function(lightbox) {
var regex = /(\/\/.*?)vimeo\.[a-z]+\/([0-9]+).*?/, matches;
lightbox.on("showitem.uk.lightbox", function(e, data){
var id, resolve = function(id, width, height) {
data.meta = {
'content': '',
'width': width,
'height': height
};
data.type = 'iframe';
data.promise.resolve();
};
if (matches = data.source.match(regex)) {
id = matches[2];
if(!cache[id]) {
ui.$.ajax({
type : 'get',
url : 'http://vimeo.com/api/oembed.json?url=' + encodeuri(data.source),
jsonp : 'callback',
datatype : 'jsonp',
success : function(data) {
cache[id] = {width:data.width, height:data.height};
resolve(id, cache[id].width, cache[id].height);
}
});
} else {
resolve(id, cache[id].width, cache[id].height);
}
e.stopimmediatepropagation();
}
});
}
});
ui.plugin("lightbox", "video", {
init: function(lightbox) {
lightbox.on("showitem.uk.lightbox", function(e, data){
var resolve = function(source, width, height) {
data.meta = {
'content': '',
'width': width,
'height': height
};
data.type = 'video';
data.promise.resolve();
};
if (data.type == 'video' || data.source.match(/\.(mp4|webm|ogv)$/i)) {
if (!cache[data.source]) {
var vid = ui.$('').attr('src', data.source).appendto('body');
var idle = setinterval(function() {
if (vid[0].videowidth) {
clearinterval(idle);
cache[data.source] = {width: vid[0].videowidth, height: vid[0].videoheight};
resolve(data.source, cache[data.source].width, cache[data.source].height);
vid.remove();
}
}, 20);
} else {
resolve(data.source, cache[data.source].width, cache[data.source].height);
}
}
});
}
});
function getmodal(lightbox) {
if (modal) {
modal.lightbox = lightbox;
return modal;
}
// init lightbox container
modal = ui.$([
''
].join('')).appendto('body');
modal.dialog = modal.find('.uk-modal-dialog:first');
modal.content = modal.find('.uk-lightbox-content:first');
modal.loader = modal.find('.uk-modal-spinner:first');
modal.closer = modal.find('.uk-close.uk-close-alt');
modal.modal = ui.modal(modal, {modal:false});
// next / previous
modal.on("swiperight swipeleft", function(e) {
modal.lightbox[e.type=='swipeleft' ? 'next':'previous']();
}).on("click", "[data-lightbox-previous], [data-lightbox-next]", function(e){
e.preventdefault();
modal.lightbox[ui.$(this).is('[data-lightbox-next]') ? 'next':'previous']();
});
// destroy content on modal hide
modal.on("hide.uk.modal", function(e) {
modal.content.html('');
});
ui.$win.on('load resize orientationchange', ui.utils.debounce(function(e){
if (modal.is(':visible') && !ui.utils.isfullscreen()) modal.lightbox.fitsize();
}.bind(this), 100));
modal.lightbox = lightbox;
return modal;
}
ui.lightbox.create = function(items, options) {
if (!items) return;
var group = [], o;
items.foreach(function(item) {
group.push(ui.$.extend({
'source' : '',
'title' : '',
'type' : 'auto',
'link' : false
}, (typeof(item) == 'string' ? {'source': item} : item)));
});
o = ui.lightbox(ui.$.extend({}, options, {'group':group}));
return o;
};
return ui.lightbox;
});