Create "additionalCss" HBS helper

This commit is contained in:
Dmitriy Simushev 2014-10-09 13:51:37 +00:00
parent bc45a6d3be
commit 9898fb0cbe
9 changed files with 162 additions and 79 deletions

View File

@ -203,8 +203,17 @@ class AssetManager implements AssetManagerInterface
} }
/** /**
* Triggers "pageAddCSS" event and prepares CSS assets which are returned by * Gets additional CSS assets by triggering some events.
* plugins. *
* Triggers "pageAddCSS" and passes to the listeners an associative array
* with the following keys:
* - "request": {@link \Symfony\Component\HttpFoundation\Request}, a
* request instance. CSS files will be attached to the requested page.
* - "css": array of assets. Each asset can be either a string with
* absolute URL of a CSS file or an array with "content" and "type"
* items. See {@link \Mibew\Asset\AssetManagerInterface::getCssAssets()}
* for details of their meaning. Modify this array to add or remove
* additional CSS files.
* *
* @return array Assets list. * @return array Assets list.
*/ */

View File

@ -26,6 +26,7 @@ use Mibew\Authentication\AuthenticationManagerAwareInterface;
use Mibew\Authentication\AuthenticationManagerInterface; use Mibew\Authentication\AuthenticationManagerInterface;
use Mibew\Cache\CacheAwareInterface; use Mibew\Cache\CacheAwareInterface;
use Mibew\Handlebars\HandlebarsAwareInterface; use Mibew\Handlebars\HandlebarsAwareInterface;
use Mibew\Handlebars\Helper\AdditionalCssHelper;
use Mibew\Handlebars\Helper\AdditionalJsHelper; use Mibew\Handlebars\Helper\AdditionalJsHelper;
use Mibew\Handlebars\Helper\AssetHelper; use Mibew\Handlebars\Helper\AssetHelper;
use Mibew\Handlebars\Helper\CsrfProtectedRouteHelper; use Mibew\Handlebars\Helper\CsrfProtectedRouteHelper;
@ -131,6 +132,9 @@ abstract class AbstractController implements
if ($handlebars->hasHelper('additionalJs')) { if ($handlebars->hasHelper('additionalJs')) {
$handlebars->getHelper('additionalJs')->setAssetManager($manager); $handlebars->getHelper('additionalJs')->setAssetManager($manager);
} }
if ($handlebars->hasHelper('additionalCss')) {
$handlebars->getHelper('additionalCss')->setAssetManager($manager);
}
} }
} }
@ -291,6 +295,10 @@ abstract class AbstractController implements
'additionalJs', 'additionalJs',
new AdditionalJsHelper($this->getAssetManager()) new AdditionalJsHelper($this->getAssetManager())
); );
$style->getHandlebars()->addHelper(
'additionalCss',
new AdditionalCssHelper($this->getAssetManager())
);
} }
return $style; return $style;

View File

@ -60,14 +60,11 @@ class OperatorChatController extends AbstractController
return $this->showErrors(array('Cannot view threads')); return $this->showErrors(array('Cannot view threads'));
} }
$page = array_merge_recursive( $page = setup_chatview_for_operator(
setup_chatview_for_operator(
$this->getRouter(), $this->getRouter(),
$request, $request,
$thread, $thread,
$operator $operator
),
get_plugins_data($request)
); );
// Build js application options // Build js application options

View File

