From 35cdc0cf3a6c39c44a90c0d0bde647f687514fc8 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 1 Feb 2014 11:17:02 +0100 Subject: [PATCH] Refactored scripts and stylesheets Styles, scripts and page titles are no longer set from controllers level. Changed because it was breaking MVC pattern and led to spaghetti code. Also, optimized JS/CSS inclusions a bit. --- src/Bootstrap.php | 23 ++- src/Controllers/AuthController.php | 2 - src/Controllers/CommentController.php | 10 -- src/Controllers/IndexController.php | 5 - src/Controllers/LogController.php | 7 - src/Controllers/PostController.php | 24 ---- src/Controllers/TagController.php | 8 -- src/Controllers/UserController.php | 26 +--- src/Helpers/LayoutHelper.php | 66 +++++++++ src/Views/auth-login.phtml | 5 + src/Views/comment-add.phtml | 5 + src/Views/comment-edit.phtml | 5 + src/Views/comment-list.phtml | 11 ++ src/Views/comment-small.phtml | 6 + src/Views/index-help.phtml | 3 + src/Views/index-index.phtml | 5 + src/Views/layout-json.phtml | 5 +- src/Views/layout-normal.phtml | 34 ++--- src/Views/log-list.phtml | 4 + src/Views/log-view.phtml | 9 ++ src/Views/paginator.phtml | 6 + src/Views/post-file-render.phtml | 1 + src/Views/post-list-wrapper.phtml | 2 + src/Views/post-list.phtml | 5 + src/Views/post-small.phtml | 30 ++-- src/Views/post-upload.phtml | 8 ++ src/Views/post-view.phtml | 8 ++ src/Views/sidebar-options.phtml | 50 +++---- src/Views/tag-list-wrapper.phtml | 15 +- src/Views/tag-list.phtml | 18 +-- src/Views/top-navigation.phtml | 200 +++++++++++++------------- src/Views/user-list.phtml | 26 ++-- src/Views/user-registration.phtml | 8 ++ src/Views/user-select.phtml | 4 + src/Views/user-view.phtml | 5 + 35 files changed, 374 insertions(+), 275 deletions(-) create mode 100644 src/Helpers/LayoutHelper.php diff --git a/src/Bootstrap.php b/src/Bootstrap.php index 0f00a2ff..cef2078b 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -7,19 +7,7 @@ class Bootstrap session_start(); $this->context->handleExceptions = false; - $this->context->title = $this->config->main->title; - $this->context->stylesheets = - [ - '../lib/jquery-ui/jquery-ui.css', - 'core.css', - ]; - $this->context->scripts = - [ - '../lib/jquery/jquery.min.js', - '../lib/jquery-ui/jquery-ui.min.js', - '../lib/mousetrap/mousetrap.min.js', - 'core.js', - ]; + LayoutHelper::setTitle($this->config->main->title); $this->context->json = isset($_GET['json']); $this->context->layoutName = $this->context->json @@ -39,7 +27,14 @@ class Bootstrap try { - $workCallback(); + if ($this->context->layoutName == 'layout-normal') + { + ob_start(['LayoutHelper', 'transformHtml']); + $workCallback(); + ob_end_flush(); + } + else + $workCallback(); } catch (\Chibi\MissingViewFileException $e) { diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index 6cd94b9b..ad4fa6b5 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -55,8 +55,6 @@ class AuthController public function loginAction() { $this->context->handleExceptions = true; - $this->context->stylesheets []= 'auth.css'; - $this->context->subTitle = 'authentication form'; //check if already logged in if ($this->context->loggedIn) diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index 25694680..0af886af 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -8,18 +8,8 @@ class CommentController */ public function listAction($page) { - $this->context->stylesheets []= 'post-small.css'; - $this->context->stylesheets []= 'comment-list.css'; - $this->context->stylesheets []= 'comment-small.css'; - $this->context->stylesheets []= 'comment-edit.css'; - $this->context->stylesheets []= 'paginator.css'; - if ($this->context->user->hasEnabledEndlessScrolling()) - $this->context->scripts []= 'paginator-endless.js'; - $this->context->scripts []= 'comment-edit.js'; - $page = intval($page); $commentsPerPage = intval($this->config->comments->commentsPerPage); - $this->context->subTitle = 'comments'; PrivilegesHelper::confirmWithException(Privilege::ListComments); $page = max(1, $page); diff --git a/src/Controllers/IndexController.php b/src/Controllers/IndexController.php index 6a072f66..05016c7d 100644 --- a/src/Controllers/IndexController.php +++ b/src/Controllers/IndexController.php @@ -7,8 +7,6 @@ class IndexController */ public function indexAction() { - $this->context->subTitle = 'home'; - $this->context->stylesheets []= 'index-index.css'; $this->context->transport->postCount = PostModel::getCount(); $featuredPost = $this->getFeaturedPost(); @@ -17,7 +15,6 @@ class IndexController $this->context->featuredPost = $featuredPost; $this->context->featuredPostDate = PropertyModel::get(PropertyModel::FeaturedPostDate); $this->context->featuredPostUser = UserModel::findByNameOrEmail(PropertyModel::get(PropertyModel::FeaturedPostUserName), false); - $this->context->pageThumb = \Chibi\UrlHelper::route('post', 'thumb', ['name' => $featuredPost->name]); } } @@ -33,8 +30,6 @@ class IndexController if (!isset($this->config->help->paths[$tab])) throw new SimpleException('Invalid tab'); $this->context->path = TextHelper::absolutePath($this->config->help->paths[$tab]); - $this->context->stylesheets []= 'index-help.css'; - $this->context->subTitle = 'help'; $this->context->tab = $tab; } diff --git a/src/Controllers/LogController.php b/src/Controllers/LogController.php index e11cdab4..90452102 100644 --- a/src/Controllers/LogController.php +++ b/src/Controllers/LogController.php @@ -6,7 +6,6 @@ class LogController */ public function listAction() { - $this->context->subTitle = 'latest logs'; PrivilegesHelper::confirmWithException(Privilege::ListLogs); $path = TextHelper::absolutePath($this->config->main->logsPath); @@ -49,12 +48,6 @@ class LogController return; } - $this->context->subTitle = 'logs (' . $name . ')'; - $this->context->stylesheets []= 'logs.css'; - $this->context->stylesheets []= 'paginator.css'; - $this->context->scripts []= 'logs.js'; - if ($this->context->user->hasEnabledEndlessScrolling()) - $this->context->scripts []= 'paginator-endless.js'; PrivilegesHelper::confirmWithException(Privilege::ViewLog); //parse input diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index a2d9b45d..47b0cf33 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -1,13 +1,6 @@ context->stylesheets []= '../lib/tagit/jquery.tagit.css'; - $this->context->scripts []= '../lib/tagit/jquery.tagit.js'; - $callback(); - } - private static function handleUploadErrors($file) { switch ($file['error']) @@ -52,12 +45,6 @@ class PostController public function listAction($query = null, $page = 1, $source = 'posts', $additionalInfo = null) { $this->context->viewName = 'post-list-wrapper'; - $this->context->stylesheets []= 'post-small.css'; - $this->context->stylesheets []= 'post-list.css'; - $this->context->stylesheets []= 'paginator.css'; - $this->context->scripts []= 'post-list.js'; - if ($this->context->user->hasEnabledEndlessScrolling()) - $this->context->scripts []= 'paginator-endless.js'; $this->context->source = $source; $this->context->additionalInfo = $additionalInfo; @@ -77,7 +64,6 @@ class PostController $query = trim($query); $page = max(1, intval($page)); $postsPerPage = intval($this->config->browsing->postsPerPage); - $this->context->subTitle = 'posts'; $this->context->transport->searchQuery = $query; $this->context->transport->lastSearchQuery = $query; PrivilegesHelper::confirmWithException(Privilege::ListPosts); @@ -180,9 +166,6 @@ class PostController */ public function uploadAction() { - $this->context->stylesheets []= 'post-upload.css'; - $this->context->scripts []= 'post-upload.js'; - $this->context->subTitle = 'upload'; PrivilegesHelper::confirmWithException(Privilege::UploadPost); if ($this->config->registration->needEmailForUploading) PrivilegesHelper::confirmEmail($this->context->user); @@ -458,13 +441,6 @@ class PostController $score = $this->context->user->getScore($post); $flagged = in_array(TextHelper::reprPost($post), SessionHelper::get('flagged', [])); - $this->context->pageThumb = \Chibi\UrlHelper::route('post', 'thumb', ['name' => $post->name]); - $this->context->stylesheets []= 'post-view.css'; - $this->context->stylesheets []= 'comment-small.css'; - $this->context->stylesheets []= 'comment-edit.css'; - $this->context->scripts []= 'post-view.js'; - $this->context->scripts []= 'comment-edit.js'; - $this->context->subTitle = 'showing ' . TextHelper::reprPost($post) . ' – ' . TextHelper::reprTags($post->getTags()); $this->context->favorite = $favorite; $this->context->score = $score; $this->context->flagged = $flagged; diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index f385bd2d..505ce4d9 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -8,8 +8,6 @@ class TagController */ public function listAction($filter = null) { - $this->context->stylesheets []= 'tag-list.css'; - $this->context->subTitle = 'tags'; $this->context->viewName = 'tag-list-wrapper'; PrivilegesHelper::confirmWithException(Privilege::ListTags); @@ -32,8 +30,6 @@ class TagController */ public function mergeAction() { - $this->context->stylesheets []= 'tag-list.css'; - $this->context->subTitle = 'tags'; $this->context->viewName = 'tag-list-wrapper'; PrivilegesHelper::confirmWithException(Privilege::MergeTags); @@ -60,8 +56,6 @@ class TagController */ public function renameAction() { - $this->context->stylesheets []= 'tag-list.css'; - $this->context->subTitle = 'tags'; $this->context->viewName = 'tag-list-wrapper'; PrivilegesHelper::confirmWithException(Privilege::MergeTags); @@ -88,8 +82,6 @@ class TagController */ public function massTagRedirectAction() { - $this->context->stylesheets []= 'tag-list.css'; - $this->context->subTitle = 'tags'; $this->context->viewName = 'tag-list-wrapper'; PrivilegesHelper::confirmWithException(Privilege::MassTag); diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 0e6f8b28..56cb1a68 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -8,8 +8,6 @@ class UserController $this->context->transport->user = $user; $this->context->handleExceptions = true; $this->context->viewName = 'user-view'; - $this->context->stylesheets []= 'user-view.css'; - $this->context->subTitle = $user->name; } private static function sendTokenizedEmail( @@ -109,11 +107,6 @@ class UserController */ public function listAction($sortStyle, $page) { - $this->context->stylesheets []= 'user-list.css'; - $this->context->stylesheets []= 'paginator.css'; - if ($this->context->user->hasEnabledEndlessScrolling()) - $this->context->scripts []= 'paginator-endless.js'; - if ($sortStyle == '' or $sortStyle == 'alpha') $sortStyle = 'alpha,asc'; if ($sortStyle == 'date') @@ -121,7 +114,6 @@ class UserController $page = intval($page); $usersPerPage = intval($this->config->browsing->usersPerPage); - $this->context->subTitle = 'users'; PrivilegesHelper::confirmWithException(Privilege::ListUsers); $page = max(1, $page); @@ -422,12 +414,6 @@ class UserController PrivilegesHelper::confirmWithException(Privilege::ViewUser, PrivilegesHelper::getIdentitySubPrivilege($user)); $this->loadUserView($user); - $this->context->stylesheets []= 'post-list.css'; - $this->context->stylesheets []= 'post-small.css'; - $this->context->stylesheets []= 'paginator.css'; - $this->context->scripts []= 'post-list.js'; - if ($this->context->user->hasEnabledEndlessScrolling()) - $this->context->scripts []= 'paginator-endless.js'; $query = ''; if ($tab == 'uploads') @@ -483,8 +469,6 @@ class UserController public function registrationAction() { $this->context->handleExceptions = true; - $this->context->stylesheets []= 'auth.css'; - $this->context->subTitle = 'registration form'; //check if already logged in if ($this->context->loggedIn) @@ -570,8 +554,8 @@ class UserController */ public function activationAction($token) { - $this->context->subTitle = 'account activation'; $this->context->viewName = 'message'; + LayoutHelper::setSubTitle('account activation'); $dbToken = TokenModel::findByToken($token); TokenModel::checkValidity($dbToken); @@ -603,8 +587,8 @@ class UserController */ public function passwordResetAction($token) { - $this->context->subTitle = 'password reset'; $this->context->viewName = 'message'; + LayoutHelper::setSubTitle('password reset'); $dbToken = TokenModel::findByToken($token); TokenModel::checkValidity($dbToken); @@ -637,9 +621,8 @@ class UserController */ public function passwordResetProxyAction() { - $this->context->subTtile = 'password reset'; $this->context->viewName = 'user-select'; - $this->context->stylesheets []= 'auth.css'; + LayoutHelper::setSubTitle('password reset'); if (InputHelper::get('submit')) { @@ -658,9 +641,8 @@ class UserController */ public function activationProxyAction() { - $this->context->subTitle = 'account activation'; $this->context->viewName = 'user-select'; - $this->context->stylesheets []= 'auth.css'; + LayoutHelper::setSubTitle('account activation'); if (InputHelper::get('submit')) { diff --git a/src/Helpers/LayoutHelper.php b/src/Helpers/LayoutHelper.php new file mode 100644 index 00000000..a4ce0a92 --- /dev/null +++ b/src/Helpers/LayoutHelper.php @@ -0,0 +1,66 @@ +' . $title . ''; + + $headSnippet .= ''; + $headSnippet .= ''; + if (!empty(self::$pageThumb)) + $headSnippet .= ''; + + foreach (array_unique(self::$stylesheets) as $name) + $headSnippet .= ''; + + foreach (array_unique(self::$scripts) as $name) + $bodySnippet .= ''; + + $bodySnippet .= ''; + + $html = str_replace('', $headSnippet . '', $html); + $html = str_replace('', $bodySnippet . '', $html); + return $html; + } +} diff --git a/src/Views/auth-login.phtml b/src/Views/auth-login.phtml index becf533b..17ad8288 100644 --- a/src/Views/auth-login.phtml +++ b/src/Views/auth-login.phtml @@ -1,3 +1,8 @@ + +

If you don't have an account yet,
click here to create a new one.

diff --git a/src/Views/comment-add.phtml b/src/Views/comment-add.phtml index 121e8595..2ef5c8d6 100644 --- a/src/Views/comment-add.phtml +++ b/src/Views/comment-add.phtml @@ -1,3 +1,8 @@ + +

add comment

diff --git a/src/Views/comment-edit.phtml b/src/Views/comment-edit.phtml index bc46c0e6..790019b1 100644 --- a/src/Views/comment-edit.phtml +++ b/src/Views/comment-edit.phtml @@ -1,3 +1,8 @@ + +

edit comment

diff --git a/src/Views/comment-list.phtml b/src/Views/comment-list.phtml index 0e92cb74..5fc005ff 100644 --- a/src/Views/comment-list.phtml +++ b/src/Views/comment-list.phtml @@ -1,6 +1,17 @@ + + context->transport->comments)): ?>

