simditoredit-rails 0.0.1

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.
@@ -0,0 +1,287 @@
1
+ (function() {
2
+ var Uploader,
3
+ __hasProp = {}.hasOwnProperty,
4
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
5
+
6
+ Uploader = (function(_super) {
7
+ __extends(Uploader, _super);
8
+
9
+ Uploader.count = 0;
10
+
11
+ Uploader.prototype.opts = {
12
+ url: '',
13
+ params: null,
14
+ connectionCount: 3,
15
+ leaveConfirm: '正在上传文件,如果离开上传会自动取消'
16
+ };
17
+
18
+ Uploader.prototype.files = [];
19
+
20
+ Uploader.prototype.queue = [];
21
+
22
+ Uploader.prototype.html5 = !!(window.File && window.FileList);
23
+
24
+ function Uploader(opts) {
25
+ var _this = this;
26
+ if (opts == null) {
27
+ opts = {};
28
+ }
29
+ $.extend(this.opts, opts);
30
+ this.id = ++Uploader.count;
31
+ this.on('uploadcomplete', function(e, file) {
32
+ _this.files.splice($.inArray(file, _this.files), 1);
33
+ if (_this.queue.length > 0 && _this.files.length < _this.opts.connectionCount) {
34
+ return _this.upload(_this.queue.shift());
35
+ } else {
36
+ return _this.uploading = false;
37
+ }
38
+ });
39
+ $(window).on('beforeunload.uploader-' + this.id, function(e) {
40
+ if (!_this.uploading) {
41
+ return;
42
+ }
43
+ e.originalEvent.returnValue = _this.opts.leaveConfirm;
44
+ return _this.opts.leaveConfirm;
45
+ });
46
+ }
47
+
48
+ Uploader.prototype.generateId = (function() {
49
+ var id;
50
+ id = 0;
51
+ return function() {
52
+ return id += 1;
53
+ };
54
+ })();
55
+
56
+ Uploader.prototype.upload = function(file, opts) {
57
+ var f, _i, _len;
58
+ if (opts == null) {
59
+ opts = {};
60
+ }
61
+ if (file == null) {
62
+ return;
63
+ }
64
+ if ($.isArray(file)) {
65
+ for (_i = 0, _len = file.length; _i < _len; _i++) {
66
+ f = file[_i];
67
+ this.upload(f, opts);
68
+ }
69
+ } else if ($(file).is('input:file') && this.html5) {
70
+ this.upload($.makeArray($(file)[0].files), opts);
71
+ } else if (!file.id || !file.obj) {
72
+ file = this.getFile(file);
73
+ }
74
+ if (!(file && file.obj)) {
75
+ return;
76
+ }
77
+ $.extend(file, opts);
78
+ if (this.files.length >= this.opts.connectionCount) {
79
+ this.queue.push(file);
80
+ return;
81
+ }
82
+ if (this.triggerHandler('beforeupload', [file]) === false) {
83
+ return;
84
+ }
85
+ this.files.push(file);
86
+ if (this.html5) {
87
+ this.xhrUpload(file);
88
+ } else {
89
+ this.iframeUpload(file);
90
+ }
91
+ return this.uploading = true;
92
+ };
93
+
94
+ Uploader.prototype.getFile = function(fileObj) {
95
+ var name, _ref, _ref1;
96
+ if (fileObj instanceof window.File || fileObj instanceof window.Blob) {
97
+ name = (_ref = fileObj.fileName) != null ? _ref : fileObj.name;
98
+ } else if ($(fileObj).is('input:file')) {
99
+ name = $input.val().replace(/.*(\/|\\)/, "");
100
+ fileObj = $(fileObj).clone();
101
+ } else {
102
+ return null;
103
+ }
104
+ return {
105
+ id: this.generateId(),
106
+ url: this.opts.url,
107
+ params: this.opts.params,
108
+ name: name,
109
+ size: (_ref1 = fileObj.fileSize) != null ? _ref1 : fileObj.size,
110
+ ext: name ? name.split('.').pop().toLowerCase() : '',
111
+ obj: fileObj
112
+ };
113
+ };
114
+
115
+ Uploader.prototype.xhrUpload = function(file) {
116
+ var formData, k, v, _ref,
117
+ _this = this;
118
+ formData = new FormData();
119
+ formData.append("upload_file", file.obj);
120
+ formData.append("original_filename", file.name);
121
+ if (file.params) {
122
+ _ref = file.params;
123
+ for (k in _ref) {
124
+ v = _ref[k];
125
+ formData.append(k, v);
126
+ }
127
+ }
128
+ return file.xhr = $.ajax({
129
+ url: file.url,
130
+ data: formData,
131
+ processData: false,
132
+ contentType: false,
133
+ type: 'POST',
134
+ headers: {
135
+ 'X-File-Name': encodeURIComponent(file.name)
136
+ },
137
+ xhr: function() {
138
+ var req,
139
+ _this = this;
140
+ req = $.ajaxSettings.xhr();
141
+ if (req) {
142
+ req.upload.onprogress = function(e) {
143
+ return _this.progress(e);
144
+ };
145
+ }
146
+ return req;
147
+ },
148
+ progress: function(e) {
149
+ if (!e.lengthComputable) {
150
+ return;
151
+ }
152
+ return _this.trigger('uploadprogress', [file, e.loaded, e.total]);
153
+ },
154
+ error: function(xhr, status, err) {
155
+ return _this.trigger('uploaderror', [file, xhr, status]);
156
+ },
157
+ success: function(result) {
158
+ _this.trigger('uploadprogress', [file, file.size, file.size]);
159
+ return _this.trigger('uploadsuccess', [file, result]);
160
+ },
161
+ complete: function(xhr, status) {
162
+ return _this.trigger('uploadcomplete', [file, xhr.responseText]);
163
+ }
164
+ });
165
+ };
166
+
167
+ Uploader.prototype.iframeUpload = function(file) {
168
+ var k, v, _ref,
169
+ _this = this;
170
+ file.iframe = $('iframe', {
171
+ src: 'javascript:false;',
172
+ name: 'uploader-' + file.id
173
+ }).hide().appendTo(document.body);
174
+ file.form = $('<form/>', {
175
+ method: 'post',
176
+ enctype: 'multipart/form-data',
177
+ action: file.url,
178
+ target: file.iframe[0].name
179
+ }).hide().append(file.obj).appendTo(document.body);
180
+ if (file.params) {
181
+ _ref = file.params;
182
+ for (k in _ref) {
183
+ v = _ref[k];
184
+ $('<input/>', {
185
+ type: 'hidden',
186
+ name: k,
187
+ value: v
188
+ }).appendTo(form);
189
+ }
190
+ }
191
+ file.iframe.on('load', function() {
192
+ var error, iframeDoc, json, responseEl, result;
193
+ if (!(iframe.parent().length > 0)) {
194
+ return;
195
+ }
196
+ iframeDoc = iframe[0].contentDocument;
197
+ if (iframeDoc && iframeDoc.body && iframeDoc.body.innerHTML === "false") {
198
+ return;
199
+ }
200
+ responseEl = iframeDoc.getElementById('json-response');
201
+ json = responseEl ? responseEl.innerHTML : iframeDoc.body.innerHTML;
202
+ try {
203
+ result = $.parseJSON(json);
204
+ } catch (_error) {
205
+ error = _error;
206
+ _this.trigger('uploaderror', [file, null, 'parsererror']);
207
+ result = {};
208
+ }
209
+ if (result.success) {
210
+ _this.trigger('uploadsuccess', [file, result]);
211
+ }
212
+ _this.trigger('uploadcomplete', [file, result]);
213
+ return file.iframe.remove();
214
+ });
215
+ return file.form.submit().remove();
216
+ };
217
+
218
+ Uploader.prototype.cancel = function(file) {
219
+ var f, _i, _len, _ref;
220
+ if (!file.id) {
221
+ _ref = this.files;
222
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
223
+ f = _ref[_i];
224
+ if (f.id === file) {
225
+ file = f;
226
+ break;
227
+ }
228
+ }
229
+ }
230
+ this.trigger('uploadcancel', [file]);
231
+ if (this.html5) {
232
+ if (file.xhr) {
233
+ file.xhr.abort();
234
+ }
235
+ return file.xhr = null;
236
+ } else {
237
+ file.iframe.attr('src', 'javascript:false;').remove();
238
+ return this.trigger('uploadcomplete', [file]);
239
+ }
240
+ };
241
+
242
+ Uploader.prototype.readImageFile = function(fileObj, callback) {
243
+ var fileReader, img;
244
+ if (!$.isFunction(callback)) {
245
+ return;
246
+ }
247
+ img = new Image();
248
+ img.onload = function() {
249
+ return callback(img);
250
+ };
251
+ img.onerror = function() {
252
+ return callback();
253
+ };
254
+ if (window.FileReader && FileReader.prototype.readAsDataURL && /^image/.test(fileObj.type)) {
255
+ fileReader = new FileReader();
256
+ fileReader.onload = function(e) {
257
+ return img.src = e.target.result;
258
+ };
259
+ return fileReader.readAsDataURL(fileObj);
260
+ } else {
261
+ return callback();
262
+ }
263
+ };
264
+
265
+ Uploader.prototype.destroy = function() {
266
+ var file, _i, _len, _ref;
267
+ this.queue.length = 0;
268
+ _ref = this.files;
269
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
270
+ file = _ref[_i];
271
+ this.cancel(file);
272
+ }
273
+ $(window).off('.uploader-' + this.id);
274
+ return $(document).off('.uploader-' + this.id);
275
+ };
276
+
277
+ return Uploader;
278
+
279
+ })(Module);
280
+
281
+ this.simple || (this.simple = {});
282
+
283
+ this.simple.uploader = function(opts) {
284
+ return new Uploader(opts);
285
+ };
286
+
287
+ }).call(this);
@@ -0,0 +1,3 @@
1
+ //= require simditor/module
2
+ //= require simditor/uploader
3
+ //= require simditor/simditor
@@ -0,0 +1,3 @@
1
+ /*
2
+ *= require_tree ./simditors
3
+ */