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));
|
|
|
|
}
|
|
|
|
|
2017-02-11 21:58:18 +01:00
|
|
|
this._postIdToListItemNode = {};
|
|
|
|
for (let listItemNode of this._listItemNodes) {
|
|
|
|
const postId = listItemNode.getAttribute('data-post-id');
|
2016-07-05 21:20:28 +02:00
|
|
|
const post = this._postIdToPost[postId];
|
2017-02-11 21:58:18 +01:00
|
|
|
this._postIdToListItemNode[postId] = listItemNode;
|
|
|
|
|
|
|
|
const tagFlipperNode = this._getTagFlipperNode(listItemNode);
|
|
|
|
if (tagFlipperNode) {
|
|
|
|
tagFlipperNode.addEventListener(
|
|
|
|
'click', e => this._evtBulkEditTagsClick(e, post));
|
|
|
|
}
|
|
|
|
|
|
|
|
const safetyFlipperNode = this._getSafetyFlipperNode(listItemNode);
|
|
|
|
if (safetyFlipperNode) {
|
|
|
|
for (let linkNode of safetyFlipperNode.querySelectorAll('a')) {
|
|
|
|
linkNode.addEventListener(
|
|
|
|
'click', e => this._evtBulkEditSafetyClick(e, post));
|
|
|
|
}
|
|
|
|
}
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 21:58:18 +01:00
|
|
|
this._syncBulkEditorsHighlights();
|
|
|
|
}
|
|
|
|
|
|
|
|
get _listItemNodes() {
|
|
|
|
return this._hostNode.querySelectorAll('li');
|
2017-02-11 19:50:22 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 21:58:18 +01:00
|
|
|
_getTagFlipperNode(listItemNode) {
|
|
|
|
return listItemNode.querySelector('.tag-flipper');
|
|
|
|
}
|
|
|
|
|
|
|
|
_getSafetyFlipperNode(listItemNode) {
|
|
|
|
return listItemNode.querySelector('.safety-flipper');
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_evtPostChange(e) {
|
2017-02-11 21:58:18 +01:00
|
|
|
const listItemNode = this._postIdToListItemNode[e.detail.post.id];
|
|
|
|
for (let node of listItemNode.querySelectorAll('[data-disabled]')) {
|
|
|
|
node.removeAttribute('data-disabled');
|
|
|
|
}
|
|
|
|
this._syncBulkEditorsHighlights();
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-02-11 20:12:44 +01:00
|
|
|
|
2017-02-11 21:58:18 +01:00
|
|
|
_evtBulkEditSafetyClick(e, post) {
|
|
|
|
e.preventDefault();
|
|
|
|
const linkNode = e.target;
|
|
|
|
if (linkNode.getAttribute('data-disabled')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const newSafety = linkNode.getAttribute('data-safety');
|
|
|
|
if (post.safety === newSafety) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
linkNode.setAttribute('data-disabled', true);
|
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(
|
|
|
|
'changeSafety', {detail: {post: post, safety: newSafety}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
_syncBulkEditorsHighlights() {
|
|
|
|
for (let listItemNode of this._listItemNodes) {
|
|
|
|
const postId = listItemNode.getAttribute('data-post-id');
|
2017-02-11 20:12:44 +01:00
|
|
|
const post = this._postIdToPost[postId];
|
2017-02-11 21:58:18 +01:00
|
|
|
|
|
|
|
const tagFlipperNode = this._getTagFlipperNode(listItemNode);
|
|
|
|
if (tagFlipperNode) {
|
|
|
|
let tagged = true;
|
|
|
|
for (let tag of this._ctx.bulkEdit.tags) {
|
2017-10-01 21:46:53 +02:00
|
|
|
tagged = tagged & post.tags.isTaggedWith(tag);
|
2017-02-11 21:58:18 +01:00
|
|
|
}
|
|
|
|
tagFlipperNode.classList.toggle('tagged', tagged);
|
|
|
|
}
|
|
|
|
|
|
|
|
const safetyFlipperNode = this._getSafetyFlipperNode(listItemNode);
|
|
|
|
if (safetyFlipperNode) {
|
|
|
|
for (let linkNode of safetyFlipperNode.querySelectorAll('a')) {
|
|
|
|
const safety = linkNode.getAttribute('data-safety');
|
|
|
|
linkNode.classList.toggle('active', post.safety == safety);
|
|
|
|
}
|
2017-02-11 20:12:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 00:07:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostsPageView;
|