scratch – Blame information for rev 25

Subversion Repositories:
Rev:
Rev Author Line No. Line
25 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: 'Ошибка'
79 }
80 },
81 // jshint camelcase:true
82  
83 plugins: {
84 upload: {
85 init: function (trumbowyg) {
86 trumbowyg.o.plugins.upload = $.extend(true, {}, defaultOptions, trumbowyg.o.plugins.upload || {});
87 var btnDef = {
88 fn: function () {
89 trumbowyg.saveRange();
90  
91 var file,
92 prefix = trumbowyg.o.prefix;
93  
94 var $modal = trumbowyg.openModalInsert(
95 // Title
96 trumbowyg.lang.upload,
97  
98 // Fields
99 {
100 file: {
101 type: 'file',
102 required: true,
103 attributes: {
104 accept: 'image/*'
105 }
106 },
107 alt: {
108 label: 'description',
109 value: trumbowyg.getRangeText()
110 }
111 },
112  
113 // Callback
114 function (values) {
115 var data = new FormData();
116 data.append(trumbowyg.o.plugins.upload.fileFieldName, file);
117  
118 trumbowyg.o.plugins.upload.data.map(function (cur) {
119 data.append(cur.name, cur.value);
120 });
121  
122 if ($('.' + prefix + 'progress', $modal).length === 0) {
123 $('.' + prefix + 'modal-title', $modal)
124 .after(
125 $('<div/>', {
126 'class': prefix + 'progress'
127 }).append(
128 $('<div/>', {
129 'class': prefix + 'progress-bar'
130 })
131 )
132 );
133 }
134  
135 $.ajax({
136 url: trumbowyg.o.plugins.upload.serverPath,
137 headers: trumbowyg.o.plugins.upload.headers,
138 xhrFields: trumbowyg.o.plugins.upload.xhrFields,
139 type: 'POST',
140 data: data,
141 cache: false,
142 dataType: 'json',
143 processData: false,
144 contentType: false,
145  
146 progressUpload: function (e) {
147 $('.' + prefix + 'progress-bar').stop().animate({
148 width: Math.round(e.loaded * 100 / e.total) + '%'
149 }, 200);
150 },
151  
152 success: function (data) {
153 if (trumbowyg.o.plugins.upload.success) {
154 trumbowyg.o.plugins.upload.success(data, trumbowyg, $modal, values);
155 } else {
156 if (!!getDeep(data, trumbowyg.o.plugins.upload.statusPropertyName.split('.'))) {
157 var url = getDeep(data, trumbowyg.o.plugins.upload.urlPropertyName.split('.'));
158 trumbowyg.execCmd('insertImage', url);
159 $('img[src="' + url + '"]:not([alt])', trumbowyg.$box).attr('alt', values.alt);
160 setTimeout(function () {
161 trumbowyg.closeModal();
162 }, 250);
163 trumbowyg.$c.trigger('tbwuploadsuccess', [trumbowyg, data, url]);
164 } else {
165 trumbowyg.addErrorOnModalField(
166 $('input[type=file]', $modal),
167 trumbowyg.lang[data.message]
168 );
169 trumbowyg.$c.trigger('tbwuploaderror', [trumbowyg, data]);
170 }
171 }
172 },
173  
174 error: trumbowyg.o.plugins.upload.error || function () {
175 trumbowyg.addErrorOnModalField(
176 $('input[type=file]', $modal),
177 trumbowyg.lang.uploadError
178 );
179 trumbowyg.$c.trigger('tbwuploaderror', [trumbowyg]);
180 }
181 });
182 }
183 );
184  
185 $('input[type=file]').on('change', function (e) {
186 try {
187 // If multiple files allowed, we just get the first.
188 file = e.target.files[0];
189 } catch (err) {
190 // In IE8, multiple files not allowed
191 file = e.target.value;
192 }
193 });
194 }
195 };
196  
197 trumbowyg.addBtnDef('upload', btnDef);
198 }
199 }
200 }
201 });
202  
203  
204 function addXhrProgressEvent() {
205 if (!$.trumbowyg && !$.trumbowyg.addedXhrProgressEvent) { // Avoid adding progress event multiple times
206 var originalXhr = $.ajaxSettings.xhr;
207 $.ajaxSetup({
208 xhr: function () {
209 var req = originalXhr(),
210 that = this;
211 if (req && typeof req.upload === 'object' && that.progressUpload !== undefined) {
212 req.upload.addEventListener('progress', function (e) {
213 that.progressUpload(e);
214 }, false);
215 }
216  
217 return req;
218 }
219 });
220 $.trumbowyg.addedXhrProgressEvent = true;
221 }
222 }
223 })(jQuery);