/*!
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2015
* @version 4.1.8
*
* File input styled for Bootstrap 3.0 that utilizes HTML5 File Input's advanced
* features including the FileReader API.
*
* The plugin drastically enhances the HTML file input to preview multiple files on the client before
* upload. In addition it provides the ability to preview content of images, text, videos, audio, html,
* flash and other objects. It also offers the ability to upload and delete files using AJAX, and add
* files in batches (i.e. preview, append, or remove before upload).
*
* Author: Kartik Visweswaran
* Copyright: 2015, Kartik Visweswaran, Krajee.com
* For more JQuery plugins visit http://plugins.krajee.com
* For more Yii related demos visit http://demos.krajee.com
*/
(function ($) {
"use strict";
String.prototype.repl = function (from, to) {
return this.split(from).join(to);
};
var isIE = function (ver) {
var div = document.createElement("div"), status;
div.innerHTML = "<!--[if IE " + ver + "]><i></i><![endif]-->";
status = (div.getElementsByTagName("i").length === 1);
document.body.appendChild(div);
div.parentNode.removeChild(div);
return status;
},
previewCache = {
data: {},
init: function (obj) {
var content = obj.initialPreview, id = obj.id;
if (content.length > 0 && !isArray(content)) {
content = content.split(obj.initialPreviewDelimiter);
}
previewCache.data[id] = {
content: content,
config: obj.initialPreviewConfig,
tags: obj.initialPreviewThumbTags,
delimiter: obj.initialPreviewDelimiter,
template: obj.previewGenericTemplate,
msg: obj.msgSelected,
initId: obj.previewInitId,
footer: obj.getLayoutTemplate('footer'),
isDelete: obj.initialPreviewShowDelete,
caption: obj.initialCaption,
actions: function (showUpload, showDelete, disabled, url, key, index) {
return obj.renderFileActions(showUpload, showDelete, disabled, url, key, index);
}
};
},
fetch: function (id) {
return previewCache.data[id].content.filter(function (n) {
return n !== undefined;
});
},
count: function (id) {
return !!previewCache.data[id] && !!previewCache.data[id].content ? previewCache.fetch(id).length : 0;
},
get: function (id, i, isDisabled) {
var ind = 'init_' + i, data = previewCache.data[id],
previewId = data.initId + '-' + ind, out;
isDisabled = isDisabled === undefined ? true : isDisabled;
if (data.content[i] === undefined) {
return '';
}
out = data.template
.repl('{previewId}', previewId)
.repl('{frameClass}', ' file-preview-initial')
.repl('{fileindex}', ind)
.repl('{content}', data.content[i])
.repl('{footer}', previewCache.footer(id, i, isDisabled));
if (data.tags.length && data.tags[i]) {
out = replaceTags(out, data.tags[i]);
}
return out;
},
add: function (id, content, config, append) {
var data = $.extend(true, {}, previewCache.data[id]), index;
if (!isArray(content)) {
content = content.split(data.delimiter);
}
if (append) {
index = data.content.push(content) - 1;
data.config[index] = config;
} else {
index = content.length;
data.content = content;
data.config = config;
}
previewCache.data[id] = data;
return index;
},
set: function (id, content, config, tags, append) {
var data = $.extend(true, {}, previewCache.data[id]), i;
if (!isArray(content)) {
content = content.split(data.delimiter);
}
if (append) {
for (i = 0; i < content.length; i++) {
data.content.push(content[i]);
}
for (i = 0; i < config.length; i++) {
data.config.push(config[i]);
}
for (i = 0; i < tags.length; i++) {
data.tags.push(tags[i]);
}
} else {
data.content = content;
data.config = config;
data.tags = tags;
}
previewCache.data[id] = data;
},
unset: function (id, index) {
var chk = previewCache.count(id);
if (!chk) {
return;
}
if (chk === 1) {
previewCache.data[id].content = [];
previewCache.data[id].config = [];
return;
}
previewCache.data[id].content[index] = undefined;
previewCache.data[id].config[index] = undefined;
},
out: function (id) {
var html = '', data = previewCache.data[id], caption, len = previewCache.count(id);
if (len === 0) {
return {content: '', caption: ''};
}
for (var i = 0; i < len; i++) {
html += previewCache.get(id, i);
}
caption = data.msg.repl('{n}', len);
return {content: html, caption: caption};
},
footer: function (id, i, isDisabled) {
var data = previewCache.data[id];
isDisabled = isDisabled === undefined ? true : isDisabled;
if (data.config.length === 0 || isEmpty(data.config[i])) {
return '';
}
var config = data.config[i],
caption = isSet('caption', config) ? config.caption : '',
width = isSet('width', config) ? config.width : 'auto',
url = isSet('url', config) ? config.url : false,
key = isSet('key', config) ? config.key : null,
disabled = (url === false) && isDisabled,
actions = data.isDelete ? data.actions(false, true, disabled, url, key, i) : '',
footer = data.footer.repl('{actions}', actions);
return footer
.repl('{caption}', caption)
.repl('{width}', width)
.repl('{indicator}', '')
.repl('{indicatorTitle}', '');
}
},
PREVIEW_FRAMES = '.file-preview-frame:not(.file-preview-initial)',
getNum = function (num, def) {
def = def || 0;
if (typeof num === "number") {
return num;
}
if (typeof num === "string") {
num = parseFloat(num);
}
return isNaN(num) ? def : num;
},
hasFileAPISupport = function () {
return window.File && window.FileReader;
},
hasDragDropSupport = function () {
var $div = document.createElement('div');
return !isIE(9) && ($div.draggable !== undefined || ($div.ondragstart !== undefined && $