/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var App = require('app'); App.FilesController = Ember.ArrayController.extend({ actions:{ moveFile:function (opt,file) { var src, title, self, file = file || this.get('selectedFiles.firstObject'), moving = this.get('movingFile'); if (opt == 'cut') { src = file.toJSON({includeId: true}); src = Em.merge(src,{name:file.get('name'),path:file.get('path')}) this.set('movingFile',src); }; if (opt == 'move') { self = this; this.store.move(moving.path,[this.get('path'),moving.name].join('/').replace('//','/')) .then(function () { self.set('movingFile',null); }); }; if (opt == 'cancel') { this.set('movingFile',null); }; }, showRenameInput:function () { this.toggleProperty('isRenaming'); }, renameDir:function (path,newName) { var self = this, basedir = path.substring(0,path.lastIndexOf('/')+1); newPath = basedir + newName; if (path === newPath) { return false; }; this.store.listdir(basedir).then(function (listdir) { var recordExists = listdir.isAny('id',newPath); listdir.forEach(function (file) { self.store.unloadRecord(file); }); if (recordExists) { return self.send('showAlert',{message:newPath + ' already exists.'}); }; self.store.move(path,newPath).then(function (newDir) { self.store.unloadRecord(newDir); self.set('path',newPath); }); }); }, deleteFile:function () { var self = this; var selected = this.get('selectedFiles'); selected.forEach(function (file) { self.store.remove(file); }); }, download:function (option) { var files = this.get('selectedFiles'); this.store.linkFor(files,option).then(function (link) { window.location.href = link; }); }, mkdir:function (opt) { var name,self,newDir; if (opt === 'edit') { this.set('isMkdir',true); }; if (opt === 'cancel') { this.set('newDirName',''); this.set('isMkdir',false); }; if (opt === 'confirm') { self = this; name = this.get('newDirName'); if (Em.isEmpty(name)) { return false; } newDir = [this.get('path'),name].join('/').replace('//','/'); this.store.mkdir(newDir).then(function () { self.set('newDirName',''); self.set('isMkdir',false); }); }; }, upload:function (opt) { if (opt === 'open') { this.set('isUploading',true); }; if (opt === 'close') { this.set('isUploading',false); }; }, sort:function (pr) { var currentProperty = this.get('sortProperties'); if (pr == currentProperty[0] || pr == 'toggle') { this.toggleProperty('sortAscending'); } else{ this.set('sortProperties',[pr]); this.set('sortAscending',true); }; } }, init:function () { var controller = this; var adapter = controller.store.adapterFor('file'); var url = adapter.buildURL('upload'); this.uploader.set('url',url); this.uploader.on('didUpload', function(e) { controller.store.pushPayload('file',{file:e}); }); }, sortProperties: ['name'], sortAscending: true, needs: ["file"], movingFile:null, uploader:App.Uploader, isRenaming:false, isRemoving:false, isMkdir:false, isUploading:false, newDirName:'', queryParams: ['path'], path: '/', isRootDir:Ember.computed.equal('path', '/'), hideMoving:function () { return (this.movingFile)?[this.path,this.movingFile.name].join('/').replace('//','/')===this.movingFile.path:false; }.property('movingFile','path'), currentDir:function () { var splitpath = this.get('path').split('/'); return splitpath.get(splitpath.length-1) || '/'; }.property('path'), selectedOne:Ember.computed.equal('selectedFiles.length', 1), isSelected:Ember.computed.gt('selectedFiles.length', 0), selectedFiles:Ember.computed.filterBy('content', 'selected', true), canConcat:function () { return this.get('selectedFiles').filterProperty('isDirectory').get('length')==0; }.property('selectedFiles.length'), fileList: Ember.computed.alias('arrangedContent') }); App.FilesAlertController = Em.ObjectController.extend({ content:null, output:function () { var error = this.get('content'),output; if (error instanceof Em.Error) { output = error; } else { output = {status:error.status, message:error.statusText||error.message}; }; return output; }.property('content') });