Skip to content

Commit

Permalink
Render sources in navigation client-side
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed May 2, 2020
1 parent 40ea713 commit 9db98dd
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 96 deletions.
6 changes: 4 additions & 2 deletions assets/js/selfoss-base.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import templates from './templates';

/**
* base javascript application
*
Expand Down Expand Up @@ -452,8 +454,8 @@ var selfoss = {
* @param sources the new sourceslist as html
*/
refreshSources: function(sources) {
$('#nav-sources li').remove();
$('#nav-sources').append(sources);
let renderedSources = templates.navSources({sources});
$('#nav-sources').html(renderedSources);
if (selfoss.filter.source) {
if (!selfoss.db.isValidSource(selfoss.filter.source)) {
selfoss.ui.showError(selfoss.ui._('error_unknown_source') + ' '
Expand Down
4 changes: 2 additions & 2 deletions assets/js/selfoss-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ selfoss.dbOnline = {
selfoss.refreshTags(data.tagshtml);
}

if ('sourceshtml' in data) {
selfoss.refreshSources(data.sourceshtml);
if ('sources' in data) {
selfoss.refreshSources(data.sources);
}

if ('stats' in data && data.stats.unread > 0 &&
Expand Down
4 changes: 2 additions & 2 deletions assets/js/selfoss-events-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ selfoss.events.navigation = function() {
selfoss.filter.sourcesNav = $('#nav-sources-title').hasClass('nav-sources-collapsed');
if (selfoss.filter.sourcesNav && !selfoss.sourcesNavLoaded) {
$.ajax({
url: 'sources/sourcesStats',
url: 'sources/stats',
type: 'GET',
success: function(data) {
selfoss.refreshSources(data.sources);
selfoss.refreshSources(data);
},
error: function(jqXHR, textStatus, errorThrown) {
selfoss.ui.showError(selfoss.ui._('error_loading_stats') + ' ' +
Expand Down
7 changes: 7 additions & 0 deletions assets/js/templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Unfortunately we need to declare everything manually for Parcel to pick those up.

import navSources from '../templates/source-nav.js';

export default {
navSources
};
8 changes: 8 additions & 0 deletions assets/templates/source-nav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default ({sources}) => sources.map(source =>
<li>
<a href="#" id={`source${source.id}`} class={(source.unread > 0) ? 'unread' : ''} data-source-id={`${source.id}`}>
<span class="nav-source">{`${source.title}`}</span>
<span class="unread">{`${(source.unread > 0) ? `${source.unread}` : ''}`}</span>
</a>
</li>
);
1 change: 0 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
$f3->route('GET /sources', controllers\Sources::class . '->show'); // html
$f3->route('GET /source', controllers\Sources::class . '->add'); // html
$f3->route('GET /sources/list', controllers\Sources::class . '->listSources'); // json
$f3->route('GET /sources/sourcesStats', controllers\Sources::class . '->sourcesStats'); // json
$f3->route('POST /source/@id', controllers\Sources::class . '\Write->write'); // json
$f3->route('POST /source', controllers\Sources::class . '\Write->write'); // json
$f3->route('DELETE /source/@id', controllers\Sources::class . '->remove'); // json
Expand Down
29 changes: 12 additions & 17 deletions src/controllers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,22 @@ public function home(Base $f3) {
// prepare tags display list
$this->view->tags = $this->tagsController->renderTags($tags);

$result = [
'lastUpdate' => \helpers\ViewHelper::date_iso8601($this->itemsDao->lastUpdate()),
'hasMore' => $items['hasMore'],
'entries' => $this->view->content,
'all' => $this->view->statsAll,
'unread' => $this->view->statsUnread,
'starred' => $this->view->statsStarred,
'tags' => $this->view->tags
];

if (isset($options['sourcesNav']) && $options['sourcesNav'] == 'true') {
// prepare sources display list
$sources = $this->sourcesDao->getWithUnread();
$this->view->sources = $this->sourcesController->renderSources($sources);
} else {
$this->view->sources = '';
$result['sources'] = $this->sourcesDao->getWithUnread();
}

// ajax call = only send entries and statistics not full template
if ($f3->ajax()) {
$this->view->jsonSuccess([
'lastUpdate' => \helpers\ViewHelper::date_iso8601($this->itemsDao->lastUpdate()),
'hasMore' => $items['hasMore'],
'entries' => $this->view->content,
'all' => $this->view->statsAll,
'unread' => $this->view->statsUnread,
'starred' => $this->view->statsStarred,
'tags' => $this->view->tags,
'sources' => $this->view->sources
]);
}
$this->view->jsonSuccess($result);
}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/controllers/Items/Stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class Stats {
/** @var \daos\Items items */
private $itemsDao;

/** @var \controllers\Sources sources controller */
private $sourcesController;

/** @var \daos\Sources sources */
private $sourcesDao;

Expand All @@ -30,10 +27,9 @@ class Stats {
/** @var View view helper */
private $view;

public function __construct(Authentication $authentication, \daos\Items $itemsDao, \controllers\Sources $sourcesController, \daos\Sources $sourcesDao, \controllers\Tags $tagsController, \daos\Tags $tagsDao, View $view) {
public function __construct(Authentication $authentication, \daos\Items $itemsDao, \daos\Sources $sourcesDao, \controllers\Tags $tagsController, \daos\Tags $tagsDao, View $view) {
$this->authentication = $authentication;
$this->itemsDao = $itemsDao;
$this->sourcesController = $sourcesController;
$this->sourcesDao = $sourcesDao;
$this->tagsController = $tagsController;
$this->tagsDao = $tagsDao;
Expand Down Expand Up @@ -64,7 +60,7 @@ public function stats() {
$stats['tagshtml'] = $this->tagsController->renderTags($tags);
}
if (array_key_exists('sources', $_GET) && $_GET['sources'] == 'true') {
$stats['sourceshtml'] = $this->sourcesController->renderSources($this->sourcesDao->getWithUnread());
$stats['sources'] = $this->sourcesDao->getWithUnread();
}

$this->view->jsonSuccess($stats);
Expand Down
8 changes: 2 additions & 6 deletions src/controllers/Items/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class Sync {
/** @var \daos\Items items */
private $itemsDao;

/** @var \controllers\Sources sources controller */
private $sourcesController;

/** @var \daos\Sources sources */
private $sourcesDao;

Expand All @@ -31,10 +28,9 @@ class Sync {
/** @var View view helper */
private $view;

public function __construct(Authentication $authentication, \daos\Items $itemsDao, \controllers\Sources $sourcesController, \daos\Sources $sourcesDao, \controllers\Tags $tagsController, \daos\Tags $tagsDao, View $view) {
public function __construct(Authentication $authentication, \daos\Items $itemsDao, \daos\Sources $sourcesDao, \controllers\Tags $tagsController, \daos\Tags $tagsDao, View $view) {
$this->authentication = $authentication;
$this->itemsDao = $itemsDao;
$this->sourcesController = $sourcesController;
$this->sourcesDao = $sourcesDao;
$this->tagsController = $tagsController;
$this->tagsDao = $tagsDao;
Expand Down Expand Up @@ -117,7 +113,7 @@ public function sync(Base $f3) {
$sync['tagshtml'] = $this->tagsController->renderTags($this->tagsDao->getWithUnread());
}
if (array_key_exists('sources', $params) && $_GET['sources'] == 'true') {
$sync['sourceshtml'] = $this->sourcesController->renderSources($this->sourcesDao->getWithUnread());
$sync['sources'] = $this->sourcesDao->getWithUnread();
}

$wantItemsStatuses = array_key_exists('itemsStatuses', $params) && $params['itemsStatuses'] == 'true';
Expand Down
48 changes: 0 additions & 48 deletions src/controllers/Sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,54 +103,6 @@ public function params() {
}
}

/**
* return all Sources suitable for navigation panel
* html
*
* @param array $sources sources to render
*
* @return string htmltext
*/
public function renderSources(array $sources) {
$html = '';
foreach ($sources as $source) {
$this->view->source = $source['title'];
$this->view->sourceid = $source['id'];
$this->view->unread = $source['unread'];
$html .= $this->view->render('src/templates/source-nav.phtml');
}

return $html;
}

/**
* load all available sources and return all Sources suitable
* for navigation panel
* html
*
* @return string htmltext
*/
public function sourcesListAsString() {
$sources = $this->sourcesDao->getWithUnread();

return $this->renderSources($sources);
}

/**
* return source stats in HTML for nav update
* json
*
* @return void
*/
public function sourcesStats() {
$this->authentication->needsLoggedInOrPublicMode();

$this->view->jsonSuccess([
'success' => true,
'sources' => $this->sourcesListAsString()
]);
}

/**
* delete source
* json
Expand Down
8 changes: 2 additions & 6 deletions src/controllers/Sources/Write.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class Write {
/** @var ContentLoader content loader */
private $contentLoader;

/** @var \controllers\Sources sources controller */
private $sourcesController;

/** @var \daos\Sources sources */
private $sourcesDao;

Expand All @@ -36,10 +33,9 @@ class Write {
/** @var View view helper */
private $view;

public function __construct(Authentication $authentication, ContentLoader $contentLoader, \controllers\Sources $sourcesController, \daos\Sources $sourcesDao, SpoutLoader $spoutLoader, \controllers\Tags $tagsController, \daos\Tags $tagsDao, View $view) {
public function __construct(Authentication $authentication, ContentLoader $contentLoader, \daos\Sources $sourcesDao, SpoutLoader $spoutLoader, \controllers\Tags $tagsController, \daos\Tags $tagsDao, View $view) {
$this->authentication = $authentication;
$this->contentLoader = $contentLoader;
$this->sourcesController = $sourcesController;
$this->sourcesDao = $sourcesDao;
$this->spoutLoader = $spoutLoader;
$this->tagsController = $tagsController;
Expand Down Expand Up @@ -147,7 +143,7 @@ public function write(Base $f3, array $params) {
$return['tags'] = $this->tagsController->tagsListAsString();

// get new sources list
$return['sources'] = $this->sourcesController->sourcesListAsString();
$return['sources'] = $this->sourcesDao->getWithUnread();
}

$this->view->jsonSuccess($return);
Expand Down
6 changes: 0 additions & 6 deletions src/templates/source-nav.phtml

This file was deleted.

0 comments on commit 9db98dd

Please sign in to comment.