示例#1
0
 /**
  * Update head - detect if devmode or themermode is enabled and less file existed, use less file instead of css
  * We also detect and update jQuery, Bootstrap to use CANVAS assets
  *
  * @return  null
  */
 function updateHead()
 {
     //state parameters
     $devmode = $this->getParam('devmode', 0);
     $themermode = $this->getParam('themermode', 1) && defined('CANVAS_THEMER');
     $theme = $this->getParam('theme', '');
     $minify = $this->getParam('minify', 0);
     $minifyjs = $this->getParam('minify_js', 0);
     // As Joomla 3.0 bootstrap is buggy, we will not use it
     // We also prevent both Joomla bootstrap and CANVAS bootsrap are loaded
     // And upgrade jquery as our Framework require jquery 1.7+ if we are loading jquery from google
     $doc = JFactory::getDocument();
     $scripts = array();
     if (version_compare(JVERSION, '3.0', 'ge')) {
         $canvasbootstrap = false;
         $jabootstrap = false;
         foreach ($doc->_scripts as $url => $script) {
             if (strpos($url, CANVAS_URL . '/bootstrap/js/bootstrap.js') !== false) {
                 $canvasbootstrap = true;
                 if ($jabootstrap) {
                     //we already have the Joomla bootstrap and we also replace to CANVAS bootstrap
                     continue;
                 }
             }
             if (preg_match('@media/jui/js/bootstrap(.min)?.js@', $url)) {
                 if ($canvasbootstrap) {
                     //we have CANVAS bootstrap, no need to add Joomla bootstrap
                     continue;
                 } else {
                     $scripts[CANVAS_URL . '/bootstrap/js/bootstrap.js'] = $script;
                 }
                 $jabootstrap = true;
             } else {
                 $scripts[$url] = $script;
             }
         }
         $doc->_scripts = $scripts;
         $scripts = array();
     }
     // VIRTUE MART / JSHOPPING compatible
     foreach ($doc->_scripts as $url => $script) {
         $replace = false;
         if (strpos($url, '//ajax.googleapis.com/ajax/libs/jquery/') !== false && preg_match_all('@/jquery/(\\d+(\\.\\d+)*)?/@msU', $url, $jqver) || preg_match_all('@(^|\\/)jquery([-_]*(\\d+(\\.\\d+)+))?(\\.min)?\\.js@i', $url, $jqver)) {
             $idx = strpos($url, '//ajax.googleapis.com/ajax/libs/jquery/') !== false ? 1 : 3;
             if (is_array($jqver) && isset($jqver[$idx]) && isset($jqver[$idx][0])) {
                 $jqver = explode('.', $jqver[$idx][0]);
                 if (isset($jqver[0]) && (int) $jqver[0] <= 1 && isset($jqver[1]) && (int) $jqver[1] < 7) {
                     $scripts[CANVAS_URL . '/js/jquery-1.8.3' . ($devmode ? '' : '.min') . '.js'] = $script;
                     $replace = true;
                 }
             }
         }
         if (!$replace) {
             $scripts[$url] = $script;
         }
     }
     $doc->_scripts = $scripts;
     // end update javascript
     // detect RTL
     $dir = $doc->direction;
     $is_rtl = $dir == 'rtl';
     //Update css/less based on devmode and themermode
     $root = JURI::root(true);
     $current = JURI::current();
     $regex = '@' . preg_quote(CANVAS_TEMPLATE_REL) . '/css/(rtl/)?(.*)\\.css((\\?|\\#).*)?$@i';
     $stylesheets = array();
     foreach ($doc->_styleSheets as $url => $css) {
         // detect if this css in template css
         if (preg_match($regex, $url, $match)) {
             $fname = $match[2];
             if (($devmode || $themermode) && is_file(CANVAS_TEMPLATE_PATH . '/less/' . $fname . '.less')) {
                 if ($themermode) {
                     $newurl = CANVAS_TEMPLATE_URL . '/less/' . $fname . '.less';
                     $css['mime'] = 'text/less';
                 } else {
                     CANVAS::import('core/less');
                     $newurl = CANVASLess::buildCss(CANVASPath::cleanPath('templates/' . CANVAS_TEMPLATE . '/less/' . $fname . '.less'), true);
                 }
                 $stylesheets[$newurl] = $css;
             } else {
                 $uri = null;
                 // detect css available base on direction & theme
                 if ($is_rtl && $theme) {
                     $uri = CANVASPath::getUrl('css/rtl/themes' . $theme . '/' . $fname . '.css');
                 }
                 if (!$uri && $is_rtl) {
                     $uri = CANVASPath::getUrl('css/rtl/' . $fname . '.css');
                 }
                 if (!$uri && $theme) {
                     $uri = CANVASPath::getUrl('css/themes/' . $theme . '/' . $fname . '.css');
                 }
                 if (!$uri) {
                     $uri = CANVASPath::getUrl('css/' . $fname . '.css');
                 }
                 if ($uri) {
                     $stylesheets[$uri] = $css;
                 }
             }
             continue;
         }
         $stylesheets[$url] = $css;
     }
     // update back
     $doc->_styleSheets = $stylesheets;
     //only check for minify if devmode is disabled
     if (!$devmode && ($minify || $minifyjs)) {
         CANVAS::import('core/minify');
         if ($minify) {
             CANVASMinify::optimizecss($this);
         }
         if ($minifyjs) {
             CANVASMinify::optimizejs($this);
         }
     }
 }
