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;
|
2017-10-23 22:34:04 +02:00
|
|
|
const BaseModel = require('lib/base-model.js').BaseModel;
|
2017-10-08 00:17:10 +02:00
|
|
|
const ListWidget = require('tkwidgets/ListWidget.js');
|
2017-10-23 23:48:29 +02:00
|
|
|
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_ = [];
|
2017-10-23 22:34:04 +02:00
|
|
|
this.searches_ = [];
|
2017-10-22 19:12:16 +02:00
|
|
|
this.selectedFolderId_ = null;
|
|
|
|
this.selectedTagId_ = null;
|
2017-10-23 22:34:04 +02:00
|
|
|
this.selectedSearchId_ = null;
|
2017-10-22 19:12:16 +02:00
|
|
|
this.notesParentType_ = 'Folder';
|
2017-10-15 13:38:22 +02:00
|
|
|
this.updateIndexFromSelectedFolderId_ = false;
|
2017-10-22 19:12:16 +02:00
|
|
|
this.updateItems_ = false;
|
2017-10-15 13:38:22 +02:00
|
|
|
|
2017-10-09 20:05:01 +02:00
|
|
|
this.itemRenderer = (item) => {
|
2017-10-22 19:12:16 +02:00
|
|
|
let output = [];
|
2017-10-23 23:48:29 +02:00
|
|
|
if (item === '-') {
|
|
|
|
output.push('-'.repeat(this.innerWidth));
|
|
|
|
} else if (item.type_ === Folder.modelType()) {
|
|
|
|
output.push(item.title);
|
2017-10-22 19:12:16 +02:00
|
|
|
} else if (item.type_ === Tag.modelType()) {
|
2017-10-23 23:48:29 +02:00
|
|
|
output.push('[' + item.title + ']');
|
2017-10-23 22:34:04 +02:00
|
|
|
} else if (item.type_ === BaseModel.TYPE_SEARCH) {
|
2017-10-23 23:48:29 +02:00
|
|
|
output.push(_('Search:'));
|
|
|
|
output.push(item.title);
|
|
|
|
}
|
2017-10-24 20:09:01 +02:00
|
|
|
|
2017-10-24 21:40:15 +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
|
|
|
}
|
|
|
|
|
|
|
|
get selectedFolderId() {
|
|
|
|
return this.selectedFolderId_;
|
|
|
|
}
|
|
|
|
|
|
|
|
set selectedFolderId(v) {
|
2017-10-23 22:34:04 +02:00
|
|
|
this.updateIndexFromSelectedItemId()
|
2017-10-08 00:17:10 +02:00
|
|
|
this.selectedFolderId_ = v;
|
2017-10-22 19:12:16 +02:00
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
|
2017-10-23 22:34:04 +02:00
|
|
|
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) {
|
2017-10-23 22:34:04 +02:00
|
|
|
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;
|
2017-10-23 22:34:04 +02:00
|
|
|
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;
|
2017-10-23 22:34:04 +02:00
|
|
|
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;
|
2017-10-23 22:34:04 +02:00
|
|
|
this.updateIndexFromSelectedItemId()
|
2017-10-22 19:12:16 +02:00
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
async onWillRender() {
|
|
|
|
if (this.updateItems_) {
|
2017-10-23 23:48:29 +02:00
|
|
|
this.logger().debug('Rebuilding items...', this.notesParentType, this.selectedJoplinItemId, this.selectedSearchId);
|
|
|
|
const wasSelectedItemId = this.selectedJoplinItemId;
|
2017-10-23 22:34:04 +02:00
|
|
|
const previousParentType = this.notesParentType;
|
2017-10-23 23:48:29 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-10-23 22:34:04 +02:00
|
|
|
this.notesParentType = previousParentType;
|
|
|
|
this.updateIndexFromSelectedItemId(wasSelectedItemId)
|
2017-10-22 19:12:16 +02:00
|
|
|
this.updateItems_ = false;
|
|
|
|
}
|
2017-10-15 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 23:48:29 +02:00
|
|
|
get selectedJoplinItemId() {
|
2017-10-23 22:34:04 +02:00
|
|
|
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);
|
|
|
|
}
|
2017-10-15 13:38:22 +02:00
|
|
|
|
2017-10-23 23:48:29 +02:00
|
|
|
get selectedJoplinItem() {
|
|
|
|
const id = this.selectedJoplinItemId;
|
|
|
|
const index = this.itemIndexByKey('id', id);
|
|
|
|
return this.itemAt(index);
|
|
|
|
}
|
|
|
|
|
2017-10-23 22:34:04 +02:00
|
|
|
updateIndexFromSelectedItemId(itemId = null) {
|
2017-10-23 23:48:29 +02:00
|
|
|
if (itemId === null) itemId = this.selectedJoplinItemId;
|
2017-10-23 22:34:04 +02:00
|
|
|
const index = this.itemIndexByKey('id', itemId);
|
|
|
|
this.currentIndex = index >= 0 ? index : 0;
|
2017-10-08 00:17:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FolderListWidget;
|