/Tester/Vendor/Trumbowyg/plugins/upload/trumbowyg.upload.js

https://bitbucket.org/MichaelCleverly/seochecker · JavaScript · 223 lines · 77 code · 11 blank · 135 comment · 4 complexity · 900857f9fb9437fff75f803c978b1003 MD5 · raw file

  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. (function ($) {
  14. 'use strict';
  15. var defaultOptions = {
  16. serverPath: './src/plugins/upload/trumbowyg.upload.php',
  17. fileFieldName: 'fileToUpload',
  18. data: [],
  19. headers: {},
  20. xhrFields: {},
  21. urlPropertyName: 'file',
  22. statusPropertyName: 'success',
  23. success: undefined,
  24. error: undefined
  25. };
  26. function getDeep(object, propertyParts) {
  27. var mainProperty = propertyParts.shift(),
  28. otherProperties = propertyParts;
  29. if (object !== null) {
  30. if (otherProperties.length === 0) {
  31. return object[mainProperty];
  32. }
  33. if (typeof object === 'object') {
  34. return getDeep(object[mainProperty], otherProperties);
  35. }
  36. }
  37. return object;
  38. }
  39. addXhrProgressEvent();
  40. $.extend(true, $.trumbowyg, {
  41. langs: {
  42. // jshint camelcase:false
  43. en: {
  44. upload: 'Upload',
  45. file: 'File',
  46. uploadError: 'Error'
  47. },
  48. sk: {
  49. upload: 'Nahrať',
  50. file: 'Súbor',
  51. uploadError: 'Chyba'
  52. },
  53. fr: {
  54. upload: 'Envoi',
  55. file: 'Fichier',
  56. uploadError: 'Erreur'
  57. },
  58. cs: {
  59. upload: 'Nahrát obrázek',
  60. file: 'Soubor',
  61. uploadError: 'Chyba'
  62. },
  63. zh_cn: {
  64. upload: '上传',
  65. file: '文件',
  66. uploadError: '错误'
  67. },
  68. ru: {
  69. upload: 'Загрузка',
  70. file: 'Файл',
  71. uploadError: 'Ошибка'
  72. }
  73. },
  74. // jshint camelcase:true
  75. plugins: {
  76. upload: {
  77. init: function (trumbowyg) {
  78. trumbowyg.o.plugins.upload = $.extend(true, {}, defaultOptions, trumbowyg.o.plugins.upload || {});
  79. var btnDef = {
  80. fn: function () {
  81. trumbowyg.saveRange();
  82. var file,
  83. prefix = trumbowyg.o.prefix;
  84. var $modal = trumbowyg.openModalInsert(
  85. // Title
  86. trumbowyg.lang.upload,
  87. // Fields
  88. {
  89. file: {
  90. type: 'file',
  91. required: true,
  92. attributes: {
  93. accept: 'image/*'
  94. }
  95. },
  96. alt: {
  97. label: 'description',
  98. value: trumbowyg.getRangeText()
  99. }
  100. },
  101. // Callback
  102. function (values) {
  103. var data = new FormData();
  104. data.append(trumbowyg.o.plugins.upload.fileFieldName, file);
  105. trumbowyg.o.plugins.upload.data.map(function (cur) {
  106. data.append(cur.name, cur.value);
  107. });
  108. if ($('.' + prefix + 'progress', $modal).length === 0) {
  109. $('.' + prefix + 'modal-title', $modal)
  110. .after(
  111. $('<div/>', {
  112. 'class': prefix + 'progress'
  113. }).append(
  114. $('<div/>', {
  115. 'class': prefix + 'progress-bar'
  116. })
  117. )
  118. );
  119. }
  120. $.ajax({
  121. url: trumbowyg.o.plugins.upload.serverPath,
  122. headers: trumbowyg.o.plugins.upload.headers,
  123. xhrFields: trumbowyg.o.plugins.upload.xhrFields,
  124. type: 'POST',
  125. data: data,
  126. cache: false,
  127. dataType: 'json',
  128. processData: false,
  129. contentType: false,
  130. progressUpload: function (e) {
  131. $('.' + prefix + 'progress-bar').stop().animate({
  132. width: Math.round(e.loaded * 100 / e.total) + '%'
  133. }, 200);
  134. },
  135. success: function (data) {
  136. if (trumbowyg.o.plugins.upload.success) {
  137. trumbowyg.o.plugins.upload.success(data, trumbowyg, $modal, values);
  138. } else {
  139. if (!!getDeep(data, trumbowyg.o.plugins.upload.statusPropertyName.split('.'))) {
  140. var url = getDeep(data, trumbowyg.o.plugins.upload.urlPropertyName.split('.'));
  141. trumbowyg.execCmd('insertImage', url);
  142. $('img[src="' + url + '"]:not([alt])', trumbowyg.$box).attr('alt', values.alt);
  143. setTimeout(function () {
  144. trumbowyg.closeModal();
  145. }, 250);
  146. trumbowyg.$c.trigger('tbwuploadsuccess', [trumbowyg, data, url]);
  147. } else {
  148. trumbowyg.addErrorOnModalField(
  149. $('input[type=file]', $modal),
  150. trumbowyg.lang[data.message]
  151. );
  152. trumbowyg.$c.trigger('tbwuploaderror', [trumbowyg, data]);
  153. }
  154. }
  155. },
  156. error: trumbowyg.o.plugins.upload.error || function () {
  157. trumbowyg.addErrorOnModalField(
  158. $('input[type=file]', $modal),
  159. trumbowyg.lang.uploadError
  160. );
  161. trumbowyg.$c.trigger('tbwuploaderror', [trumbowyg]);
  162. }
  163. });
  164. }
  165. );
  166. $('input[type=file]').on('change', function (e) {
  167. try {
  168. // If multiple files allowed, we just get the first.
  169. file = e.target.files[0];
  170. } catch (err) {
  171. // In IE8, multiple files not allowed
  172. file = e.target.value;
  173. }
  174. });
  175. }
  176. };
  177. trumbowyg.addBtnDef('upload', btnDef);
  178. }
  179. }
  180. }
  181. });
  182. function addXhrProgressEvent() {
  183. if (!$.trumbowyg && !$.trumbowyg.addedXhrProgressEvent) { // Avoid adding progress event multiple times
  184. var originalXhr = $.ajaxSettings.xhr;
  185. $.ajaxSetup({
  186. xhr: function () {
  187. var req = originalXhr(),
  188. that = this;
  189. if (req && typeof req.upload === 'object' && that.progressUpload !== undefined) {
  190. req.upload.addEventListener('progress', function (e) {
  191. that.progressUpload(e);
  192. }, false);
  193. }
  194. return req;
  195. }
  196. });
  197. $.trumbowyg.addedXhrProgressEvent = true;
  198. }
  199. }
  200. })(jQuery);