Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ### jQuery Multiple File Upload Plugin v1.3 - 2008-09-30 ###
- * http://www.fyneworks.com/ - [email protected]
- * Dual licensed under the MIT and GPL licenses:
- * http://www.opensource.org/licenses/mit-license.php
- * http://www.gnu.org/licenses/gpl.html
- ###
- Project: http://jquery.com/plugins/project/MultiFile/
- Website: http://www.fyneworks.com/jquery/multiple-file-upload/
- */
- ;
- if (window.jQuery)(function ($) {
- $.extend($, {
- MultiFile: function (o) {
- return $("input:file.multi").MultiFile(o)
- }
- });
- $.extend($.MultiFile, {
- options: {
- accept: '',
- max: -1,
- error: function (s) {
- if ($.blockUI) {
- $.blockUI({
- message: s.replace(/\n/gi, '<br/>'),
- css: {
- border: 'none',
- padding: '15px',
- float: 'right',
- size: '12.0pt',
- backgroundColor: '#900',
- color: '#fff',
- opacity: '.8',
- '-webkit-border-radius': '10px',
- '-moz-border-radius': '10px'
- }
- });
- window.setTimeout($.unblockUI, 2000)
- } else {
- alert(s)
- }
- },
- namePattern: '$name',
- STRING: {
- remove: 'حذف',
- denied: 'You cannot select a $ext file.\nTry again...',
- selected: 'File selected: $file',
- duplicate: 'این فایل در حال حاضر انتخاب شده است\n$file'
- }
- }
- });
- $.extend($.MultiFile, {
- disableEmpty: function (a) {
- var o = [];
- $('input:file').each(function () {
- if ($(this).val() == '') o[o.length] = this
- });
- return $(o).each(function () {
- this.disabled = true
- }).addClass(a || 'mfD')
- },
- reEnableEmpty: function (a) {
- a = a || 'mfD';
- return $('input:file.' + a).removeClass(a).each(function () {
- this.disabled = false
- })
- },
- autoIntercept: ['submit', 'ajaxSubmit', 'validate'],
- intercepted: {},
- intercept: function (b, c, d) {
- var e, value;
- d = d || [];
- if (d.constructor.toString().indexOf("Array") < 0) d = [d];
- if (typeof (b) == 'function') {
- $.MultiFile.disableEmpty();
- value = b.apply(c || window, d);
- $.MultiFile.reEnableEmpty();
- return value
- };
- if (b.constructor.toString().indexOf("Array") < 0) b = [b];
- for (var i = 0; i < b.length; i++) {
- e = b[i] + '';
- if (e)(function (a) {
- $.MultiFile.intercepted[a] = $.fn[a] ||
- function () {};
- $.fn[a] = function () {
- $.MultiFile.disableEmpty();
- value = $.MultiFile.intercepted[a].apply(this, arguments);
- $.MultiFile.reEnableEmpty();
- return value
- }
- })(e)
- }
- }
- });
- $.extend($.fn, {
- reset: function () {
- return this.each(function () {
- try {
- this.reset()
- } catch (e) {}
- })
- },
- MultiFile: function (h) {
- if ($.MultiFile.autoIntercept) {
- $.MultiFile.intercept($.MultiFile.autoIntercept);
- $.MultiFile.autoIntercept = null
- };
- return $(this).each(function (e) {
- if (this._MultiFile) return;
- this._MultiFile = true;
- window.MultiFile = (window.MultiFile || 0) + 1;
- e = window.MultiFile;
- var g = {
- e: this,
- E: $(this),
- clone: $(this).clone()
- };
- if (typeof h == 'number') h = {
- max: h
- };
- if (typeof h == 'string') h = {
- accept: h
- };
- var o = $.extend({}, $.MultiFile.options, h || {}, ($.meta ? g.E.data() : ($.metadata ? g.E.metadata() : null)) || {});
- if (!(o.max > 0)) {
- o.max = g.E.attr('maxlength');
- if (!(o.max > 0)) {
- o.max = (String(g.e.className.match(/\b(max|limit)\-([0-9]+)\b/gi) || ['']).match(/[0-9]+/gi) || [''])[0];
- if (!(o.max > 0)) o.max = -1;
- else o.max = String(o.max).match(/[0-9]+/gi)[0]
- }
- };
- o.max = new Number(o.max);
- o.accept = o.accept || g.E.attr('accept') || '';
- if (!o.accept) {
- o.accept = (g.e.className.match(/\b(accept\-[\w\|]+)\b/gi)) || '';
- o.accept = new String(o.accept).replace(/^(accept|ext)\-/i, '')
- };
- $.extend(g, o || {});
- g.STRING = $.extend({}, $.MultiFile.options.STRING, g.STRING);
- $.extend(g, {
- n: 0,
- slaves: [],
- files: [],
- instanceKey: g.e.id || 'MultiFile' + String(e),
- generateID: function (z) {
- return g.instanceKey + (z > 0 ? '_F' + String(z) : '')
- },
- trigger: function (a, b) {
- var c = g[a],
- value = $(b).attr('value');
- if (c) {
- var d = c(b, value, g);
- if (d != null) return d
- }
- return true
- }
- });
- if (String(g.accept).length > 1) {
- g.rxAccept = new RegExp('\\.(' + (g.accept ? g.accept : '') + ')$', 'gi')
- };
- g.wrapID = g.instanceKey + '_wrap';
- g.E.wrap('<div id="' + g.wrapID + '"></div>');
- g.wrapper = $('#' + g.wrapID + '');
- g.e.name = g.e.name || 'file' + e + '[]';
- g.wrapper.append('<span id="' + g.wrapID + '_labels"></span>');
- g.labels = $('#' + g.wrapID + '_labels');
- g.addSlave = function (c, d) {
- g.n++;
- c.MF = g;
- c.i = d;
- if (c.i > 0) c.id = c.name = null;
- c.id = c.id || g.generateID(c.i);
- c.name = String(g.namePattern.replace(/\$name/gi, g.E.attr('name')).replace(/\$id/gi, g.E.attr('id')).replace(/\$g/gi, (e > 0 ? e : '')).replace(/\$i/gi, (d > 0 ? d : '')));
- $(c).val('').attr('value', '')[0].value = '';
- if ((g.max > 0) && ((g.n - 1) > (g.max))) c.disabled = true;
- g.current = g.slaves[c.i] = c;
- c = $(c);
- $(c).change(function () {
- $(this).blur();
- if (!g.trigger('onFileSelect', this, g)) return false;
- var a = '',
- v = String(this.value || '');
- if (g.accept && v && !v.match(g.rxAccept)) a = g.STRING.denied.replace('$ext', String(v.match(/\.\w{1,4}$/gi)));
- for (var f in g.slaves) if (g.slaves[f] && g.slaves[f] != this) if (g.slaves[f].value == v) a = g.STRING.duplicate.replace('$file', v.match(/[^\/\\]+$/gi));
- var b = $(g.clone).clone();
- b.addClass('MultiFile');
- if (a != '') {
- g.error(a);
- g.n--;
- g.addSlave(b[0], this.i);
- c.parent().prepend(b);
- c.remove();
- return false
- };
- $(this).css({
- position: 'absolute',
- top: '-3000px'
- });
- g.labels.before(b);
- g.addToList(this);
- g.addSlave(b[0], this.i + 1);
- if (!g.trigger('afterFileSelect', this, g)) return false
- })
- };
- g.addToList = function (c) {
- if (!g.trigger('onFileAppend', c, g)) return false;
- var r = $('<div></div>'),
- v = String(c.value || ''),
- a = $('<span class="file" title="' + g.STRING.selected.replace('$file', v) + '">' + v.match(/[^\/\\]+$/gi)[0] + '</span>'),
- b = $('<a href="#' + g.wrapID + '">' + g.STRING.remove + '</a>');
- g.labels.append(r.append(' [', b, '] ', a));
- b.click(function () {
- if (!g.trigger('onFileRemove', c, g)) return false;
- g.n--;
- g.current.disabled = false;
- g.slaves[c.i] = null;
- $(c).remove();
- $(this).parent().remove();
- $(g.current).css({
- position: '',
- top: ''
- });
- $(g.current).reset().val('').attr('value', '')[0].value = '';
- if (!g.trigger('afterFileRemove', c, g)) return false;
- return false
- });
- if (!g.trigger('afterFileAppend', c, g)) return false
- };
- if (!g.MF) g.addSlave(g.e, 0);
- g.n++
- })
- }
- });
- $(function () {
- $.MultiFile()
- })
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment