This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
/
ui.atk4_uploader.js
207 lines (178 loc) · 5.93 KB
/
ui.atk4_uploader.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
/* Welcome to Agile Toolkit JS framework. This file implements Uploader. */
// The following HTML structure should be used:
//
// <input type=file> <!-- binding to this element -->
//
$.widget("ui.atk4_uploader", {
options: {
'flash': false,
'iframe': false,
'multiple': 1,
},
shown: true,
_setChanged: function(){
this.element.closest('form').addClass('form_changed');
},
_create: function(){
var self=this;
if(!this.options.form)this.options.form="#"+closest('form').parent().attr('id');
this.name=this.element.attr('id');
if(this.options.flash){
this.initSWF();
}
if(this.options.iframe){
this.element.change(function(){
self.upload();
})
}
},
// we supply different upload techniques, and can change later based on browser support.
initSWF: function(){
var uploader=this;
uploader.element.hide(); // do not show while loading..
uploader.element.after('<input type="hidden" id="'+uploader.name+'_token">');
$.atk4.includeCSS('/amodules3/templates/js/uploadify/uploadify.css');
$.atk4.includeJS('/amodules3/templates/js/uploadify/swfobject.js');
$.atk4.includeJS('/amodules3/templates/js/uploadify/jquery.uploadify.v2.1.0.min.js');
$.atk4(function(){
uploader.element.uploadify(i={
'uploader':'/amodules3/templates/js/uploadify/uploadify.swf',
'script': '/upload/',
'scriptAccess': 'always',
'buttonText':'Upload new',
'auto': true,
'fileDataName': 'Default',
'sizeLimit': uploader.options.size_limit,
'onComplete': function(){ return uploader.completeSWF.apply(uploader,arguments)},
'cancelImg': '/amodules3/templates/js/uploadify/cancel.png'
});
console.log(i);
});
},
upload: function(){
var form_wrapper=$(this.options.form);
var form=form_wrapper.find('form');
var oa=form.attr('action');
// add dynamically if it's missing
var i=$('<div style="display: inline"/>');
i.insertBefore(this.element);
i[0].innerHTML='<iframe id="'+this.name+'_iframe" name="'+this.name+'_iframe" src="about:blank" style="width:0;height:0;border:0px solid black;"></iframe>';
//insertBefore(this.element);
var g=$('<div class="atk-loader" id="'+this.name+'_progress"><i></i>Uploading '+this.element.val()+'</div>').
insertBefore(this.element);
form
.attr('action',oa+'&'+this.element.attr('name')+'_upload_action='+this.name)
.attr('target',this.name+"_iframe");
form_wrapper.atk4_form('submitPlain');
form
.removeAttr('target')
.attr('action',oa)
;
// fool-proof way to clone element. Firefox will copy seelcted file, while safari will not
var el=this.element.clone().attr('id',this.name+'_');
// Silly firefox - copies uploaded file value
el=el.wrap('<div/>').parent();
el[0].innerHTML=el[0].innerHTML;
el=el.find('input');
el.insertAfter(this.element).atk4_uploader(this.options);
var files=$("#"+this.element.attr('name')+"_files").find('.files-container').children('div').not('.template').length;
if(files+1>=this.options.multiple)el.hide(); //does this work actually? I mean the el.hide()
this.element.hide();
},
addFiles: function(data){
// Uses template to populate rows in the table
var tb=$("#"+this.element.attr('name')+"_files").find('.files-container');
var self=this;
var act=this.element.closest('form').attr('action');
$.each(data,function(i,row){
var tpl=tb.find('.template')
.clone().attr('rel',row['id'])// <--easier to debug
.removeClass('template')
.show();
$.each(row,function(key,val){
tpl.find('[data-template='+key+']').text(val);
});
tpl.find('.delete_doc').click(function(ev){
ev.preventDefault();
$(this).univ().ajaxec(act+'&'+
self.element.attr('name')+'_delete_action='+
$(this).closest('div').attr('rel')
);
})
tpl.find('.view_doc').click(function(ev){
ev.preventDefault();
$(this).univ().newWindow(act+'&view=true&'+
self.element.attr('name')+'_save_action='+
$(this).closest('div').attr('rel')
);
})
tpl.find('.save_doc').click(function(ev){
ev.preventDefault();
$(this).univ().location(act+'&'+
self.element.attr('name')+'_save_action='+
$(this).closest('div').attr('rel')
);
});
tpl.appendTo(tb);
});
self.updateToken();
var files=$("#"+this.element.attr('name')+"_files").find('.files-container').children('div').not('.template').length;
if(files>=this.options.multiple)this.element.hide();
},
removeFiles: function(ids){
var tb=$("#"+this.element.attr('name')+"_files").find('.files-container');
var self=this;
$.each(ids,function(junk,id){
tb.find('[rel='+id+']').remove();
});
self.updateToken();
this.element.show();
},
updateToken: function(){
var tb=$("#"+this.element.attr('name')+"_files").find('.files-container');
var ids=[];
tb.find('div').not('.template').each(function(){
ids.push($(this).attr('rel'));
});
$("#"+this.element.attr('name')+"_token").val(ids.join(','));
},
uploadComplete: function(data){
// This method is called when iFrame upload is complete
if(!data){
console.error('File upload was completed but no action was defined.');
return;
}
$('#'+this.name+'_progress').remove();
this.element.trigger('upload');
this.element.attr('disabled',false);
this.addFiles([data]);
//this.element.next('br').remove();
this.element.remove();
this._setChanged();
//$('#'+this.name+'_token').val(data.id);
},
uploadFailed: function(message,debug){
if(debug){
$.univ().successMessage('Debug: '+$.univ().toJSON(debug));
}
$(this.options.form).atk4_form('fieldError',this.element,message);
$('#'+this.name+'_progress').remove();
this.element.next().show()
this.element.remove();
},
completeSWF: function(a,b,c,d,e){
var token={
'fileInfo':c,
'filename':d
}
try{
$('#'+this.name+'_token').val($.univ().toJSON(token));
this.element.after('File: '+token.fileInfo.name+' uploaded successfuly <br/>');
this._setChanged();
}catch(e){
console.log(e);
}
console.log('hoho');
return true;
}
});