Skip to content

Commit fb2c863

Browse files
committed
remove jquery dependency in the core plugin
1 parent f94e99c commit fb2c863

File tree

1 file changed

+62
-25
lines changed

1 file changed

+62
-25
lines changed

ajaxUpload.js

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
'<a class="cancel-upload" title="cancel"></a>'+
1111
'</li>';
1212

13+
function extend(a, b) {
14+
for(var key in b) {
15+
if(b.hasOwnProperty(key)) {
16+
a[key] = b[key];
17+
}
18+
}
19+
return a;
20+
}
21+
22+
1323

1424
var defaults = {
1525
list : '',
@@ -32,22 +42,39 @@
3242
minHeight:0,
3343
minSize:0,
3444
maxSize:0
35-
},
36-
o = $.extend({},defaults, options);
45+
};
46+
47+
var user_op = extend({}, options);
48+
// overiding defaults option
49+
var o = extend(defaults, user_op);
50+
51+
3752
return this.each(function(){
3853

39-
var input = $(this);
54+
var input = this;
4055

4156

4257
function uploadFile(file){
43-
var item = $(o.item);
44-
$(o.list).append(item);
4558

46-
var bar = item.find(o.progressBar);
47-
var progressor = item.find(o.progressor).text('0%');
59+
var list = document.querySelector(o.list);
60+
61+
var i = create_dom_from_string(o.item);
62+
63+
list.appendChild(i);
64+
65+
// var item = $(i);
66+
67+
var bar = i.querySelector(o.progressBar);
68+
69+
// i.querySelector(o.progressor).innerHTML = '0%';
70+
var progressor = i.querySelector(o.progressor);
71+
progressor.innerHTML = '0%';
72+
// var bar = item.find(o.progressBar);
73+
// var progressor = item.find(o.progressor).text('0%');
4874
var size = parseInt(file.size/1024,10);
4975
var name = file.name;
50-
var object = item;
76+
// var object = $(i);
77+
var object = i;
5178

5279

5380

@@ -58,7 +85,10 @@
5885
// this path show the temporary file on browser
5986
// which can be access via image src
6087
var path = URL.createObjectURL(file);
61-
var index = item.index();
88+
// var index = $(i).index();
89+
var index = Array.prototype.slice.call(list.children).indexOf(i);
90+
console.log(index);
91+
// var index = document.querySelectorAll('');
6292
var response;
6393
var xhr = new XMLHttpRequest();
6494

@@ -81,17 +111,19 @@
81111
object.loaded = loaded = parseInt(e.loaded/1024,10);
82112
// total = e.total *1024;
83113

84-
if(bar.length) bar[0].style.width = progessFlow;
85-
if(progressor.length) progressor.text(progress);
114+
// if(bar.length) bar.style.width = progessFlow;
115+
if(document.contains(bar)) bar.style.width = progessFlow;
116+
if(document.contains(progressor)) progressor.innerHTML = progress;
86117

87118
o.onProgress.call(this, object);
88119
};
89120
// xhr.addEventListener("load", function () {}, false);
90121
xhr.onload = function(){
91122
//update progress bar to 100% in firefox
92123
//update progress number to display in the view
93-
if(bar.length) progress = bar[0].style.width = 100 + "%";
94-
if(progressor.length) progressor.text(progress);
124+
// if(bar.length) progress = bar.style.width = 100 + "%";
125+
if(document.contains(bar)) progress = bar.style.width = 100 + "%";
126+
if(document.contains(progressor)) progressor.innerHTML = progress;
95127
// loaded should be equal total by now
96128
// loaded = total;
97129

@@ -145,15 +177,16 @@
145177

146178
var type= getTrueFileType(files[i]);
147179
// console.log(type);
180+
var item_vol = document.querySelector(o.list).children.length;
148181

149182
if(o.allowedType.length && !o.allowedType.match(type)){
150183
o.onTypeError.call(this, {name: files[i].name} );
151184
}
152185
else if(o.maxSize >0 && parseInt(files[i].size/1024) > o.maxSize){
153186
o.onSizeError.call(this, {name: files[i].name});
154187
}
155-
else if(o.maxLength>0 && $(o.list).children().length >= o.maxLength){
156-
o.onLengthError.call(this, $(o.list).children().length);
188+
else if(o.maxLength>0 && item_vol >= o.maxLength){
189+
o.onLengthError.call(this, item_vol);
157190
}
158191
else if (typeof FileReader !== "undefined"){
159192

@@ -165,8 +198,8 @@
165198
else if(o.minHeight>0 && height<o.minHeight){
166199
o.onDimensionError.call(this, {name :file.name, width: width, height: height});
167200
}
168-
else if($(o.list).children().length >= o.maxLength){//needed double check items length
169-
o.onLengthError.call(this, $(o.list).children().length);
201+
else if(item_vol >= o.maxLength){//needed double check items length
202+
o.onLengthError.call(this, item_vol);
170203
}
171204
else {
172205
uploadFile(file);
@@ -180,14 +213,20 @@
180213
}
181214
}
182215

216+
function create_dom_from_string(string){
217+
var div = document.createElement('div');
218+
div.innerHTML = string;
219+
return div.firstChild;
220+
}
221+
183222
function getImgSize(file, callback){
184223
var reader = new FileReader();
185224
reader.onload = function(evt) {
186225
var image = new Image();
187226
image.onload = function(evt) {
188227
var width = this.width;
189228
var height = this.height;
190-
if (callback) callback(file, width, height);
229+
callback && callback(file, width, height);
191230
};
192231
image.src = evt.target.result;
193232
};
@@ -207,32 +246,30 @@
207246
function sortResponse(response){
208247
var isJSON = true;
209248

210-
try {$.parseJSON(response);} catch (e) {isJSON = false;}
249+
try {JSON.parse(response);} catch (e) {isJSON = false;}
211250

212-
// response = JSON.parse(xhr.responseText);
213-
if(isJSON === true){ sorted = $.parseJSON(response);}
214-
else{sorted = response;}
251+
var sorted = isJSON ? JSON.parse(response) : response;
215252

216253
return sorted;
217254
}
218255

219256

220257
// cancel
221258
// call cancel by trigger cancel event on input
222-
input[0].addEventListener('cancel', function(){
259+
input.addEventListener('cancel', function(){
223260
uploadFile.abort();
224261
o.onCancel.call(this);
225262
}, false);
226263

227-
input[0].addEventListener("change", function () {
264+
input.addEventListener("change", function () {
228265
traverseFiles(this.files);
229266
}, false);
230267

231268

232269
// drag and drop file
233270
// detect dropzone, if there is no dropzone, create a random element for the event to bind with
234271
// that means the events are useless without a delegated dropzone
235-
var DZ = (o.dropZone.length ? $(o.dropZone)[0] : document.createElement('section'));
272+
var DZ = o.dropZone.length ? document.querySelector(o.dropZone) : document.createElement('section');
236273

237274
DZ.addEventListener("dragleave", function (evt) {
238275
var target = evt.target;

0 commit comments

Comments
 (0)