uploader.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.FileUploaderComponent = Ember.Component.extend({
  20. didInsertElement:function () {
  21. var _this = this;
  22. this.uploader.reopen({
  23. sendAlert:function (e) {
  24. _this.sendAction('alert',e);
  25. }
  26. });
  27. this.fileInput.reopen({
  28. filesDidChange: function() {
  29. var files = this.get('files');
  30. if (!files) {
  31. this.set('parentView.files',null);
  32. this.set('parentView.controlInput.value','');
  33. this.set('value','');
  34. return;
  35. }
  36. var numFiles = files ? files.length : 1;
  37. var label = this.get('value').replace(/\\/g, '/').replace(/.*\//, '');
  38. var log = numFiles > 1 ? numFiles + ' files selected' : label;
  39. this.set('parentView.controlInput.value',log);
  40. this.set('parentView.files',files);
  41. }.observes('files')
  42. });
  43. },
  44. actions:{
  45. upload:function () {
  46. this.uploadFile();
  47. },
  48. clear:function () {
  49. this.set('fileInput.files',null);
  50. }
  51. },
  52. uploader: null,
  53. layoutName:'components/uploader',
  54. path:'',
  55. info:'',
  56. files:null,
  57. isFiles:function () {
  58. return !this.get('files.length');
  59. }.property('files'),
  60. uploadFile:function () {
  61. var path = this.get('path');
  62. var uploader = this.get('uploader');
  63. var uploadBtn = Ladda.create(this.uploadButton.get('element'));
  64. var reset = function () {
  65. uploadBtn.stop();
  66. this.send('clear');
  67. };
  68. if (!uploader.get('isUploading')) {
  69. if (!Ember.isEmpty(this.get('files'))) {
  70. var file = this.get('files')[0];
  71. uploadBtn.start();
  72. uploader.on('progress',function (e) {
  73. uploadBtn.setProgress(e.percent/100);
  74. });
  75. uploader.upload(file,{path:path}).finally(Em.run.bind(this,reset));
  76. }
  77. }
  78. },
  79. uploadButton: Em.View.createWithMixins(Ember.TargetActionSupport, {
  80. tagName:'button',
  81. target: Ember.computed.alias('controller'),
  82. classNames:['btn','ladda-button'],
  83. classNameBindings:['isFiles:hide','target.isError:btn-danger:btn-success'],
  84. attributeBindings: ["data-style","data-size"],
  85. action:'upload',
  86. click: function() {
  87. this.triggerAction();
  88. }
  89. }),
  90. fileInput : Ember.TextField.create({
  91. type: 'file',
  92. attributeBindings: ['multiple'],
  93. multiple: false,
  94. files:null,
  95. change: function(e) {
  96. var input = e.target;
  97. if (!Ember.isEmpty(input.files)) {
  98. this.set('files', input.files);
  99. }
  100. }
  101. }),
  102. controlInput:Ember.TextField.create({
  103. readonly:true,
  104. classNames:['form-control']
  105. })
  106. });