Guest User

Untitled

a guest
Sep 19th, 2011
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  ### jQuery Multiple File Upload Plugin v1.3 - 2008-09-30 ###
  3.  * http://www.fyneworks.com/ - [email protected]
  4.  * Dual licensed under the MIT and GPL licenses:
  5.  *   http://www.opensource.org/licenses/mit-license.php
  6.  *   http://www.gnu.org/licenses/gpl.html
  7.  ###
  8.  Project: http://jquery.com/plugins/project/MultiFile/
  9.  Website: http://www.fyneworks.com/jquery/multiple-file-upload/
  10. */
  11. ;
  12. if (window.jQuery)(function ($) {
  13.     $.extend($, {
  14.         MultiFile: function (o) {
  15.             return $("input:file.multi").MultiFile(o)
  16.         }
  17.     });
  18.     $.extend($.MultiFile, {
  19.         options: {
  20.             accept: '',
  21.             max: -1,
  22.             error: function (s) {
  23.                 if ($.blockUI) {
  24.                     $.blockUI({
  25.                         message: s.replace(/\n/gi, '<br/>'),
  26.                         css: {
  27.                             border: 'none',
  28.                             padding: '15px',
  29.                             float: 'right',
  30.                             size: '12.0pt',
  31.                             backgroundColor: '#900',
  32.                             color: '#fff',
  33.                             opacity: '.8',
  34.                             '-webkit-border-radius': '10px',
  35.                             '-moz-border-radius': '10px'
  36.                         }
  37.                     });
  38.                     window.setTimeout($.unblockUI, 2000)
  39.                 } else {
  40.                     alert(s)
  41.                 }
  42.             },
  43.             namePattern: '$name',
  44.             STRING: {
  45.                 remove: 'حذف',
  46.                 denied: 'You cannot select a $ext file.\nTry again...',
  47.                 selected: 'File selected: $file',
  48.                 duplicate: 'این فایل در حال حاضر انتخاب شده است\n$file'
  49.             }
  50.         }
  51.     });
  52.     $.extend($.MultiFile, {
  53.         disableEmpty: function (a) {
  54.             var o = [];
  55.             $('input:file').each(function () {
  56.                 if ($(this).val() == '') o[o.length] = this
  57.             });
  58.             return $(o).each(function () {
  59.                 this.disabled = true
  60.             }).addClass(a || 'mfD')
  61.         },
  62.         reEnableEmpty: function (a) {
  63.             a = a || 'mfD';
  64.             return $('input:file.' + a).removeClass(a).each(function () {
  65.                 this.disabled = false
  66.             })
  67.         },
  68.         autoIntercept: ['submit', 'ajaxSubmit', 'validate'],
  69.         intercepted: {},
  70.         intercept: function (b, c, d) {
  71.             var e, value;
  72.             d = d || [];
  73.             if (d.constructor.toString().indexOf("Array") < 0) d = [d];
  74.             if (typeof (b) == 'function') {
  75.                 $.MultiFile.disableEmpty();
  76.                 value = b.apply(c || window, d);
  77.                 $.MultiFile.reEnableEmpty();
  78.                 return value
  79.             };
  80.             if (b.constructor.toString().indexOf("Array") < 0) b = [b];
  81.             for (var i = 0; i < b.length; i++) {
  82.                 e = b[i] + '';
  83.                 if (e)(function (a) {
  84.                     $.MultiFile.intercepted[a] = $.fn[a] ||
  85.                     function () {};
  86.                     $.fn[a] = function () {
  87.                         $.MultiFile.disableEmpty();
  88.                         value = $.MultiFile.intercepted[a].apply(this, arguments);
  89.                         $.MultiFile.reEnableEmpty();
  90.                         return value
  91.                     }
  92.                 })(e)
  93.             }
  94.         }
  95.     });
  96.     $.extend($.fn, {
  97.         reset: function () {
  98.             return this.each(function () {
  99.                 try {
  100.                     this.reset()
  101.                 } catch (e) {}
  102.             })
  103.         },
  104.         MultiFile: function (h) {
  105.             if ($.MultiFile.autoIntercept) {
  106.                 $.MultiFile.intercept($.MultiFile.autoIntercept);
  107.                 $.MultiFile.autoIntercept = null
  108.             };
  109.             return $(this).each(function (e) {
  110.                 if (this._MultiFile) return;
  111.                 this._MultiFile = true;
  112.                 window.MultiFile = (window.MultiFile || 0) + 1;
  113.                 e = window.MultiFile;
  114.                 var g = {
  115.                     e: this,
  116.                     E: $(this),
  117.                     clone: $(this).clone()
  118.                 };
  119.                 if (typeof h == 'number') h = {
  120.                     max: h
  121.                 };
  122.                 if (typeof h == 'string') h = {
  123.                     accept: h
  124.                 };
  125.                 var o = $.extend({}, $.MultiFile.options, h || {}, ($.meta ? g.E.data() : ($.metadata ? g.E.metadata() : null)) || {});
  126.                 if (!(o.max > 0)) {
  127.                     o.max = g.E.attr('maxlength');
  128.                     if (!(o.max > 0)) {
  129.                         o.max = (String(g.e.className.match(/\b(max|limit)\-([0-9]+)\b/gi) || ['']).match(/[0-9]+/gi) || [''])[0];
  130.                         if (!(o.max > 0)) o.max = -1;
  131.                         else o.max = String(o.max).match(/[0-9]+/gi)[0]
  132.                     }
  133.                 };
  134.                 o.max = new Number(o.max);
  135.                 o.accept = o.accept || g.E.attr('accept') || '';
  136.                 if (!o.accept) {
  137.                     o.accept = (g.e.className.match(/\b(accept\-[\w\|]+)\b/gi)) || '';
  138.                     o.accept = new String(o.accept).replace(/^(accept|ext)\-/i, '')
  139.                 };
  140.                 $.extend(g, o || {});
  141.                 g.STRING = $.extend({}, $.MultiFile.options.STRING, g.STRING);
  142.                 $.extend(g, {
  143.                     n: 0,
  144.                     slaves: [],
  145.                     files: [],
  146.                     instanceKey: g.e.id || 'MultiFile' + String(e),
  147.                     generateID: function (z) {
  148.                         return g.instanceKey + (z > 0 ? '_F' + String(z) : '')
  149.                     },
  150.                     trigger: function (a, b) {
  151.                         var c = g[a],
  152.                             value = $(b).attr('value');
  153.                         if (c) {
  154.                             var d = c(b, value, g);
  155.                             if (d != null) return d
  156.                         }
  157.                         return true
  158.                     }
  159.                 });
  160.                 if (String(g.accept).length > 1) {
  161.                     g.rxAccept = new RegExp('\\.(' + (g.accept ? g.accept : '') + ')$', 'gi')
  162.                 };
  163.                 g.wrapID = g.instanceKey + '_wrap';
  164.                 g.E.wrap('<div id="' + g.wrapID + '"></div>');
  165.                 g.wrapper = $('#' + g.wrapID + '');
  166.                 g.e.name = g.e.name || 'file' + e + '[]';
  167.                 g.wrapper.append('<span id="' + g.wrapID + '_labels"></span>');
  168.                 g.labels = $('#' + g.wrapID + '_labels');
  169.                 g.addSlave = function (c, d) {
  170.                     g.n++;
  171.                     c.MF = g;
  172.                     c.i = d;
  173.                     if (c.i > 0) c.id = c.name = null;
  174.                     c.id = c.id || g.generateID(c.i);
  175.                     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 : '')));
  176.                     $(c).val('').attr('value', '')[0].value = '';
  177.                     if ((g.max > 0) && ((g.n - 1) > (g.max))) c.disabled = true;
  178.                     g.current = g.slaves[c.i] = c;
  179.                     c = $(c);
  180.                     $(c).change(function () {
  181.                         $(this).blur();
  182.                         if (!g.trigger('onFileSelect', this, g)) return false;
  183.                         var a = '',
  184.                             v = String(this.value || '');
  185.                         if (g.accept && v && !v.match(g.rxAccept)) a = g.STRING.denied.replace('$ext', String(v.match(/\.\w{1,4}$/gi)));
  186.                         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));
  187.                         var b = $(g.clone).clone();
  188.                         b.addClass('MultiFile');
  189.                         if (a != '') {
  190.                             g.error(a);
  191.                             g.n--;
  192.                             g.addSlave(b[0], this.i);
  193.                             c.parent().prepend(b);
  194.                             c.remove();
  195.                             return false
  196.                         };
  197.                         $(this).css({
  198.                             position: 'absolute',
  199.                             top: '-3000px'
  200.                         });
  201.                         g.labels.before(b);
  202.                         g.addToList(this);
  203.                         g.addSlave(b[0], this.i + 1);
  204.                         if (!g.trigger('afterFileSelect', this, g)) return false
  205.                     })
  206.                 };
  207.                 g.addToList = function (c) {
  208.                     if (!g.trigger('onFileAppend', c, g)) return false;
  209.                     var r = $('<div></div>'),
  210.                         v = String(c.value || ''),
  211.                         a = $('<span class="file" title="' + g.STRING.selected.replace('$file', v) + '">' + v.match(/[^\/\\]+$/gi)[0] + '</span>'),
  212.                         b = $('<a href="#' + g.wrapID + '">' + g.STRING.remove + '</a>');
  213.                     g.labels.append(r.append('&nbsp;&nbsp;&nbsp;&nbsp;[', b, ']&nbsp;', a));
  214.                     b.click(function () {
  215.                         if (!g.trigger('onFileRemove', c, g)) return false;
  216.                         g.n--;
  217.                         g.current.disabled = false;
  218.                         g.slaves[c.i] = null;
  219.                         $(c).remove();
  220.                         $(this).parent().remove();
  221.                         $(g.current).css({
  222.                             position: '',
  223.                             top: ''
  224.                         });
  225.                         $(g.current).reset().val('').attr('value', '')[0].value = '';
  226.                         if (!g.trigger('afterFileRemove', c, g)) return false;
  227.                         return false
  228.                     });
  229.                     if (!g.trigger('afterFileAppend', c, g)) return false
  230.                 };
  231.                 if (!g.MF) g.addSlave(g.e, 0);
  232.                 g.n++
  233.             })
  234.         }
  235.     });
  236.     $(function () {
  237.         $.MultiFile()
  238.     })
  239. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment