Example #1
0
 /**
  * @param ScriptContainer $container
  * @param string|number $place
  * @return callable
  */
 protected function getInjector($container, $place)
 {
     $insert = null;
     switch ($place) {
         case self::PLACE_APPEND:
             $insert = function ($jsFile, $type, $attrs) use($container) {
                 $container->appendFile($jsFile, $type, $attrs);
             };
             break;
         case self::PLACE_PREPEND:
             $insert = function ($jsFile, $type, $attrs) use($container) {
                 $container->prependFile($jsFile, $type, $attrs);
             };
             break;
         default:
             if (is_numeric($place)) {
                 $insert = function ($jsFile, $type, $attrs) use($container, &$place) {
                     $container->offsetSetFile($place, $jsFile, $type, $attrs);
                     $place++;
                 };
             }
             break;
     }
     if (!is_callable($insert)) {
         throw new \RuntimeException('Failed to make JS files injector.');
     }
     return $insert;
 }
 /**
  * getNewInstanceAction
  *
  * @return Response
  */
 public function getNewInstanceAction()
 {
     $routeMatch = $this->getEvent()->getRouteMatch();
     $pluginType = $routeMatch->getParam('pluginType');
     $instanceId = $routeMatch->getParam('instanceId');
     $pluginManager = $this->getServiceLocator()->get('Rcm\\Service\\PluginManager');
     if ($instanceId < 0) {
         $instanceConfig = $pluginManager->getDefaultInstanceConfig($pluginType);
     } else {
         $instanceConfig = $pluginManager->getInstanceConfig($instanceId);
     }
     //Allow plugins to preview with an unsaved instance configuration
     $instanceConfigPreview = $this->params()->fromPost('previewInstanceConfig');
     if ($instanceConfigPreview) {
         $instanceConfig = array_merge($instanceConfig, $instanceConfigPreview);
     }
     $viewData = $pluginManager->getPluginViewData($pluginType, $instanceId, $instanceConfig);
     $html = $viewData['html'];
     $headLink = new HeadLink();
     foreach ($viewData['css'] as $css) {
         $cssInfo = unserialize($css);
         $headLink->append($cssInfo);
     }
     $headScript = new HeadScript();
     foreach ($viewData['js'] as $js) {
         $jsInfo = unserialize($js);
         $headScript->append($jsInfo);
     }
     $html = $headLink->toString() . $headScript->toString() . $html;
     $response = new Response();
     $response->setContent($html);
     return $response;
 }
Example #3
0
 /**
  * Loads custom variables (available via config) in the head scripts
  */
 public function __invoke()
 {
     $script = '';
     foreach ($this->bootstrap->getVariables() as $key => $value) {
         $script .= 'var ' . $key . '=' . json_encode($value) . ';' . PHP_EOL;
     }
     $this->headScript->appendScript($script);
 }
Example #4
0
 /**
  * Loading the ExtJs library and CSS in a view
  */
 public function loadLibrary()
 {
     $lib = rtrim($this->config['library_path'], '/') . '/';
     foreach (array_reverse($this->config['css']) as $css) {
         $this->headLink->prependStylesheet($lib . $css);
     }
     foreach (array_reverse($this->config['js']) as $js) {
         $this->headScript->prependFile($lib . $js);
     }
 }
Example #5
0
 /**
  * Appends the required configs in a head script
  */
 public function __invoke()
 {
     $namespaces = $this->bootstrap->getPaths();
     if ($namespaces) {
         foreach ($namespaces as $namespace => $path) {
             if ($path[0] !== '/') {
                 $namespaces[$namespace] = $this->basePath->__invoke($path);
             }
         }
         $data = array('enabled' => true, 'paths' => $namespaces);
         $this->headScript->appendScript('Ext.Loader.setConfig(' . json_encode($data) . ');');
     }
     if ($requires = $this->bootstrap->getRequires()) {
         $this->headScript->appendScript('Ext.syncRequire(' . json_encode($requires) . ');');
     }
 }
Example #6
0
 public function testNoEscapeTrue()
 {
     $this->helper->__invoke()->appendScript('// some script' . PHP_EOL, 'text/javascript', array('noescape' => true));
     $test = $this->helper->__invoke()->toString();
     $this->assertNotContains('//<!--', $test);
     $this->assertNotContains('//-->', $test);
 }
Example #7
0
 /**
  * @return HeadScript
  */
 public function headScript()
 {
     $this->__initialized__ && $this->__initialized__->__invoke();
     if (null === $this->headScript) {
         $this->headScript = new HeadScript();
         $this->headScript->setView($this->view);
     }
     return $this->headScript;
 }
