2016-06-02 00:07:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-12 20:11:43 +02:00
|
|
|
const router = require('../router.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');
|
|
|
|
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-06-14 10:31:48 +02:00
|
|
|
class PostsHeaderView {
|
|
|
|
constructor(ctx) {
|
|
|
|
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-06-14 10:31:48 +02:00
|
|
|
if (this._queryInputNode) {
|
2016-07-03 19:37:10 +02:00
|
|
|
new TagAutoCompleteControl(this._queryInputNode, {addSpace: true});
|
2016-06-02 00:07:51 +02:00
|
|
|
}
|
2016-07-05 21:20:28 +02:00
|
|
|
if (this._massTagInputNode) {
|
|
|
|
new TagAutoCompleteControl(
|
|
|
|
this._massTagInputNode, {addSpace: false});
|
|
|
|
}
|
2016-06-02 00:07:51 +02:00
|
|
|
|
|
|
|
keyboard.bind('q', () => {
|
2016-08-27 20:08:58 +02:00
|
|
|
this._formNode.querySelector('input:first-of-type').focus();
|
2016-06-02 00:07:51 +02:00
|
|
|
});
|
2016-06-03 19:46:28 +02:00
|
|
|
|
|
|
|
keyboard.bind('p', () => {
|
2016-06-12 22:10:20 +02:00
|
|
|
const firstPostNode =
|
|
|
|
document.body.querySelector('.post-list li:first-child a');
|
2016-06-03 19:46:28 +02:00
|
|
|
if (firstPostNode) {
|
|
|
|
firstPostNode.focus();
|
|
|
|
}
|
|
|
|
});
|
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-08-27 20:08:58 +02:00
|
|
|
// this._massTagFormNode.addEventListener(
|
|
|
|
// 'submit', e => this._evtMassTagFormSubmit(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-07-07 21:18:35 +02:00
|
|
|
router.show('/posts/' + misc.formatUrlParameters({
|
|
|
|
query: this._ctx.parameters.query,
|
|
|
|
page: this._ctx.parameters.page,
|
2016-07-05 21:20:28 +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-07-08 00:46:48 +02:00
|
|
|
router.show(router.url);
|
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-27 20:08:58 +02:00
|
|
|
let params = {
|
|
|
|
query: this._queryInputNode.value,
|
2016-07-07 21:18:35 +02:00
|
|
|
page: this._ctx.parameters.page,
|
2016-08-27 20:08:58 +02:00
|
|
|
};
|
|
|
|
if (this._massTagInputNode) {
|
|
|
|
params.tag = this._massTagInputNode.value;
|
|
|
|
this._massTagInputNode.blur();
|
|
|
|
}
|
|
|
|
router.show('/posts/' + misc.formatUrlParameters(params));
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
2016-06-02 00:07:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostsHeaderView;
|