2015-08-27 11:22:58 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2014
|
|
|
|
|
*
|
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
|
* or later.
|
|
|
|
|
*
|
|
|
|
|
* See the COPYING-README file.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* global Files */
|
|
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct a new NewFileMenu instance
|
|
|
|
|
* @constructs NewFileMenu
|
|
|
|
|
*
|
|
|
|
|
* @memberof OCA.Files
|
|
|
|
|
*/
|
|
|
|
|
var NewFileMenu = OC.Backbone.View.extend({
|
|
|
|
|
tagName: 'div',
|
2017-01-17 10:17:40 +00:00
|
|
|
|
// Menu is opened by default because it's rendered on "add-button" click
|
2017-01-21 19:05:58 +00:00
|
|
|
|
className: 'newFileMenu popovermenu bubble menu open menu-left',
|
2015-08-27 11:22:58 +00:00
|
|
|
|
|
|
|
|
|
events: {
|
|
|
|
|
'click .menuitem': '_onClickAction'
|
|
|
|
|
},
|
|
|
|
|
|
2015-11-19 13:39:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* @type OCA.Files.FileList
|
|
|
|
|
*/
|
|
|
|
|
fileList: null,
|
|
|
|
|
|
2015-08-27 11:22:58 +00:00
|
|
|
|
initialize: function(options) {
|
|
|
|
|
var self = this;
|
|
|
|
|
var $uploadEl = $('#file_upload_start');
|
|
|
|
|
if ($uploadEl.length) {
|
|
|
|
|
$uploadEl.on('fileuploadstart', function() {
|
|
|
|
|
self.trigger('actionPerformed', 'upload');
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
console.warn('Missing upload element "file_upload_start"');
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-19 13:39:41 +00:00
|
|
|
|
this.fileList = options && options.fileList;
|
2015-10-21 21:15:38 +00:00
|
|
|
|
|
|
|
|
|
this._menuItems = [{
|
|
|
|
|
id: 'folder',
|
2017-05-16 19:06:05 +00:00
|
|
|
|
displayName: t('files', 'New folder'),
|
2015-10-21 21:15:38 +00:00
|
|
|
|
templateName: t('files', 'New folder'),
|
|
|
|
|
iconClass: 'icon-folder',
|
|
|
|
|
fileType: 'folder',
|
|
|
|
|
actionHandler: function(name) {
|
2015-11-19 13:39:41 +00:00
|
|
|
|
self.fileList.createDirectory(name);
|
2015-10-21 21:15:38 +00:00
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
OC.Plugins.attach('OCA.Files.NewFileMenu', this);
|
2015-08-27 11:22:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
template: function(data) {
|
2018-10-01 20:40:26 +00:00
|
|
|
|
return OCA.Files.Templates['newfilemenu'](data);
|
2015-08-27 11:22:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Event handler whenever an action has been clicked within the menu
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} event event object
|
|
|
|
|
*/
|
|
|
|
|
_onClickAction: function(event) {
|
|
|
|
|
var $target = $(event.target);
|
|
|
|
|
if (!$target.hasClass('menuitem')) {
|
|
|
|
|
$target = $target.closest('.menuitem');
|
|
|
|
|
}
|
|
|
|
|
var action = $target.attr('data-action');
|
|
|
|
|
// note: clicking the upload label will automatically
|
|
|
|
|
// set the focus on the "file_upload_start" hidden field
|
|
|
|
|
// which itself triggers the upload dialog.
|
|
|
|
|
// Currently the upload logic is still in file-upload.js and filelist.js
|
|
|
|
|
if (action === 'upload') {
|
|
|
|
|
OC.hideMenus();
|
|
|
|
|
} else {
|
|
|
|
|
event.preventDefault();
|
2015-09-30 08:44:59 +00:00
|
|
|
|
this.$el.find('.menuitem.active').removeClass('active');
|
|
|
|
|
$target.addClass('active');
|
2015-08-27 11:22:58 +00:00
|
|
|
|
this._promptFileName($target);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_promptFileName: function($target) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
if ($target.find('form').length) {
|
2018-02-28 11:55:33 +00:00
|
|
|
|
$target.find('input[type=\'text\']').focus();
|
2015-08-27 11:22:58 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// discard other forms
|
|
|
|
|
this.$el.find('form').remove();
|
|
|
|
|
this.$el.find('.displayname').removeClass('hidden');
|
|
|
|
|
|
|
|
|
|
$target.find('.displayname').addClass('hidden');
|
|
|
|
|
|
|
|
|
|
var newName = $target.attr('data-templatename');
|
|
|
|
|
var fileType = $target.attr('data-filetype');
|
2018-10-01 20:40:26 +00:00
|
|
|
|
var $form = $(OCA.Files.Templates['newfilemenu_filename_form']({
|
2015-08-27 11:22:58 +00:00
|
|
|
|
fileName: newName,
|
|
|
|
|
cid: this.cid,
|
|
|
|
|
fileType: fileType
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
//this.trigger('actionPerformed', action);
|
2015-09-02 22:58:03 +00:00
|
|
|
|
$target.append($form);
|
2015-08-27 11:22:58 +00:00
|
|
|
|
|
|
|
|
|
// here comes the OLD code
|
2018-02-28 11:55:33 +00:00
|
|
|
|
var $input = $form.find('input[type=\'text\']');
|
|
|
|
|
var $submit = $form.find('input[type=\'submit\']');
|
2015-08-27 11:22:58 +00:00
|
|
|
|
|
|
|
|
|
var lastPos;
|
|
|
|
|
var checkInput = function () {
|
|
|
|
|
var filename = $input.val();
|
|
|
|
|
try {
|
|
|
|
|
if (!Files.isFileNameValid(filename)) {
|
|
|
|
|
// Files.isFileNameValid(filename) throws an exception itself
|
2015-11-19 13:39:41 +00:00
|
|
|
|
} else if (self.fileList.inList(filename)) {
|
2016-09-30 10:44:49 +00:00
|
|
|
|
throw t('files', '{newName} already exists', {newName: filename}, undefined, {
|
|
|
|
|
escape: false
|
|
|
|
|
});
|
2015-08-27 11:22:58 +00:00
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
$input.attr('title', error);
|
2018-03-05 16:37:05 +00:00
|
|
|
|
$input.tooltip({placement: 'right', trigger: 'manual', 'container': '.newFileMenu'});
|
2017-05-20 16:40:50 +00:00
|
|
|
|
$input.tooltip('fixTitle');
|
2015-08-27 11:22:58 +00:00
|
|
|
|
$input.tooltip('show');
|
|
|
|
|
$input.addClass('error');
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// verify filename on typing
|
|
|
|
|
$input.keyup(function() {
|
|
|
|
|
if (checkInput()) {
|
|
|
|
|
$input.tooltip('hide');
|
|
|
|
|
$input.removeClass('error');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-03-07 16:10:46 +00:00
|
|
|
|
$submit.click(function(event) {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
event.preventDefault();
|
2018-02-28 11:55:33 +00:00
|
|
|
|
$form.submit();
|
|
|
|
|
});
|
|
|
|
|
|
2015-08-27 11:22:58 +00:00
|
|
|
|
$input.focus();
|
|
|
|
|
// pre select name up to the extension
|
|
|
|
|
lastPos = newName.lastIndexOf('.');
|
|
|
|
|
if (lastPos === -1) {
|
|
|
|
|
lastPos = newName.length;
|
|
|
|
|
}
|
|
|
|
|
$input.selectRange(0, lastPos);
|
|
|
|
|
|
|
|
|
|
$form.submit(function(event) {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
if (checkInput()) {
|
2019-01-31 14:30:20 +00:00
|
|
|
|
var newname = $input.val().trim();
|
2015-10-21 21:15:38 +00:00
|
|
|
|
|
|
|
|
|
/* Find the right actionHandler that should be called.
|
|
|
|
|
* Actions is retrieved by using `actionSpec.id` */
|
2018-10-19 09:37:11 +00:00
|
|
|
|
var action = _.filter(self._menuItems, function(item) {
|
2015-10-21 21:15:38 +00:00
|
|
|
|
return item.id == $target.attr('data-action');
|
|
|
|
|
}).pop();
|
|
|
|
|
action.actionHandler(newname);
|
|
|
|
|
|
2015-08-27 11:22:58 +00:00
|
|
|
|
$form.remove();
|
|
|
|
|
$target.find('.displayname').removeClass('hidden');
|
|
|
|
|
OC.hideMenus();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2015-10-21 21:15:38 +00:00
|
|
|
|
* Add a new item menu entry in the “New” file menu (in
|
|
|
|
|
* last position). By clicking on the item, the
|
|
|
|
|
* `actionHandler` function is called.
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} actionSpec item’s properties
|
|
|
|
|
*/
|
|
|
|
|
addMenuEntry: function(actionSpec) {
|
|
|
|
|
this._menuItems.push({
|
|
|
|
|
id: actionSpec.id,
|
|
|
|
|
displayName: actionSpec.displayName,
|
|
|
|
|
templateName: actionSpec.templateName,
|
|
|
|
|
iconClass: actionSpec.iconClass,
|
|
|
|
|
fileType: actionSpec.fileType,
|
|
|
|
|
actionHandler: actionSpec.actionHandler,
|
|
|
|
|
});
|
2015-08-27 11:22:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Renders the menu with the currently set items
|
|
|
|
|
*/
|
|
|
|
|
render: function() {
|
|
|
|
|
this.$el.html(this.template({
|
|
|
|
|
uploadMaxHumanFileSize: 'TODO',
|
2017-05-16 19:06:05 +00:00
|
|
|
|
uploadLabel: t('files', 'Upload file'),
|
2015-10-21 21:15:38 +00:00
|
|
|
|
items: this._menuItems
|
2015-08-27 11:22:58 +00:00
|
|
|
|
}));
|
2018-07-20 22:06:17 +00:00
|
|
|
|
|
|
|
|
|
// Trigger upload action also with keyboard navigation on enter
|
2018-07-21 17:30:58 +00:00
|
|
|
|
this.$el.find('[for="file_upload_start"]').on('keyup', function(event) {
|
|
|
|
|
if (event.key === " " || event.key === "Enter") {
|
|
|
|
|
$('#file_upload_start').trigger('click');
|
|
|
|
|
}
|
2018-07-20 22:06:17 +00:00
|
|
|
|
});
|
2015-08-27 11:22:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Displays the menu under the given element
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} $target target element
|
|
|
|
|
*/
|
|
|
|
|
showAt: function($target) {
|
|
|
|
|
this.render();
|
|
|
|
|
OC.showMenu(null, this.$el);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
OCA.Files.NewFileMenu = NewFileMenu;
|
|
|
|
|
|
|
|
|
|
})();
|