DotaNoobs main site.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
7.2 KiB

  1. /*! UIkit 2.5.0 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
  2. (function(addon) {
  3. if (typeof define == "function" && define.amd) { // AMD
  4. define(["uikit"], function(){
  5. return jQuery.UIkit || addon(window.jQuery, window.jQuery.UIkit);
  6. });
  7. }
  8. if (window && window.jQuery && window.jQuery.UIkit) {
  9. addon(window.jQuery, window.jQuery.UIkit);
  10. }
  11. })(function($, UI){
  12. var UploadSelect = function(element, options) {
  13. var $this = this,
  14. $element = $(element),
  15. options = $.extend({}, xhrupload.defaults, UploadSelect.defaults, options);
  16. if ($element.data("uploadSelect")) return;
  17. this.element = $element.on("change", function() {
  18. xhrupload($this.element[0].files, options);
  19. });
  20. $element.data("uploadSelect", this);
  21. };
  22. UploadSelect.defaults = {};
  23. var UploadDrop = function(element, options) {
  24. var $this = this,
  25. $element = $(element),
  26. options = $.extend({}, xhrupload.defaults, UploadDrop.defaults, options),
  27. hasdragCls = false;
  28. if ($element.data("uploadDrop")) return;
  29. $element.on("drop", function(e){
  30. if (e.dataTransfer && e.dataTransfer.files) {
  31. e.stopPropagation();
  32. e.preventDefault();
  33. $element.removeClass(options.dragoverClass);
  34. xhrupload(e.dataTransfer.files, options);
  35. }
  36. }).on("dragenter", function(e){
  37. e.stopPropagation();
  38. e.preventDefault();
  39. }).on("dragover", function(e){
  40. e.stopPropagation();
  41. e.preventDefault();
  42. if (!hasdragCls) {
  43. $element.addClass(options.dragoverClass);
  44. hasdragCls = true;
  45. }
  46. }).on("dragleave", function(e){
  47. e.stopPropagation();
  48. e.preventDefault();
  49. $element.removeClass(options.dragoverClass);
  50. hasdragCls = false;
  51. });
  52. $element.data("uploadDrop", this);
  53. };
  54. UploadDrop.defaults = {
  55. 'dragoverClass': 'uk-dragover'
  56. };
  57. UI["upload"] = { "select" : UploadSelect, "drop" : UploadDrop };
  58. UI.support.ajaxupload = (function() {
  59. function supportFileAPI() {
  60. var fi = document.createElement('INPUT'); fi.type = 'file'; return 'files' in fi;
  61. }
  62. function supportAjaxUploadProgressEvents() {
  63. var xhr = new XMLHttpRequest(); return !! (xhr && ('upload' in xhr) && ('onprogress' in xhr.upload));
  64. }
  65. function supportFormData() {
  66. return !! window.FormData;
  67. }
  68. return supportFileAPI() && supportAjaxUploadProgressEvents() && supportFormData();
  69. })();
  70. if (UI.support.ajaxupload){
  71. $.event.props.push("dataTransfer");
  72. }
  73. function xhrupload(files, settings) {
  74. if (!UI.support.ajaxupload){
  75. return this;
  76. }
  77. settings = $.extend({}, xhrupload.defaults, settings);
  78. if (!files.length){
  79. return;
  80. }
  81. if (settings.allow !== '*.*') {
  82. for(var i=0,file;file=files[i];i++) {
  83. if(!matchName(settings.allow, file.name)) {
  84. if(typeof(settings.notallowed) == 'string') {
  85. alert(settings.notallowed);
  86. } else {
  87. settings.notallowed(file, settings);
  88. }
  89. return;
  90. }
  91. }
  92. }
  93. var complete = settings.complete;
  94. if (settings.single){
  95. var count = files.length,
  96. uploaded = 0;
  97. settings.complete = function(response, xhr){
  98. uploaded = uploaded+1;
  99. complete(response, xhr);
  100. if (uploaded<count){
  101. upload([files[uploaded]], settings);
  102. } else {
  103. settings.allcomplete(response, xhr);
  104. }
  105. };
  106. upload([files[0]], settings);
  107. } else {
  108. settings.complete = function(response, xhr){
  109. complete(response, xhr);
  110. settings.allcomplete(response, xhr);
  111. };
  112. upload(files, settings);
  113. }
  114. function upload(files, settings){
  115. // upload all at once
  116. var formData = new FormData(), xhr = new XMLHttpRequest();
  117. if (settings.before(settings, files)===false) return;
  118. for (var i = 0, f; f = files[i]; i++) { formData.append(settings.param, f); }
  119. for (var p in settings.params) { formData.append(p, settings.params[p]); }
  120. // Add any event handlers here...
  121. xhr.upload.addEventListener("progress", function(e){
  122. var percent = (e.loaded / e.total)*100;
  123. settings.progress(percent, e);
  124. }, false);
  125. xhr.addEventListener("loadstart", function(e){ settings.loadstart(e); }, false);
  126. xhr.addEventListener("load", function(e){ settings.load(e); }, false);
  127. xhr.addEventListener("loadend", function(e){ settings.loadend(e); }, false);
  128. xhr.addEventListener("error", function(e){ settings.error(e); }, false);
  129. xhr.addEventListener("abort", function(e){ settings.abort(e); }, false);
  130. xhr.open(settings.method, settings.action, true);
  131. xhr.onreadystatechange = function() {
  132. settings.readystatechange(xhr);
  133. if (xhr.readyState==4){
  134. var response = xhr.responseText;
  135. if (settings.type=="json") {
  136. try {
  137. response = $.parseJSON(response);
  138. } catch(e) {
  139. response = false;
  140. }
  141. }
  142. settings.complete(response, xhr);
  143. }
  144. };
  145. xhr.send(formData);
  146. }
  147. }
  148. xhrupload.defaults = {
  149. 'action': '',
  150. 'single': true,
  151. 'method': 'POST',
  152. 'param' : 'files[]',
  153. 'params': {},
  154. 'allow' : '*.*',
  155. 'type' : 'text',
  156. // events
  157. 'before' : function(o){},
  158. 'loadstart' : function(){},
  159. 'load' : function(){},
  160. 'loadend' : function(){},
  161. 'error' : function(){},
  162. 'abort' : function(){},
  163. 'progress' : function(){},
  164. 'complete' : function(){},
  165. 'allcomplete' : function(){},
  166. 'readystatechange': function(){},
  167. 'notallowed' : function(file, settings){ alert('Only the following file types are allowed: '+settings.allow); }
  168. };
  169. function matchName(pattern, path) {
  170. var parsedPattern = '^' + pattern.replace(/\//g, '\\/').
  171. replace(/\*\*/g, '(\\/[^\\/]+)*').
  172. replace(/\*/g, '[^\\/]+').
  173. replace(/((?!\\))\?/g, '$1.') + '$';
  174. parsedPattern = '^' + parsedPattern + '$';
  175. return (path.match(new RegExp(parsedPattern)) !== null);
  176. }
  177. UI.Utils.xhrupload = xhrupload;
  178. return xhrupload;
  179. });