szurubooru/client/js/views/posts_header_view.js

71 lines
2.2 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();
this._hostNode = ctx.hostNode;
views.replaceContent(this._hostNode, template(ctx));
if (this._queryInputNode) {
new TagAutoCompleteControl(this._queryInputNode, {addSpace: true});
}
keyboard.bind('q', () => {
this._formNode.querySelector('input').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 safetyButton of this._formNode.querySelectorAll('.safety')) {
2016-06-03 19:12:10 +02:00
safetyButton.addEventListener(
'click', e => this._evtSafetyButtonClick(e, ctx.clientUrl));
}
this._formNode.addEventListener(
'submit', e => this._evtFormSubmit(e, this._queryInputNode));
}
get _formNode() {
return this._hostNode.querySelector('form');
}
get _queryInputNode() {
return this._formNode.querySelector('[name=search-text]');
}
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(url.replace(/{page}/, 1));
2016-06-03 19:12:10 +02:00
}
_evtFormSubmit(e, queryInputNode) {
2016-06-03 19:12:10 +02:00
e.preventDefault();
const text = queryInputNode.value;
queryInputNode.blur();
router.show('/posts/' + misc.formatSearchQuery({text: text}));
2016-06-03 19:12:10 +02:00
}
}
module.exports = PostsHeaderView;