addon/components/ui-uploader.js

  1. import Ember from 'ember';
  2. import { isEmpty } from '@ember/utils';
  3. import $ from 'jquery';
  4. import { A } from '@ember/array';
  5. import { computed } from '@ember/object';
  6. import { htmlSafe } from '@ember/template';
  7. import Component from '@ember/component';
  8.  
  9. import emberUploader from '../utils/ember-uploader';
  10. import { fileObject } from '../utils/file-object';
  11. import layout from '../templates/components/ui-uploader';
  12.  
  13.  
  14.  
  15. /**
  16. ui-uploader component
  17.  
  18. @module components
  19. @namespace components
  20. @class UiUploader
  21. @constructor
  22. */
  23. export default Component.extend({
  24. layout: layout,
  25. /**
  26. * file request ajax setting traditional, by default true
  27. * @property {boolean} traditional
  28. *
  29. */
  30. traditional: true,
  31. actions: {
  32. /**
  33. activate upload action
  34. @event start
  35. @param {Object} obj fileInput instance see {{#crossLink " fileInput"}}{{/crossLink}}
  36. **/
  37. start: function(obj) {
  38. let url = this.get('url'),
  39. self = this;
  40.  
  41. obj.uploader = emberUploader.create({
  42. url: url,
  43. traditional: self.get('traditional')
  44. });
  45.  
  46. obj.uploadPromise = obj.uploader.upload(obj.fileToUpload, this.params);
  47.  
  48. self.sendAction('uploadStart', obj);
  49. obj.set('isUploading', computed.alias('uploader.isUploading'));
  50.  
  51. obj.uploader.on('progress', function(e) {
  52. obj.set('percent', parseInt(e.percent));
  53. self.sendAction('uploadProgress', e);
  54. });
  55.  
  56. obj.uploader.on('didUpload', function(data) {
  57. obj.set('isUploaded', true);
  58. obj.set('data', data);
  59.  
  60. self.sendAction('uploadSuccess', obj);
  61. });
  62. },
  63. /**
  64. deactivate upload action
  65. @event abort
  66. @param {Object} obj fileInput instance see {{#crossLink " fileInput"}}{{/crossLink}}
  67. **/
  68. abort: function(obj) {
  69. this.sendAction('uploadAbort', obj);
  70. if (obj.uploader) {
  71. try {
  72. obj.uploader.abort();
  73. } catch (e) {
  74. Ember.Logger.error(e)
  75. }finally {
  76. this.get('queue').removeObject(obj);
  77. }
  78. } else {
  79. this.get('queue').removeObject(obj);
  80. }
  81. },
  82. /**
  83. delete file from queue
  84. @event
  85. @param {Object} obj fileInput instance see {{#crossLink " fileInput"}}{{/crossLink}}
  86. **/
  87. deleteFile: function(obj) {
  88. this.get('queue').removeObject(obj);
  89. this.sendAction('deleteFile', obj);
  90. }
  91. },
  92. /**
  93. * The upload url
  94. *
  95. * @property url
  96. * @type String
  97. * @default ""
  98. */
  99. url: '',
  100.  
  101. /**
  102. * The root component element
  103. *
  104. * @property tagName
  105. * @type String
  106. * @default "div"
  107. */
  108. tagName: 'div',
  109.  
  110. /**
  111. * A array contain class names apply to root element
  112. *
  113. * @property classNames
  114. * @type Array
  115. * @default ['ui', 'segment']
  116. */
  117. classNames: ['ui', 'segment'],
  118.  
  119. /**
  120. * To allow file autoUpload
  121. *
  122. * @property autoUpload
  123. * @type boolean
  124. * @default true
  125. */
  126. autoUpload: true,
  127.  
  128. /**
  129. * upload file queue
  130. *
  131. * @property queue
  132. * @type Array
  133. * @default []
  134. */
  135. queue: null,
  136.  
  137. /**
  138. * upload multiple file
  139. *
  140. * @property multiple
  141. * @type boolean
  142. * @default false
  143. */
  144. multiple: false,
  145.  
  146. /**
  147. * extra params
  148. *
  149. * @property params
  150. * @type params
  151. * @default null
  152. */
  153. params: null,
  154.  
  155. /**
  156. * file accept
  157. *
  158. * @property accept
  159. * @type String
  160. * @default null
  161. */
  162. accept: 'audio/*,video/*,image/*',
  163.  
  164. /**
  165. * @method didInsertElement
  166. * @observes "didInsertElement" event
  167. * @returns {void}
  168. */
  169. didInsertElement() {
  170. let self = this;
  171. this.$('input').change(function(e) {
  172. let input = e.target;
  173. if (!isEmpty(input.files)) {
  174. for (let i = input.files.length - 1; i >= 0; i--) {
  175. let obj = fileObject.create({
  176. fileToUpload: input.files[i]
  177. });
  178. self.queue.pushObject(obj);
  179. if (self.autoUpload) {
  180. self.send('start', obj);
  181. }
  182. }
  183.  
  184. //$(this).after($(this).clone().val(""));
  185. //empty input files
  186. $(this).val("");
  187. }
  188. });
  189. },
  190. /**
  191. * @function willDestroy empty queue
  192. *
  193. * @returns null
  194. */
  195. willDestroy(){
  196. this._super();
  197. this.queue.clear();//clear input file
  198. },
  199.  
  200. /**
  201. * @function willDestroy empty queue
  202. *
  203. * @returns null
  204. */
  205. init(){
  206. this._super(...arguments);
  207. if(this.queue === null){
  208. this.set('queue', A());
  209. }
  210. },
  211. /**
  212. * @function inputStyle
  213. *
  214. * @returns {string}
  215. */
  216. inputStyle: computed({
  217. get(){
  218. let style_array = [
  219. 'opacity: 0',
  220. 'width:100% !important',
  221. 'overflow:hidden',
  222. 'position:relative',
  223. 'left:-100%',
  224. 'display:block',
  225. ];
  226. return htmlSafe(style_array.join(';'));
  227. }
  228. }),
  229. /**
  230. * @function labelStyle
  231. *
  232. * @returns {string}
  233. */
  234. labelStyle: computed({
  235. get(){
  236. let style_array = [
  237. 'height: 6.25em',
  238. 'line-height: 5.25em',
  239. 'text-align: center',
  240. ];
  241. return htmlSafe(style_array.join(';'));
  242. }
  243. }),
  244. });