2016-06-02 00:07:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-07-05 21:20:28 +02:00
|
|
|
const events = require('../events.js');
|
2016-06-02 00:07:51 +02:00
|
|
|
const views = require('../util/views.js');
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
const template = views.getTemplate('posts-page');
|
2016-06-02 00:07:51 +02:00
|
|
|
|
2016-07-05 21:20:28 +02:00
|
|
|
class PostsPageView extends events.EventTarget {
|
2016-06-14 10:31:48 +02:00
|
|
|
constructor(ctx) {
|
2016-07-05 21:20:28 +02:00
|
|
|
super();
|
|
|
|
this._ctx = ctx;
|
|
|
|
this._hostNode = ctx.hostNode;
|
|
|
|
views.replaceContent(this._hostNode, template(ctx));
|
|
|
|
|
|
|
|
this._postIdToPost = {};
|
2017-02-09 00:48:06 +01:00
|
|
|
for (let post of ctx.response.results) {
|
2016-07-05 21:20:28 +02:00
|
|
|
this._postIdToPost[post.id] = post;
|
|
|
|
post.addEventListener('change', e => this._evtPostChange(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
this._postIdToLinkNode = {};
|
2017-02-11 19:50:22 +01:00
|
|
|
for (let linkNode of this._tagFlipperNodes) {
|
2016-07-05 21:20:28 +02:00
|
|
|
const postId = linkNode.getAttribute('data-post-id');
|
|
|
|
const post = this._postIdToPost[postId];
|
|
|
|
this._postIdToLinkNode[postId] = linkNode;
|
|
|
|
linkNode.addEventListener(
|
2017-02-11 19:50:22 +01:00
|
|
|
'click', e => this._evtBulkEditTagsClick(e, post));
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 19:50:22 +01:00
|
|
|
this._syncBulkEditTagsHighlights();
|
|
|
|
}
|
|
|
|
|
|
|
|
get _tagFlipperNodes() {
|
|
|
|
return this._hostNode.querySelectorAll('.tag-flipper');
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_evtPostChange(e) {
|
|
|
|
const linkNode = this._postIdToLinkNode[e.detail.post.id];
|
|
|
|
linkNode.removeAttribute('data-disabled');
|
2017-02-11 19:50:22 +01:00
|
|
|
this._syncBulkEditTagsHighlights();
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 19:50:22 +01:00
|
|
|
_syncBulkEditTagsHighlights() {
|
|
|
|
for (let linkNode of this._tagFlipperNodes) {
|
2016-07-05 21:20:28 +02:00
|
|
|
const postId = linkNode.getAttribute('data-post-id');
|
|
|
|
const post = this._postIdToPost[postId];
|
|
|
|
let tagged = true;
|
2017-02-11 19:50:22 +01:00
|
|
|
for (let tag of this._ctx.bulkEdit.tags) {
|
2016-07-05 21:20:28 +02:00
|
|
|
tagged = tagged & post.isTaggedWith(tag);
|
|
|
|
}
|
|
|
|
linkNode.classList.toggle('tagged', tagged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 19:50:22 +01:00
|
|
|
_evtBulkEditTagsClick(e, post) {
|
2016-07-05 21:20:28 +02:00
|
|
|
e.preventDefault();
|
|
|
|
const linkNode = e.target;
|
|
|
|
if (linkNode.getAttribute('data-disabled')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
linkNode.setAttribute('data-disabled', true);
|
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(
|
|
|
|
linkNode.classList.contains('tagged') ? 'untag' : 'tag',
|
|
|
|
{detail: {post: post}}));
|
2016-06-02 00:07:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostsPageView;
|