scratch – Diff between revs 58 and 125
?pathlinks?
Rev 58 | Rev 125 | |||
---|---|---|---|---|
1 | /* =========================================================== |
1 | /* =========================================================== |
|
2 | * trumbowyg.upload.js v1.2 |
2 | * trumbowyg.upload.js v1.2 |
|
3 | * Upload plugin for Trumbowyg |
3 | * Upload plugin for Trumbowyg |
|
4 | * http://alex-d.github.com/Trumbowyg |
4 | * http://alex-d.github.com/Trumbowyg |
|
5 | * =========================================================== |
5 | * =========================================================== |
|
6 | * Author : Alexandre Demode (Alex-D) |
6 | * Author : Alexandre Demode (Alex-D) |
|
7 | * Twitter : @AlexandreDemode |
7 | * Twitter : @AlexandreDemode |
|
8 | * Website : alex-d.fr |
8 | * Website : alex-d.fr |
|
9 | * Mod by : Aleksandr-ru |
9 | * Mod by : Aleksandr-ru |
|
10 | * Twitter : @Aleksandr_ru |
10 | * Twitter : @Aleksandr_ru |
|
11 | * Website : aleksandr.ru |
11 | * Website : aleksandr.ru |
|
12 | */ |
12 | */ |
|
13 | |
13 | |
|
14 | (function ($) { |
14 | (function ($) { |
|
15 | 'use strict'; |
15 | 'use strict'; |
|
16 | |
16 | |
|
17 | var defaultOptions = { |
17 | var defaultOptions = { |
|
18 | serverPath: './src/plugins/upload/trumbowyg.upload.php', |
18 | serverPath: './src/plugins/upload/trumbowyg.upload.php', |
|
19 | fileFieldName: 'fileToUpload', |
19 | fileFieldName: 'fileToUpload', |
|
20 | data: [], // Additional data for ajax [{name: 'key', value: 'value'}] |
20 | data: [], // Additional data for ajax [{name: 'key', value: 'value'}] |
|
21 | headers: {}, // Additional headers |
21 | headers: {}, // Additional headers |
|
22 | xhrFields: {}, // Additional fields |
22 | xhrFields: {}, // Additional fields |
|
23 | urlPropertyName: 'file', // How to get url from the json response (for instance 'url' for {url: ....}) |
23 | urlPropertyName: 'file', // How to get url from the json response (for instance 'url' for {url: ....}) |
|
24 | statusPropertyName: 'success', // How to get status from the json response |
24 | statusPropertyName: 'success', // How to get status from the json response |
|
25 | success: undefined, // Success callback: function (data, trumbowyg, $modal, values) {} |
25 | success: undefined, // Success callback: function (data, trumbowyg, $modal, values) {} |
|
26 | error: undefined // Error callback: function () {} |
26 | error: undefined // Error callback: function () {} |
|
27 | }; |
27 | }; |
|
28 | |
28 | |
|
29 | function getDeep(object, propertyParts) { |
29 | function getDeep(object, propertyParts) { |
|
30 | var mainProperty = propertyParts.shift(), |
30 | var mainProperty = propertyParts.shift(), |
|
31 | otherProperties = propertyParts; |
31 | otherProperties = propertyParts; |
|
32 | |
32 | |
|
33 | if (object !== null) { |
33 | if (object !== null) { |
|
34 | if (otherProperties.length === 0) { |
34 | if (otherProperties.length === 0) { |
|
35 | return object[mainProperty]; |
35 | return object[mainProperty]; |
|
36 | } |
36 | } |
|
37 | |
37 | |
|
38 | if (typeof object === 'object') { |
38 | if (typeof object === 'object') { |
|
39 | return getDeep(object[mainProperty], otherProperties); |
39 | return getDeep(object[mainProperty], otherProperties); |
|
40 | } |
40 | } |
|
41 | } |
41 | } |
|
42 | return object; |
42 | return object; |
|
43 | } |
43 | } |
|
44 | |
44 | |
|
45 | addXhrProgressEvent(); |
45 | addXhrProgressEvent(); |
|
46 | |
46 | |
|
47 | $.extend(true, $.trumbowyg, { |
47 | $.extend(true, $.trumbowyg, { |
|
48 | langs: { |
48 | langs: { |
|
49 | // jshint camelcase:false |
49 | // jshint camelcase:false |
|
50 | en: { |
50 | en: { |
|
51 | upload: 'Upload', |
51 | upload: 'Upload', |
|
52 | file: 'File', |
52 | file: 'File', |
|
53 | uploadError: 'Error' |
53 | uploadError: 'Error' |
|
54 | }, |
54 | }, |
|
55 | sk: { |
55 | sk: { |
|
56 | upload: 'Nahrať', |
56 | upload: 'Nahrať', |
|
57 | file: 'Súbor', |
57 | file: 'Súbor', |
|
58 | uploadError: 'Chyba' |
58 | uploadError: 'Chyba' |
|
59 | }, |
59 | }, |
|
60 | fr: { |
60 | fr: { |
|
61 | upload: 'Envoi', |
61 | upload: 'Envoi', |
|
62 | file: 'Fichier', |
62 | file: 'Fichier', |
|
63 | uploadError: 'Erreur' |
63 | uploadError: 'Erreur' |
|
64 | }, |
64 | }, |
|
65 | cs: { |
65 | cs: { |
|
66 | upload: 'Nahrát obrázek', |
66 | upload: 'Nahrát obrázek', |
|
67 | file: 'Soubor', |
67 | file: 'Soubor', |
|
68 | uploadError: 'Chyba' |
68 | uploadError: 'Chyba' |
|
69 | }, |
69 | }, |
|
70 | zh_cn: { |
70 | zh_cn: { |
|
71 | upload: '上传', |
71 | upload: '上传', |
|
72 | file: '文件', |
72 | file: '文件', |
|
73 | uploadError: '错误' |
73 | uploadError: '错误' |
|
74 | }, |
74 | }, |
|
75 | ru: { |
75 | ru: { |
|
76 | upload: 'Загрузка', |
76 | upload: 'Загрузка', |
|
77 | file: 'Файл', |
77 | file: 'Файл', |
|
78 | uploadError: 'Ошибка' |
78 | uploadError: 'Ошибка' |
|
79 | } |
79 | }, |
|
- | 80 | ja: { |
||
- | 81 | upload: 'アップロード', |
||
- | 82 | file: 'ファイル', |
||
- | 83 | uploadError: 'エラー' |
||
- | 84 | }, |
||
- | 85 | pt_br: { |
||
- | 86 | upload: 'Enviar do local', |
||
- | 87 | file: 'Arquivo', |
||
- | 88 | uploadError: 'Erro' |
||
- | 89 | }, |
||
80 | }, |
90 | }, |
|
81 | // jshint camelcase:true |
91 | // jshint camelcase:true |
|
82 | |
92 | |
|
83 | plugins: { |
93 | plugins: { |
|
84 | upload: { |
94 | upload: { |
|
85 | init: function (trumbowyg) { |
95 | init: function (trumbowyg) { |
|
86 | trumbowyg.o.plugins.upload = $.extend(true, {}, defaultOptions, trumbowyg.o.plugins.upload || {}); |
96 | trumbowyg.o.plugins.upload = $.extend(true, {}, defaultOptions, trumbowyg.o.plugins.upload || {}); |
|
87 | var btnDef = { |
97 | var btnDef = { |
|
88 | fn: function () { |
98 | fn: function () { |
|
89 | trumbowyg.saveRange(); |
99 | trumbowyg.saveRange(); |
|
90 | |
100 | |
|
91 | var file, |
101 | var file, |
|
92 | prefix = trumbowyg.o.prefix; |
102 | prefix = trumbowyg.o.prefix; |
|
93 | |
103 | |
|
94 | var $modal = trumbowyg.openModalInsert( |
104 | var $modal = trumbowyg.openModalInsert( |
|
95 | // Title |
105 | // Title |
|
96 | trumbowyg.lang.upload, |
106 | trumbowyg.lang.upload, |
|
97 | |
107 | |
|
98 | // Fields |
108 | // Fields |
|
99 | { |
109 | { |
|
100 | file: { |
110 | file: { |
|
101 | type: 'file', |
111 | type: 'file', |
|
102 | required: true, |
112 | required: true, |
|
103 | attributes: { |
113 | attributes: { |
|
104 | accept: 'image/*' |
114 | accept: 'image/*' |
|
105 | } |
115 | } |
|
106 | }, |
116 | }, |
|
107 | alt: { |
117 | alt: { |
|
108 | label: 'description', |
118 | label: 'description', |
|
109 | value: trumbowyg.getRangeText() |
119 | value: trumbowyg.getRangeText() |
|
110 | } |
120 | } |
|
111 | }, |
121 | }, |
|
112 | |
122 | |
|
113 | // Callback |
123 | // Callback |
|
114 | function (values) { |
124 | function (values) { |
|
115 | var data = new FormData(); |
125 | var data = new FormData(); |
|
116 | data.append(trumbowyg.o.plugins.upload.fileFieldName, file); |
126 | data.append(trumbowyg.o.plugins.upload.fileFieldName, file); |
|
117 | |
127 | |
|
118 | trumbowyg.o.plugins.upload.data.map(function (cur) { |
128 | trumbowyg.o.plugins.upload.data.map(function (cur) { |
|
119 | data.append(cur.name, cur.value); |
129 | data.append(cur.name, cur.value); |
|
120 | }); |
130 | }); |
|
121 | |
131 | |
|
122 | if ($('.' + prefix + 'progress', $modal).length === 0) { |
132 | if ($('.' + prefix + 'progress', $modal).length === 0) { |
|
123 | $('.' + prefix + 'modal-title', $modal) |
133 | $('.' + prefix + 'modal-title', $modal) |
|
124 | .after( |
134 | .after( |
|
125 | $('<div/>', { |
135 | $('<div/>', { |
|
126 | 'class': prefix + 'progress' |
136 | 'class': prefix + 'progress' |
|
127 | }).append( |
137 | }).append( |
|
128 | $('<div/>', { |
138 | $('<div/>', { |
|
129 | 'class': prefix + 'progress-bar' |
139 | 'class': prefix + 'progress-bar' |
|
130 | }) |
140 | }) |
|
131 | ) |
141 | ) |
|
132 | ); |
142 | ); |
|
133 | } |
143 | } |
|
134 | |
144 | |
|
135 | $.ajax({ |
145 | $.ajax({ |
|
136 | url: trumbowyg.o.plugins.upload.serverPath, |
146 | url: trumbowyg.o.plugins.upload.serverPath, |
|
137 | headers: trumbowyg.o.plugins.upload.headers, |
147 | headers: trumbowyg.o.plugins.upload.headers, |
|
138 | xhrFields: trumbowyg.o.plugins.upload.xhrFields, |
148 | xhrFields: trumbowyg.o.plugins.upload.xhrFields, |
|
139 | type: 'POST', |
149 | type: 'POST', |
|
140 | data: data, |
150 | data: data, |
|
141 | cache: false, |
151 | cache: false, |
|
142 | dataType: 'json', |
152 | dataType: 'json', |
|
143 | processData: false, |
153 | processData: false, |
|
144 | contentType: false, |
154 | contentType: false, |
|
145 | |
155 | |
|
146 | progressUpload: function (e) { |
156 | progressUpload: function (e) { |
|
147 | $('.' + prefix + 'progress-bar').stop().animate({ |
157 | $('.' + prefix + 'progress-bar').stop().animate({ |
|
148 | width: Math.round(e.loaded * 100 / e.total) + '%' |
158 | width: Math.round(e.loaded * 100 / e.total) + '%' |
|
149 | }, 200); |
159 | }, 200); |
|
150 | }, |
160 | }, |
|
151 | |
161 | |
|
152 | success: function (data) { |
162 | success: function (data) { |
|
153 | if (trumbowyg.o.plugins.upload.success) { |
163 | if (trumbowyg.o.plugins.upload.success) { |
|
154 | trumbowyg.o.plugins.upload.success(data, trumbowyg, $modal, values); |
164 | trumbowyg.o.plugins.upload.success(data, trumbowyg, $modal, values); |
|
155 | } else { |
165 | } else { |
|
156 | if (!!getDeep(data, trumbowyg.o.plugins.upload.statusPropertyName.split('.'))) { |
166 | if (!!getDeep(data, trumbowyg.o.plugins.upload.statusPropertyName.split('.'))) { |
|
157 | var url = getDeep(data, trumbowyg.o.plugins.upload.urlPropertyName.split('.')); |
167 | var url = getDeep(data, trumbowyg.o.plugins.upload.urlPropertyName.split('.')); |
|
158 | trumbowyg.execCmd('insertImage', url); |
168 | trumbowyg.execCmd('insertImage', url); |
|
159 | $('img[src="' + url + '"]:not([alt])', trumbowyg.$box).attr('alt', values.alt); |
169 | $('img[src="' + url + '"]:not([alt])', trumbowyg.$box).attr('alt', values.alt); |
|
160 | setTimeout(function () { |
170 | setTimeout(function () { |
|
161 | trumbowyg.closeModal(); |
171 | trumbowyg.closeModal(); |
|
162 | }, 250); |
172 | }, 250); |
|
163 | trumbowyg.$c.trigger('tbwuploadsuccess', [trumbowyg, data, url]); |
173 | trumbowyg.$c.trigger('tbwuploadsuccess', [trumbowyg, data, url]); |
|
164 | } else { |
174 | } else { |
|
165 | trumbowyg.addErrorOnModalField( |
175 | trumbowyg.addErrorOnModalField( |
|
166 | $('input[type=file]', $modal), |
176 | $('input[type=file]', $modal), |
|
167 | trumbowyg.lang[data.message] |
177 | trumbowyg.lang[data.message] |
|
168 | ); |
178 | ); |
|
169 | trumbowyg.$c.trigger('tbwuploaderror', [trumbowyg, data]); |
179 | trumbowyg.$c.trigger('tbwuploaderror', [trumbowyg, data]); |
|
170 | } |
180 | } |
|
171 | } |
181 | } |
|
172 | }, |
182 | }, |
|
173 | |
183 | |
|
174 | error: trumbowyg.o.plugins.upload.error || function () { |
184 | error: trumbowyg.o.plugins.upload.error || function () { |
|
175 | trumbowyg.addErrorOnModalField( |
185 | trumbowyg.addErrorOnModalField( |
|
176 | $('input[type=file]', $modal), |
186 | $('input[type=file]', $modal), |
|
177 | trumbowyg.lang.uploadError |
187 | trumbowyg.lang.uploadError |
|
178 | ); |
188 | ); |
|
179 | trumbowyg.$c.trigger('tbwuploaderror', [trumbowyg]); |
189 | trumbowyg.$c.trigger('tbwuploaderror', [trumbowyg]); |
|
180 | } |
190 | } |
|
181 | }); |
191 | }); |
|
182 | } |
192 | } |
|
183 | ); |
193 | ); |
|
184 | |
194 | |
|
185 | $('input[type=file]').on('change', function (e) { |
195 | $('input[type=file]').on('change', function (e) { |
|
186 | try { |
196 | try { |
|
187 | // If multiple files allowed, we just get the first. |
197 | // If multiple files allowed, we just get the first. |
|
188 | file = e.target.files[0]; |
198 | file = e.target.files[0]; |
|
189 | } catch (err) { |
199 | } catch (err) { |
|
190 | // In IE8, multiple files not allowed |
200 | // In IE8, multiple files not allowed |
|
191 | file = e.target.value; |
201 | file = e.target.value; |
|
192 | } |
202 | } |
|
193 | }); |
203 | }); |
|
194 | } |
204 | } |
|
195 | }; |
205 | }; |
|
196 | |
206 | |
|
197 | trumbowyg.addBtnDef('upload', btnDef); |
207 | trumbowyg.addBtnDef('upload', btnDef); |
|
198 | } |
208 | } |
|
199 | } |
209 | } |
|
200 | } |
210 | } |
|
201 | }); |
211 | }); |
|
202 | |
212 | |
|
203 | |
213 | |
|
204 | function addXhrProgressEvent() { |
214 | function addXhrProgressEvent() { |
|
205 | if (!$.trumbowyg && !$.trumbowyg.addedXhrProgressEvent) { // Avoid adding progress event multiple times |
215 | if (!$.trumbowyg && !$.trumbowyg.addedXhrProgressEvent) { // Avoid adding progress event multiple times |
|
206 | var originalXhr = $.ajaxSettings.xhr; |
216 | var originalXhr = $.ajaxSettings.xhr; |
|
207 | $.ajaxSetup({ |
217 | $.ajaxSetup({ |
|
208 | xhr: function () { |
218 | xhr: function () { |
|
209 | var req = originalXhr(), |
219 | var req = originalXhr(), |
|
210 | that = this; |
220 | that = this; |
|
211 | if (req && typeof req.upload === 'object' && that.progressUpload !== undefined) { |
221 | if (req && typeof req.upload === 'object' && that.progressUpload !== undefined) { |
|
212 | req.upload.addEventListener('progress', function (e) { |
222 | req.upload.addEventListener('progress', function (e) { |
|
213 | that.progressUpload(e); |
223 | that.progressUpload(e); |
|
214 | }, false); |
224 | }, false); |
|
215 | } |
225 | } |
|
216 | |
226 | |
|
217 | return req; |
227 | return req; |
|
218 | } |
228 | } |
|
219 | }); |
229 | }); |
|
220 | $.trumbowyg.addedXhrProgressEvent = true; |
230 | $.trumbowyg.addedXhrProgressEvent = true; |
|
221 | } |
231 | } |
|
222 | } |
232 | } |
|
223 | })(jQuery); |
233 | })(jQuery); |
|
224 | |
234 | |
|
225 | |
235 | |
|
226 |
|
236 |
|
|
227 | |
237 | |
|
228 | |
238 | |
|
229 | |
239 | |