havior 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3fb31f6e5af52fab0d690067fd2d982ff46a8f31
|
|
4
|
+
data.tar.gz: e83ef64b63cd500e9cc25cf79dea6c7cb4285479
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2c519f8aadef512a4eb6e39f9661a03ad3f0c2f158885a1a7be2f87383b586d1c2555def327492c0e1ae531d903a8b88ec64ee699e35bbbce181ef8b397383f0
|
|
7
|
+
data.tar.gz: ae4bb773f2a0df830cfb39582e9858f400c1c04455d6d45148bd269c689f853e8ab606fc727e3009b380ed19317a552aad646953257397305a02c8344c10fe0d
|
|
@@ -10,6 +10,7 @@ module Havior
|
|
|
10
10
|
|
|
11
11
|
def copy_haxe_libs
|
|
12
12
|
copy_file "haxe/lib/Havior.hx", "haxe/lib/Havior.hx"
|
|
13
|
+
copy_file "haxe/lib/FileUploader.hx", "haxe/lib/FileUploader.hx"
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
def inject_havior_helper
|
|
@@ -22,7 +23,7 @@ module Havior
|
|
|
22
23
|
def inject_routes
|
|
23
24
|
inject_into_file "config/routes.rb",
|
|
24
25
|
after: "Rails.application.routes.draw do\n" do
|
|
25
|
-
|
|
26
|
+
" Dir[\"#{Rails.root}/app/haviors/**/routes.rb\"].each{|r| load r }\n"
|
|
26
27
|
end
|
|
27
28
|
end
|
|
28
29
|
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import jQuery.*;
|
|
2
|
+
import js.html.*;
|
|
3
|
+
|
|
4
|
+
enum Mode{
|
|
5
|
+
Single;
|
|
6
|
+
Prompt;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
enum Upload{
|
|
10
|
+
Empty;
|
|
11
|
+
//Canceled;
|
|
12
|
+
//Errored;
|
|
13
|
+
Pending(file:File);
|
|
14
|
+
Processing(file:File);
|
|
15
|
+
Finished(file:File, response:Dynamic);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
enum FileUploaderType{
|
|
19
|
+
Droparea(droparea:String);
|
|
20
|
+
Selector(field:String, button:String);
|
|
21
|
+
DropareaAndSelector(droparea:String, field:String, button:String);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
typedef FileUploadParam = {
|
|
25
|
+
action : String,
|
|
26
|
+
method : String,
|
|
27
|
+
query : String,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
typedef Filter = {
|
|
31
|
+
type : String,
|
|
32
|
+
size : Int,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
typedef Callback = {
|
|
36
|
+
after_upload : String,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
typedef Option = {
|
|
40
|
+
filter : Filter,
|
|
41
|
+
callback : Callback,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class FileUploader extends Havior {
|
|
45
|
+
|
|
46
|
+
public static function main(){
|
|
47
|
+
Havior.start("system/file_uploader");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public override function onLoad(){
|
|
51
|
+
var mode = Single;
|
|
52
|
+
var type = DropareaAndSelector("#droparea","#file_field","#upload_button");
|
|
53
|
+
var param = {
|
|
54
|
+
action: getParam("action"),
|
|
55
|
+
method: getParam("method"),
|
|
56
|
+
query: getParam("query"),
|
|
57
|
+
};
|
|
58
|
+
var option:Option = {
|
|
59
|
+
filter: {
|
|
60
|
+
type: getParam("type_filter"),
|
|
61
|
+
size: haxe.Json.parse( getParam("size_filter") ),
|
|
62
|
+
},
|
|
63
|
+
callback: {
|
|
64
|
+
after_upload: getParam("after_upload"),
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
initialize(mode, type, param, option);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private var debug = true;
|
|
71
|
+
private var mode : Mode;
|
|
72
|
+
private var param : FileUploadParam;
|
|
73
|
+
private var option : Option;
|
|
74
|
+
|
|
75
|
+
private var request : XMLHttpRequest;
|
|
76
|
+
private var queue : Array<File>;
|
|
77
|
+
private var results : Array< Upload >;
|
|
78
|
+
private var uploading : Upload;
|
|
79
|
+
|
|
80
|
+
public function initialize(mode: Mode, type:FileUploaderType,
|
|
81
|
+
param:FileUploadParam, ?option:Option){
|
|
82
|
+
this.mode = mode;
|
|
83
|
+
this.request = new XMLHttpRequest();
|
|
84
|
+
this.param = param;
|
|
85
|
+
this.queue = new Array<File>();
|
|
86
|
+
this.results = new Array< Upload >();
|
|
87
|
+
this.uploading = Empty;
|
|
88
|
+
this.option = option;
|
|
89
|
+
|
|
90
|
+
setRequestEvents();
|
|
91
|
+
setFileUploaderEvents(type);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private function setRequestEvents(){
|
|
95
|
+
this.request.upload.onprogress = function(ev){
|
|
96
|
+
var rate = (ev.loaded * 1.0) / ev.total;
|
|
97
|
+
onProgressed(rate, ev.loaded, ev.total);
|
|
98
|
+
};
|
|
99
|
+
this.request.upload.onload = function(ev){
|
|
100
|
+
onUploaded();
|
|
101
|
+
};
|
|
102
|
+
this.request.onreadystatechange = function(ev){
|
|
103
|
+
if(this.request.readyState == 4){
|
|
104
|
+
var json = haxe.Json.parse(this.request.responseText);
|
|
105
|
+
finishUploading(json);
|
|
106
|
+
onResponsed(json);
|
|
107
|
+
callback(getOption(this.option,"callback.after_upload"), json);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public function setFileUploaderEvents(type:FileUploaderType){
|
|
113
|
+
switch(type){
|
|
114
|
+
case Droparea(d):
|
|
115
|
+
setDropareaEvents(d);
|
|
116
|
+
case Selector(f,b):
|
|
117
|
+
setSelectorEvents(f,b);
|
|
118
|
+
case DropareaAndSelector(d,f,b):
|
|
119
|
+
setDropareaEvents(d);
|
|
120
|
+
setSelectorEvents(f,b);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
private function setDropareaEvents(droparea:String){
|
|
124
|
+
// for miss drop
|
|
125
|
+
new JQuery(js.Browser.window).on("dragenter dragover dragexit dragleave drop", function(ev){
|
|
126
|
+
ev.stopPropagation();
|
|
127
|
+
ev.preventDefault();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
var area = new JQuery(droparea);
|
|
131
|
+
area.on("dragenter dragover", function(ev){
|
|
132
|
+
ev.stopPropagation();
|
|
133
|
+
ev.preventDefault();
|
|
134
|
+
if(mode==Single && this.uploading != Empty){
|
|
135
|
+
onEnteredForbidden();
|
|
136
|
+
}else{
|
|
137
|
+
onEnteredDropArea();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
area.on("dragexit dragleave drop", function(ev){
|
|
141
|
+
ev.stopPropagation();
|
|
142
|
+
ev.preventDefault();
|
|
143
|
+
if(mode==Single && this.uploading != Empty){
|
|
144
|
+
onExitedForbidden();
|
|
145
|
+
}else{
|
|
146
|
+
onExitedDropArea();
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
area.on("drop", function(ev){
|
|
150
|
+
ev.stopPropagation();
|
|
151
|
+
ev.preventDefault();
|
|
152
|
+
var files = ev.originalEvent.dataTransfer.files;
|
|
153
|
+
if(mode==Single && this.uploading != Empty){
|
|
154
|
+
onDroppedForbidden(files);
|
|
155
|
+
}else{
|
|
156
|
+
onDroppedFiles(files);
|
|
157
|
+
setFiles(files);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
private function setSelectorEvents(field:String, button:String){
|
|
162
|
+
new JQuery(button).click(function(ev){
|
|
163
|
+
ev.stopPropagation();
|
|
164
|
+
ev.preventDefault();
|
|
165
|
+
var files = cast(new JQuery(field).prop("files"), FileList);
|
|
166
|
+
if(files.length > 0){
|
|
167
|
+
onPressedButton(files);
|
|
168
|
+
setFiles(files);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private function setFiles(files:FileList){
|
|
174
|
+
this.request.onload = function(ev){ }; // reset callback
|
|
175
|
+
switch(this.mode){
|
|
176
|
+
case Single:
|
|
177
|
+
sendFile(files.item(0));
|
|
178
|
+
case Prompt:
|
|
179
|
+
enqueueFiles(files);
|
|
180
|
+
if(this.uploading == Empty){
|
|
181
|
+
this.results = new Array< Upload >();
|
|
182
|
+
this.request.onload = function(ev){ promptUpload(); };
|
|
183
|
+
promptUpload();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private function enqueueFiles(files:FileList){
|
|
189
|
+
for(file in files){
|
|
190
|
+
if(applyFilter(file)) queue.push(file);
|
|
191
|
+
}
|
|
192
|
+
refreshView();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private function finishUploading(json: Dynamic){
|
|
196
|
+
switch(this.uploading){
|
|
197
|
+
case Processing(file):
|
|
198
|
+
var x = Finished(file, json);
|
|
199
|
+
this.results.push(x);
|
|
200
|
+
this.uploading = Empty;
|
|
201
|
+
refreshView();
|
|
202
|
+
default:
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// for Single
|
|
207
|
+
private function sendFile(file:File){
|
|
208
|
+
if(this.uploading == Empty){
|
|
209
|
+
this.request.onload = function(ev){
|
|
210
|
+
onFinished(this.results);
|
|
211
|
+
};
|
|
212
|
+
this.uploading = Pending(file);
|
|
213
|
+
this.results = new Array< Upload >();
|
|
214
|
+
refreshView();
|
|
215
|
+
send(this.uploading);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// for Prompt
|
|
220
|
+
private function promptUpload(){
|
|
221
|
+
if(queue.length > 0){
|
|
222
|
+
this.uploading = Pending(queue.shift());
|
|
223
|
+
refreshView();
|
|
224
|
+
send(this.uploading);
|
|
225
|
+
}else{
|
|
226
|
+
onFinished(this.results);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
// send a file
|
|
232
|
+
private function send(file:Upload){
|
|
233
|
+
switch(file){
|
|
234
|
+
case Pending(f):
|
|
235
|
+
this.uploading = Processing(f);
|
|
236
|
+
this.request.open(this.param.method, this.param.action, true);
|
|
237
|
+
var token = new JQuery("meta[name=csrf-token]").attr("content");
|
|
238
|
+
this.request.setRequestHeader("X-CSRF-Token", token);
|
|
239
|
+
var query = this.param.query;
|
|
240
|
+
var fd = buildFormData([{name:query, value:f}]);
|
|
241
|
+
trace(fd);
|
|
242
|
+
this.request.send(fd);
|
|
243
|
+
default:
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
private function buildFormData(xs:Array<{name:String,value:Dynamic}>){
|
|
247
|
+
var fd = untyped __js__("new FormData()");
|
|
248
|
+
untyped __js__("for(var x of xs){ fd.append(x.name, x.value); }");
|
|
249
|
+
return fd;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// filters
|
|
253
|
+
private function applyFilter(file:File){
|
|
254
|
+
var fs = [ typeFilter, sizeFilter, customFilter ];
|
|
255
|
+
for(f in fs){
|
|
256
|
+
if(!f(file)) return false;
|
|
257
|
+
}
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
private function typeFilter(file:File){
|
|
261
|
+
var type = getOption(this.option, "filter.type");
|
|
262
|
+
if(type != null){
|
|
263
|
+
var r = new EReg(type, "");
|
|
264
|
+
if(!r.match(file.type) ){
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
private function sizeFilter(file:File){
|
|
271
|
+
var size = getOption(this.option, "filter.size");
|
|
272
|
+
if(size != null){
|
|
273
|
+
return file.size <= size;
|
|
274
|
+
}
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
private function refreshView(){
|
|
279
|
+
refresh(this.results, this.uploading, this.queue);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/// callbacks
|
|
283
|
+
public function refresh(results:Array<Upload>,
|
|
284
|
+
uploading:Upload, queue:Array<File>){
|
|
285
|
+
if(debug) trace("refresh view");
|
|
286
|
+
}
|
|
287
|
+
public function customFilter(file:File){
|
|
288
|
+
if(debug) trace("apply custom filter");
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
public function onProgressed(rate:Float, loaded:Int, total:Int){
|
|
292
|
+
if(debug) trace("progress:" + (Math.ceil(rate*100)) + "%");
|
|
293
|
+
children("#status").html(Math.ceil(rate*100) + "%");
|
|
294
|
+
}
|
|
295
|
+
public function onUploaded(){
|
|
296
|
+
if(debug) trace("uploaded, now saving.");
|
|
297
|
+
}
|
|
298
|
+
public function onResponsed(response:Dynamic){
|
|
299
|
+
if(debug) { trace("responsed:"); trace(response); }
|
|
300
|
+
if(response.result == "success"){
|
|
301
|
+
children("#image").html("<img src='system/profile/takeshi/icon?"+Date.now()+Math.random()+"' />");
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
public function onFinished(results:Array< Upload >){
|
|
305
|
+
if(debug) { trace("finished:"); trace(results); }
|
|
306
|
+
}
|
|
307
|
+
public function onEnteredDropArea(){
|
|
308
|
+
if(debug) trace("enterd");
|
|
309
|
+
}
|
|
310
|
+
public function onExitedDropArea(){
|
|
311
|
+
if(debug) trace("exited");
|
|
312
|
+
}
|
|
313
|
+
public function onDroppedFiles(files:FileList){
|
|
314
|
+
if(debug){ trace("dropped"); trace(files); }
|
|
315
|
+
}
|
|
316
|
+
public function onPressedButton(files:FileList){
|
|
317
|
+
if(debug) trace("pressed button");
|
|
318
|
+
}
|
|
319
|
+
public function onEnteredForbidden(){
|
|
320
|
+
if(debug) trace("forbidden entered");
|
|
321
|
+
}
|
|
322
|
+
public function onExitedForbidden(){
|
|
323
|
+
if(debug) trace("forbidden exited");
|
|
324
|
+
}
|
|
325
|
+
public function onDroppedForbidden(files:FileList){
|
|
326
|
+
if(debug){ trace("forbidden dropped"); trace(files); }
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
data/lib/havior/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: havior
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Takeshi Kojima
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-02-
|
|
11
|
+
date: 2015-02-14 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -56,6 +56,7 @@ files:
|
|
|
56
56
|
- lib/generators/havior/templates/haviors/model.rb.erb
|
|
57
57
|
- lib/generators/havior/templates/haviors/routes.rb.erb
|
|
58
58
|
- lib/generators/havior/templates/haxe/havior.hxml.erb
|
|
59
|
+
- lib/generators/havior/templates/haxe/lib/FileUploader.hx
|
|
59
60
|
- lib/generators/havior/templates/haxe/lib/Havior.hx
|
|
60
61
|
- lib/havior.rb
|
|
61
62
|
- lib/havior/builder.rb
|