files.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.FilesController = Ember.ArrayController.extend({
  20. actions:{
  21. moveFile:function (opt,file) {
  22. var src, title, self,
  23. file = file || this.get('selectedFiles.firstObject'),
  24. moving = this.get('movingFile');
  25. if (opt == 'cut') {
  26. src = file.toJSON({includeId: true});
  27. src = Em.merge(src,{name:file.get('name'),path:file.get('path')})
  28. this.set('movingFile',src);
  29. };
  30. if (opt == 'move') {
  31. self = this;
  32. this.store.move(moving.path,[this.get('path'),moving.name].join('/').replace('//','/'))
  33. .then(function () {
  34. self.set('movingFile',null);
  35. });
  36. };
  37. if (opt == 'cancel') {
  38. this.set('movingFile',null);
  39. };
  40. },
  41. showRenameInput:function () {
  42. this.toggleProperty('isRenaming');
  43. },
  44. renameDir:function (path,newName) {
  45. var self = this,
  46. basedir = path.substring(0,path.lastIndexOf('/')+1);
  47. newPath = basedir + newName;
  48. if (path === newPath) {
  49. return false;
  50. };
  51. this.store.listdir(basedir).then(function (listdir) {
  52. var recordExists = listdir.isAny('id',newPath);
  53. listdir.forEach(function (file) {
  54. self.store.unloadRecord(file);
  55. });
  56. if (recordExists) {
  57. return self.send('showAlert',{message:newPath + ' already exists.'});
  58. };
  59. self.store.move(path,newPath).then(function (newDir) {
  60. self.store.unloadRecord(newDir);
  61. self.set('path',newPath);
  62. });
  63. });
  64. },
  65. deleteFile:function () {
  66. var self = this;
  67. var selected = this.get('selectedFiles');
  68. selected.forEach(function (file) {
  69. self.store.remove(file);
  70. });
  71. },
  72. download:function (option) {
  73. var files = this.get('selectedFiles');
  74. this.store.linkFor(files,option).then(function (link) {
  75. window.location.href = link;
  76. });
  77. },
  78. mkdir:function (opt) {
  79. var name,self,newDir;
  80. if (opt === 'edit') {
  81. this.set('isMkdir',true);
  82. };
  83. if (opt === 'cancel') {
  84. this.set('newDirName','');
  85. this.set('isMkdir',false);
  86. };
  87. if (opt === 'confirm') {
  88. self = this;
  89. name = this.get('newDirName');
  90. if (Em.isEmpty(name)) {
  91. return false;
  92. }
  93. newDir = [this.get('path'),name].join('/').replace('//','/');
  94. this.store.mkdir(newDir).then(function () {
  95. self.set('newDirName','');
  96. self.set('isMkdir',false);
  97. });
  98. };
  99. },
  100. upload:function (opt) {
  101. if (opt === 'open') {
  102. this.set('isUploading',true);
  103. };
  104. if (opt === 'close') {
  105. this.set('isUploading',false);
  106. };
  107. },
  108. sort:function (pr) {
  109. var currentProperty = this.get('sortProperties');
  110. if (pr == currentProperty[0] || pr == 'toggle') {
  111. this.toggleProperty('sortAscending');
  112. } else{
  113. this.set('sortProperties',[pr]);
  114. this.set('sortAscending',true);
  115. };
  116. }
  117. },
  118. init:function () {
  119. var controller = this;
  120. var adapter = controller.store.adapterFor('file');
  121. var url = adapter.buildURL('upload');
  122. this.uploader.set('url',url);
  123. this.uploader.on('didUpload', function(e) {
  124. controller.store.pushPayload('file',{file:e});
  125. });
  126. },
  127. sortProperties: ['name'],
  128. sortAscending: true,
  129. needs: ["file"],
  130. movingFile:null,
  131. uploader:App.Uploader,
  132. isRenaming:false,
  133. isRemoving:false,
  134. isMkdir:false,
  135. isUploading:false,
  136. newDirName:'',
  137. queryParams: ['path'],
  138. path: '/',
  139. isRootDir:Ember.computed.equal('path', '/'),
  140. hideMoving:function () {
  141. return (this.movingFile)?[this.path,this.movingFile.name].join('/').replace('//','/')===this.movingFile.path:false;
  142. }.property('movingFile','path'),
  143. currentDir:function () {
  144. var splitpath = this.get('path').split('/');
  145. return splitpath.get(splitpath.length-1) || '/';
  146. }.property('path'),
  147. selectedOne:Ember.computed.equal('selectedFiles.length', 1),
  148. isSelected:Ember.computed.gt('selectedFiles.length', 0),
  149. selectedFiles:Ember.computed.filterBy('content', 'selected', true),
  150. canConcat:function () {
  151. return this.get('selectedFiles').filterProperty('isDirectory').get('length')==0;
  152. }.property('selectedFiles.length'),
  153. fileList: Ember.computed.alias('arrangedContent')
  154. });
  155. App.FilesAlertController = Em.ObjectController.extend({
  156. content:null,
  157. output:function () {
  158. var error = this.get('content'),output;
  159. if (error instanceof Em.Error) {
  160. output = error;
  161. } else {
  162. output = {status:error.status, message:error.statusText||error.message};
  163. };
  164. return output;
  165. }.property('content')
  166. });