示例#2
0
 public static function cb_import_path($match)
 {
     $f = $match[1];
     $newf = CANVASPath::cleanPath(self::$_path . $f);
     return str_replace($f, $newf, $match[0]);
 }
示例#3
0
    public static function addAssets()
    {
        $japp = JFactory::getApplication();
        $user = JFactory::getUser();
        //do nothing when site is offline and user has not login (the offline page is only show login form)
        if ($japp->getCfg('offline') && !$user->authorise('core.login.offline')) {
            return;
        }
        $jdoc = JFactory::getDocument();
        $params = $japp->getTemplate(true)->params;
        if (defined('CANVAS_THEMER') && $params->get('themermode', 1)) {
            $jdoc->addStyleSheet(CANVAS_URL . '/css/thememagic.css');
            $jdoc->addScript(CANVAS_URL . '/js/thememagic.js');
            $theme = $params->get('theme');
            $params = new JRegistry();
            $themeinfo = new stdClass();
            if ($theme) {
                $themepath = CANVAS_TEMPLATE_PATH . '/less/themes/' . $theme;
                if (file_exists($themepath . '/variables-custom.less')) {
                    if (!class_exists('JRegistryFormatLESS')) {
                        include_once CANVAS_ADMIN_PATH . '/includes/format/less.php';
                    }
                    //default variables
                    $varfile = CANVAS_TEMPLATE_PATH . '/less/variables.less';
                    if (file_exists($varfile)) {
                        $params->loadString(JFile::read($varfile), 'LESS');
                        //get all less files in "theme" folder
                        $others = JFolder::files($themepath, '.less');
                        foreach ($others as $other) {
                            //get those developer custom values
                            if ($other == 'variables.less') {
                                $devparams = new JRegistry();
                                $devparams->loadString(JFile::read($themepath . '/variables.less'), 'LESS');
                                //overwrite the default variables
                                foreach ($devparams->toArray() as $key => $value) {
                                    $params->set($key, $value);
                                }
                            }
                            //ok, we will import it later
                            if ($other != 'variables-custom.less' && $other != 'variables.less') {
                                $themeinfo->{$other} = true;
                            }
                        }
                        //load custom variables
                        $cparams = new JRegistry();
                        $cparams->loadString(JFile::read($themepath . '/variables-custom.less'), 'LESS');
                        //and overwrite those defaults variables
                        foreach ($cparams->toArray() as $key => $value) {
                            $params->set($key, $value);
                        }
                    }
                }
            }
            $cache = array();
            // a little security
            if ($user->authorise('core.manage', 'com_templates') || isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], JUri::base() . 'administrator') !== false) {
                CANVAS::import('core/path');
                $baseurl = JUri::base();
                //should we provide a list of less path
                foreach (array(CANVAS_TEMPLATE_PATH . '/less', CANVAS_PATH . '/bootstrap/less', CANVAS_PATH . '/less') as $lesspath) {
                    if (is_dir($lesspath)) {
                        $lessfiles = JFolder::files($lesspath, '.less', true, true);
                        if (is_array($lessfiles)) {
                            foreach ($lessfiles as $less) {
                                $path = ltrim(str_replace(array(JPATH_ROOT, '\\'), array('', '/'), $less), '/');
                                $path = CANVASPath::cleanPath($path);
                                $fullurl = $baseurl . preg_replace('@(\\+)|(/+)@', '/', $path);
                                $cache[$fullurl] = JFile::read($less);
                            }
                        }
                    }
                }
            }
            //workaround for bootstrap icon path
            $sparams = new JRegistry();
            if (defined('CANVAS_BASE_RSP_IN_CLASS') && CANVAS_BASE_RSP_IN_CLASS) {
                $sparams->set('icon-font-path', '"' . JUri::base() . 'plugins/system/canvas/base-bs3/bootstrap/fonts/"');
            }
            $jdoc->addScriptDeclaration('
				var CANVASTheme = window.CANVASTheme || {};
				CANVASTheme.vars = ' . json_encode($params->toArray()) . ';
				CANVASTheme.svars = ' . json_encode($sparams->toArray()) . ';
				CANVASTheme.others = ' . json_encode($themeinfo) . ';
				CANVASTheme.theme = \'' . $theme . '\';
				CANVASTheme.template = \'' . CANVAS_TEMPLATE . '\';
				CANVASTheme.base = \'' . JURI::base() . '\';
				CANVASTheme.cache = ' . json_encode($cache) . ';
				if(typeof less != \'undefined\'){
					
					//we need to build one - cause the js will have unexpected behavior
					try{
						if(window.parent != window && 
							window.parent.CANVASTheme && 
							window.parent.CANVASTheme.applyLess){
							
							window.parent.CANVASTheme.applyLess(true);
						} else {
							less.refresh();
						}
					} catch(e){

					}
				}');
        }
    }