public function collectRuntimeJavaScripts(CollectAssetsEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
 {
     if ($eventName == ThemePlusEvents::COLLECT_HEAD_JAVASCRIPT_ASSETS && $event->getLayout()->theme_plus_default_javascript_position != 'head' || $eventName == ThemePlusEvents::COLLECT_BODY_JAVASCRIPT_ASSETS && $event->getLayout()->theme_plus_default_javascript_position != 'body') {
         return;
     }
     if (is_array($GLOBALS['TL_JAVASCRIPT']) && !empty($GLOBALS['TL_JAVASCRIPT'])) {
         foreach ($GLOBALS['TL_JAVASCRIPT'] as $javaScript) {
             if ($javaScript instanceof AssetInterface) {
                 $event->append($javaScript);
             } else {
                 list($javaScript, $mode) = explode('|', $javaScript);
                 $stripStaticDomainEvent = new StripStaticDomainEvent($event->getRenderMode(), $event->getPage(), $event->getLayout(), $javaScript);
                 $eventDispatcher->dispatch(ThemePlusEvents::STRIP_STATIC_DOMAIN, $stripStaticDomainEvent);
                 $javaScript = $stripStaticDomainEvent->getUrl();
                 $asset = new ExtendedFileAsset(TL_ROOT . '/' . $javaScript, [], TL_ROOT, $javaScript);
                 $asset->setStandalone($mode != 'static');
                 $generateAssetPathEvent = new GenerateAssetPathEvent($event->getRenderMode(), $event->getPage(), $event->getLayout(), $asset, $event->getDefaultFilters(), 'js');
                 $eventDispatcher->dispatch(ThemePlusEvents::GENERATE_ASSET_PATH, $generateAssetPathEvent);
                 $asset->setTargetPath($generateAssetPathEvent->getPath());
                 $event->append($asset);
             }
         }
         $GLOBALS['TL_JAVASCRIPT'] = [];
     }
 }
 public function collectUserStylesheets(CollectAssetsEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
 {
     if (is_array($GLOBALS['TL_USER_CSS']) && !empty($GLOBALS['TL_USER_CSS'])) {
         foreach ($GLOBALS['TL_USER_CSS'] as $stylesheet) {
             if ($stylesheet instanceof AssetInterface) {
                 $event->append($stylesheet);
             } else {
                 list($source, $media, $mode, $version) = explode('|', $stylesheet);
                 $stripStaticDomainEvent = new StripStaticDomainEvent($event->getRenderMode(), $event->getPage(), $event->getLayout(), $source);
                 $eventDispatcher->dispatch(ThemePlusEvents::STRIP_STATIC_DOMAIN, $stripStaticDomainEvent);
                 $source = $stripStaticDomainEvent->getUrl();
                 $asset = new ExtendedFileAsset(TL_ROOT . '/' . $source, [new CssRewriteFilter()], TL_ROOT, $stylesheet);
                 $asset->setMediaQuery($media);
                 $asset->setStandalone($mode != 'static');
                 $generateAssetPathEvent = new GenerateAssetPathEvent($event->getRenderMode(), $event->getPage(), $event->getLayout(), $asset, $event->getDefaultFilters(), 'css');
                 $eventDispatcher->dispatch(ThemePlusEvents::GENERATE_ASSET_PATH, $generateAssetPathEvent);
                 $asset->setTargetPath($generateAssetPathEvent->getPath());
                 $event->append($asset, 150);
             }
         }
         $GLOBALS['TL_USER_CSS'] = [];
     }
 }
Ejemplo n.º 3
0
 /**
  * Parse all javascripts and add them to the search and replace array.
  *
  * @param \LayoutModel $layout
  * @param array        $scripts The search and replace array.
  *
  * @return void
  */
 protected function parseJavaScripts($renderMode, \PageModel $page, \LayoutModel $layout, &$headScripts, &$bodyScripts)
 {
     $defaultFilters = $this->getDefaultFilters($renderMode, $layout);
     list($headAssets, $bodyAssets) = $this->collectJavaScripts($renderMode, $page, $layout, $defaultFilters);
     $assetCount = count($headAssets) + count($bodyAssets);
     // inject async.js if required
     if ($layout->theme_plus_javascript_lazy_load && $assetCount) {
         if ($assetCount > 1) {
             $asyncScript = 'async_multi';
         } else {
             $asyncScript = 'async_single';
         }
         if (RenderMode::DESIGN == $renderMode) {
             $asyncScript .= '_dev';
         }
         $asset = new ExtendedFileAsset(TL_ROOT . '/assets/theme-plus/javascripts/' . $asyncScript . '.js', [], TL_ROOT, 'assets/theme-plus/javascripts/' . $asyncScript . '.js');
         $asset->setInline(true);
         $event = new RenderAssetHtmlEvent($renderMode, $page, $layout, $asset, $defaultFilters);
         $this->eventDispatcher->dispatch(ThemePlusEvents::RENDER_JAVASCRIPT_HTML, $event);
         if ($layout->theme_plus_default_javascript_position == 'body') {
             $bodyScripts .= $event->getHtml();
         } else {
             $headScripts .= $event->getHtml();
         }
     }
     // write assets html
     foreach ($headAssets as $asset) {
         $event = new RenderAssetHtmlEvent($renderMode, $page, $layout, $asset, $defaultFilters);
         $this->eventDispatcher->dispatch(ThemePlusEvents::RENDER_JAVASCRIPT_HTML, $event);
         $headScripts .= $event->getHtml();
     }
     foreach ($bodyAssets as $asset) {
         $event = new RenderAssetHtmlEvent($renderMode, $page, $layout, $asset, $defaultFilters);
         $this->eventDispatcher->dispatch(ThemePlusEvents::RENDER_JAVASCRIPT_HTML, $event);
         $bodyScripts .= $event->getHtml();
     }
 }