2016-06-02 00:07:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-28 18:53:06 +02:00
|
|
|
const events = require('../events.js');
|
2016-06-14 10:31:48 +02:00
|
|
|
const settings = require('../models/settings.js');
|
2016-06-02 00:07:51 +02:00
|
|
|
const keyboard = require('../util/keyboard.js');
|
|
|
|
const misc = require('../util/misc.js');
|
2016-08-28 18:53:06 +02:00
|
|
|
const search = require('../util/search.js');
|
2016-06-02 00:07:51 +02:00
|
|
|
const views = require('../util/views.js');
|
|
|
|
const TagAutoCompleteControl =
|
|
|
|
require('../controls/tag_auto_complete_control.js');
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
const template = views.getTemplate('posts-header');
|
2016-06-02 00:07:51 +02:00
|
|
|
|
2017-02-11 20:12:44 +01:00
|
|
|
class BulkTagEditor extends events.EventTarget {
|
|
|
|
constructor(hostNode) {
|
|
|
|
super();
|
|
|
|
this._hostNode = hostNode;
|
|
|
|
|
|
|
|
this._autoCompleteControl = new TagAutoCompleteControl(
|
|
|
|
this._inputNode, {addSpace: false});
|
|
|
|
this._openLinkNode.addEventListener(
|
|
|
|
'click', e => this._evtOpenLinkClick(e));
|
|
|
|
this._closeLinkNode.addEventListener(
|
|
|
|
'click', e => this._evtCloseLinkClick(e));
|
|
|
|
this._hostNode.addEventListener('submit', e => this._evtFormSubmit(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
return this._inputNode.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get opened() {
|
|
|
|
return this._hostNode.classList.contains('opened');
|
|
|
|
}
|
|
|
|
|
|
|
|
get _openLinkNode() {
|
|
|
|
return this._hostNode.querySelector('.open');
|
|
|
|
}
|
|
|
|
|
|
|
|
get _closeLinkNode() {
|
|
|
|
return this._hostNode.querySelector('.close');
|
|
|
|
}
|
|
|
|
|
|
|
|
get _inputNode() {
|
|
|
|
return this._hostNode.querySelector('input[name=tag]');
|
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
this._inputNode.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this._autoCompleteControl.hide();
|
|
|
|
this._inputNode.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleOpen(state) {
|
|
|
|
this._hostNode.classList.toggle('opened', state);
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtFormSubmit(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.dispatchEvent(new CustomEvent('submit', {detail: {}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtOpenLinkClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.toggleOpen(true);
|
|
|
|
this.focus();
|
|
|
|
this.dispatchEvent(new CustomEvent('open', {detail: {}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtCloseLinkClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this._inputNode.value = '';
|
|
|
|
this.toggleOpen(false);
|
|
|
|
this.blur();
|
|
|
|
this.dispatchEvent(new CustomEvent('close', {detail: {}}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-28 18:53:06 +02:00
|
|
|
class PostsHeaderView extends events.EventTarget {
|
2016-06-14 10:31:48 +02:00
|
|
|
constructor(ctx) {
|
2016-08-28 18:53:06 +02:00
|
|
|
super();
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
ctx.settings = settings.get();
|
2016-07-05 21:20:28 +02:00
|
|
|
this._ctx = ctx;
|
2016-06-14 10:31:48 +02:00
|
|
|
this._hostNode = ctx.hostNode;
|
|
|
|
views.replaceContent(this._hostNode, template(ctx));
|
2016-06-02 00:07:51 +02:00
|
|
|
|
2016-08-28 18:53:06 +02:00
|
|
|
this._queryAutoCompleteControl = new TagAutoCompleteControl(
|
2016-09-29 22:45:40 +02:00
|
|
|
this._queryInputNode,
|
|
|
|
{addSpace: true, transform: misc.escapeSearchTerm});
|
2016-06-02 00:07:51 +02:00
|
|
|
|
2016-08-28 18:53:06 +02:00
|
|
|
keyboard.bind('p', () => this._focusFirstPostNode());
|
|
|
|
search.searchInputNodeFocusHelper(this._queryInputNode);
|
2016-06-02 00:07:51 +02:00
|
|
|
|
2016-08-22 20:45:58 +02:00
|
|
|
for (let safetyButtonNode of this._safetyButtonNodes) {
|
|
|
|
safetyButtonNode.addEventListener(
|
2016-07-05 21:20:28 +02:00
|
|
|
'click', e => this._evtSafetyButtonClick(e));
|
|
|
|
}
|
2016-08-27 20:08:58 +02:00
|
|
|
this._formNode.addEventListener(
|
|
|
|
'submit', e => this._evtFormSubmit(e));
|
2016-07-05 21:20:28 +02:00
|
|
|
|
2017-02-11 20:12:44 +01:00
|
|
|
if (this._bulkEditTagsNode) {
|
|
|
|
this._bulkTagEditor = new BulkTagEditor(this._bulkEditTagsNode);
|
|
|
|
this._bulkTagEditor.toggleOpen(!!ctx.parameters.tag);
|
|
|
|
this._bulkTagEditor.addEventListener('submit', e => {
|
|
|
|
this._navigate();
|
|
|
|
});
|
|
|
|
this._bulkTagEditor.addEventListener('close', e => {
|
|
|
|
this._navigate();
|
|
|
|
});
|
2016-06-03 19:12:10 +02:00
|
|
|
}
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
|
|
|
|
2016-08-27 20:08:58 +02:00
|
|
|
get _formNode() {
|
2017-02-11 20:12:44 +01:00
|
|
|
return this._hostNode.querySelector('form.search');
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get _safetyButtonNodes() {
|
2016-08-27 20:08:58 +02:00
|
|
|
return this._hostNode.querySelectorAll('form .safety');
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
2016-06-02 00:07:51 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
get _queryInputNode() {
|
2016-08-27 20:08:58 +02:00
|
|
|
return this._hostNode.querySelector('form [name=search-text]');
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 20:12:44 +01:00
|
|
|
get _bulkEditTagsNode() {
|
|
|
|
return this._hostNode.querySelector('.bulk-edit-tags');
|
2016-06-02 00:07:51 +02:00
|
|
|
}
|
2016-06-03 19:12:10 +02:00
|
|
|
|
|
|
|
_evtSafetyButtonClick(e, url) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.target.classList.toggle('disabled');
|
|
|
|
const safety = e.target.getAttribute('data-safety');
|
2016-06-14 10:31:48 +02:00
|
|
|
let browsingSettings = settings.get();
|
2016-06-12 22:10:20 +02:00
|
|
|
browsingSettings.listPosts[safety] =
|
|
|
|
!browsingSettings.listPosts[safety];
|
2016-06-14 10:31:48 +02:00
|
|
|
settings.save(browsingSettings, true);
|
2016-08-28 18:53:06 +02:00
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(
|
2016-08-28 23:11:08 +02:00
|
|
|
'navigate', {
|
|
|
|
detail: {
|
2016-09-16 21:31:09 +02:00
|
|
|
parameters: Object.assign(
|
2017-02-09 00:48:06 +01:00
|
|
|
{}, this._ctx.parameters, {tag: null, offset: 0}),
|
2016-08-28 23:11:08 +02:00
|
|
|
},
|
|
|
|
}));
|
2016-06-03 19:12:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-27 20:08:58 +02:00
|
|
|
_evtFormSubmit(e) {
|
2016-07-05 21:20:28 +02:00
|
|
|
e.preventDefault();
|
2017-02-11 20:12:44 +01:00
|
|
|
this._navigate();
|
|
|
|
}
|
|
|
|
|
|
|
|
_navigate() {
|
2016-08-28 18:53:06 +02:00
|
|
|
this._queryAutoCompleteControl.hide();
|
2016-08-28 23:08:29 +02:00
|
|
|
let parameters = {query: this._queryInputNode.value};
|
2017-02-09 00:48:06 +01:00
|
|
|
parameters.offset = parameters.query === this._ctx.parameters.query ?
|
|
|
|
this._ctx.parameters.offset : 0;
|
2017-02-11 20:12:44 +01:00
|
|
|
if (this._bulkTagEditor && this._bulkTagEditor.opened) {
|
|
|
|
parameters.tag = this._bulkTagEditor.value;
|
|
|
|
this._bulkTagEditor.blur();
|
2016-09-16 21:31:09 +02:00
|
|
|
} else {
|
|
|
|
parameters.tag = null;
|
2016-08-27 20:08:58 +02:00
|
|
|
}
|
2016-08-28 18:53:06 +02:00
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent('navigate', {detail: {parameters: parameters}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
_focusFirstPostNode() {
|
|
|
|
const firstPostNode =
|
|
|
|
document.body.querySelector('.post-list li:first-child a');
|
|
|
|
if (firstPostNode) {
|
|
|
|
firstPostNode.focus();
|
|
|
|
}
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
2016-06-02 00:07:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostsHeaderView;
|