-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (114 loc) · 4.11 KB
/
index.js
File metadata and controls
140 lines (114 loc) · 4.11 KB
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
//自动获取swf的地址
(function(){
var doc = document, currentScript;
//获取upoader.js的url
if(doc.currentScript){
currentScript = doc.currentScript.src;
}else{
var stack;
try{
currentScript();
}catch(e){
stack = e.stack;
if(!stack && window.opera){
stack = (String(e).match(/of linked script \S+/g) || []).join(" ");
}
}
if(stack){
stack = stack.split( /[@ ]/g).pop();
stack = stack[0] == "(" ? stack.slice(1,-1) : stack;
currentScript = stack.replace(/(:\d+)?:\d+$/i, "");
}else{
var scripts = doc.getElementsByTagName("script");
for(var i = scripts.length - 1; i >= 0; i--){
var script = scripts[i];
if(script.readyState === "interactive"){
currentScript = script.src;
break;
}
}
}
}
//如果未使用编译工具,则直接返回
function __uri(url){
return url;
}
var prefix = '';
var swfUrl = __uri('./uploader.swf');
if(swfUrl == './uploader.swf'){
prefix = currentScript.replace(/[^\/]+$/, '');
}else if(!/^(?:(?:https?:)?\/\/)?[^\/]+/.test(swfUrl)){
prefix = (currentScript.match(/^(?:(?:https?:)?\/\/)?[^\/]+/) || [''])[0];
}
window.__featherUiUploaderSwfUrl__ = prefix + swfUrl;
})();
;(function(factory){
if(typeof module === 'object' && typeof module.exports == 'object'){
module.exports = factory(
require('jquery'),
require('../class/class'),
require('../cookie/cookie'),
require('./lib/uploadify')
);
}else{
factory(window.jQuery, window.jQuery.klass, window.jQuery.cookie, window.jQuery.fn.uplodify);
}
})(function($, Class, Cookie){
var DATANAME = Class.NAMESPACE + '.uploader';
var prototype = {
initialize: function(opt){
var self = this;
self.dom = $(opt.dom);
self.options = $.extend({
swf: window.__featherUiUploaderSwfUrl__,
debug: false,
width: self.dom.width(),
height: self.dom.height(),
buttonText: '上传',
fixedCookie: false
}, opt || {});
self.init();
},
init: function(){
var self = this, options = self.options;
if(options.fixedCookie){
options.formData = $.extend(options.formData || {}, Cookie.get() || {});
}
if(!options.queueID){
options.overrideEvents = ['onUploadProgress', 'onUploadComplete', 'onUploadSuccess', 'onUploadStart', 'onUploadError', 'onSelect'];
}
$.each('cancel clearQueue destroy dialogOpen dialogClose select selectError queueComplete uploadComplete uploadError uploadProgress uploadStart uploadSuccess'.split(' '), function(key, event){
var fullName = 'on' + event.replace(/^\w/, function(first){
return first.toUpperCase();
});
options[fullName] && self.on(event, options[fullName]);
options[fullName] = function(){
self.trigger(event, arguments);
};
});
if(!self.dom.attr('id')){
self.dom.attr('id', 'ui2-uploader-' + $.now());
}
self.dom.uploadify(options);
//hack uploadify plugin
//uploadify会重新创建一个同名的dom,所以再次通过jquery获取upload对象时,会为空,所以,直接将值同样赋值给新创建的元素的data中
var id = options.id ? options.id : self.dom.attr('id');
self.uploader = $('#' + id).data(DATANAME, self);
}
};
$.each('cancel destroy disable settings stop upload'.split(' '), function(key, method){
prototype[method] = function(){
var self = this, args = Array.prototype.slice.call(arguments);
args.unshift(method);
self.dom.uploadify.apply(self.dom, args);
if(method == 'destroy'){
self.dom.removeData(DATANAME);
/ui2-uploader-\d+/.test(self.dom.attr('id')) && self.dom.removeAttr('id');
self.dom = null;
self.uploader.removeData(DATANAME);
self.uploader = null;
}
};
});
return Class.$factory('uploader', prototype);
});