keeweb/app/scripts/presenters/entry-presenter.js

98 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import { DateFormat } from 'util/formatting/date-format';
import { Locale } from 'util/locale';
2015-10-17 23:49:24 +02:00
2020-06-01 16:53:51 +02:00
const EntryPresenter = function (descField, noColor, activeEntryId) {
2015-10-17 23:49:24 +02:00
this.entry = null;
this.descField = descField;
2015-12-02 22:12:14 +01:00
this.noColor = noColor || '';
this.activeEntryId = activeEntryId;
2015-10-17 23:49:24 +02:00
};
EntryPresenter.prototype = {
2019-08-18 10:17:09 +02:00
present(item) {
2015-11-08 22:25:00 +01:00
if (item.entry) {
this.entry = item;
2020-04-15 16:50:01 +02:00
} else if (item.group) {
2015-11-08 22:25:00 +01:00
this.group = item;
2020-04-15 16:50:01 +02:00
} else if (item.external) {
this.entry = item;
this.external = true;
2015-11-08 22:25:00 +01:00
}
return this;
},
2019-08-16 23:05:39 +02:00
get id() {
return this.entry ? this.entry.id : this.group.id;
},
get icon() {
2019-09-18 23:37:57 +02:00
return this.entry ? this.entry.icon : this.group.icon || 'folder';
2019-08-16 23:05:39 +02:00
},
get customIcon() {
return this.entry ? this.entry.customIcon : undefined;
},
get color() {
2019-08-18 08:05:38 +02:00
return this.entry
? this.entry.color || (this.entry.customIcon ? this.noColor : undefined)
: undefined;
2019-08-16 23:05:39 +02:00
},
get title() {
2019-09-18 23:37:57 +02:00
return this.entry ? this.entry.title : this.group.title;
2019-08-16 23:05:39 +02:00
},
get notes() {
return this.entry ? this.entry.notes : undefined;
},
get url() {
return this.entry ? this.entry.displayUrl : undefined;
},
get user() {
return this.entry ? this.entry.user : undefined;
},
get active() {
return this.entry ? this.entry.id === this.activeEntryId : this.group.active;
},
get created() {
2019-09-15 08:11:11 +02:00
return this.entry ? DateFormat.dtStr(this.entry.created) : undefined;
2019-08-16 23:05:39 +02:00
},
get updated() {
2019-09-15 08:11:11 +02:00
return this.entry ? DateFormat.dtStr(this.entry.updated) : undefined;
2019-08-16 23:05:39 +02:00
},
get expired() {
return this.entry ? this.entry.expired : false;
},
get tags() {
return this.entry ? this.entry.tags : undefined;
},
get groupName() {
return this.entry ? this.entry.groupName : undefined;
},
get fileName() {
return this.entry ? this.entry.fileName : undefined;
},
2015-10-17 23:49:24 +02:00
get description() {
2015-11-08 22:25:00 +01:00
if (!this.entry) {
2015-12-17 19:25:25 +01:00
return '[' + Locale.listGroup + ']';
2015-11-08 22:25:00 +01:00
}
2020-04-15 16:50:01 +02:00
if (this.external) {
return this.entry.description;
}
2015-10-17 23:49:24 +02:00
switch (this.descField) {
case 'website':
2015-12-17 19:25:25 +01:00
return this.url || '(' + Locale.listNoWebsite + ')';
2015-10-17 23:49:24 +02:00
case 'user':
2015-12-17 19:25:25 +01:00
return this.user || '(' + Locale.listNoUser + ')';
2015-10-17 23:49:24 +02:00
case 'created':
return this.created;
case 'updated':
return this.updated;
case 'attachments':
2019-08-18 08:05:38 +02:00
return (
2020-06-01 16:53:51 +02:00
this.entry.attachments.map((a) => a.title).join(', ') ||
2019-08-18 08:05:38 +02:00
'(' + Locale.listNoAttachments + ')'
);
2015-10-17 23:49:24 +02:00
default:
return this.user || this.notes || this.url;
2015-10-17 23:49:24 +02:00
}
}
};
2019-09-15 14:16:32 +02:00
export { EntryPresenter };