Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 KB | None | 0 0
  1. /* *
  2. * Bixie Printshop
  3. * bix_upload_uikit.js
  4. * Created on 21-12-2014 00:58
  5. *
  6. * @author Matthijs
  7. * @copyright Copyright (C)2014 Bixie.nl
  8. *
  9. */
  10.  
  11. (function (addon) {
  12. "use strict";
  13.  
  14. var component;
  15.  
  16. if (jQuery && jQuery.UIkit) {
  17. component = addon(jQuery, jQuery.UIkit);
  18. }
  19.  
  20. if (typeof define == "function" && define.amd) {
  21. define("uikit-bixupload_uikit", ["uikit"], function () {
  22. return component || addon(jQuery, jQuery.UIkit);
  23. });
  24. }
  25.  
  26.  
  27. })(function ($, UI) {
  28.  
  29. UI.component('bixupload_uikit', {
  30.  
  31. defaults: {
  32. debug: false,
  33. uploadID: '',
  34. uploadType: 'order',
  35. orderFilelimit: 0,
  36. selectors: {
  37. progressbar: '.bps-upload_uikit-progress',
  38. processbar: '.bps-upload_uikit-process',
  39. select: 'input[type=file]',
  40. drop: '.bps-upload_uikit-drop'
  41. },
  42. fileDetails: {
  43. showDate: true,
  44. allowDelete: true
  45. },
  46. deleteUrl: '/index.php?option=com_bixprintshop&format=raw&task=order.deleteFile',
  47. uploader: {
  48. action: '/index.php?option=com_bixprintshop&format=raw&task=order.upload',
  49. allow: '*.(pdf|jpg|jpeg|gif|png)',
  50. type: 'json',
  51. filelimit: false,
  52. sizelimit: 134217728, //128Mb
  53. params: {
  54. settingsKey: 'plugins.upload_uikit',
  55. uploadID: '',
  56. uploadType: 'order',
  57. orderID: 0,
  58. sendMail: 0
  59. }
  60. },
  61. uploadTemplate: '<li>\
  62. {{#hasImage}}\
  63. <div class="uk-float-right uk-thumbnail">\
  64. <img src="{{thumb.url}}" width="{{thumb.width}}" height="{{thumb.height}}" alt="{{fileName}}"/></div>\
  65. {{/hasImage}}\
  66. <strong>{{fileName}}</strong><br/>\
  67. <span>{{sizeText}}</span><br/>\
  68. {{#showDate}}\
  69. <span>{{dateText}}</span><br/>\
  70. {{/showDate}}\
  71. <a class="uk-button uk-button-mini" href="/{{download}}">\
  72. <i class="uk-icon-download uk-margin-small-right"></i>Downloaden</a>\
  73. {{#allowDelete}}\
  74. <button class="uk-button uk-button-mini uk-button-danger" type="button" data-bix-upload_uikit-button="delete">\
  75. <i class="uk-icon-download uk-margin-small-right"></i>Verwijderen</button>\
  76. {{/allowDelete}}\
  77. </li>'
  78. },
  79.  
  80. boot: function () {
  81. // init code
  82. UI.ready(function (context) {
  83. $("[data-bix-upload_uikit]", context).each(function () {
  84. var $ele = $(this);
  85. if (!$ele.data("bixupload_uikit")) {
  86. UI.bixupload_uikit($ele, UI.Utils.options($ele.attr('data-bix-upload_uikit')));
  87. }
  88. });
  89.  
  90. });
  91. },
  92.  
  93. init: function () {
  94. var $this = this,
  95. progressbar = this.find(this.options.selectors.progressbar),
  96. processbar = this.find(this.options.selectors.processbar),
  97. bar = progressbar.find('.uk-progress-bar'),
  98. settings = $.extend(true, $this.options.uploader, {
  99.  
  100. notallowed: function(file, settings) {
  101. var msg = UI.bixLang._('notAllowedExtension')
  102. .replace('{file}', file.name)
  103. .replace('{extensions}', UI.bixLang._('allowedExtensionsString'));
  104. UI.notify(msg, {status: 'warning'});
  105. },
  106.  
  107. before: function(settings, files) {
  108. if($this.options.uploader.sizelimit > 0 && files[0].size > $this.options.uploader.sizelimit) {
  109. var msg = UI.bixLang._('fileTooLarge')
  110. .replace('{file}', files[0].name)
  111. .replace('{sizeLimit}', UI.bixTools.formatFileSize($this.options.uploader.sizelimit));
  112. UI.notify(msg, {status: 'warning'});
  113. return false;
  114. }
  115. },
  116.  
  117. loadstart: function() {
  118. bar.css("width", "0%").text("0%");
  119. progressbar.removeClass("uk-hidden");
  120. },
  121.  
  122. progress: function(percent) {
  123. percent = Math.ceil(percent);
  124. if (percent == 100) {
  125. processbar.removeClass("uk-hidden")
  126. }
  127. bar.css("width", percent+"%").text(percent+"%");
  128. },
  129.  
  130. complete: function(response) {
  131. $this.uploadCompleted(response)
  132. },
  133.  
  134. allcomplete: function(response) {
  135.  
  136. bar.css("width", "100%").text("100%");
  137.  
  138. setTimeout(function(){
  139. progressbar.addClass("uk-hidden");
  140. processbar.addClass("uk-hidden");
  141. }, 250);
  142.  
  143. }
  144. });
  145.  
  146. var select = UI.uploadSelect($(this.options.selectors.select), settings),
  147. drop = UI.uploadDrop($(this.options.selectors.drop), settings);
  148.  
  149. //requestvars
  150. this.options.uploader.params.uploadID = this.options.uploadID;
  151. this.options.uploader.params.uploadType = this.options.uploadType;
  152.  
  153. //bestanden
  154. this.uploadTemplate = $('script[type="text/uploadTemplate"]').text();
  155. this.uploadTemplate = UI.Utils.template(this.uploadTemplate || this.options.uploadTemplate);
  156. this.fileList = this.find('[data-bix-upload_uikit-bestanden]');
  157. this.bestandenData = UI.Utils.options(this.fileList.data('bix-upload_uikit-bestanden'));
  158. this.bestandenData.bestanden.forEach(function (uploadFile) {
  159. $this.fileList.append($this.getUploadHtml(uploadFile));
  160. });
  161. this.checkFileCount();
  162. //bestanden events
  163. this.fileList.on('click','[data-bix-upload_uikit-button]', function (e) {
  164. var $button = $(e.target), task = $button.data('bix-upload_uikit-button'),
  165. fileID = $button.closest('[data-bix-upload_uikit-fileid]').data('bix-upload_uikit-fileid');
  166. switch (task) {
  167. case 'delete':
  168. $this.deleteFile($button, fileID);
  169. break;
  170. default:
  171. $this.trigger('clicked.bps.upload.' + task, [$this.options.uploader.params.orderID, $this.options.uploadID, fileID]);
  172. break;
  173. }
  174.  
  175. })
  176.  
  177. },
  178. getUploadHtml: function (uploadFile) {
  179. return this.uploadTemplate($.extend(true, this.options.fileDetails, uploadFile))
  180. .replace(new RegExp('\/\/','g'),'/'); //filter mystery slash vanuit DOM templates
  181. },
  182. uploadCompleted: function (response) {
  183. if (response.success) {
  184. response.hasImage = response.thumb.url != '';
  185. response.isThumb = response.thumb.lbox != '';
  186. response.isIcon = response.thumb.isIcon;
  187. this.fileList.append(this.getUploadHtml(response));
  188. this.checkFileCount();
  189. } else {
  190. UI.notify({message: response.error, status: 'danger'});
  191. }
  192. },
  193. deleteFile: function ($button, fileID) {
  194. var $this = this;
  195. UI.modal.confirm('<h3 class="uk-modal-header">' + UI.bixLang._('verwijderBestandTitle') + '</h3>' + UI.bixLang._('verwijderBestandText'), function () {
  196. $button.find('i').addClass('uk-icon-spin');
  197.  
  198. this.ajaxReq = $.ajax({
  199. type: 'POST',
  200. dataType: 'json',
  201. url: $this.options.deleteUrl,
  202. data: {id: fileID, orderID: $this.options.uploader.params.orderID}
  203. })
  204. .done(function (response) {
  205. if (response.success) {
  206. var fileHolder = $this.find('[data-bix-upload_uikit-fileid=' + fileID + ']').slideUp();
  207. setTimeout(function () {
  208. fileHolder.parent().append('<div class="uk-alert uk-alert-success" data-uk-alert>' +
  209. '<a href="" class="uk-alert-close uk-close"></a>' +
  210. UI.bixTools.icon('trash-o') + UI.bixLang._('verwijderBestandDeleted') + '</div>');
  211. fileHolder.remove();
  212. $this.checkFileCount();
  213. }, 500);
  214. }
  215. })
  216. .fail(function (jqXHR, textStatus) {
  217. if (textStatus !== 'abort') {
  218. UI.notify({message: UI.bixLang._('foutRequest'), status: 'danger'});
  219. }
  220. })
  221. .always(function (response) {
  222. $button.find('i').removeClass('uk-icon-spin');
  223. if (response.messages) {
  224. UI.bixTools.showNotifications(response.messages);
  225. }
  226. });
  227.  
  228.  
  229. });
  230. },
  231. checkFileCount: function () {
  232. if (this.options.orderFilelimit > 0) {
  233. var functionName = this.fileList.find('[data-bix-upload_uikit-fileid]').length >= this.options.orderFilelimit ?
  234. 'addClass' : 'removeClass';
  235. this.find(this.options.selectors.drop)[functionName]('uk-hidden')
  236. }
  237. }
  238. });
  239.  
  240. return UI.bixupload_uikit;
  241. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement