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