Example #8
0
 /**
  * Sets a javascript asset into the HeadScript or InlineScript
  * @param HeadScript $element
  * @param array $js
  */
 private function setJavascript(HeadScript $element, array $js = array())
 {
     $jsPath = $this->options->getJs()->getPath();
     if (isset($js["options"])) {
         $element->appendFile($jsPath . "/" . $js["name"], "text/javascript", $js["options"]);
     } else {
         $element->appendFile($jsPath . "/" . $js["name"]);
     }
 }
Example #9
0
 /**
  * Find an initialize Dojo helper before rendering head scripts
  * 
  * @see \Zend\View\Helper\HeadScript::toString()
  * @param string $indent
  * @return string
  */
 public function toString($indent = null)
 {
     $helperManager = $this->getView()->getHelperPluginManager();
     if ($helperManager->has('dojo')) {
         $dojo = $helperManager->get('dojo');
         $dojo->initialize();
     }
     return parent::toString($indent);
 }
Example #10
0
 /**
  * Create script HTML
  *
  * @param mixed  $item        Item to convert
  * @param string $indent      String to add before the item
  * @param string $escapeStart Starting sequence
  * @param string $escapeEnd   Ending sequence
  *
  * @return string
  */
 public function itemToString($item, $indent, $escapeStart, $escapeEnd)
 {
     // Normalize href to account for themes:
     if (!empty($item->attributes['src'])) {
         $relPath = 'js/' . $item->attributes['src'];
         $currentTheme = ThemeTools::findContainingTheme($relPath);
         if (!empty($currentTheme)) {
             $urlHelper = $this->getView()->plugin('url');
             $item->attributes['src'] = $urlHelper('home') . "themes/{$currentTheme}/" . $relPath;
         }
     }
     return parent::itemToString($item, $indent, $escapeStart, $escapeEnd);
 }
 /**
  * Create script HTML
  *
  * @param  mixed  $item        Item to convert
  * @param  string $indent      String to add before the item
  * @param  string $escapeStart Starting sequence
  * @param  string $escapeEnd   Ending sequence
  * @return string
  */
 public function itemToString($item, $indent, $escapeStart, $escapeEnd)
 {
     if (!empty($item->source)) {
         $config = $this->getServiceLocator()->getServiceLocator()->get('Config');
         $config = $config['TpMinify']['helpers']['headScript'];
         if ($config['enabled']) {
             $result = Minify::serve('Files', array_merge($config, array('quiet' => true, 'encodeOutput' => false, 'files' => new \Minify_Source(array('contentType' => Minify::TYPE_JS, 'content' => $item->source, 'id' => __CLASS__ . hash('crc32', $item->source))))));
             if ($result['success']) {
                 $item->source = $result['content'];
             }
         }
     }
     return parent::itemToString($item, $indent, $escapeStart, $escapeEnd);
 }
 /**
  * Creates an instance of \Zend\View\Helper\Headscript
  * 
  * - injects the MvcEvent instance
  * 
  * @param ServiceLocatorInterface $serviceLocator
  * @return HeadScript
  * @see \Zend\ServiceManager\FactoryInterface::createService()
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $helper = new HeadScript();
     $services = $serviceLocator->getServiceLocator();
     $config = $services->get('Config');
     if (!isset($config['view_helper_config']['headscript'])) {
         return $helper;
     }
     $config = $config['view_helper_config']['headscript'];
     $routeMatch = $services->get('Application')->getMvcEvent()->getRouteMatch();
     $routeName = $routeMatch ? $routeMatch->getMatchedRouteName() : '';
     $basepath = $serviceLocator->get('basepath');
     foreach ($config as $routeStart => $specs) {
         if (!is_int($routeStart)) {
             if (0 !== strpos($routeName, $routeStart)) {
                 continue;
             }
         } else {
             $specs = array($specs);
         }
         if (is_string($specs)) {
             $helper->appendScript('// if you are missing the script ' . $specs . ' look up your config and enclose it in an array');
             continue;
         }
         foreach ($specs as $spec) {
             if (is_string($spec)) {
                 $helper->appendFile($basepath($spec));
                 continue;
             }
             if ($helper::SCRIPT != $spec[0]) {
                 $spec[1] = $basepath($spec[1]);
             }
             call_user_func_array($helper, $spec);
         }
     }
     return $helper;
 }
Example #13
0
 /**
  * Create script HTML
  *
  * @param mixed  $item        Item to convert
  * @param string $indent      String to add before the item
  * @param string $escapeStart Starting sequence
  * @param string $escapeEnd   Ending sequence
  *
  * @return string
  */
 public function itemToString($item, $indent, $escapeStart, $escapeEnd)
 {
     // Normalize href to account for themes:
     if (!empty($item->attributes['src'])) {
         $relPath = 'js/' . $item->attributes['src'];
         $details = $this->themeInfo->findContainingTheme($relPath, ThemeInfo::RETURN_ALL_DETAILS);
         if (!empty($details)) {
             $urlHelper = $this->getView()->plugin('url');
             $url = $urlHelper('home') . "themes/{$details['theme']}/" . $relPath;
             $url .= strstr($url, '?') ? '&_=' : '?_=';
             $url .= filemtime($details['path']);
             $item->attributes['src'] = $url;
         }
     }
     return parent::itemToString($item, $indent, $escapeStart, $escapeEnd);
 }
