1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/CliClient/app/gui/FolderListWidget.js

177 lines
4.1 KiB
JavaScript
Raw Normal View History

const Folder = require('lib/models/Folder.js');
const Tag = require('lib/models/Tag.js');
const BaseModel = require('lib/BaseModel.js');
2017-10-08 00:17:10 +02:00
const ListWidget = require('tkwidgets/ListWidget.js');
const _ = require('lib/locale.js')._;
2017-10-08 00:17:10 +02:00
class FolderListWidget extends ListWidget {
constructor() {
super();
2017-10-22 19:12:16 +02:00
this.tags_ = [];
this.folders_ = [];
this.searches_ = [];
2017-10-22 19:12:16 +02:00
this.selectedFolderId_ = null;
this.selectedTagId_ = null;
this.selectedSearchId_ = null;
2017-10-22 19:12:16 +02:00
this.notesParentType_ = 'Folder';
this.updateIndexFromSelectedFolderId_ = false;
2017-10-22 19:12:16 +02:00
this.updateItems_ = false;
2018-05-09 13:39:27 +02:00
this.trimItemTitle = false;
2017-10-09 20:05:01 +02:00
this.itemRenderer = (item) => {
2017-10-22 19:12:16 +02:00
let output = [];
if (item === '-') {
output.push('-'.repeat(this.innerWidth));
} else if (item.type_ === Folder.modelType()) {
2018-05-09 13:39:27 +02:00
output.push(' '.repeat(this.folderDepth(this.folders, item.id)) + Folder.displayTitle(item));
2017-10-22 19:12:16 +02:00
} else if (item.type_ === Tag.modelType()) {
2017-12-26 12:38:53 +02:00
output.push('[' + Folder.displayTitle(item) + ']');
} else if (item.type_ === BaseModel.TYPE_SEARCH) {
output.push(_('Search:'));
output.push(item.title);
2018-05-09 13:39:27 +02:00
}
2017-10-24 20:09:01 +02:00
// if (item && item.id) output.push(item.id.substr(0, 5));
2017-10-24 20:09:01 +02:00
2017-10-22 19:12:16 +02:00
return output.join(' ');
2017-10-09 20:05:01 +02:00
};
2017-10-08 00:17:10 +02:00
}
2018-05-09 13:39:27 +02:00
folderDepth(folders, folderId) {
let output = 0;
while (true) {
const folder = BaseModel.byId(folders, folderId);
if (!folder.parent_id) return output;
output++;
folderId = folder.parent_id;
}
throw new Error('unreachable');
}
2017-10-08 00:17:10 +02:00
get selectedFolderId() {
return this.selectedFolderId_;
}
set selectedFolderId(v) {
this.selectedFolderId_ = v;
this.updateIndexFromSelectedItemId()
2017-10-22 19:12:16 +02:00
this.invalidate();
}
get selectedSearchId() {
return this.selectedSearchId_;
}
set selectedSearchId(v) {
this.selectedSearchId_ = v;
this.updateIndexFromSelectedItemId()
this.invalidate();
}
2017-10-22 19:12:16 +02:00
get selectedTagId() {
return this.selectedTagId_;
}
set selectedTagId(v) {
this.selectedTagId_ = v;
this.updateIndexFromSelectedItemId()
2017-10-22 19:12:16 +02:00
this.invalidate();
}
get notesParentType() {
return this.notesParentType_;
}
set notesParentType(v) {
//if (this.notesParentType_ === v) return;
2017-10-22 19:12:16 +02:00
this.notesParentType_ = v;
this.updateIndexFromSelectedItemId()
this.invalidate();
}
get searches() {
return this.searches_;
}
set searches(v) {
this.searches_ = v;
this.updateItems_ = true;
this.updateIndexFromSelectedItemId()
2017-10-22 19:12:16 +02:00
this.invalidate();
}
get tags() {
return this.tags_;
}
set tags(v) {
this.tags_ = v;
this.updateItems_ = true;
this.updateIndexFromSelectedItemId()
2017-10-22 19:12:16 +02:00
this.invalidate();
}
get folders() {
return this.folders_;
}
set folders(v) {
this.folders_ = v;
this.updateItems_ = true;
this.updateIndexFromSelectedItemId()
2017-10-22 19:12:16 +02:00
this.invalidate();
}
render() {
2017-10-22 19:12:16 +02:00
if (this.updateItems_) {
this.logger().debug('Rebuilding items...', this.notesParentType, this.selectedJoplinItemId, this.selectedSearchId);
const wasSelectedItemId = this.selectedJoplinItemId;
const previousParentType = this.notesParentType;
let newItems = this.folders.slice();
if (this.tags.length) {
if (newItems.length) newItems.push('-');
newItems = newItems.concat(this.tags);
}
if (this.searches.length) {
if (newItems.length) newItems.push('-');
newItems = newItems.concat(this.searches);
}
this.items = newItems;
this.notesParentType = previousParentType;
this.updateIndexFromSelectedItemId(wasSelectedItemId)
2017-10-22 19:12:16 +02:00
this.updateItems_ = false;
}
super.render();
}
get selectedJoplinItemId() {
if (!this.notesParentType) return '';
if (this.notesParentType === 'Folder') return this.selectedFolderId;
if (this.notesParentType === 'Tag') return this.selectedTagId;
if (this.notesParentType === 'Search') return this.selectedSearchId;
throw new Error('Unknown parent type: ' + this.notesParentType);
}
get selectedJoplinItem() {
const id = this.selectedJoplinItemId;
const index = this.itemIndexByKey('id', id);
return this.itemAt(index);
}
updateIndexFromSelectedItemId(itemId = null) {
if (itemId === null) itemId = this.selectedJoplinItemId;
const index = this.itemIndexByKey('id', itemId);
this.currentIndex = index >= 0 ? index : 0;
2017-10-08 00:17:10 +02:00
}
}
module.exports = FolderListWidget;