Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implementation of Add button folder creation and Add button text docu…
…ment creation

Signed-off-by: Kavita Sonawane <[email protected]>
  • Loading branch information
TSI-kavitasonawane authored and szaimen committed Apr 19, 2022
commit 4e448ba6578f49e4c470e5f0be5fcd83d2ec359a
108 changes: 11 additions & 97 deletions apps/files/js/newfilemenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@
iconClass: 'icon-folder',
fileType: 'folder',
actionHandler: function(name) {
self.fileList.createDirectory(name);
const uniqueName = self.fileList.getUniqueName(name);
let tempPromise = self.fileList.createDirectory(uniqueName);
Promise.all([tempPromise]).then(() => {
self.fileList.rename(uniqueName);
});
}
}];

Expand Down Expand Up @@ -90,102 +94,12 @@

_promptFileName: function($target) {
var self = this;

if ($target.find('form').length) {
$target.find('input[type=\'text\']').focus();
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');
var $form = $(OCA.Files.Templates['newfilemenu_filename_form']({
fileName: newName,
cid: this.cid,
fileType: fileType
}));

//this.trigger('actionPerformed', action);
$target.append($form);

// here comes the OLD code
var $input = $form.find('input[type=\'text\']');
var $submit = $form.find('input[type=\'submit\']');

var lastPos;
var checkInput = function () {
// Special handling for the setup template directory
if ($target.attr('data-action') === 'template-init') {
return true;
}

var filename = $input.val();
try {
if (!Files.isFileNameValid(filename)) {
// Files.isFileNameValid(filename) throws an exception itself
} else if (self.fileList.inList(filename)) {
throw t('files', '{newName} already exists', {newName: filename}, undefined, {
escape: false
});
} else {
return true;
}
} catch (error) {
$input.attr('title', error);
$input.tooltip({placement: 'right', trigger: 'manual', container: self.$el});
$input.tooltip('_fixTitle');
$input.tooltip('show');
$input.addClass('error');
}
return false;
};

// verify filename on typing
$input.keyup(function() {
if (checkInput()) {
$input.tooltip('hide');
$input.removeClass('error');
}
});

$submit.click(function(event) {
event.stopPropagation();
event.preventDefault();
$form.submit();
});

$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()) {
var newname = $input.val().trim();

/* Find the right actionHandler that should be called.
* Actions is retrieved by using `actionSpec.id` */
var action = _.filter(self._menuItems, function(item) {
return item.id == $target.attr('data-action');
}).pop();
action.actionHandler(newname);

$form.remove();
$target.find('.displayname').removeClass('hidden');
OC.hideMenus();
}
});
var newname = $target.attr('data-templatename');
var action = _.filter(self._menuItems, function(item) {
return item.id == $target.attr('data-action');
}).pop();
action.actionHandler(newname);
OC.hideMenus();
},

/**
Expand Down
3 changes: 2 additions & 1 deletion apps/files/src/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ templates.forEach((provider, index) => {
iconClass: provider.iconClass || 'icon-file',
fileType: 'file',
actionHandler(name) {
TemplatePicker.open(name, provider)
const fileName = FileList.getUniqueName(name)
TemplatePicker.open(fileName, provider)
},
})
},
Expand Down
35 changes: 9 additions & 26 deletions apps/files/src/views/TemplatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,35 +201,18 @@ export default {
const currentDirectory = getCurrentDirectory()
const fileList = OCA?.Files?.App?.currentFileList

// If the file doesn't have an extension, add the default one
if (this.nameWithoutExt === this.name) {
this.logger.debug('Fixed invalid filename', { name: this.name, extension: this.provider?.extension })
this.name = this.name + this.provider?.extension
}

try {
const fileInfo = await createFromTemplate(
normalize(`${currentDirectory}/${this.name}`),
this.selectedTemplate?.filename,
this.selectedTemplate?.templateType,
)
this.logger.debug('Created new file', fileInfo)

const data = await fileList?.addAndFetchFileInfo(this.name).then((status, data) => data)

const model = new OCA.Files.FileInfoModel(data, {
filesClient: fileList?.filesClient,
})
// Run default action
const fileAction = OCA.Files.fileActions.getDefaultFileAction(fileInfo.mime, 'file', OC.PERMISSION_ALL)
fileAction.action(fileInfo.basename, {
$file: fileList?.findFileEl(this.name),
dir: currentDirectory,
fileList,
fileActions: fileList?.fileActions,
fileInfoModel: model,
const response = await axios.post(generateOcsUrl('apps/files/api/v1/templates/create'), {
filePath: `${currentDirectory}/${this.name}`,
templatePath: this.selectedTemplate?.filename,
templateType: this.selectedTemplate?.templateType,
})

const fileInfo = response.data.ocs.data
this.logger.debug('Created new file', fileInfo)

await fileList?.addAndFetchFileInfo(this.name)
fileList.rename(this.name)
this.close()
} catch (error) {
this.logger.error('Error while creating the new file from template')
Expand Down
42 changes: 2 additions & 40 deletions apps/files/tests/js/newfilemenuSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,41 +62,13 @@ describe('OCA.Files.NewFileMenu', function() {
beforeEach(function() {
createDirectoryStub = sinon.stub(FileList.prototype, 'createDirectory');
menu.$el.find('.menuitem').eq(1).click();
$input = menu.$el.find('form.filenameform input');
});
afterEach(function() {
createDirectoryStub.restore();
});

it('sets default text in field', function() {
// text + submit
expect($input.length).toEqual(2);
expect($input.val()).toEqual('New folder');
});
it('prevents entering invalid file names', function() {
$input.val('..');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.closest('form').submit();

expect(createDirectoryStub.notCalled).toEqual(true);
});
it('prevents entering file names that already exist', function() {
var inListStub = sinon.stub(fileList, 'inList').returns(true);
$input.val('existing.txt');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.closest('form').submit();

expect(createDirectoryStub.notCalled).toEqual(true);
inListStub.restore();
});
it('creates directory when clicking on create directory field', function() {
$input = menu.$el.find('form.filenameform input');
$input.val('some folder');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.closest('form').submit();

expect(createDirectoryStub.calledOnce).toEqual(true);
expect(createDirectoryStub.getCall(0).args[0]).toEqual('some folder');
expect(createDirectoryStub.getCall(0).args[0]).toEqual('New folder');
});
});
describe('custom entries', function() {
Expand Down Expand Up @@ -135,17 +107,7 @@ describe('OCA.Files.NewFileMenu', function() {
});
it('calls action handler when clicking on custom item', function() {
menu.$el.find('.menuitem').eq(2).click();
var $input = menu.$el.find('form.filenameform input');
$input.val('some name');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.closest('form').submit();

expect(actionStub.calledOnce).toEqual(true);
expect(actionStub.getCall(0).args[0]).toEqual('some name');
});
it('switching fields removes the previous form', function() {
menu.$el.find('.menuitem').eq(2).click();
expect(menu.$el.find('form').length).toEqual(1);
expect(actionStub.getCall(0).args[0]).toEqual('New text file.txt');
});
});
});