File size: 5,834 Bytes
e1d589b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | import _find from 'lodash-es/find';
import _get from 'lodash-es/get';
import FormFactory from '../../tools/formFactory';
export default class ModifyTranslationFolder {
constructor( translationFolder ) {
this.translationFolder = translationFolder;
this.translationFoldersList = Hoot.folders.translationFolders;
let descendents = this.getDescendents([ this.translationFolder.id ], this.translationFoldersList);
//filter out the folder itself and all of it's descendents
this.folderList = [
{
path : '/',
id : 0,
name: 'root',
userId: Hoot.user().id //hack to make root always visible to user
}
].concat(Hoot.folders.translationFolders)
.filter(f => {
return !descendents.includes(f.id) && this.translationFolder.parentId !== f.id;
});
this.form = [
{
label: 'Move to Existing Folder',
id: 'modifyPathName',
class: 'path-name',
inputType: 'combobox',
placeholder: 'Select a path',
data: this.folderList,
readonly: 'readonly',
sort: true,
itemKey: 'path'
},
{
label: 'Public',
id: 'modifyVisibility',
inputType: 'checkbox',
value: 'Public',
checked: false,
class: 'folder-checkbox'
}
];
}
//get list of folder ids and all their descendents
getDescendents(ids, folders) {
let children = folders.filter(f => ids.includes(f.parentId))
.map(f => f.id);
if (children.length) {
return [...new Set(ids.concat( children ).concat(this.getDescendents(children, folders)))];
} else {
return [ ids ];
}
}
render() {
let metadata = {
title: 'Modify Folder',
form: this.form,
button: {
text: 'Modify',
location: 'right',
id: 'modifySubmitBtn',
onClick: () => this.handleSubmit()
}
};
this.container = new FormFactory().generateForm( 'body', 'modify-folder-form', metadata );
this.pathNameInput = this.container.select( '#modifyPathName' );
this.folderVisibilityInput = this.container.select( '#modifyVisibility' );
this.submitButton = this.container.select( '#modifySubmitBtn' );
this.pathNameInput.property( 'value', this.translationFolder.path );
this.folderVisibilityInput.property( 'checked', this.translationFolder.public );
this.submitButton.node().disabled = false;
return this;
}
async handleSubmit() {
let folderName = this.translationFolder.name,
pathName = this.pathNameInput.property( 'value' ),
isPublic = this.folderVisibilityInput.property( 'checked' ),
folderId = _get( _find( this.translationFoldersList, folder => folder.path === pathName ), 'id' ) || 0,
targetFolderExists = _find( Hoot.folders.translationFolders, folder => folder.id === folderId && folder.name === folderName );
if ( pathName !== this.translationFolder.path && targetFolderExists) {
let message = 'A folder already exists with this name in the destination path. Please remove the old folder or select a new name for this folder.',
type = 'warn';
Hoot.message.alert( { message, type } );
return false;
} else if ( folderId === this.translationFolder.id && this.translationFolder.public === isPublic ) {
let message = 'The new parent folder cannot be the current folder. Please select a different folder.',
type = 'warn';
Hoot.message.alert( { message, type } );
return false;
}
let message;
let moveRequest = [];
message = 'Successfully ';
if ( pathName !== '' && pathName !== this.translationFolder.path ) {
let updateParams = {
folderId: this.translationFolder.id,
targetFolderId: folderId
};
moveRequest.push( Hoot.api.moveTranslationFolder( updateParams ) );
message += 'moved folder';
}
this.processRequest = Promise.all(moveRequest)
.then( () => {
let visibilityRequest = [];
if ( this.translationFolder.public !== isPublic ) {
let visibilityParams = {
folderId: this.translationFolder.id,
visibility: (isPublic) ? 'public' : 'private'
};
visibilityRequest.push( Hoot.api.changeTranslationVisibility( visibilityParams ) );
if (message.substr(-1) !== ' ') message += ' & ';
message += `changed visibility to ${ visibilityParams.visibility }`;
}
return Promise.all(visibilityRequest);
})
.then( () => Hoot.folders.refreshTranslationInfo() )
.then( () => Hoot.events.emit( 'render-translations-table' ) )
.then( () => {
let type = 'success';
Hoot.message.alert( { message, type } );
} )
.catch( (err) => {
err.message = err.data;
Hoot.message.alert( err );
})
.finally( () => {
this.container.remove();
Hoot.events.emit( 'modal-closed' );
} );
}
}
|