@ -47,13 +47,10 @@ class UserChatController extends AbstractController
throw new NotFoundException('The thread is not found.'); throw new NotFoundException('The thread is not found.');
} }
$page = array_merge_recursive( $page = setup_chatview_for_user(
setup_chatview_for_user(
$this->getRouter(), $this->getRouter(),
$request, $request,
$thread $thread
),
get_plugins_data($request)
); );
// Build js application options // Build js application options
@ -148,7 +145,6 @@ class UserChatController extends AbstractController
$info, $info,
$referrer $referrer
), ),
get_plugins_data($request),
array( array(
'mibewBasePath' => $request->getBasePath(), 'mibewBasePath' => $request->getBasePath(),
'mibewBaseUrl' => $request->getBaseUrl(), 'mibewBaseUrl' => $request->getBaseUrl(),
@ -185,7 +181,6 @@ class UserChatController extends AbstractController
$info, $info,
$referrer $referrer
), ),
get_plugins_data($request),
array( array(
'mibewBasePath' => $request->getBasePath(), 'mibewBasePath' => $request->getBasePath(),
'mibewBaseUrl' => $request->getBaseUrl(), 'mibewBaseUrl' => $request->getBaseUrl(),
@ -250,10 +245,7 @@ class UserChatController extends AbstractController
$thread = Thread::load($invitation_state['threadid']); $thread = Thread::load($invitation_state['threadid']);
// Prepare page // Prepare page
$page = array_merge_recursive( $page = setup_invitation_view($thread);
setup_invitation_view($thread),
get_plugins_data($request)
);
// Build js application options // Build js application options
$page['invitationOptions'] = json_encode($page['invitation']); $page['invitationOptions'] = json_encode($page['invitation']);

View File

@ -80,8 +80,6 @@ class UsersController extends AbstractController
$page['title'] = getlocal("List of visitors waiting"); $page['title'] = getlocal("List of visitors waiting");
$page['menuid'] = "users"; $page['menuid'] = "users";
// Get additional plugins data
$page = array_merge($page, get_plugins_data($request));
$page = array_merge($page, prepare_menu($operator)); $page = array_merge($page, prepare_menu($operator));
return $this->render('users', $page); return $this->render('users', $page);

View File

@ -0,0 +1,131 @@
<?php
/*
* This file is a part of Mibew Messenger.
*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Handlebars\Helper;
use Handlebars\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\SafeString;
use Handlebars\Template;
use Mibew\Asset\AssetManagerAwareInterface;
use Mibew\Asset\AssetManagerInterface;
/**
* A helper that generates additional CSS list from assets attached to
* Asset Manager.
*
* Example of usage:
* <code>
* {{additionalCss}}
* </code>
*/
class AdditionalCssHelper implements HelperInterface, AssetManagerAwareInterface
{
/**
* @var AssetManagerInterface|null
*/
protected $manager = null;
/**
* Class constructor.
*
* @param AssetUrlGeneratorInterface $manager An instance of Asset Manager.
*/
public function __construct(AssetManagerInterface $manager)
{
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public function getAssetManager()
{
return $this->manager;
}
/**
* {@inheritdoc}
*/
public function setAssetManager(AssetManagerInterface $manager)
{
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public function execute(Template $template, Context $context, $args, $source)
{
$generator = $this->getAssetManager()->getUrlGenerator();
$buffer = array();
foreach ($this->getAssetManager()->getCssAssets() as $asset) {
switch ($asset['type']) {
case AssetManagerInterface::ABSOLUTE_URL:
$buffer[] = $this->renderUrl($asset['content']);
break;
case AssetManagerInterface::RELATIVE_URL:
$buffer[] = $this->renderUrl($generator->generate($asset['content']));
break;
case AssetManagerInterface::INLINE:
$buffer[] = $this->renderContent($asset['content']);
break;
default:
throw new \RuntimeException(sprintf(
'Unknown asset type "%s"',
$asset['type']
));
}
}
return new SafeString(implode("\n", $buffer));
}
/**
* Renders URL of an asset.
*
* @param string $url URL of an asset.
* @return string HTML markup.
*/
protected function renderUrl($url)
{
return sprintf(
'<link rel="stylesheet" type="text/css" href="%s" />',
safe_htmlspecialchars($url)
);
}
/**
* Renders content of an asset.
*
* @param string $content Content of an asset.
* @return string HTML markup.
*/
protected function renderContent($content)
{
return sprintf(
'<style type="text/css">%s</style>',
$content
);
}
}

View File

@ -17,9 +17,6 @@
* limitations under the License. * limitations under the License.
*/ */
use Mibew\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
function get_popup($href, $js_href, $message, $title, $wnd_name, $options) function get_popup($href, $js_href, $message, $title, $wnd_name, $options)
{ {
if (!$js_href) { if (!$js_href) {
@ -33,55 +30,6 @@ function get_popup($href, $js_href, $message, $title, $wnd_name, $options)
. "this.newWindow.focus();this.newWindow.opener=window;return false;\">$message</a>"; . "this.newWindow.focus();this.newWindow.opener=window;return false;\">$message</a>";
} }
/**
* Load additional CSS files, required by plugins, and build HTML code to
* include them
*
* Triggers 'pageAddCSS' and pass listeners associative array with
* following keys:
* - 'request': {@link \Symfony\Component\HttpFoundation\Request}, a request
* instance. CSS files will be attached to the requested page.
* - 'css': array, with CSS files paths. Modify this array to add or remove
* additional CSS files.
*
* @param Request $request A Request instance.
* @return string HTML block of 'link' tags
*/
function get_additional_css(Request $request)
{
// Prepare event arguments array
$args = array(
'request' => $request,
'css' => array(),
);
// Trigger event
$dispatcher = EventDispatcher::getInstance();
$dispatcher->triggerEvent('pageAddCSS', $args);
// Build resulting css list
$result = array();
foreach ($args['css'] as $css) {
$result[] = '<link rel="stylesheet" type="text/css" href="' . $css . '">';
}
return implode("\n", $result);
}
/**
* Get additional plugins data for specified page
*
* @param Request $request A Request instance.
* @return array Associative array of plugins data. It contains following keys:
* - 'additional_css': contains results of the 'get_additional_css function
*/
function get_plugins_data(Request $request)
{
return array(
'additional_css' => get_additional_css($request),
);
}
function no_field($key) function no_field($key)
{ {
return getlocal('Please fill "{0}".', array(getlocal($key))); return getlocal('Please fill "{0}".', array(getlocal($key)));

View File

@ -4,7 +4,7 @@
<link rel="stylesheet" type="text/css" href="{{asset "@CurrentStyle/chat_ie7.css"}}" media="all" /> <link rel="stylesheet" type="text/css" href="{{asset "@CurrentStyle/chat_ie7.css"}}" media="all" />
<![endif]--> <![endif]-->
{{{additional_css}}} {{additionalCss}}
<!-- External libs --> <!-- External libs -->
<script type="text/javascript" src="{{asset "js/libs/jquery.min.js"}}"></script> <script type="text/javascript" src="{{asset "js/libs/jquery.min.js"}}"></script>

View File

@ -5,7 +5,7 @@
{{#override "head"}} {{#override "head"}}
<!-- Plugins CSS files --> <!-- Plugins CSS files -->
{{{additional_css}}} {{additionalCss}}
<script type="text/javascript" src="{{asset "js/compiled/users_app.js"}}"></script> <script type="text/javascript" src="{{asset "js/compiled/users_app.js"}}"></script>