123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- /*!
- * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014
- * @version 1.9.0
- *
- * File input styled for Bootstrap 3.0 that utilizes HTML5 File Input's advanced
- * features including the FileReader API. This plugin is inspired by the blog article at
- * http://www.abeautifulsite.net/blog/2013/08/whipping-file-inputs-into-shape-with-bootstrap-3/
- * and Jasny's File Input plugin http://jasny.github.io/bootstrap/javascript/#fileinput
- *
- * The plugin drastically enhances the file input to preview multiple files on the client before
- * upload. In addition it provides the ability to preview content of images and text files.
- *
- * Author: Kartik Visweswaran
- * Copyright: 2013, Kartik Visweswaran, Krajee.com
- * For more JQuery plugins visit http://plugins.krajee.com
- * For more Yii related demos visit http://demos.krajee.com
- */
- (function ($) {
- var MAIN_TEMPLATE_1 = '{preview}\n' +
- '<div class="input-group {class}">\n' +
- ' {caption}\n' +
- ' <div class="input-group-btn">\n' +
- ' {remove}\n' +
- ' {upload}\n' +
- ' {browse}\n' +
- ' </div>\n' +
- '</div>';
- var MAIN_TEMPLATE_2 = '{preview}\n{remove}\n{upload}\n{browse}\n';
- var PREVIEW_TEMPLATE = '<div class="file-preview {class}">\n' +
- ' <div class="file-preview-status text-center text-success"></div>\n' +
- ' <div class="close fileinput-remove text-right">×</div>\n' +
- ' <div class="file-preview-thumbnails"></div>\n' +
- ' <div class="clearfix"></div>' +
- '</div>';
- var CAPTION_TEMPLATE = '<div class="form-control file-caption {class}">\n' +
- ' <span class="glyphicon glyphicon-file kv-caption-icon"></span><div class="file-caption-name"></div>\n' +
- '</div>';
- var MODAL_TEMPLATE = '<div id="{id}" class="modal fade">\n' +
- ' <div class="modal-dialog modal-lg">\n' +
- ' <div class="modal-content">\n' +
- ' <div class="modal-header">\n' +
- ' <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\n' +
- ' <h3 class="modal-title">Detailed Preview <small>{title}</small></h3>\n' +
- ' </div>\n' +
- ' <div class="modal-body">\n' +
- ' <textarea class="form-control" style="font-family:Monaco,Consolas,monospace; height: {height}px;" readonly>{body}</textarea>\n' +
- ' </div>\n' +
- ' </div>\n' +
- ' </div>\n' +
- '</div>\n';
- var isEmpty = function (value, trim) {
- return value === null || value === undefined || value == []
- || value === '' || trim && $.trim(value) === '';
- };
- var isArray = Array.isArray || function (a) {
- return Object.prototype.toString.call(a) === '[object Array]';
- };
- var getValue = function (options, param, value) {
- return (isEmpty(options) || isEmpty(options[param])) ? value : options[param];
- };
- var getElement = function (options, param, value) {
- return (isEmpty(options) || isEmpty(options[param])) ? value : $(options[param]);
- };
- var isImageFile = function (type, name) {
- return (typeof type !== "undefined") ? type.match('image.*') : name.match(/\.(gif|png|jpe?g)$/i);
- };
- var isTextFile = function (type, name) {
- return (typeof type !== "undefined") ? type.match('text.*') : name.match(/\.(txt|md|csv|htm|html|php|ini)$/i);
- };
- var uniqId = function () {
- return Math.round(new Date().getTime() + (Math.random() * 100));
- };
- var FileInput = function (element, options) {
- this.$element = $(element);
- this.init(options);
- this.listen();
- };
- FileInput.prototype = {
- constructor: FileInput,
- init: function (options) {
- var self = this;
- self.showCaption = options.showCaption;
- self.showPreview = options.showPreview;
- self.initialPreview = options.initialPreview;
- self.initialCaption = options.initialCaption;
- self.overwriteInitial = options.overwriteInitial;
- self.showRemove = options.showRemove;
- self.showUpload = options.showUpload;
- self.captionClass = options.captionClass;
- self.previewClass = options.previewClass;
- self.mainClass = options.mainClass;
- if (isEmpty(options.mainTemplate)) {
- self.mainTemplate = self.showCaption ? MAIN_TEMPLATE_1 : MAIN_TEMPLATE_2;
- }
- else {
- self.mainTemplate = options.mainTemplate;
- }
- self.previewTemplate = (self.showPreview) ? options.previewTemplate : '';
- self.captionTemplate = options.captionTemplate;
- self.browseLabel = options.browseLabel;
- self.browseIcon = options.browseIcon;
- self.browseClass = options.browseClass;
- self.removeLabel = options.removeLabel;
- self.removeIcon = options.removeIcon;
- self.removeClass = options.removeClass;
- self.uploadLabel = options.uploadLabel;
- self.uploadIcon = options.uploadIcon;
- self.uploadClass = options.uploadClass;
- self.uploadUrl = options.uploadUrl;
- self.msgLoading = options.msgLoading;
- self.msgProgress = options.msgProgress;
- self.msgSelected = options.msgSelected;
- self.previewFileType = options.previewFileType;
- self.wrapTextLength = options.wrapTextLength;
- self.wrapIndicator = options.wrapIndicator;
- self.isDisabled = self.$element.attr('disabled') || self.$element.attr('readonly');
- if (isEmpty(self.$element.attr('id'))) {
- self.$element.attr('id', uniqId());
- }
- if (typeof self.$container == 'undefined') {
- self.$container = self.createContainer();
- }
- else {
- self.refreshContainer();
- }
- /* Initialize plugin option parameters */
- self.$captionContainer = getElement(options, 'elCaptionContainer', self.$container.find('.file-caption'));
- self.$caption = getElement(options, 'elCaptionText', self.$container.find('.file-caption-name'));
- self.$previewContainer = getElement(options, 'elPreviewContainer', self.$container.find('.file-preview'));
- self.$preview = getElement(options, 'elPreviewImage', self.$container.find('.file-preview-thumbnails'));
- self.$previewStatus = getElement(options, 'elPreviewStatus', self.$container.find('.file-preview-status'));
- self.initPreview();
- self.original = {
- preview: self.$preview.html(),
- caption: self.$caption.html()
- };
- this.options = options;
- self.$element.removeClass('file-loading');
- },
- listen: function () {
- var self = this;
- self.$element.on('change', $.proxy(self.change, self));
- $(self.$element[0].form).on('reset', $.proxy(self.reset, self));
- self.$container.find('.fileinput-remove').on('click', $.proxy(self.clear, self));
- },
- refresh: function (options) {
- var self = this, params = (arguments.length) ? $.extend(self.options, options) : self.options;
- self.init(params);
- },
- initPreview: function () {
- var self = this, html = '',
- content = self.initialPreview,
- len = self.initialPreview.length,
- cap = self.initialCaption.length,
- caption = (cap > 0) ? self.initialCaption : len + ' file selected';
- if (isArray(content) && len > 0) {
- for (var i = 0; i < len; i++) {
- html += '<div class="file-preview-frame">' + content[i] + "</div>\n";
- }
- if (len > 1 && cap == 0) {
- caption = len + ' files selected';
- }
- }
- else if (len > 0) {
- html = '<div class="file-preview-frame">' + content + '</div>';
- }
- else if (cap > 0) {
- self.$caption.html(caption);
- self.$captionContainer.attr('title', caption);
- self.$captionContainer.find('.kv-caption-icon').show();
- return;
- }
- else {
- return;
- }
- self.initialPreviewContent = html;
- self.$preview.html(html);
- self.$caption.html(caption);
- self.$captionContainer.attr('title', caption);
- self.$container.removeClass('file-input-new');
- },
- clear: function (e) {
- var self = this;
- if (e) {
- e.preventDefault();
- }
- self.$element.val('');
- if (e !== false) {
- self.$element.trigger('change');
- self.$element.trigger('fileclear');
- }
- if (!self.overwriteInitial && !isEmpty(self.initialPreviewContent)) {
- self.$preview.html(self.original.preview);
- self.$caption.html(self.original.caption);
- self.$container.removeClass('file-input-new');
- }
- else {
- self.$preview.html('');
- var cap = (!self.overwriteInitial && self.initialCaption.length > 0) ?
- self.original.caption : '';
- self.$caption.html(cap);
- self.$captionContainer.attr('title', '');
- self.$container.removeClass('file-input-new').addClass('file-input-new');
- }
- if (self.overwriteInitial) {
- self.$captionContainer.find('.kv-caption-icon').hide();
- }
- },
- reset: function (e) {
- var self = this;
- self.clear(false);
- self.$preview.html(self.original.preview);
- self.$caption.html(self.original.caption);
- self.$container.find('.fileinput-filename').text('');
- self.$element.trigger('filereset');
- if (self.initialPreview.length > 0) {
- self.$container.removeClass('file-input-new');
- }
- },
- change: function (e) {
- var self = this;
- var elem = self.$element, files = elem.get(0).files, numFiles = files ? files.length : 1,
- label = elem.val().replace(/\\/g, '/').replace(/.*\//, ''), $preview = self.$preview,
- $container = self.$previewContainer, $status = self.$previewStatus, msgLoading = self.msgLoading,
- msgProgress = self.msgProgress, msgSelected = self.msgSelected, tfiles,
- fileType = self.previewFileType, wrapLen = parseInt(self.wrapTextLength),
- wrapInd = self.wrapIndicator;
- if (e.target.files === undefined) {
- tfiles = e.target && e.target.value ? [
- {name: e.target.value.replace(/^.+\\/, '')}
- ] : [];
- }
- else {
- tfiles = e.target.files;
- }
- if (tfiles.length === 0) {
- return;
- }
- $preview.html('');
- if (!self.overwriteInitial) {
- $preview.html(self.initialPreviewContent);
- }
- var total = tfiles.length;
- for (var i = 0; i < total; i++) {
- (function (file) {
- var caption = file.name;
- var isImg = isImageFile(file.type, file.name);
- var isTxt = isTextFile(file.type, file.name);
- if ($preview.length > 0 && (fileType == "any" ? (isImg || isTxt) : (fileType == "text" ? isTxt : isImg)) && typeof FileReader !== "undefined") {
- var reader = new FileReader();
- $status.html(msgLoading);
- $container.addClass('loading');
- reader.onload = function (theFile) {
- var content = '', modal = '';
- if (isTxt) {
- var strText = theFile.target.result;
- if (strText.length > wrapLen) {
- var id = uniqId(), height = window.innerHeight * .75,
- modal = MODAL_TEMPLATE.replace("{id}", id).replace("{title}", caption).replace("{body}", strText).replace("{height}", height);
- wrapInd = wrapInd.replace("{title}", caption).replace("{dialog}", "$('#" + id + "').modal('show')");
- strText = strText.substring(0, (wrapLen - 1)) + wrapInd;
- }
- content = '<div class="file-preview-frame"><div class="file-preview-text" title="' + caption + '">' + strText + '</div></div>' + modal;
- }
- else {
- content = '<div class="file-preview-frame"><img src="' + theFile.target.result + '" class="file-preview-image" title="' + caption + '" alt="' + caption + '"></div>';
- }
- $preview.append("\n" + content);
- if (i >= total - 1) {
- $container.removeClass('loading');
- $status.html('');
- }
- };
- reader.onprogress = function (data) {
- if (data.lengthComputable) {
- var progress = parseInt(((data.loaded / data.total) * 100), 10);
- var msg = msgProgress.replace('{percent}', progress).replace('{file}', file.name);
- $status.html(msg);
- }
- };
- if (isTxt) {
- reader.readAsText(file);
- }
- else {
- reader.readAsDataURL(file);
- }
- }
- else {
- $preview.append("\n" + '<div class="file-preview-frame"><div class="file-preview-other"><h2><i class="glyphicon glyphicon-file"></i></h2>' + caption + '</div></div>');
- }
- })(tfiles[i]);
- }
- var log = numFiles > 1 ? msgSelected.replace('{n}', numFiles) : label;
- self.$caption.html(log);
- self.$captionContainer.attr('title', log);
- self.$container.removeClass('file-input-new');
- elem.trigger('fileselect', [numFiles, label]);
- },
- createContainer: function () {
- var self = this;
- var container = $(document.createElement("div")).attr({"class": 'file-input file-input-new'}).html(self.renderMain());
- self.$element.before(container);
- container.find('.btn-file').append(self.$element);
- return container;
- },
- refreshContainer: function () {
- var self = this;
- self.$container.before(self.$element);
- self.$container.html(self.renderMain());
- self.$container.find('.btn-file').append(self.$element);
- },
- renderMain: function () {
- var self = this;
- var preview = self.previewTemplate.replace('{class}', self.previewClass);
- var css = self.isDisabled ? self.captionClass + ' file-caption-disabled' : self.captionClass;
- var caption = self.captionTemplate.replace('{class}', css);
- return self.mainTemplate.replace('{class}', self.mainClass).
- replace('{preview}', preview).
- replace('{caption}', caption).
- replace('{upload}', self.renderUpload()).
- replace('{remove}', self.renderRemove()).
- replace('{browse}', self.renderBrowse());
- },
- renderBrowse: function () {
- var self = this, css = self.browseClass + ' btn-file', status = '';
- if (self.isDisabled) {
- status = ' disabled ';
- }
- return '<div class="' + css + '"' + status + '> ' + self.browseIcon + self.browseLabel + ' </div>';
- },
- renderRemove: function () {
- var self = this, css = self.removeClass + ' fileinput-remove fileinput-remove-button', status = '';
- if (!self.showRemove) {
- return '';
- }
- if (self.isDisabled) {
- status = ' disabled ';
- }
- return '<button type="button" class="' + css + '"' + status + '>' + self.removeIcon + self.removeLabel + '</button>';
- },
- renderUpload: function () {
- var self = this, content = '', status = '';
- if (!self.showUpload) {
- return '';
- }
- if (self.isDisabled) {
- status = ' disabled ';
- }
- if (isEmpty(self.uploadUrl)) {
- content = '<button type="submit" class="' + self.uploadClass + '"' + status + '>' + self.uploadIcon + self.uploadLabel + '</button>';
- }
- else {
- content = '<a href="' + self.uploadUrl + '" class="' + self.uploadClass + '"' + status + '>' + self.uploadIcon + self.uploadLabel + '</a>';
- }
- return content;
- },
- }
- $.fn.fileinput = function (options) {
- return this.each(function () {
- var $this = $(this), data = $this.data('fileinput')
- if (!data) {
- $this.data('fileinput', (data = new FileInput(this, options)))
- }
- if (typeof options == 'string') {
- data[options]()
- }
- })
- };
- //FileInput plugin definition
- $.fn.fileinput = function (option) {
- var args = Array.apply(null, arguments);
- args.shift();
- return this.each(function () {
- var $this = $(this),
- data = $this.data('fileinput'),
- options = typeof option === 'object' && option;
- if (!data) {
- $this.data('fileinput', (data = new FileInput(this, $.extend({}, $.fn.fileinput.defaults, options, $(this).data()))));
- }
- if (typeof option === 'string') {
- data[option].apply(data, args);
- }
- });
- };
- $.fn.fileinput.defaults = {
- showCaption: true,
- showPreview: true,
- showRemove: true,
- showUpload: true,
- captionClass: '',
- previewClass: '',
- mainClass: '',
- mainTemplate: null,
- initialPreview: '',
- initialCaption: '',
- initialPreviewContent: '',
- overwriteInitial: true,
- previewTemplate: PREVIEW_TEMPLATE,
- captionTemplate: CAPTION_TEMPLATE,
- browseLabel: 'Browse …',
- browseIcon: '<i class="glyphicon glyphicon-folder-open"></i> ',
- browseClass: 'btn btn-primary',
- removeLabel: 'Remove',
- removeIcon: '<i class="glyphicon glyphicon-ban-circle"></i> ',
- removeClass: 'btn btn-default',
- uploadLabel: 'Upload',
- uploadIcon: '<i class="glyphicon glyphicon-upload"></i> ',
- uploadClass: 'btn btn-default',
- uploadUrl: null,
- msgLoading: 'Loading …',
- msgProgress: 'Loaded {percent}% of {file}',
- msgSelected: '{n} files selected',
- previewFileType: 'image',
- wrapTextLength: 250,
- wrapIndicator: ' <span class="wrap-indicator" title="{title}" onclick="{dialog}">[…]</span>',
- elCaptionContainer: null,
- elCaptionText: null,
- elPreviewContainer: null,
- elPreviewImage: null,
- elPreviewStatus: null
- };
-
- /**
- * Convert automatically file inputs with class 'file'
- * into a bootstrap fileinput control.
- */
- $(document).ready(function () {
- var $input = $('input.file[type=file]'), count = Object.keys($input).length;
- if (count > 0) {
- $input.fileinput();
- }
- });
- })(window.jQuery);
|