client/posts: add safety buttons
This commit is contained in:
parent
8a5c6f0b31
commit
c114cec642
6 changed files with 68 additions and 12 deletions
|
@ -86,6 +86,17 @@
|
||||||
font-size: 0.95em
|
font-size: 0.95em
|
||||||
color: $inactive-link-color
|
color: $inactive-link-color
|
||||||
|
|
||||||
|
.safety
|
||||||
|
border-color: lighten(saturation($main-color, 0%), 20%)
|
||||||
|
&.safety-safe
|
||||||
|
background: #97CF97
|
||||||
|
&.safety-sketchy
|
||||||
|
background: #E7E78D
|
||||||
|
&.safety-unsafe
|
||||||
|
background: #F2B48C
|
||||||
|
&.disabled
|
||||||
|
opacity: .2
|
||||||
|
|
||||||
.post-container
|
.post-container
|
||||||
text-align: center
|
text-align: center
|
||||||
.post-content
|
.post-content
|
||||||
|
|
|
@ -9,6 +9,9 @@
|
||||||
</div>
|
</div>
|
||||||
<div class='buttons'>
|
<div class='buttons'>
|
||||||
<input type='submit' value='Search'/>
|
<input type='submit' value='Search'/>
|
||||||
|
<input data-safety=safe type='button' class='safety safety-safe <%= ctx.settings.listPosts.safe ? '' : 'disabled' %>'/>
|
||||||
|
<input data-safety=sketchy type='button' class='safety safety-sketchy <%= ctx.settings.listPosts.sketchy ? '' : 'disabled' %>'/>
|
||||||
|
<input data-safety=unsafe type='button' class='safety safety-unsafe <%= ctx.settings.listPosts.unsafe ? '' : 'disabled' %>'/>
|
||||||
<a class='button append' href='/help/search/posts'>Syntax help</a>
|
<a class='button append' href='/help/search/posts'>Syntax help</a>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const misc = require('../util/misc.js');
|
|
||||||
const page = require('page');
|
const page = require('page');
|
||||||
const api = require('../api.js');
|
const api = require('../api.js');
|
||||||
|
const settings = require('../settings.js');
|
||||||
|
const misc = require('../util/misc.js');
|
||||||
const topNavController = require('../controllers/top_nav_controller.js');
|
const topNavController = require('../controllers/top_nav_controller.js');
|
||||||
const pageController = require('../controllers/page_controller.js');
|
const pageController = require('../controllers/page_controller.js');
|
||||||
const PostsHeaderView = require('../views/posts_header_view.js');
|
const PostsHeaderView = require('../views/posts_header_view.js');
|
||||||
|
@ -40,10 +41,22 @@ class PostsController {
|
||||||
pageController.run({
|
pageController.run({
|
||||||
state: ctx.state,
|
state: ctx.state,
|
||||||
requestPage: page => {
|
requestPage: page => {
|
||||||
const text = ctx.searchQuery.text;
|
const browsingSettings = settings.getSettings();
|
||||||
|
let text = ctx.searchQuery.text;
|
||||||
|
let disabledSafety = [];
|
||||||
|
for (let key of Object.keys(browsingSettings.listPosts)) {
|
||||||
|
if (browsingSettings.listPosts[key] === false) {
|
||||||
|
disabledSafety.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (disabledSafety.length) {
|
||||||
|
text = `-rating:${disabledSafety.join(',')} ${text}`;
|
||||||
|
}
|
||||||
|
text = text.trim();
|
||||||
return api.get(
|
return api.get(
|
||||||
`/posts/?query=${text}&page=${page}&pageSize=40&_fields=` +
|
`/posts/?query=${text}&page=${page}&pageSize=40&_fields=` +
|
||||||
`id,type,tags,score,favoriteCount,commentCount,thumbnailUrl`);
|
`id,type,tags,score,favoriteCount,` +
|
||||||
|
`commentCount,thumbnailUrl`);
|
||||||
},
|
},
|
||||||
clientUrl: '/posts/' + misc.formatSearchQuery({
|
clientUrl: '/posts/' + misc.formatSearchQuery({
|
||||||
text: ctx.searchQuery.text, page: '{page}'}),
|
text: ctx.searchQuery.text, page: '{page}'}),
|
||||||
|
|
|
@ -2,14 +2,21 @@
|
||||||
|
|
||||||
const events = require('./events.js');
|
const events = require('./events.js');
|
||||||
|
|
||||||
function saveSettings(browsingSettings) {
|
function saveSettings(browsingSettings, silent) {
|
||||||
localStorage.setItem('settings', JSON.stringify(browsingSettings));
|
localStorage.setItem('settings', JSON.stringify(browsingSettings));
|
||||||
|
if (silent !== true) {
|
||||||
events.notify(events.Success, 'Settings saved');
|
events.notify(events.Success, 'Settings saved');
|
||||||
events.notify(events.SettingsChange);
|
events.notify(events.SettingsChange);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSettings(settings) {
|
function getSettings(settings) {
|
||||||
const defaultSettings = {
|
const defaultSettings = {
|
||||||
|
listPosts: {
|
||||||
|
safe: true,
|
||||||
|
sketchy: true,
|
||||||
|
unsafe: false,
|
||||||
|
},
|
||||||
upscaleSmallPosts: false,
|
upscaleSmallPosts: false,
|
||||||
endlessScroll: false,
|
endlessScroll: false,
|
||||||
keyboardShortcuts: true,
|
keyboardShortcuts: true,
|
||||||
|
|
|
@ -100,6 +100,7 @@ function makeInput(options) {
|
||||||
type: options.inputType,
|
type: options.inputType,
|
||||||
name: options.name,
|
name: options.name,
|
||||||
id: options.id,
|
id: options.id,
|
||||||
|
class: options.class,
|
||||||
value: options.value || '',
|
value: options.value || '',
|
||||||
required: options.required,
|
required: options.required,
|
||||||
pattern: options.pattern,
|
pattern: options.pattern,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const page = require('page');
|
const page = require('page');
|
||||||
|
const settings = require('../settings.js');
|
||||||
const keyboard = require('../util/keyboard.js');
|
const keyboard = require('../util/keyboard.js');
|
||||||
const misc = require('../util/misc.js');
|
const misc = require('../util/misc.js');
|
||||||
const views = require('../util/views.js');
|
const views = require('../util/views.js');
|
||||||
|
@ -13,6 +14,8 @@ class PostsHeaderView {
|
||||||
}
|
}
|
||||||
|
|
||||||
render(ctx) {
|
render(ctx) {
|
||||||
|
ctx.settings = settings.getSettings();
|
||||||
|
|
||||||
const target = ctx.target;
|
const target = ctx.target;
|
||||||
const source = this._template(ctx);
|
const source = this._template(ctx);
|
||||||
|
|
||||||
|
@ -27,14 +30,32 @@ class PostsHeaderView {
|
||||||
form.querySelector('input').focus();
|
form.querySelector('input').focus();
|
||||||
});
|
});
|
||||||
|
|
||||||
form.addEventListener('submit', e => {
|
for (let safetyButton of form.querySelectorAll('.safety')) {
|
||||||
|
safetyButton.addEventListener(
|
||||||
|
'click', e => this._evtSafetyButtonClick(e, ctx.clientUrl));
|
||||||
|
}
|
||||||
|
form.addEventListener(
|
||||||
|
'submit', e => this._evtFormSubmit(e, searchTextInput));
|
||||||
|
|
||||||
|
views.showView(target, source);
|
||||||
|
}
|
||||||
|
|
||||||
|
_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();
|
e.preventDefault();
|
||||||
const text = searchTextInput.value;
|
const text = searchTextInput.value;
|
||||||
searchTextInput.blur();
|
searchTextInput.blur();
|
||||||
page('/posts/' + misc.formatSearchQuery({text: text}));
|
page('/posts/' + misc.formatSearchQuery({text: text}));
|
||||||
});
|
|
||||||
|
|
||||||
views.showView(target, source);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue