/** * 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); }
/** * 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) . ');'); } }
public function init($init) { if (!empty($init)) { $script = ""; $init = is_array($init) ? $init : array($init); foreach ($init as $value) { $script .= "try{" . $value . "}catch(e){alert('TrackMyTruck JS INIT ERROR: '+e)}\n"; } $script = 'jQuery(function () {' . "\n" . $script . '});'; $this->_headScript->appendScript($script); } }
public function testIndentationIsHonored() { $this->helper->setIndent(4); $this->helper->appendScript(' var foo = "bar"; document.write(foo.strlen());'); $this->helper->appendScript(' var bar = "baz"; document.write(bar.strlen());'); $string = $this->helper->toString(); $scripts = substr_count($string, ' <script'); $this->assertEquals(2, $scripts); $this->assertContains(' //', $string); $this->assertContains('var', $string); $this->assertContains('document', $string); $this->assertContains(' document', $string); }
/** * 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; }
/** * Loads Ext.direct.Manager API configuration in head scripts */ public function __invoke() { if ($directApi = $this->bootstrap->getDirectApi()) { $this->headScript->appendScript($directApi->buildRemotingProvider()->render()); } }