No comments to show.

+ +
+
context->comment->getCommenter() ?> diff --git a/src/Views/index-help.phtml b/src/Views/index-help.phtml index 427e946f..59cbee89 100644 --- a/src/Views/index-help.phtml +++ b/src/Views/index-help.phtml @@ -1,4 +1,7 @@ config->help->subTitles; $firstTab = !empty($tabs) ? array_keys($tabs)[0] : null; ?> diff --git a/src/Views/index-index.phtml b/src/Views/index-index.phtml index 9daefc79..e5523586 100644 --- a/src/Views/index-index.phtml +++ b/src/Views/index-index.phtml @@ -1,3 +1,8 @@ + +

config->main->title ?>

diff --git a/src/Views/layout-json.phtml b/src/Views/layout-json.phtml index 9086c6f9..70db1f48 100644 --- a/src/Views/layout-json.phtml +++ b/src/Views/layout-json.phtml @@ -1,2 +1,3 @@ - -context->transport, '/.*(email|confirm|pass|salt)/i') ?> +context->transport, '/.*(email|confirm|pass|salt)/i'); diff --git a/src/Views/layout-normal.phtml b/src/Views/layout-normal.phtml index ab9a90cf..3ceef2f5 100644 --- a/src/Views/layout-normal.phtml +++ b/src/Views/layout-normal.phtml @@ -1,23 +1,17 @@ + + - context->subTitle) - ? sprintf('%s – %s', $this->context->title, $this->context->subTitle) - : $this->context->title - ?> - <?php echo $title ?> - context->stylesheets) as $name): ?> - - - - - - context->pageThumb)): ?> - - @@ -64,16 +58,6 @@ - context->scripts) as $name): ?> - - - -

diff --git a/src/Views/log-list.phtml b/src/Views/log-list.phtml index 3c4c4611..4c100ee6 100644 --- a/src/Views/log-list.phtml +++ b/src/Views/log-list.phtml @@ -1,3 +1,7 @@ +context->subTitle = 'latest logs'; +?> + context->transport->logs)): ?>

No logs to show.

diff --git a/src/Views/log-view.phtml b/src/Views/log-view.phtml index 08c8b96c..aa8d5ad6 100644 --- a/src/Views/log-view.phtml +++ b/src/Views/log-view.phtml @@ -1,6 +1,15 @@ + + context->transport->lines)): ?>

This log is empty. Go back

+ + Keep only lines that contain: diff --git a/src/Views/paginator.phtml b/src/Views/paginator.phtml index b4df0eed..28c935ba 100644 --- a/src/Views/paginator.phtml +++ b/src/Views/paginator.phtml @@ -38,6 +38,12 @@ if (!function_exists('pageUrl')) ?> + context->user->hasEnabledEndlessScrolling()) + LayoutHelper::addScript('paginator-endless.js'); + ?> +