From bc45a6d3bec61804c223325630646c4943173d74 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 9 Oct 2014 13:25:17 +0000 Subject: [PATCH] Create "additionalJs" HBS helper --- src/mibew/js/source/chat/app.js | 3 - src/mibew/js/source/users/app.js | 3 - .../libs/classes/Mibew/Asset/AssetManager.php | 22 ++- .../Mibew/Controller/AbstractController.php | 8 ++ .../Handlebars/Helper/AdditionalJsHelper.php | 131 ++++++++++++++++++ src/mibew/libs/common/response.php | 70 ---------- .../templates_src/server_side/chat.handlebars | 5 +- .../server_side/users.handlebars | 5 +- 8 files changed, 163 insertions(+), 84 deletions(-) create mode 100644 src/mibew/libs/classes/Mibew/Handlebars/Helper/AdditionalJsHelper.php diff --git a/src/mibew/js/source/chat/app.js b/src/mibew/js/source/chat/app.js index f04fd1a9..d8e2012c 100644 --- a/src/mibew/js/source/chat/app.js +++ b/src/mibew/js/source/chat/app.js @@ -28,9 +28,6 @@ // Initialize application app.addInitializer(function(options){ - // Store plugin options - Mibew.PluginOptions = options.plugins || {}; - // Initialize Server Mibew.Objects.server = new Mibew.Server(_.extend( {'interactionType': MibewAPIChatInteraction}, diff --git a/src/mibew/js/source/users/app.js b/src/mibew/js/source/users/app.js index 6c2f3ce5..22917f82 100644 --- a/src/mibew/js/source/users/app.js +++ b/src/mibew/js/source/users/app.js @@ -51,9 +51,6 @@ // Initialize application App.addInitializer(function(options){ - // Store plugin options - Mibew.PluginOptions = options.plugins || {}; - // Create some shortcuts var objs = Mibew.Objects; var models = Mibew.Objects.Models; diff --git a/src/mibew/libs/classes/Mibew/Asset/AssetManager.php b/src/mibew/libs/classes/Mibew/Asset/AssetManager.php index 159f9e40..1de8c23b 100644 --- a/src/mibew/libs/classes/Mibew/Asset/AssetManager.php +++ b/src/mibew/libs/classes/Mibew/Asset/AssetManager.php @@ -151,8 +151,26 @@ class AssetManager implements AssetManagerInterface } /** - * Triggers "pageAddJS" and "pageAddJSPluginOptions" events and prepares JS - * assets which are returned by plugins. + * Gets additional JS assets by triggering some events. + * + * Triggers "pageAddJS" and pass to the listeners an associative array with + * the following keys: + * - "request": {@link \Symfony\Component\HttpFoundation\Request}, a + * request instance. JavaScript files will be attached to the requested + * page. + * - "js": array of assets. Each asset can be either string with absolute + * JavaScript file URL or an array with "content" and "type" items. See + * {@link \Mibew\Asset\AssetManagerInterface::getJsAssets()} for details + * of their meaning. Modify this array to add or remove additional + * JavaScript files. + * + * Triggers "pageAddJSPluginOptions" and pass to the listeners an + * associative array with the following keys: + * - "request": {@link \Symfony\Component\HttpFoundation\Request}, a + * request instance. Plugins will work at the requested page. + * - "plugins": associative array, whose keys are plugins names and values + * are plugins options. Modify this array to add or change plugins + * options. * * @return array Assets list. */ diff --git a/src/mibew/libs/classes/Mibew/Controller/AbstractController.php b/src/mibew/libs/classes/Mibew/Controller/AbstractController.php index 7e01ed64..e20c9635 100644 --- a/src/mibew/libs/classes/Mibew/Controller/AbstractController.php +++ b/src/mibew/libs/classes/Mibew/Controller/AbstractController.php @@ -26,6 +26,7 @@ use Mibew\Authentication\AuthenticationManagerAwareInterface; use Mibew\Authentication\AuthenticationManagerInterface; use Mibew\Cache\CacheAwareInterface; use Mibew\Handlebars\HandlebarsAwareInterface; +use Mibew\Handlebars\Helper\AdditionalJsHelper; use Mibew\Handlebars\Helper\AssetHelper; use Mibew\Handlebars\Helper\CsrfProtectedRouteHelper; use Mibew\Handlebars\Helper\RouteHelper; @@ -127,6 +128,9 @@ abstract class AbstractController implements if ($handlebars->hasHelper('asset')) { $handlebars->getHelper('asset')->setAssetUrlGenerator($manager->getUrlGenerator()); } + if ($handlebars->hasHelper('additionalJs')) { + $handlebars->getHelper('additionalJs')->setAssetManager($manager); + } } } @@ -283,6 +287,10 @@ abstract class AbstractController implements array('CurrentStyle' => $style->getFilesPath()) ) ); + $style->getHandlebars()->addHelper( + 'additionalJs', + new AdditionalJsHelper($this->getAssetManager()) + ); } return $style; diff --git a/src/mibew/libs/classes/Mibew/Handlebars/Helper/AdditionalJsHelper.php b/src/mibew/libs/classes/Mibew/Handlebars/Helper/AdditionalJsHelper.php new file mode 100644 index 00000000..44f7313e --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Handlebars/Helper/AdditionalJsHelper.php @@ -0,0 +1,131 @@ + + * {{additionalJs}} + * + */ +class AdditionalJsHelper 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()->getJsAssets() 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( + '', + 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( + '', + $content + ); + } +} diff --git a/src/mibew/libs/common/response.php b/src/mibew/libs/common/response.php index a60bd6d4..e3530b76 100644 --- a/src/mibew/libs/common/response.php +++ b/src/mibew/libs/common/response.php @@ -68,87 +68,17 @@ function get_additional_css(Request $request) return implode("\n", $result); } -/** - * Load additional JavaScript files, required by plugins, and build HTML code - * to include them - * - * Triggers 'pageAddJS' and pass listeners associative array with - * following keys: - * - 'request': {@link \Symfony\Component\HttpFoundation\Request}, a request - * instance. JavaScript files will be attached to the requested page. - * - 'js': array, with JavaScript files paths. Modify this array to add or - * remove additional JavaScript files. - * - * @param Request $request A Request instance. - * @return string HTML block of 'script' tags - */ -function get_additional_js(Request $request) -{ - // Prepare event arguments array - $args = array( - 'request' => $request, - 'js' => array() - ); - - // Trigger event - $dispatcher = EventDispatcher::getInstance(); - $dispatcher->triggerEvent('pageAddJS', $args); - - // Build resulting css list - $result = array(); - foreach ($args['js'] as $js) { - $result[] = ''; - } - - return implode("\n", $result); -} - -/** - * Build Javascript code that contains initializing options for JavaScript - * plugins - * - * Triggers 'pageAddJSPluginOptions' and pass listeners associative array with - * following keys: - * - 'request': {@link \Symfony\Component\HttpFoundation\Request}, a request - * instance. Plugins will work at the requested page. - * - 'plugins': associative array, whose keys are plugins names and values are - * plugins options. Modify this array to add or change plugins options - * - * @param Request $request A Request instance. - * @return string JavaScript options block - */ -function get_js_plugin_options(Request $request) -{ - // Prepare event arguments array - $args = array( - 'request' => $request, - 'plugins' => array() - ); - - // Trigger event - $dispatcher = EventDispatcher::getInstance(); - $dispatcher->triggerEvent('pageAddJSPluginOptions', $args); - - // Return encoded options - return json_encode($args['plugins']); -} - /** * 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 - * - 'additional_js': contains results of the 'get_additional_js' function - * - 'js_plugin_options': contains results of the 'get_js_plugin_options' - * function */ function get_plugins_data(Request $request) { return array( 'additional_css' => get_additional_css($request), - 'additional_js' => get_additional_js($request), - 'js_plugin_options' => get_js_plugin_options($request) ); } diff --git a/src/mibew/styles/dialogs/default/templates_src/server_side/chat.handlebars b/src/mibew/styles/dialogs/default/templates_src/server_side/chat.handlebars index 2cee7c10..97fd415d 100644 --- a/src/mibew/styles/dialogs/default/templates_src/server_side/chat.handlebars +++ b/src/mibew/styles/dialogs/default/templates_src/server_side/chat.handlebars @@ -28,7 +28,7 @@ - {{{additional_js}}} + {{additionalJs}} diff --git a/src/mibew/styles/pages/default/templates_src/server_side/users.handlebars b/src/mibew/styles/pages/default/templates_src/server_side/users.handlebars index ac5cea08..8988a911 100644 --- a/src/mibew/styles/pages/default/templates_src/server_side/users.handlebars +++ b/src/mibew/styles/pages/default/templates_src/server_side/users.handlebars @@ -10,7 +10,7 @@ - {{{additional_js}}} + {{additionalJs}}