/** * insert css rules into the header of the page * * @param string $css_style without html tag around */ public static function insert_style($css_style) { Js_css::insert($css_style, 'css'); }
/** * This function wrap all end of page javascript snippets * in a special function "execute_after_loading" * The function will be called later by yacs.js when script * are ready to be executed * This is because you cannot relay on $.ready() for a script loaded asynchroniusly * the function has to pay attention about portion of codes that are only declaration of a function or variable * they should not be nested in the closure of execute_after_loading * * @return void */ private static function wrap_footer_scripts() { global $context; $reset = 'delete execute_after_loading;'; if (!isset($context['javascript']['footer'])) { Js_css::insert($reset); return; } // extract code from <script></script> tags $scripts = array(); if (!preg_match_all('/<script.*?>(.*?)<\\/script/sim', $context['javascript']['footer'], $scripts)) { return; } array_shift($scripts); // remove matches[0] with all pattern // parse array and look for function declarations $declare_only = array(); for ($i = 0; $i < count($scripts[0]); $i++) { $matches = array(); if (preg_match_all('/(function\\ [a-zA-Z_]+\\ ?\\([a-zA-Z0-9_,$\\-\\ ]*\\)\\ ?(\\{ ( (?>[^{}]+) | (?-2) )* \\}) )/simx', $scripts[0][$i], $matches)) { // consider matches for the whole pattern foreach ($matches[0] as $match) { // store them $declare_only[] = $match; // remove this part of the script $scripts[0][$i] = str_replace($match, '', $scripts[0][$i]); } // unset if void if (!trim($scripts[0][$i])) { unset($scripts[0][$i]); } } } $wrapped = implode("\n", $declare_only) . "\n"; $wrapped .= 'function execute_after_loading() {' . implode("\n", $scripts[0]) . ' Yacs.updateModalBox(true)' . "\n" . '}' . "\n"; // erase footer $context['javascript']['footer'] = ''; // fill with new wrapped code Js_css::insert($wrapped); return; }