Changed last search query tracking to server-side

Recent changes to last search query handling still haven't fixed all
possible routes, so I've simplified the whole thing at expense of clean
URLs.
This commit is contained in:
Marcin Kurczewski 2014-08-09 12:15:03 +02:00
parent 1ea7c187ab
commit a9a5bea1c7
5 changed files with 23 additions and 34 deletions

View file

@ -25,12 +25,6 @@ function getCookie(name)
return unescape(value.substring(start, end));
}
function rememberLastSearchQuery()
{
var lastSearchQuery = $('#settings').attr('data-last-search-query');
setCookie('last-search-query', lastSearchQuery);
}
//core functionalities, prototypes
function getJSON(data)
{
@ -52,7 +46,6 @@ function postJSON(data)
function getHtml(data)
{
rememberLastSearchQuery();
return $.get(data);
}
@ -77,19 +70,6 @@ $.fn.bindOnce = function(name, eventName, callback)
//basic event listeners
$(function()
{
$(window).on('beforeunload', function()
{
rememberLastSearchQuery();
});
if (window.history && window.history.pushState)
{
$(window).on('popstate', function()
{
rememberLastSearchQuery();
});
}
rememberLastSearchQuery();
$('body').bind('dom-update', function()
{
//event confirmations

View file

@ -11,7 +11,6 @@ class PostController extends AbstractController
{
$query = trim($query);
$context->transport->searchQuery = $query;
$context->transport->lastSearchQuery = $query;
if ($source == 'mass-tag')
{
Access::assert(new Privilege(Privilege::MassTag));
@ -49,7 +48,6 @@ class PostController extends AbstractController
$additionalInfo = trim(InputHelper::get('tag'));
$context->transport->searchQuery = $query;
$context->transport->lastSearchQuery = $query;
if (strpos($query, '/') !== false)
throw new SimpleException('Search query contains invalid characters');
@ -284,7 +282,7 @@ class PostController extends AbstractController
$this->redirectToGenericView($identifier);
}
public function genericView($identifier)
public function genericView($identifier, $query = null)
{
$context = Core::getContext();
@ -294,17 +292,17 @@ class PostController extends AbstractController
try
{
$context->transport->lastSearchQuery = InputHelper::get('last-search-query');
$context->transport->searchQuery = $query;
list ($prevPostId, $nextPostId) =
PostSearchService::getPostIdsAround(
$context->transport->lastSearchQuery, $identifier);
$context->transport->searchQuery, $identifier);
}
#search for some reason was invalid, e.g. tag was deleted in the meantime
catch (Exception $e)
{
$context->transport->lastSearchQuery = '';
$context->transport->searchQuery = '';
list ($prevPostId, $nextPostId) =
PostSearchService::getPostIdsAround($context->transport->lastSearchQuery, $identifier);
PostSearchService::getPostIdsAround($context->transport->searchQuery, $identifier);
}
$isUserFavorite = Auth::getCurrentUser()->hasFavorited($post);

View file

@ -63,8 +63,9 @@ class Router extends \Chibi\Routing\Router
$this->register(['PostController', 'listView'], 'GET', '/{source}/{query}/{additionalInfo}/{page}', $postValidation);
$this->register(['PostController', 'listRedirectAction'], 'POST', '/{source}-redirect', $postValidation);
$this->register(['PostController', 'genericView'], 'GET', '/post/{identifier}', $postValidation);
$this->register(['PostController', 'fileView'], 'GET', '/post/{name}/retrieve', $postValidation);
$this->register(['PostController', 'genericView'], 'GET', '/post/{identifier}', $postValidation);
$this->register(['PostController', 'genericView'], 'GET', '/post/{identifier}/search={query}', $postValidation);
$this->register(['PostController', 'thumbnailView'], 'GET', '/post/{name}/thumb', $postValidation);
$this->register(['PostController', 'toggleTagAction'], null, '/post/{identifier}/toggle-tag/{tag}/{enable}', $postValidation);

View file

@ -45,7 +45,11 @@ if ($masstag)
<?php if (Auth::getCurrentUser()->getSettings()->hasEnabledPostTagTitles()): ?>
title="<?= TextHelper::reprTags($this->context->post->getTags()) ?>"
<?php endif ?>
href="<?= Core::getRouter()->linkTo(['PostController', 'genericView'], ['identifier' => $this->context->post->getId()]) ?>">
href="<?= Core::getRouter()->linkTo(['PostController', 'genericView'],
empty($this->context->transport->searchQuery)
? ['identifier' => $this->context->post->getId()]
: ['identifier' => $this->context->post->getId(),
'query' => $this->context->transport->searchQuery]) ?>">
<img
class="thumb"

View file

@ -33,7 +33,10 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<?php if ($this->context->transport->nextPostId): ?>
<a href="<?= Core::getRouter()->linkTo(
['PostController', 'genericView'],
['identifier' => $this->context->transport->nextPostId]) ?>">
empty($this->context->transport->searchQuery)
? ['identifier' => $this->context->transport->nextPostId]
: ['identifier' => $this->context->transport->nextPostId,
'query' => $this->context->transport->searchQuery]) ?>">
<?php else: ?>
<a class="disabled">
<?php endif ?>
@ -46,7 +49,10 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<?php if ($this->context->transport->prevPostId): ?>
<a href="<?= Core::getRouter()->linkTo(
['PostController', 'genericView'],
['identifier' => $this->context->transport->prevPostId]) ?>">
empty($this->context->transport->searchQuery)
? ['identifier' => $this->context->transport->prevPostId]
: ['identifier' => $this->context->transport->prevPostId,
'query' => $this->context->transport->searchQuery]) ?>">
<?php else: ?>
<a class="disabled">
<?php endif ?>
@ -57,13 +63,13 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<div class="clear"></div>
<?php if (!empty($this->context->transport->lastSearchQuery)): ?>
<?php if (!empty($this->context->transport->searchQuery)): ?>
<div class="text">
Current&nbsp;search:<br/>
<a href="<?= Core::getRouter()->linkTo(
['PostController', 'listView'],
['query' => $this->context->transport->lastSearchQuery]) ?>">
<?= $this->context->transport->lastSearchQuery ?>
['query' => $this->context->transport->searchQuery]) ?>">
<?= $this->context->transport->searchQuery ?>
</a>
</div>
<?php endif ?>