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

145 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-10-08 00:17:10 +02:00
const Folder = require('lib/models/folder.js').Folder;
2017-10-22 19:12:16 +02:00
const Tag = require('lib/models/tag.js').Tag;
const BaseModel = require('lib/base-model.js').BaseModel;
2017-10-08 00:17:10 +02:00
const ListWidget = require('tkwidgets/ListWidget.js');
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;
2017-10-09 20:05:01 +02:00
this.itemRenderer = (item) => {
2017-10-22 19:12:16 +02:00
let output = [];
if (item.type_ === Folder.modelType()) {
output.push('[n]');
} else if (item.type_ === Tag.modelType()) {
output.push('[t]');
} else if (item.type_ === BaseModel.TYPE_SEARCH) {
output.push('[s]');
2017-10-22 19:12:16 +02:00
}
output.push(item.title);
// output.push(item.id.substr(0, 5));
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
}
get selectedFolderId() {
return this.selectedFolderId_;
}
set selectedFolderId(v) {
this.updateIndexFromSelectedItemId()
2017-10-08 00:17:10 +02:00
this.selectedFolderId_ = v;
2017-10-22 19:12:16 +02:00
this.invalidate();
}
get selectedSearchId() {
return this.selectedSearchId_;
}
set selectedSearchId(v) {
this.updateIndexFromSelectedItemId()
this.selectedSearchId_ = v;
this.invalidate();
}
2017-10-22 19:12:16 +02:00
get selectedTagId() {
return this.selectedTagId_;
}
set selectedTagId(v) {
this.updateIndexFromSelectedItemId()
2017-10-22 19:12:16 +02:00
this.selectedTagId_ = v;
this.invalidate();
}
get notesParentType() {
return this.notesParentType_;
}
set notesParentType(v) {
if (this.notesParentType_ === v) return;
this.notesParentType_ = v;
this.updateIndexFromSelectedItemId()
this.invalidate();
}
get searches() {
return this.searches_;
}
set searches(v) {
if (this.searches_ === v) return;
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) {
if (this.tags_ === v) return;
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) {
if (this.folders_ === v) return;
this.folders_ = v;
this.updateItems_ = true;
this.updateIndexFromSelectedItemId()
2017-10-22 19:12:16 +02:00
this.invalidate();
}
async onWillRender() {
if (this.updateItems_) {
this.logger().info('Rebuilding items...', this.notesParentType, this.selectedItemId, this.selectedSearchId);
const wasSelectedItemId = this.selectedItemId;
const previousParentType = this.notesParentType;
this.items = this.folders.concat(this.tags).concat(this.searches);
this.notesParentType = previousParentType;
this.updateIndexFromSelectedItemId(wasSelectedItemId)
2017-10-22 19:12:16 +02:00
this.updateItems_ = false;
}
}
get selectedItemId() {
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);
}
updateIndexFromSelectedItemId(itemId = null) {
if (itemId === null) itemId = this.selectedItemId;
const index = this.itemIndexByKey('id', itemId);
//this.logger().info('Setting index to', this.notesParentType, index);
this.currentIndex = index >= 0 ? index : 0;
2017-10-08 00:17:10 +02:00
}
}
module.exports = FolderListWidget;