2012-03-07 15:04:25 +01:00
|
|
|
/**
|
|
|
|
* eGroupWare eTemplate2 - Stylesheet class
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel
|
|
|
|
* @copyright Stylite 2011
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
egw_core;
|
|
|
|
*/
|
|
|
|
|
2012-03-07 16:20:04 +01:00
|
|
|
/**
|
|
|
|
* Module which allows to add stylesheet rules at runtime. Exports the following
|
|
|
|
* functions:
|
|
|
|
* - css
|
2014-02-25 17:02:46 +01:00
|
|
|
* @param {string} _app application name object is instanciated for
|
|
|
|
* @param {object} _wnd window object is instanciated for
|
2012-03-07 16:20:04 +01:00
|
|
|
*/
|
2016-02-29 16:50:24 +01:00
|
|
|
egw.extend('css', egw.MODULE_WND_LOCAL, function(_app, _wnd)
|
|
|
|
{
|
|
|
|
"use strict";
|
2012-03-07 15:04:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assoziative array which stores the current css rule for a given selector.
|
|
|
|
*/
|
|
|
|
var selectors = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variable used to calculate unique id for the selectors.
|
|
|
|
*/
|
|
|
|
var selectorCount = 0;
|
2014-02-25 17:02:46 +01:00
|
|
|
var sheet;
|
2012-03-07 15:04:25 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* The css function can be used to introduce a rule for the given css
|
|
|
|
* selector. So you're capable of adding new custom css selector while
|
|
|
|
* runtime and also update them.
|
|
|
|
*
|
|
|
|
* @param _selector is the css select which can be used to apply the
|
|
|
|
* stlyes to the html elements.
|
|
|
|
* @param _rule is the rule which should be connected to the selector.
|
|
|
|
* if empty or omitted, the given selector gets removed.
|
|
|
|
*/
|
|
|
|
css: function(_selector, _rule) {
|
|
|
|
// Set the current index to the maximum index
|
2015-09-23 01:43:20 +02:00
|
|
|
var index = sheet ? Math.min(selectorCount, sheet.cssRules.length) : 0;
|
2012-03-07 15:04:25 +01:00
|
|
|
|
2015-09-23 18:22:47 +02:00
|
|
|
if (!sheet || !sheet.ownerNode || sheet.ownerNode.ownerDocument !== _wnd.document)
|
2014-02-25 17:02:46 +01:00
|
|
|
{
|
|
|
|
// Generate a style tag, which will be used to hold the newly generated css
|
|
|
|
// rules.
|
|
|
|
var style = _wnd.document.createElement('style');
|
|
|
|
_wnd.document.getElementsByTagName('head')[0].appendChild(style);
|
|
|
|
|
|
|
|
// Obtain the reference to the styleSheet object of the generated style tag
|
|
|
|
sheet = style.sheet ? style.sheet : style.styleSheet;
|
2015-09-23 18:22:47 +02:00
|
|
|
|
|
|
|
selectorCount = 0;
|
|
|
|
selectors = {};
|
2014-02-25 17:02:46 +01:00
|
|
|
}
|
|
|
|
|
2012-03-07 15:04:25 +01:00
|
|
|
// Remove any existing rule first, of no rule exists for the
|
|
|
|
if (typeof selectors[_selector] !== "undefined")
|
|
|
|
{
|
|
|
|
// Store the old index
|
|
|
|
index = selectors[_selector];
|
2015-09-23 01:43:20 +02:00
|
|
|
if(index < sheet.cssRules.length)
|
2012-03-07 15:04:25 +01:00
|
|
|
{
|
2015-09-23 01:43:20 +02:00
|
|
|
if (typeof sheet.removeRule !== "undefined")
|
|
|
|
{
|
|
|
|
sheet.removeRule(index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sheet.deleteRule(index);
|
|
|
|
}
|
2012-03-07 15:04:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
delete (selectors[_selector]);
|
2015-02-19 18:14:31 +01:00
|
|
|
if(!_rule)
|
|
|
|
{
|
|
|
|
selectorCount--;
|
|
|
|
}
|
2012-03-07 15:04:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
selectorCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_rule)
|
|
|
|
{
|
|
|
|
// Add the rule to the stylesheet
|
|
|
|
if (typeof sheet.addRule !== "undefined")
|
|
|
|
{
|
|
|
|
sheet.addRule(_selector, _rule, index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sheet.insertRule(_selector + "{" + _rule + "}", index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the new index
|
|
|
|
selectors[_selector] = index;
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 17:02:46 +01:00
|
|
|
};
|
2012-03-07 15:04:25 +01:00
|
|
|
});
|
|
|
|
|