szurubooru/client/js/views/posts_header_view.js

143 lines
4.5 KiB
JavaScript
Raw Normal View History

'use strict';
const events = require('../events.js');
const settings = require('../models/settings.js');
const keyboard = require('../util/keyboard.js');
const misc = require('../util/misc.js');
const search = require('../util/search.js');
const views = require('../util/views.js');
const TagAutoCompleteControl =
require('../controls/tag_auto_complete_control.js');
const template = views.getTemplate('posts-header');
class PostsHeaderView extends events.EventTarget {
constructor(ctx) {
super();
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));
this._queryAutoCompleteControl = new TagAutoCompleteControl(
this._queryInputNode, {addSpace: true});
2016-07-05 21:20:28 +02:00
if (this._massTagInputNode) {
this._masstagAutoCompleteControl = new TagAutoCompleteControl(
2016-07-05 21:20:28 +02:00
this._massTagInputNode, {addSpace: false});
}
keyboard.bind('p', () => this._focusFirstPostNode());
search.searchInputNodeFocusHelper(this._queryInputNode);
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._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();
this._massTagInputNode.value = '';
this._toggleMassTagVisibility(false);
this.dispatchEvent(new CustomEvent('navigate', {detail: {parameters: {
query: this._ctx.parameters.query,
page: this._ctx.parameters.page,
tag: null,
}}}));
}
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);
this.dispatchEvent(
new CustomEvent(
'navigate', {
detail: {
parameters: Object.assign(
{}, this._ctx.parameters, {tag: null, page: 1}),
},
}));
2016-06-03 19:12:10 +02:00
}
_evtFormSubmit(e) {
2016-07-05 21:20:28 +02:00
e.preventDefault();
this._queryAutoCompleteControl.hide();
if (this._masstagAutoCompleteControl) {
this._masstagAutoCompleteControl.hide();
}
let parameters = {query: this._queryInputNode.value};
parameters.page = parameters.query === this._ctx.parameters.query ?
this._ctx.parameters.page : 1;
if (this._massTagInputNode) {
parameters.tag = this._massTagInputNode.value;
this._massTagInputNode.blur();
} else {
parameters.tag = null;
}
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
}
}
module.exports = PostsHeaderView;