Example #14
0
 public function main()
 {
     $config = $this->_serviceManager->getServiceLocator()->get('config');
     if (isset($config["nutrition_strings"])) {
         $this->_strings = ArrayUtils::merge($this->_strings, $config["nutrition_strings"]);
     }
     if (count($this->_strings) > 0) {
         $translator = $this->_serviceManager->getServiceLocator()->get('translator');
         foreach ($this->_strings as $key => $value) {
             $this->_strings[$key] = $translator->translate($value);
         }
         $this->_options["strings"] = $this->_strings;
     }
     $user = new Container('user');
     $options = Json::encode($this->_options);
     $script = 'jQuery(function () { gtm.init(' . $options . '); });';
     $this->_headScript->prependScript($script);
 }
Example #15
0
 public function __invoke($mode = HeadScript::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript')
 {
     return parent::__invoke($mode, $spec, $placement, $attrs, $type);
 }
Example #16
0
 /**
  * Overload method access
  *
  * @param  string                           $method
  *                                                  Method to call
  * @param  array                            $args
  *                                                  Arguments of method
  * @throws Exception\BadMethodCallException if too few arguments or invalid method
  * @return mixed
  */
 public function __call($method, $args)
 {
     $basePath = '';
     if (method_exists($this->view, 'plugin')) {
         $basePath = $this->view->plugin('basepath')->__invoke();
     }
     $ret = false;
     if (preg_match('/^(?P<action>(ap|pre)pend)(?P<mode>Bootstrap|Jquery)$/', $method, $matches)) {
         $ret = $this->callWithCdn($matches, $basePath, $args);
     } elseif (preg_match('/^(?P<action>(ap|pre)pend)(?P<mode>Chosen|Moment)$/', $method, $matches)) {
         $ret = $this->callWithoutCdn($matches, $basePath, $args);
     }
     return $ret !== false ? $ret : parent::__call($method, $args);
 }
Example #17
0
 public function offsetSet($index, $value)
 {
     $value->attributes['src'] = $this->appendCdn($value->attributes['src']);
     parent::offsetSet($index, $value);
 }
Example #18
0
 /**
  * Loads Ext.direct.Manager API configuration in head scripts
  */
 public function __invoke()
 {
     if ($directApi = $this->bootstrap->getDirectApi()) {
         $this->headScript->appendScript($directApi->buildRemotingProvider()->render());
     }
 }
 /**
  * Ensure JS error handler is the first JS on page
  *
  * @param  string|int $indent Amount of whitespaces or string to use for indention
  *
  * @return string
  */
 public function toString($indent = null)
 {
     return "\n<script type=\"text/javascript\" src=\"/modules/rcm-error-handler/js-error-logger.js\"></script>\n" . parent::toString($indent);
 }
Example #20
0
 /**
  * Retrieve string representation
  *
  * @param  string|int $indent Amount of whitespaces or string to use for indention
  * @return string
  */
 public function toString($indent = null)
 {
     $this->preToString();
     return parent::toString($indent);
 }
Example #21
0
 /**
  * Create script HTML
  *
  * @param  mixed  $item        Item to convert
  * @param  string $indent      String to add before the item
  * @param  string $escapeStart Starting sequence
  * @param  string $escapeEnd   Ending sequence
  * @return string
  */
 public function itemToString($item, $indent, $escapeStart, $escapeEnd)
 {
     if (isset($item->attributes) && !empty($item->attributes['src'])) {
         $src = $item->attributes['src'];
         $item->attributes['src'] = $this->startsWith($src, '//') || $this->startsWith($src, 'http') || $this->startsWith($src, 'ftp') ? $src : $this->view->mediapath($src);
     }
     return parent::itemToString($item, $indent, $escapeStart, $escapeEnd);
 }