diff options
Diffstat (limited to 'apps/files/js/filelist.js')
-rw-r--r-- | apps/files/js/filelist.js | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js index 3f0ee932d1e..c52e414e3a7 100644 --- a/apps/files/js/filelist.js +++ b/apps/files/js/filelist.js @@ -9,6 +9,9 @@ */ (function() { + + var TEMPLATE_ADDBUTTON = '<a href="#" class="button new" title="{{addText}}"><img src="{{iconUrl}}"></img></a>'; + /** * @class OCA.Files.FileList * @classdesc @@ -220,6 +223,8 @@ this.$el.find('#controls').prepend(this.breadcrumb.$el); + this._renderNewButton(); + this.$el.find('thead th .columntitle').click(_.bind(this._onClickHeader, this)); this._onResize = _.debounce(_.bind(this._onResize, this), 100); @@ -262,6 +267,12 @@ * Destroy / uninitialize this instance. */ destroy: function() { + if (this._newFileMenu) { + this._newFileMenu.remove(); + } + if (this._newButton) { + this._newButton.remove(); + } // TODO: also unregister other event handlers this.fileActions.off('registerAction', this._onFileActionsUpdated); this.fileActions.off('setDefault', this._onFileActionsUpdated); @@ -1730,6 +1741,106 @@ form.trigger('submit'); }); }, + + /** + * Create an empty file inside the current directory. + * + * @param {string} name name of the file + * + * @return {Promise} promise that will be resolved after the + * file was created + * + * @since 8.2 + */ + createFile: function(name) { + var self = this; + var deferred = $.Deferred(); + var promise = deferred.promise(); + + OCA.Files.Files.isFileNameValid(name); + name = this.getUniqueName(name); + + if (this.lastAction) { + this.lastAction(); + } + + $.post( + OC.generateUrl('/apps/files/ajax/newfile.php'), + { + dir: this.getCurrentDirectory(), + filename: name + }, + function(result) { + if (result.status === 'success') { + self.add(result.data, {animate: true, scrollTo: true}); + deferred.resolve(result.status, result.data); + } else { + if (result.data && result.data.message) { + OC.Notification.showTemporary(result.data.message); + } else { + OC.Notification.showTemporary(t('core', 'Could not create file')); + } + deferred.reject(result.status, result.data); + } + } + ); + + return promise; + }, + + /** + * Create a directory inside the current directory. + * + * @param {string} name name of the directory + * + * @return {Promise} promise that will be resolved after the + * directory was created + * + * @since 8.2 + */ + createDirectory: function(name) { + var self = this; + var deferred = $.Deferred(); + var promise = deferred.promise(); + + OCA.Files.Files.isFileNameValid(name); + name = this.getUniqueName(name); + + if (this.lastAction) { + this.lastAction(); + } + + $.post( + OC.generateUrl('/apps/files/ajax/newfolder.php'), + { + dir: this.getCurrentDirectory(), + foldername: name + }, + function(result) { + if (result.status === 'success') { + self.add(result.data, {animate: true, scrollTo: true}); + deferred.resolve(result.status, result.data); + } else { + if (result.data && result.data.message) { + OC.Notification.showTemporary(result.data.message); + } else { + OC.Notification.showTemporary(t('core', 'Could not create folder')); + } + deferred.reject(result.status); + } + } + ); + + return promise; + }, + + /** + * Returns whether the given file name exists in the list + * + * @param {string} file file name + * + * @return {bool} true if the file exists in the list, false otherwise + */ inList:function(file) { return this.findFileEl(file).length; }, @@ -2391,6 +2502,47 @@ }); }, + _renderNewButton: function() { + // if an upload button (legacy) already exists, skip + if ($('#controls .button.upload').length) { + return; + } + if (!this._addButtonTemplate) { + this._addButtonTemplate = Handlebars.compile(TEMPLATE_ADDBUTTON); + } + var $newButton = $(this._addButtonTemplate({ + addText: t('files', 'New'), + iconUrl: OC.imagePath('core', 'actions/add') + })); + + $('#controls .actions').prepend($newButton); + $newButton.tooltip({'placement': 'bottom'}); + + $newButton.click(_.bind(this._onClickNewButton, this)); + this._newButton = $newButton; + }, + + _onClickNewButton: function(event) { + var $target = $(event.target); + if (!$target.hasClass('.button')) { + $target = $target.closest('.button'); + } + this._newButton.tooltip('hide'); + event.preventDefault(); + if ($target.hasClass('disabled')) { + return false; + } + if (!this._newFileMenu) { + this._newFileMenu = new OCA.Files.NewFileMenu({ + fileList: this + }); + $('body').append(this._newFileMenu.$el); + } + this._newFileMenu.showAt($target); + + return false; + }, + /** * Register a tab view to be added to all views */ |