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
|
|
|
|
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-07-05 21:20:28 +02:00
|
|
|
if (this._massTagInputNode) {
|
2016-08-28 18:53:06 +02:00
|
|
|
this._masstagAutoCompleteControl = new TagAutoCompleteControl(
|
2016-07-05 21:20:28 +02:00
|
|
|
this._massTagInputNode, {addSpace: false});
|
|
|
|
}
|
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
|
|
|
|
2016-08-27 20:08:58 +02:00
|
|
|
if (this._massTagInputNode) {
|
2016-07-05 21:20:28 +02:00
|
|
|
if (this._openMassTagLinkNode) {
|
|
|
|
this._openMassTagLinkNode.addEventListener(
|
|
|
|
'click', e => this._evtMassTagClick(e));
|
|
|
|
}
|
|
|
|
this._stopMassTagLinkNode.addEventListener(
|
|
|
|
'click', e => this._evtStopTaggingClick(e));
|
2016-07-07 21:18:35 +02:00
|
|
|
this._toggleMassTagVisibility(!!ctx.parameters.tag);
|
2016-06-03 19:12:10 +02:00
|
|
|
}
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
|
|
|
|
2016-07-05 21:20:28 +02:00
|
|
|
_toggleMassTagVisibility(state) {
|
2016-08-27 20:08:58 +02:00
|
|
|
this._formNode.querySelector('.masstag')
|
|
|
|
.classList.toggle('active', state);
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
2016-08-27 20:08:58 +02:00
|
|
|
get _formNode() {
|
|
|
|
return this._hostNode.querySelector('form');
|
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
|
|
|
}
|
|
|
|
|
|
|
|
get _massTagInputNode() {
|
2016-08-27 20:08:58 +02:00
|
|
|
return this._hostNode.querySelector('form [name=masstag]');
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get _openMassTagLinkNode() {
|
2016-08-27 20:08:58 +02:00
|
|
|
return this._hostNode.querySelector('form .open-masstag');
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get _stopMassTagLinkNode() {
|
2016-08-27 20:08:58 +02:00
|
|
|
return this._hostNode.querySelector('form .stop-tagging');
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_evtMassTagClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this._toggleMassTagVisibility(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtStopTaggingClick(e) {
|
|
|
|
e.preventDefault();
|
2016-09-18 10:38:53 +02:00
|
|
|
this._massTagInputNode.value = '';
|
2016-08-28 18:53:06 +02:00
|
|
|
this._toggleMassTagVisibility(false);
|
|
|
|
this.dispatchEvent(new CustomEvent('navigate', {detail: {parameters: {
|
2016-07-07 21:18:35 +02:00
|
|
|
query: this._ctx.parameters.query,
|
|
|
|
page: this._ctx.parameters.page,
|
2016-09-16 21:31:09 +02:00
|
|
|
tag: null,
|
2016-08-28 18:53:06 +02:00
|
|
|
}}}));
|
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(
|
|
|
|
{}, this._ctx.parameters, {tag: null, page: 1}),
|
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();
|
2016-08-28 18:53:06 +02:00
|
|
|
this._queryAutoCompleteControl.hide();
|
|
|
|
if (this._masstagAutoCompleteControl) {
|
|
|
|
this._masstagAutoCompleteControl.hide();
|
|
|
|
}
|
2016-08-28 23:08:29 +02:00
|
|
|
let parameters = {query: this._queryInputNode.value};
|
|
|
|
parameters.page = parameters.query === this._ctx.parameters.query ?
|
|
|
|
this._ctx.parameters.page : 1;
|
2016-08-27 20:08:58 +02:00
|
|
|
if (this._massTagInputNode) {
|
2016-08-28 18:53:06 +02:00
|
|
|
parameters.tag = this._massTagInputNode.value;
|
2016-08-27 20:08:58 +02:00
|
|
|
this._massTagInputNode.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;
|