2016-06-02 00:07:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const page = require('page');
|
2016-06-03 19:12:10 +02:00
|
|
|
const settings = require('../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');
|
|
|
|
|
|
|
|
class PostsHeaderView {
|
|
|
|
constructor() {
|
|
|
|
this._template = views.getTemplate('posts-header');
|
|
|
|
}
|
|
|
|
|
|
|
|
render(ctx) {
|
2016-06-03 19:12:10 +02:00
|
|
|
ctx.settings = settings.getSettings();
|
|
|
|
|
2016-06-02 00:07:51 +02:00
|
|
|
const target = ctx.target;
|
|
|
|
const source = this._template(ctx);
|
|
|
|
|
|
|
|
const form = source.querySelector('form');
|
|
|
|
const searchTextInput = form.querySelector('[name=search-text]');
|
|
|
|
|
|
|
|
if (searchTextInput) {
|
|
|
|
new TagAutoCompleteControl(searchTextInput);
|
|
|
|
}
|
|
|
|
|
|
|
|
keyboard.bind('q', () => {
|
|
|
|
form.querySelector('input').focus();
|
|
|
|
});
|
|
|
|
|
2016-06-03 19:12:10 +02:00
|
|
|
for (let safetyButton of form.querySelectorAll('.safety')) {
|
|
|
|
safetyButton.addEventListener(
|
|
|
|
'click', e => this._evtSafetyButtonClick(e, ctx.clientUrl));
|
|
|
|
}
|
|
|
|
form.addEventListener(
|
|
|
|
'submit', e => this._evtFormSubmit(e, searchTextInput));
|
2016-06-02 00:07:51 +02:00
|
|
|
|
|
|
|
views.showView(target, source);
|
|
|
|
}
|
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.getSettings();
|
|
|
|
browsingSettings.listPosts[safety]
|
|
|
|
= !browsingSettings.listPosts[safety];
|
|
|
|
settings.saveSettings(browsingSettings, true);
|
|
|
|
page(url.replace(/{page}/, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtFormSubmit(e, searchTextInput) {
|
|
|
|
e.preventDefault();
|
|
|
|
const text = searchTextInput.value;
|
|
|
|
searchTextInput.blur();
|
|
|
|
page('/posts/' + misc.formatSearchQuery({text: text}));
|
|
|
|
}
|
2016-06-02 00:07:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostsHeaderView;
|