szurubooru/client/js/views/posts_header_view.js

128 lines
3.9 KiB
JavaScript
Raw Normal View History

'use strict';
const router = require('../router.js');
const settings = require('../models/settings.js');
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');
const template = views.getTemplate('posts-header');
class PostsHeaderView {
constructor(ctx) {
ctx.settings = settings.get();
2016-07-05 21:20:28 +02:00
this._ctx = ctx;
this._hostNode = ctx.hostNode;
views.replaceContent(this._hostNode, template(ctx));
if (this._queryInputNode) {
new TagAutoCompleteControl(this._queryInputNode, {addSpace: true});
}
2016-07-05 21:20:28 +02:00
if (this._massTagInputNode) {
new TagAutoCompleteControl(
this._massTagInputNode, {addSpace: false});
}
keyboard.bind('q', () => {
this._formNode.querySelector('input:first-of-type').focus();
});
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();
}
});
for (let safetyButtonNode of this._safetyButtonNodes) {
safetyButtonNode.addEventListener(
2016-07-05 21:20:28 +02:00
'click', e => this._evtSafetyButtonClick(e));
}
this._formNode.addEventListener(
'submit', e => this._evtFormSubmit(e));
2016-07-05 21:20:28 +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));
// this._massTagFormNode.addEventListener(
// 'submit', e => this._evtMassTagFormSubmit(e));
this._toggleMassTagVisibility(!!ctx.parameters.tag);
2016-06-03 19:12:10 +02:00
}
}
2016-07-05 21:20:28 +02:00
_toggleMassTagVisibility(state) {
this._formNode.querySelector('.masstag')
.classList.toggle('active', state);
2016-07-05 21:20:28 +02:00
}
get _formNode() {
return this._hostNode.querySelector('form');
2016-07-05 21:20:28 +02:00
}
get _safetyButtonNodes() {
return this._hostNode.querySelectorAll('form .safety');
}
get _queryInputNode() {
return this._hostNode.querySelector('form [name=search-text]');
2016-07-05 21:20:28 +02:00
}
get _massTagInputNode() {
return this._hostNode.querySelector('form [name=masstag]');
2016-07-05 21:20:28 +02:00
}
get _openMassTagLinkNode() {
return this._hostNode.querySelector('form .open-masstag');
2016-07-05 21:20:28 +02:00
}
get _stopMassTagLinkNode() {
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();
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-03 19:12:10 +02:00
_evtSafetyButtonClick(e, url) {
e.preventDefault();
e.target.classList.toggle('disabled');
const safety = e.target.getAttribute('data-safety');
let browsingSettings = settings.get();
2016-06-12 22:10:20 +02:00
browsingSettings.listPosts[safety] =
!browsingSettings.listPosts[safety];
settings.save(browsingSettings, true);
router.show(router.url);
2016-06-03 19:12:10 +02:00
}
_evtFormSubmit(e) {
2016-07-05 21:20:28 +02:00
e.preventDefault();
let params = {
query: this._queryInputNode.value,
page: this._ctx.parameters.page,
};
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
}
}
module.exports = PostsHeaderView;