-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.fineuploader-3.7.1.js
5807 lines (4933 loc) · 185 KB
/
jquery.fineuploader-3.7.1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Fine Uploader
*
* Copyright 2013, Widen Enterprises, Inc. [email protected]
*
* Version: 3.7.1
*
* Homepage: http://fineuploader.com
*
* Repository: git://github.com/Widen/fine-uploader.git
*
* Licensed under GNU GPL v3, see LICENSE
*/
/*globals window, navigator, document, FormData, File, HTMLInputElement, XMLHttpRequest, Blob*/
var qq = function(element) {
"use strict";
return {
hide: function() {
element.style.display = 'none';
return this;
},
/** Returns the function which detaches attached event */
attach: function(type, fn) {
if (element.addEventListener){
element.addEventListener(type, fn, false);
} else if (element.attachEvent){
element.attachEvent('on' + type, fn);
}
return function() {
qq(element).detach(type, fn);
};
},
detach: function(type, fn) {
if (element.removeEventListener){
element.removeEventListener(type, fn, false);
} else if (element.attachEvent){
element.detachEvent('on' + type, fn);
}
return this;
},
contains: function(descendant) {
// The [W3C spec](http://www.w3.org/TR/domcore/#dom-node-contains)
// says a `null` (or ostensibly `undefined`) parameter
// passed into `Node.contains` should result in a false return value.
// IE7 throws an exception if the parameter is `undefined` though.
if (!descendant) {
return false;
}
// compareposition returns false in this case
if (element === descendant) {
return true;
}
if (element.contains){
return element.contains(descendant);
} else {
/*jslint bitwise: true*/
return !!(descendant.compareDocumentPosition(element) & 8);
}
},
/**
* Insert this element before elementB.
*/
insertBefore: function(elementB) {
elementB.parentNode.insertBefore(element, elementB);
return this;
},
remove: function() {
element.parentNode.removeChild(element);
return this;
},
/**
* Sets styles for an element.
* Fixes opacity in IE6-8.
*/
css: function(styles) {
if (styles.opacity != null){
if (typeof element.style.opacity !== 'string' && typeof(element.filters) !== 'undefined'){
styles.filter = 'alpha(opacity=' + Math.round(100 * styles.opacity) + ')';
}
}
qq.extend(element.style, styles);
return this;
},
hasClass: function(name) {
var re = new RegExp('(^| )' + name + '( |$)');
return re.test(element.className);
},
addClass: function(name) {
if (!qq(element).hasClass(name)){
element.className += ' ' + name;
}
return this;
},
removeClass: function(name) {
var re = new RegExp('(^| )' + name + '( |$)');
element.className = element.className.replace(re, ' ').replace(/^\s+|\s+$/g, "");
return this;
},
getByClass: function(className) {
var candidates,
result = [];
if (element.querySelectorAll){
return element.querySelectorAll('.' + className);
}
candidates = element.getElementsByTagName("*");
qq.each(candidates, function(idx, val) {
if (qq(val).hasClass(className)){
result.push(val);
}
});
return result;
},
children: function() {
var children = [],
child = element.firstChild;
while (child){
if (child.nodeType === 1){
children.push(child);
}
child = child.nextSibling;
}
return children;
},
setText: function(text) {
element.innerText = text;
element.textContent = text;
return this;
},
clearText: function() {
return qq(element).setText("");
}
};
};
qq.log = function(message, level) {
"use strict";
if (window.console) {
if (!level || level === 'info') {
window.console.log(message);
}
else
{
if (window.console[level]) {
window.console[level](message);
}
else {
window.console.log('<' + level + '> ' + message);
}
}
}
};
qq.isObject = function(variable) {
"use strict";
return variable && !variable.nodeType && Object.prototype.toString.call(variable) === '[object Object]';
};
qq.isFunction = function(variable) {
"use strict";
return typeof(variable) === "function";
};
qq.isArray = function(variable) {
"use strict";
return Object.prototype.toString.call(variable) === "[object Array]";
}
qq.isString = function(maybeString) {
"use strict";
return Object.prototype.toString.call(maybeString) === '[object String]';
};
qq.trimStr = function(string) {
if (String.prototype.trim) {
return string.trim();
}
return string.replace(/^\s+|\s+$/g,'');
};
// Returns a string, swapping argument values with the associated occurrence of {} in the passed string.
qq.format = function(str) {
"use strict";
var args = Array.prototype.slice.call(arguments, 1),
newStr = str;
qq.each(args, function(idx, val) {
newStr = newStr.replace(/{}/, val);
});
return newStr;
};
qq.isFile = function(maybeFile) {
"use strict";
return window.File && Object.prototype.toString.call(maybeFile) === '[object File]'
};
qq.isFileList = function(maybeFileList) {
return window.FileList && Object.prototype.toString.call(maybeFileList) === '[object FileList]'
}
qq.isFileOrInput = function(maybeFileOrInput) {
"use strict";
return qq.isFile(maybeFileOrInput) || qq.isInput(maybeFileOrInput);
};
qq.isInput = function(maybeInput) {
if (window.HTMLInputElement) {
if (Object.prototype.toString.call(maybeInput) === '[object HTMLInputElement]') {
if (maybeInput.type && maybeInput.type.toLowerCase() === 'file') {
return true;
}
}
}
if (maybeInput.tagName) {
if (maybeInput.tagName.toLowerCase() === 'input') {
if (maybeInput.type && maybeInput.type.toLowerCase() === 'file') {
return true;
}
}
}
return false;
};
qq.isBlob = function(maybeBlob) {
"use strict";
return window.Blob && Object.prototype.toString.call(maybeBlob) === '[object Blob]';
};
qq.isXhrUploadSupported = function() {
"use strict";
var input = document.createElement('input');
input.type = 'file';
return (
input.multiple !== undefined &&
typeof File !== "undefined" &&
typeof FormData !== "undefined" &&
typeof (new XMLHttpRequest()).upload !== "undefined" );
};
qq.isFolderDropSupported = function(dataTransfer) {
"use strict";
return (dataTransfer.items && dataTransfer.items[0].webkitGetAsEntry);
};
qq.isFileChunkingSupported = function() {
"use strict";
return !qq.android() && //android's impl of Blob.slice is broken
qq.isXhrUploadSupported() &&
(File.prototype.slice !== undefined || File.prototype.webkitSlice !== undefined || File.prototype.mozSlice !== undefined);
};
qq.extend = function (first, second, extendNested) {
"use strict";
qq.each(second, function(prop, val) {
if (extendNested && qq.isObject(val)) {
if (first[prop] === undefined) {
first[prop] = {};
}
qq.extend(first[prop], val, true);
}
else {
first[prop] = val;
}
});
return first;
};
/**
* Searches for a given element in the array, returns -1 if it is not present.
* @param {Number} [from] The index at which to begin the search
*/
qq.indexOf = function(arr, elt, from){
"use strict";
if (arr.indexOf) {
return arr.indexOf(elt, from);
}
from = from || 0;
var len = arr.length;
if (from < 0) {
from += len;
}
for (; from < len; from+=1){
if (arr.hasOwnProperty(from) && arr[from] === elt){
return from;
}
}
return -1;
};
//this is a version 4 UUID
qq.getUniqueId = function(){
"use strict";
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
/*jslint eqeq: true, bitwise: true*/
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
};
//
// Browsers and platforms detection
qq.ie = function(){
"use strict";
return navigator.userAgent.indexOf('MSIE') !== -1;
};
qq.ie10 = function(){
"use strict";
return navigator.userAgent.indexOf('MSIE 10') !== -1;
};
qq.safari = function(){
"use strict";
return navigator.vendor !== undefined && navigator.vendor.indexOf("Apple") !== -1;
};
qq.chrome = function(){
"use strict";
return navigator.vendor !== undefined && navigator.vendor.indexOf('Google') !== -1;
};
qq.firefox = function(){
"use strict";
return (navigator.userAgent.indexOf('Mozilla') !== -1 && navigator.vendor !== undefined && navigator.vendor === '');
};
qq.windows = function(){
"use strict";
return navigator.platform === "Win32";
};
qq.android = function(){
"use strict";
return navigator.userAgent.toLowerCase().indexOf('android') !== -1;
};
qq.ios = function() {
"use strict";
return navigator.userAgent.indexOf("iPad") !== -1
|| navigator.userAgent.indexOf("iPod") !== -1
|| navigator.userAgent.indexOf("iPhone") !== -1;
};
//
// Events
qq.preventDefault = function(e){
"use strict";
if (e.preventDefault){
e.preventDefault();
} else{
e.returnValue = false;
}
};
/**
* Creates and returns element from html string
* Uses innerHTML to create an element
*/
qq.toElement = (function(){
"use strict";
var div = document.createElement('div');
return function(html){
div.innerHTML = html;
var element = div.firstChild;
div.removeChild(element);
return element;
};
}());
//key and value are passed to callback for each item in the object or array
qq.each = function(objOrArray, callback) {
"use strict";
var keyOrIndex, retVal;
if (objOrArray) {
if (qq.isArray(objOrArray)) {
for (keyOrIndex = 0; keyOrIndex < objOrArray.length; keyOrIndex++) {
retVal = callback(keyOrIndex, objOrArray[keyOrIndex]);
if (retVal === false) {
break;
}
}
}
else {
for (keyOrIndex in objOrArray) {
if (Object.prototype.hasOwnProperty.call(objOrArray, keyOrIndex)) {
retVal = callback(keyOrIndex, objOrArray[keyOrIndex]);
if (retVal === false) {
break;
}
}
}
}
}
};
//include any args that should be passed to the new function after the context arg
qq.bind = function(oldFunc, context) {
if (qq.isFunction(oldFunc)) {
var args = Array.prototype.slice.call(arguments, 2);
return function() {
if (arguments.length) {
args = args.concat(Array.prototype.slice.call(arguments))
}
return oldFunc.apply(context, args);
};
}
throw new Error("first parameter must be a function!");
};
/**
* obj2url() takes a json-object as argument and generates
* a querystring. pretty much like jQuery.param()
*
* how to use:
*
* `qq.obj2url({a:'b',c:'d'},'http://any.url/upload?otherParam=value');`
*
* will result in:
*
* `http://any.url/upload?otherParam=value&a=b&c=d`
*
* @param Object JSON-Object
* @param String current querystring-part
* @return String encoded querystring
*/
qq.obj2url = function(obj, temp, prefixDone){
"use strict";
/*jshint laxbreak: true*/
var i, len,
uristrings = [],
prefix = '&',
add = function(nextObj, i){
var nextTemp = temp
? (/\[\]$/.test(temp)) // prevent double-encoding
? temp
: temp+'['+i+']'
: i;
if ((nextTemp !== 'undefined') && (i !== 'undefined')) {
uristrings.push(
(typeof nextObj === 'object')
? qq.obj2url(nextObj, nextTemp, true)
: (Object.prototype.toString.call(nextObj) === '[object Function]')
? encodeURIComponent(nextTemp) + '=' + encodeURIComponent(nextObj())
: encodeURIComponent(nextTemp) + '=' + encodeURIComponent(nextObj)
);
}
};
if (!prefixDone && temp) {
prefix = (/\?/.test(temp)) ? (/\?$/.test(temp)) ? '' : '&' : '?';
uristrings.push(temp);
uristrings.push(qq.obj2url(obj));
} else if ((Object.prototype.toString.call(obj) === '[object Array]') && (typeof obj !== 'undefined') ) {
// we wont use a for-in-loop on an array (performance)
for (i = -1, len = obj.length; i < len; i+=1){
add(obj[i], i);
}
} else if ((typeof obj !== 'undefined') && (obj !== null) && (typeof obj === "object")){
// for anything else but a scalar, we will use for-in-loop
for (i in obj){
if (obj.hasOwnProperty(i)) {
add(obj[i], i);
}
}
} else {
uristrings.push(encodeURIComponent(temp) + '=' + encodeURIComponent(obj));
}
if (temp) {
return uristrings.join(prefix);
} else {
return uristrings.join(prefix)
.replace(/^&/, '')
.replace(/%20/g, '+');
}
};
qq.obj2FormData = function(obj, formData, arrayKeyName) {
"use strict";
if (!formData) {
formData = new FormData();
}
qq.each(obj, function(key, val) {
key = arrayKeyName ? arrayKeyName + '[' + key + ']' : key;
if (qq.isObject(val)) {
qq.obj2FormData(val, formData, key);
}
else if (qq.isFunction(val)) {
formData.append(key, val());
}
else {
formData.append(key, val);
}
});
return formData;
};
qq.obj2Inputs = function(obj, form) {
"use strict";
var input;
if (!form) {
form = document.createElement('form');
}
qq.obj2FormData(obj, {
append: function(key, val) {
input = document.createElement('input');
input.setAttribute('name', key);
input.setAttribute('value', val);
form.appendChild(input);
}
});
return form;
};
qq.setCookie = function(name, value, days) {
var date = new Date(),
expires = "";
if (days) {
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
}
document.cookie = name+"="+value+expires+"; path=/";
};
qq.getCookie = function(name) {
var nameEQ = name + "=",
ca = document.cookie.split(';'),
cookie;
qq.each(ca, function(idx, part) {
var cookiePart = part;
while (cookiePart.charAt(0)==' ') {
cookiePart = cookiePart.substring(1, cookiePart.length);
}
if (cookiePart.indexOf(nameEQ) === 0) {
cookie = cookiePart.substring(nameEQ.length, cookiePart.length);
return false;
}
});
return cookie;
};
qq.getCookieNames = function(regexp) {
var cookies = document.cookie.split(';'),
cookieNames = [];
qq.each(cookies, function(idx, cookie) {
cookie = qq.trimStr(cookie);
var equalsIdx = cookie.indexOf("=");
if (cookie.match(regexp)) {
cookieNames.push(cookie.substr(0, equalsIdx));
}
});
return cookieNames;
};
qq.deleteCookie = function(name) {
qq.setCookie(name, "", -1);
};
qq.areCookiesEnabled = function() {
var randNum = Math.random() * 100000,
name = "qqCookieTest:" + randNum;
qq.setCookie(name, 1);
if (qq.getCookie(name)) {
qq.deleteCookie(name);
return true;
}
return false;
};
/**
* Not recommended for use outside of Fine Uploader since this falls back to an unchecked eval if JSON.parse is not
* implemented. For a more secure JSON.parse polyfill, use Douglas Crockford's json2.js.
*/
qq.parseJson = function(json) {
/*jshint evil: true*/
if (window.JSON && qq.isFunction(JSON.parse)) {
return JSON.parse(json);
} else {
return eval("(" + json + ")");
}
};
/**
* A generic module which supports object disposing in dispose() method.
* */
qq.DisposeSupport = function() {
"use strict";
var disposers = [];
return {
/** Run all registered disposers */
dispose: function() {
var disposer;
do {
disposer = disposers.shift();
if (disposer) {
disposer();
}
}
while (disposer);
},
/** Attach event handler and register de-attacher as a disposer */
attach: function() {
var args = arguments;
/*jslint undef:true*/
this.addDisposer(qq(args[0]).attach.apply(this, Array.prototype.slice.call(arguments, 1)));
},
/** Add disposer to the collection */
addDisposer: function(disposeFunction) {
disposers.push(disposeFunction);
}
};
};
;qq.version="3.7.1";;qq.supportedFeatures = (function () {
var supportsUploading,
supportsAjaxFileUploading,
supportsFolderDrop,
supportsChunking,
supportsResume,
supportsUploadViaPaste,
supportsUploadCors,
supportsDeleteFileXdr,
supportsDeleteFileCorsXhr,
supportsDeleteFileCors;
function testSupportsFileInputElement() {
var supported = true,
tempInput;
try {
tempInput = document.createElement('input');
tempInput.type = 'file';
qq(tempInput).hide();
if (tempInput.disabled) {
supported = false;
}
}
catch (ex) {
supported = false;
}
return supported;
}
//only way to test for Filesystem API support since webkit does not expose the DataTransfer interface
function isChrome21OrHigher() {
return qq.chrome() &&
navigator.userAgent.match(/Chrome\/[2][1-9]|Chrome\/[3-9][0-9]/) !== undefined;
}
//only way to test for complete Clipboard API support at this time
function isChrome14OrHigher() {
return qq.chrome() &&
navigator.userAgent.match(/Chrome\/[1][4-9]|Chrome\/[2-9][0-9]/) !== undefined;
}
//Ensure we can send cross-origin `XMLHttpRequest`s
function isCrossOriginXhrSupported() {
if (window.XMLHttpRequest) {
var xhr = new XMLHttpRequest();
//Commonly accepted test for XHR CORS support.
return xhr.withCredentials !== undefined;
}
return false;
}
//Test for (terrible) cross-origin ajax transport fallback for IE9 and IE8
function isXdrSupported() {
return window.XDomainRequest !== undefined;
}
// CORS Ajax requests are supported if it is either possible to send credentialed `XMLHttpRequest`s,
// or if `XDomainRequest` is an available alternative.
function isCrossOriginAjaxSupported() {
if (isCrossOriginXhrSupported()) {
return true;
}
return isXdrSupported();
}
supportsUploading = testSupportsFileInputElement();
supportsAjaxFileUploading = supportsUploading && qq.isXhrUploadSupported();
supportsFolderDrop = supportsAjaxFileUploading && isChrome21OrHigher();
supportsChunking = supportsAjaxFileUploading && qq.isFileChunkingSupported();
supportsResume = supportsAjaxFileUploading && supportsChunking && qq.areCookiesEnabled();
supportsUploadViaPaste = supportsAjaxFileUploading && isChrome14OrHigher();
supportsUploadCors = supportsUploading && (window.postMessage !== undefined || supportsAjaxFileUploading);
supportsDeleteFileCorsXhr = isCrossOriginXhrSupported();
supportsDeleteFileXdr = isXdrSupported();
supportsDeleteFileCors = isCrossOriginAjaxSupported();
return {
uploading: supportsUploading,
ajaxUploading: supportsAjaxFileUploading,
fileDrop: supportsAjaxFileUploading, //NOTE: will also return true for touch-only devices. It's not currently possible to accurately test for touch-only devices
folderDrop: supportsFolderDrop,
chunking: supportsChunking,
resume: supportsResume,
uploadCustomHeaders: supportsAjaxFileUploading,
uploadNonMultipart: supportsAjaxFileUploading,
itemSizeValidation: supportsAjaxFileUploading,
uploadViaPaste: supportsUploadViaPaste,
progressBar: supportsAjaxFileUploading,
uploadCors: supportsUploadCors,
deleteFileCorsXhr: supportsDeleteFileCorsXhr,
deleteFileCorsXdr: supportsDeleteFileXdr, //NOTE: will also return true in IE10, where XDR is also supported
deleteFileCors: supportsDeleteFileCors,
canDetermineSize: supportsAjaxFileUploading
}
}());
;/*globals qq*/
qq.Promise = function() {
"use strict";
var successValue, failureValue,
successCallbacks = [],
failureCallbacks = [],
doneCallbacks = [],
state = 0;
return {
then: function(onSuccess, onFailure) {
if (state === 0) {
if (onSuccess) {
successCallbacks.push(onSuccess);
}
if (onFailure) {
failureCallbacks.push(onFailure);
}
}
else if (state === -1 && onFailure) {
onFailure(failureValue);
}
else if (onSuccess) {
onSuccess(successValue);
}
return this;
},
done: function(callback) {
if (state === 0) {
doneCallbacks.push(callback);
}
else {
callback();
}
return this;
},
success: function(val) {
state = 1;
successValue = val;
if (successCallbacks.length) {
qq.each(successCallbacks, function(idx, callback) {
callback(val);
})
}
if(doneCallbacks.length) {
qq.each(doneCallbacks, function(idx, callback) {
callback();
})
}
return this;
},
failure: function(val) {
state = -1;
failureValue = val;
if (failureCallbacks.length) {
qq.each(failureCallbacks, function(idx, callback) {
callback(val);
})
}
if(doneCallbacks.length) {
qq.each(doneCallbacks, function(idx, callback) {
callback();
})
}
return this;
}
};
};
qq.isPromise = function(maybePromise) {
return maybePromise && maybePromise.then && maybePromise.done;
};;/*globals qq*/
/**
* This module represents an upload or "Select File(s)" button. It's job is to embed an opaque `<input type="file">`
* element as a child of a provided "container" element. This "container" element (`options.element`) is used to provide
* a custom style for the `<input type="file">` element. The ability to change the style of the container element is also
* provided here by adding CSS classes to the container on hover/focus.
*
* TODO Eliminate the mouseover and mouseout event handlers since the :hover CSS pseudo-class should now be
* available on all supported browsers.
*
* @param o Options to override the default values
*/
qq.UploadButton = function(o) {
"use strict";
var input,
// Used to detach all event handlers created at once for this instance
disposeSupport = new qq.DisposeSupport(),
options = {
// "Container" element
element: null,
// If true adds `multiple` attribute to `<input type="file">`
multiple: false,
// Corresponds to the `accept` attribute on the associated `<input type="file">`
acceptFiles: null,
// `name` attribute of `<input type="file">`
name: 'qqfile',
// Called when the browser invokes the onchange handler on the `<input type="file">`
onChange: function(input) {},
// **This option will be removed** in the future as the :hover CSS pseudo-class is available on all supported browsers
hoverClass: 'qq-upload-button-hover',
focusClass: 'qq-upload-button-focus'
};
// Overrides any of the default option values with any option values passed in during construction.
qq.extend(options, o);
// Embed an opaque `<input type="file">` element as a child of `options.element`.
function createInput() {
var input = document.createElement("input");
if (options.multiple){
input.setAttribute("multiple", "multiple");
}
if (options.acceptFiles) {
input.setAttribute("accept", options.acceptFiles);
}
input.setAttribute("type", "file");
input.setAttribute("name", options.name);
qq(input).css({
position: 'absolute',
// in Opera only 'browse' button
// is clickable and it is located at
// the right side of the input
right: 0,
top: 0,
fontFamily: 'Arial',
// 4 persons reported this, the max values that worked for them were 243, 236, 236, 118
fontSize: '118px',
margin: 0,
padding: 0,
cursor: 'pointer',
opacity: 0
});
options.element.appendChild(input);
disposeSupport.attach(input, 'change', function(){
options.onChange(input);
});
// **These event handlers will be removed** in the future as the :hover CSS pseudo-class is available on all supported browsers
disposeSupport.attach(input, 'mouseover', function(){
qq(options.element).addClass(options.hoverClass);
});
disposeSupport.attach(input, 'mouseout', function(){
qq(options.element).removeClass(options.hoverClass);
});
disposeSupport.attach(input, 'focus', function(){
qq(options.element).addClass(options.focusClass);
});
disposeSupport.attach(input, 'blur', function(){
qq(options.element).removeClass(options.focusClass);
});
// IE and Opera, unfortunately have 2 tab stops on file input
// which is unacceptable in our case, disable keyboard access
if (window.attachEvent) {
// it is IE or Opera
input.setAttribute('tabIndex', "-1");
}
return input;
}
// Make button suitable container for input
qq(options.element).css({
position: 'relative',
overflow: 'hidden',
// Make sure browse button is in the right side in Internet Explorer
direction: 'ltr'
});
input = createInput();
// Exposed API
return {
getInput: function(){
return input;
},
reset: function(){
if (input.parentNode){
qq(input).remove();
}
qq(options.element).removeClass(options.focusClass);
input = createInput();
}
};