files.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. var bind = Ember.run.bind;
  20. App.FilesController = Ember.ArrayController.extend({
  21. actions:{
  22. moveFile:function (opt,fileArg) {
  23. var src, title,
  24. file = fileArg || this.get('selectedFiles.firstObject'),
  25. moving = this.get('movingFile');
  26. if (opt == 'cut') {
  27. src = file.toJSON({includeId: true});
  28. src = Em.merge(src,{name:file.get('name'),path:file.get('path')});
  29. this.set('movingFile',src);
  30. }
  31. if (opt == 'move') {
  32. this.store.move(moving.path,[this.get('path'),moving.name].join('/').replace('//','/'))
  33. .then(bind(this,this.set,'movingFile',null),bind(this,this.throwAlert));
  34. }
  35. if (opt == 'cancel') {
  36. this.set('movingFile',null);
  37. }
  38. },
  39. showRenameInput:function () {
  40. this.toggleProperty('isRenaming');
  41. },
  42. renameDir:function (path,newName) {
  43. var _this = this,
  44. basedir = path.substring(0,path.lastIndexOf('/')+1);
  45. newPath = basedir + newName;
  46. if (path === newPath) {
  47. return false;
  48. }
  49. this.store.listdir(basedir).then(function (listdir) {
  50. var recordExists = listdir.isAny('id',newPath);
  51. listdir.forEach(function (file) {
  52. _this.store.unloadRecord(file);
  53. });
  54. if (recordExists) {
  55. return _this.throwAlert({message:newPath + ' already exists.'});
  56. }
  57. return _this.store.move(path,newPath);
  58. }).then(function (newDir) {
  59. if (newDir) {
  60. _this.store.unloadRecord(newDir);
  61. _this.set('path',newPath);
  62. }
  63. }).catch(bind(this,this.throwAlert));
  64. },
  65. deleteFile:function (deleteForever) {
  66. var self = this,
  67. selected = this.get('selectedFiles'),
  68. moveToTrash = !deleteForever;
  69. if (this.get('content.meta.writeAccess')) {
  70. selected.forEach(function (file) {
  71. self.store.remove(file,moveToTrash).then(null,bind(self,self.deleteErrorCallback,file));
  72. });
  73. } else {
  74. this.throwAlert({message:'Permission denied'});
  75. }
  76. },
  77. download:function (option) {
  78. var files = this.get('selectedFiles').filterBy('readAccess',true);
  79. this.store.linkFor(files,option).then(function (link) {
  80. window.location.href = link;
  81. });
  82. },
  83. mkdir:function (newDirName) {
  84. this.store.mkdir(newDirName)
  85. .then(bind(this,this.mkdirSuccessCalback),bind(this,this.throwAlert));
  86. },
  87. upload:function (opt) {
  88. if (opt === 'open') {
  89. this.set('isUploading',true);
  90. }
  91. if (opt === 'close') {
  92. this.set('isUploading',false);
  93. }
  94. },
  95. sort:function (pr) {
  96. var currentProperty = this.get('sortProperties');
  97. if (pr == currentProperty[0] || pr == 'toggle') {
  98. this.toggleProperty('sortAscending');
  99. } else{
  100. this.set('sortProperties',[pr]);
  101. this.set('sortAscending',true);
  102. }
  103. },
  104. confirmChmod:function (file) {
  105. this.store
  106. .chmod(file)
  107. .then(null,Em.run.bind(this,this.chmodErrorCallback,file));
  108. },
  109. clearSearchField:function () {
  110. this.set('searchString','');
  111. }
  112. },
  113. init:function () {
  114. if (App.testing) {
  115. return this._super();
  116. }
  117. var controller = this;
  118. var adapter = controller.store.adapterFor('file');
  119. var url = adapter.buildURL('upload');
  120. this.uploader.set('url',url);
  121. this.uploader.on('didUpload', function (payload) {
  122. controller.store.pushPayload('file', {'file': payload });
  123. });
  124. this._super();
  125. },
  126. sortProperties: ['name'],
  127. sortAscending: true,
  128. needs: ["file"],
  129. movingFile:null,
  130. uploader:App.Uploader,
  131. isRenaming:false,
  132. isUploading:false,
  133. queryParams: ['path'],
  134. path: '/',
  135. isRootDir:Ember.computed.equal('path', '/'),
  136. hideMoving:function () {
  137. return (this.movingFile)?[this.path,this.movingFile.name].join('/').replace('//','/')===this.movingFile.path:false;
  138. }.property('movingFile','path'),
  139. currentDir:function () {
  140. return this.get('path').split('/').get('lastObject') || '/';
  141. }.property('path'),
  142. selectedOne:Ember.computed.equal('selectedFiles.length', 1),
  143. isSelected:Ember.computed.gt('selectedFiles.length', 0),
  144. selectedFiles:function () {
  145. return this.get('content').filterBy('selected', true);
  146. }.property('content.@each.selected'),
  147. canConcat:function () {
  148. return this.get('selectedFiles').filterProperty('isDirectory').get('length')===0;
  149. }.property('selectedFiles.length'),
  150. isSortPropertyEqualsDate: function() {
  151. return this.get('sortProperties').get('firstObject') === 'date';
  152. }.property('sortProperties.firstObject'),
  153. searchString:'',
  154. fileList: function () {
  155. var fileList = this.get('arrangedContent');
  156. var search = this.get('searchString');
  157. return (search)?fileList.filter(function (file) {
  158. return !!file.get('name').match(search);
  159. }):fileList;
  160. }.property('arrangedContent','searchString'),
  161. mkdirSuccessCalback:function (newDir) {
  162. if (newDir.get('path') != [this.get('path'),newDir.get('name')].join('/')){
  163. newDir.unloadRecord();
  164. newDir.store.listdir(this.get('path'));
  165. }
  166. },
  167. clearSearch:function () {
  168. this.set('searchString','');
  169. }.observes('path'),
  170. deleteErrorCallback:function (record,error) {
  171. this.model.pushRecord(record);
  172. this.throwAlert(error);
  173. },
  174. chmodErrorCallback:function (record,error) {
  175. record.rollback();
  176. this.throwAlert({message:'Permissions change failed'});
  177. },
  178. throwAlert:function (error) {
  179. this.send('showAlert',error);
  180. },
  181. showSpinner:function () {
  182. this.set('isLoadingFiles',true);
  183. },
  184. hideSpinner:function () {
  185. this.set('isLoadingFiles',false);
  186. }
  187. });