snappymail/dev/View/Popup/FolderCreate.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2019-07-05 03:19:24 +08:00
import { Notification } from 'Common/Enums';
import { UNUSED_OPTION_VALUE } from 'Common/Consts';
import { defaultOptionsAfterRender } from 'Common/Utils';
import { folderListOptionsBuilder, sortFolders } from 'Common/UtilsUser';
import { getNotification } from 'Common/Translator';
2014-08-21 23:08:34 +08:00
import { FolderUserStore } from 'Stores/User/Folder';
2014-08-25 15:10:51 +08:00
import Remote from 'Remote/User/Fetch';
import { decorateKoCommands } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
import { setFolder, getFolderFromCacheList } from 'Common/Cache';
import { FolderModel } from 'Model/FolderCollection';
class FolderCreatePopupView extends AbstractViewPopup {
constructor() {
super('FolderCreate');
this.viewNoUserSelect = true;
this.addObservables({
folderName: '',
2014-04-27 05:53:37 +08:00
selectedParentValue: UNUSED_OPTION_VALUE
});
2014-08-21 23:08:34 +08:00
this.parentFolderSelectList = ko.computed(() =>
folderListOptionsBuilder(
[],
[['', '']],
2021-08-19 21:14:47 +08:00
oItem =>
oItem ? (oItem.isSystemFolder() ? oItem.name() + ' ' + oItem.manageFolderSystemName() : oItem.name()) : '',
FolderUserStore.namespace
? item => FolderUserStore.namespace !== item.fullName.slice(0, FolderUserStore.namespace.length)
: null,
2021-08-19 21:14:47 +08:00
true
)
);
2014-08-21 23:08:34 +08:00
this.defaultOptionsAfterRender = defaultOptionsAfterRender;
decorateKoCommands(this, {
createFolderCommand: self => self.simpleFolderNameValidation(self.folderName())
});
2016-09-10 06:38:16 +08:00
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
createFolderCommand() {
let parentFolderName = this.selectedParentValue();
if (!parentFolderName && 1 < FolderUserStore.namespace.length) {
parentFolderName = FolderUserStore.namespace.slice(0, FolderUserStore.namespace.length - 1);
2016-09-10 06:38:16 +08:00
}
2014-08-21 23:08:34 +08:00
Remote.abort('Folders').post('FolderCreate', FolderUserStore.foldersCreating, {
Folder: this.folderName(),
Parent: parentFolderName
})
.then(
data => {
const folder = getFolderFromCacheList(parentFolderName),
subFolder = FolderModel.reviveFromJson(data.Result),
folders = (folder ? folder.subFolders : FolderUserStore.folderList);
setFolder(subFolder);
folders.push(subFolder);
sortFolders(folders);
/*
var collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
console.log((folder ? folder.subFolders : FolderUserStore.folderList).sort(collator.compare));
*/
},
error => {
FolderUserStore.folderListError(
getNotification(error.code, '', Notification.CantCreateFolder)
+ '.\n' + error.message);
}
);
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
this.cancelCommand();
}
simpleFolderNameValidation(sName) {
return /^[^\\/]+$/g.test(sName);
}
2021-11-03 21:58:34 +08:00
onShow() {
this.folderName('');
this.selectedParentValue('');
}
}
2013-12-29 04:42:07 +08:00
export { FolderCreatePopupView, FolderCreatePopupView as default };