summaryrefslogtreecommitdiff
path: root/js/components/upload.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/components/upload.js')
-rwxr-xr-xjs/components/upload.js257
1 files changed, 0 insertions, 257 deletions
diff --git a/js/components/upload.js b/js/components/upload.js
deleted file mode 100755
index f80f26e..0000000
--- a/js/components/upload.js
+++ /dev/null
@@ -1,257 +0,0 @@
1/*! UIkit 2.26.4 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
2(function(addon) {
3
4 var component;
5
6 if (window.UIkit) {
7 component = addon(UIkit);
8 }
9
10 if (typeof define == "function" && define.amd) {
11 define("uikit-upload", ["uikit"], function(){
12 return component || addon(UIkit);
13 });
14 }
15
16})(function(UI){
17
18 "use strict";
19
20 UI.component('uploadSelect', {
21
22 init: function() {
23
24 var $this = this;
25
26 this.on("change", function() {
27 xhrupload($this.element[0].files, $this.options);
28 var twin = $this.element.clone(true).data('uploadSelect', $this);
29 $this.element.replaceWith(twin);
30 $this.element = twin;
31 });
32 }
33 });
34
35 UI.component('uploadDrop', {
36
37 defaults: {
38 'dragoverClass': 'uk-dragover'
39 },
40
41 init: function() {
42
43 var $this = this, hasdragCls = false;
44
45 this.on("drop", function(e){
46
47 if (e.originalEvent.dataTransfer && e.originalEvent.dataTransfer.files) {
48
49 e.stopPropagation();
50 e.preventDefault();
51
52 $this.element.removeClass($this.options.dragoverClass);
53 $this.element.trigger('dropped.uk.upload', [e.originalEvent.dataTransfer.files]);
54
55 xhrupload(e.originalEvent.dataTransfer.files, $this.options);
56 }
57
58 }).on("dragenter", function(e){
59 e.stopPropagation();
60 e.preventDefault();
61 }).on("dragover", function(e){
62 e.stopPropagation();
63 e.preventDefault();
64
65 if (!hasdragCls) {
66 $this.element.addClass($this.options.dragoverClass);
67 hasdragCls = true;
68 }
69 }).on("dragleave", function(e){
70 e.stopPropagation();
71 e.preventDefault();
72 $this.element.removeClass($this.options.dragoverClass);
73 hasdragCls = false;
74 });
75 }
76 });
77
78
79 UI.support.ajaxupload = (function() {
80
81 function supportFileAPI() {
82 var fi = document.createElement('INPUT'); fi.type = 'file'; return 'files' in fi;
83 }
84
85 function supportAjaxUploadProgressEvents() {
86 var xhr = new XMLHttpRequest(); return !! (xhr && ('upload' in xhr) && ('onprogress' in xhr.upload));
87 }
88
89 function supportFormData() {
90 return !! window.FormData;
91 }
92
93 return supportFileAPI() && supportAjaxUploadProgressEvents() && supportFormData();
94 })();
95
96
97 function xhrupload(files, settings) {
98
99 if (!UI.support.ajaxupload){
100 return this;
101 }
102
103 settings = UI.$.extend({}, xhrupload.defaults, settings);
104
105 if (!files.length){
106 return;
107 }
108
109 if (settings.allow !== '*.*') {
110
111 for(var i=0,file;file=files[i];i++) {
112
113 if(!matchName(settings.allow, file.name)) {
114
115 if(typeof(settings.notallowed) == 'string') {
116 alert(settings.notallowed);
117 } else {
118 settings.notallowed(file, settings);
119 }
120 return;
121 }
122 }
123 }
124
125 var complete = settings.complete;
126
127 if (settings.single){
128
129 var count = files.length,
130 uploaded = 0,
131 allow = true;
132
133 settings.beforeAll(files);
134
135 settings.complete = function(response, xhr){
136
137 uploaded = uploaded + 1;
138
139 complete(response, xhr);
140
141 if (settings.filelimit && uploaded >= settings.filelimit){
142 allow = false;
143 }
144
145 if (allow && uploaded<count){
146 upload([files[uploaded]], settings);
147 } else {
148 settings.allcomplete(response, xhr);
149 }
150 };
151
152 upload([files[0]], settings);
153
154 } else {
155
156 settings.complete = function(response, xhr){
157 complete(response, xhr);
158 settings.allcomplete(response, xhr);
159 };
160
161 upload(files, settings);
162 }
163
164 function upload(files, settings){
165
166 // upload all at once
167 var formData = new FormData(), xhr = new XMLHttpRequest();
168
169 if (settings.before(settings, files)===false) return;
170
171 for (var i = 0, f; f = files[i]; i++) { formData.append(settings.param, f); }
172 for (var p in settings.params) { formData.append(p, settings.params[p]); }
173
174 // Add any event handlers here...
175 xhr.upload.addEventListener("progress", function(e){
176 var percent = (e.loaded / e.total)*100;
177 settings.progress(percent, e);
178 }, false);
179
180 xhr.addEventListener("loadstart", function(e){ settings.loadstart(e); }, false);
181 xhr.addEventListener("load", function(e){ settings.load(e); }, false);
182 xhr.addEventListener("loadend", function(e){ settings.loadend(e); }, false);
183 xhr.addEventListener("error", function(e){ settings.error(e); }, false);
184 xhr.addEventListener("abort", function(e){ settings.abort(e); }, false);
185
186 xhr.open(settings.method, settings.action, true);
187
188 if (settings.type=="json") {
189 xhr.setRequestHeader("Accept", "application/json");
190 }
191
192 xhr.onreadystatechange = function() {
193
194 settings.readystatechange(xhr);
195
196 if (xhr.readyState==4){
197
198 var response = xhr.responseText;
199
200 if (settings.type=="json") {
201 try {
202 response = UI.$.parseJSON(response);
203 } catch(e) {
204 response = false;
205 }
206 }
207
208 settings.complete(response, xhr);
209 }
210 };
211 settings.beforeSend(xhr);
212 xhr.send(formData);
213 }
214 }
215
216 xhrupload.defaults = {
217 'action': '',
218 'single': true,
219 'method': 'POST',
220 'param' : 'files[]',
221 'params': {},
222 'allow' : '*.*',
223 'type' : 'text',
224 'filelimit': false,
225
226 // events
227 'before' : function(o){},
228 'beforeSend' : function(xhr){},
229 'beforeAll' : function(){},
230 'loadstart' : function(){},
231 'load' : function(){},
232 'loadend' : function(){},
233 'error' : function(){},
234 'abort' : function(){},
235 'progress' : function(){},
236 'complete' : function(){},
237 'allcomplete' : function(){},
238 'readystatechange': function(){},
239 'notallowed' : function(file, settings){ alert('Only the following file types are allowed: '+settings.allow); }
240 };
241
242 function matchName(pattern, path) {
243
244 var parsedPattern = '^' + pattern.replace(/\//g, '\\/').
245 replace(/\*\*/g, '(\\/[^\\/]+)*').
246 replace(/\*/g, '[^\\/]+').
247 replace(/((?!\\))\?/g, '$1.') + '$';
248
249 parsedPattern = '^' + parsedPattern + '$';
250
251 return (path.match(new RegExp(parsedPattern, 'i')) !== null);
252 }
253
254 UI.Utils.xhrupload = xhrupload;
255
256 return xhrupload;
257});