/* Riassence Framework * Copyright 2006 Riassence Inc. * http://riassence.com/ * * You should have received a copy of the GNU General Public License along * with this software package. If not, contact licensing@riassence.com */ /*** = Description ** A single instance class. ** The theme manager knows the name of the currently loaded theme and handles ** the loading of theme specific markup snippets and style declarations. ** ** = Instance variables ** +themePath+:: Relative path to the components' top directory. ** +currentTheme+:: The name of the theme currently in use. Initially the ** default unnamed theme is used. ** +usesComponentDir+:: True when the components are separated in their own ** directories, usually when using the ** source/development version. False when the components ** are all in same directory. This is the case in the ** release build. ** ***/ HDefaultThemePath = '/H/themes'; HDefaultThemeName = 'default'; HNoComponentCSS = []; HNoCommonCSS = []; HThemeHasIE6GifsInsteadOfPng = []; HThemeManager = HClass.extend({ constructor: null, init: function(){ // Default root path of the themes path, should contain at least the default theme. this.themePath = HDefaultThemePath; // Hash map of loaded template markup (html templates), by theme. // componentName is used as the secondary key. this._tmplCache = {}; // Hash map of loaded css templates, by theme. // componentName is used as the secondary key. this._cssCache = {}; // The currently selected default theme name. this.currentTheme = HDefaultThemeName; }, setThemePath: function( _path ){ this.themePath = _path; }, // Error messages, should be refined. _errTemplateNotFound: function( _url ) { console.log( "ERROR: Template Not Found: '" + _url + "' "); }, _errTemplateFailure: function( _url ) { console.log( "ERROR: Template Failure: '" + _url + "' "); }, _errTemplateException: function( _url ) { console.log( "ERROR: Template Exception: '" + _url + "' "); }, /** = Description * Loads a template file and returns its contents. * If errors occurred, calls the error management functions. * * = Parameters * +_url+:: A valid local file path or http url pointing to the * resource to load. * +_contentType+:: An optional parameter, specifies the content type wanted, * defaults to text/html. * * = Returns * The contents of the path. **/ fetch: function( _url, _contentType, _callBack, _async ) { var _callBackFun; if( !_contentType ){ _contentType = 'text/html; charset=UTF-8'; } if(_async){ _callBackFun = function( resp ){ _callBack( resp.X.responseText ); }; } else{ // console.log('WARNING: Fetching synchronously using HThemeManager is not recommended. Use pre-packaged themes instead.'); var _respText; _callBackFun = function( resp ){ _respText = resp.X.responseText; }; } COMM.request( _url, { onSuccess: _callBackFun, on404: function(resp){ HThemeManager._errTemplateNotFound( resp.url ); }, onFailure: function(resp){ HThemeManager._errTemplateFailure( resp.url ); }, onException: function(resp){ HThemeManager._errTemplateException( resp.url ); }, method: 'GET', async: _async } ); if(!_async){ return _respText; } }, /** Returns the theme/component -specific path, called from inside css * themes, a bit kludgy approach to tell the theme graphics file paths. **/ getThemeGfxPath: function() { var _themeName = this._cssEvalParams[0], _componentName = this._cssEvalParams[1], _themePath = this._cssEvalParams[2], _urlPrefix = this._urlPrefix( _themeName, _componentName, _themePath ); return this._joinPath( _urlPrefix, 'gfx' ); }, /** = Description * Returns the theme/component -specific graphics file path with proper css wrappers. * Used from inside css themes, a bit kludgy approach to tell the file name path. * * = Parameters * +_fileName+:: The File name to load. * **/ getCssFilePath: function( _fileName ){ var _themeName = this._cssEvalParams[0]; if((HThemeHasIE6GifsInsteadOfPng.indexOf(_themeName)!==-1) && (BROWSER_TYPE.ie6 || BROWSER_TYPE.symbian) ){ return "url('"+this._joinPath( this.getThemeGfxPath(), _fileName.replace('.png','-ie6.gif') )+"')"; } else { return "url('"+this._joinPath( this.getThemeGfxPath(), _fileName )+"')"; } }, /** = Description * Loads a css file based on the given url (or file path). * Evaluates the css data. * Makes sure the browser uses the data for component styles. * * = Parameter * +_url+:: A valid url that points to a valid css file. * * = Returns * The source of the url. * **/ loadCSS: function( _url ) { var _contentType = 'text/css', _cssFun = function(_cssText){ // Don't try to do anything with empty or invalid css data: if (!_cssText || _cssText === "") { return; } HThemeManager.useCSS( _cssText ); }; this.fetch( _url, _contentType, _cssFun, true ); }, useCSS: function( _cssText ){ var _contentType = 'text/css'; // Evaluate the css text _cssText = this._bindCSSVariables( _cssText ); var _style, _styleSheet, _head; if(BROWSER_TYPE.ie) { // Internet Explorer (at least 6.x; check what 7.x does) _style = document.createStyleSheet(); _style.cssText = _cssText; } else { // Common, standard