/**
  * 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();
     }
 }