files.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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,{title:file.get('title'),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.title].join('/').replace('//','/'))
  33. .then(function () {
  34. self.set('movingFile',null);
  35. });
  36. };
  37. if (opt == 'cancel') {
  38. this.set('movingFile',null);
  39. };
  40. },
  41. deleteFile:function () {
  42. var self = this;
  43. var selected = this.get('selectedFiles');
  44. selected.forEach(function (file) {
  45. self.store.remove(file);
  46. });
  47. },
  48. download:function (option) {
  49. var files = this.get('selectedFiles');
  50. this.store.linkFor(files,option).then(function (link) {
  51. window.location.href = link;
  52. });
  53. },
  54. mkdir:function (opt) {
  55. var name,self,newDir;
  56. if (opt === 'edit') {
  57. this.set('isMkdir',true);
  58. };
  59. if (opt === 'cancel') {
  60. this.set('newDirName','');
  61. this.set('isMkdir',false);
  62. };
  63. if (opt === 'confirm') {
  64. self = this;
  65. name = this.get('newDirName');
  66. if (Em.isEmpty(name)) {
  67. return false;
  68. }
  69. newDir = [this.get('path'),name].join('/').replace('//','/');
  70. this.store.mkdir(newDir).then(function () {
  71. self.set('newDirName','');
  72. self.set('isMkdir',false);
  73. });
  74. };
  75. },
  76. upload:function (opt) {
  77. if (opt === 'open') {
  78. this.set('isUploading',true);
  79. };
  80. if (opt === 'close') {
  81. this.set('isUploading',false);
  82. };
  83. },
  84. sort:function (pr) {
  85. var currentProperty = this.get('sortProperties');
  86. if (pr == currentProperty[0] || pr == 'toggle') {
  87. this.toggleProperty('sortAscending');
  88. } else{
  89. this.set('sortProperties',[pr]);
  90. this.set('sortAscending',true);
  91. };
  92. }
  93. },
  94. init:function () {
  95. var controller = this;
  96. var adapter = controller.store.adapterFor('file');
  97. var url = adapter.buildURL('upload');
  98. this.uploader.set('url',url);
  99. this.uploader.on('didUpload', function(e) {
  100. controller.store.pushPayload('file',{file:e});
  101. });
  102. },
  103. sortProperties: ['title'],
  104. sortAscending: true,
  105. needs: ["file"],
  106. movingFile:null,
  107. uploader:App.Uploader,
  108. isRemoving:false,
  109. isMkdir:false,
  110. isUploading:false,
  111. newDirName:'',
  112. queryParams: ['path'],
  113. path: '/',
  114. hideMoving:function () {
  115. return (this.movingFile)?[this.path,this.movingFile.title].join('/').replace('//','/')===this.movingFile.path:false;
  116. }.property('movingFile','path'),
  117. currentDir:function () {
  118. var splitpath = this.get('path').split('/');
  119. return splitpath.get(splitpath.length-1) || '/';
  120. }.property('path'),
  121. selectedOne:function () {
  122. return this.get('selectedFiles.length') == 1;
  123. }.property('selectedFiles'),
  124. isSelected:function () {
  125. return this.get('selectedFiles.length') > 0;
  126. }.property('selectedFiles'),
  127. selectedFiles:function () {
  128. return this.get('content').filterProperty('selected',true);
  129. }.property('content.@each.selected'),
  130. canConcat:function () {
  131. return this.get('selectedFiles').filterProperty('isDirectory').get('length')==0;
  132. }.property('selectedFiles'),
  133. fileList:function () {
  134. return this.get('arrangedContent');
  135. }.property('arrangedContent')
  136. });