source: products/quintagroup.dropdownmenu/trunk/quintagroup/dropdownmenu/browser/resources/csshover.htc @ 1182

Last change on this file since 1182 was 1182, checked in by piv, 11 years ago

add registration for csshover configlet, moved here csshover.htc as browser view with content-type x-component, started with customization of global sections viewlet, move css from qPloneDropDownMenu product, added TODO.txt

File size: 5.3 KB
Line 
1<attach event="ondocumentready" handler="parseStylesheets" />
2<script language="JScript">
3/**
4 *    HOVER - V1.11.040203 - whatever:hover in IE
5 *    ---------------------------------------------
6 *    Peterned - http://www.xs4all.nl/~peterned/
7 *    (c) 2004 - Peter Nederlof
8 *
9 *    Credits  - Arnoud Berendsen
10 *        for finding some really -sick- bugs
11 *
12 *    howto: body { behavior:url("csshover.htc"); }
13 *    ---------------------------------------------
14 */
15var currentSheet, doc = window.document;
16var csshover_nodes = getElementsByClassName("csshover", doc);
17var csshover_rules = [], hoverStyleSheets = "", addRules = true;
18var IE5 = (doc.getElementsByTagName('*').length == 0) ? true : false, configlet = doc.getElementById('csshovering');
19var DEBUG = false, styles = "", parseCSSRulecounter = 0, timer, timerTable = "", d=true;
20
21function parseStylesheets() {
22    var sheets = doc.styleSheets, l = sheets.length;
23    for(var i=0; i<l; i++){
24        parseStylesheet(sheets[i]);
25    };
26    if (configlet) {configlet.innerText = hoverStyleSheets;};
27    if(DEBUG) {
28      alert(styles);
29      alert(timerTable);
30    };
31};
32
33function parseStylesheet(sheet) {
34    if(DEBUG) window.status += '|';
35//    if(sheet.href && sheet.href.indexOf('/ploneCustom.css') == -1) return;
36    var l, rules, imports;
37    if(sheet.imports) {
38        imports = sheet.imports, l = imports.length;
39        for(var i=0; i<l; i++)
40            parseStylesheet(sheet.imports[i]);
41    };
42    rules = (currentSheet = sheet).rules, l = rules.length;
43    for(var j=0; j<l; j++){parseCSSRule(rules[j]);};
44};
45
46function parseCSSRule(rule) {
47    if(DEBUG) {
48        if((parseCSSRulecounter++)%15 == 0) window.status += '.';
49        startTimer();
50    };
51    var select = rule.selectorText, style = rule.style.cssText;
52    if (addRules && (/\.cssHoverCSSExists/i).test(select)) {addRules = false;};
53    if(!(/\.csshover/i).test(select)) return;
54    if(style.replace(/ /, "") == "") return;
55    if(!(/(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):hover/i).test(select)) return;
56
57    var newSelect = select.replace(/(\.([a-z0-9_-]+):hover)|(:hover)/g, '.$2onHover');
58    if (addRules) {currentSheet.addRule(newSelect, style);};
59    if (configlet) {hoverStyleSheets += newSelect + ' \{\n  ' + style.replace('; ', '\;\n  ') + ';\n\}\n';};
60
61    var hasClass = (/(\.([a-z0-9_-]+):hover)/g).exec(select);
62    var className = (hasClass? hasClass[2]:'') + 'onHover';
63    var affected = select.replace(/:hover.*$/g, '');
64    affected = affected.replace(/^.*([ ]|^)(([^ ]*)\.csshover)/i, '$2');
65    if (DEBUG) {timerTable += stopTimer() + '\n';};
66    if(!csshover_rules[className + ',' + affected]) {
67        if (DEBUG) {startTimer();};
68        var elements = getElementsBySelect(affected);
69        if(DEBUG) {
70            window.status += " "+elements.length;
71            styles += "" + elements.length + ": " + affected + '\n';
72        };
73        for(var i=0; i<elements.length; i++)
74            new HoverElement(elements[i], className);
75        csshover_rules[className + ',' + affected] = true;
76    };
77};
78
79function HoverElement(node, className) {
80    if(!node.hovers) node.hovers = {};
81    if(node.hovers[className]) return;
82    node.hovers[className] = true;
83    node.attachEvent('onmouseover',
84        function() { node.className += ' ' + className; });
85    node.attachEvent('onmouseout',
86        function() { node.className =
87            node.className.replace((new RegExp('\\s+'+className)),''); });
88};
89
90function getElementsBySelect(rule) {
91    var parts, nodes = csshover_nodes;
92    parts = rule.split(' ');
93    for(var i=0; i<parts.length; i++) {
94        nodes = getSelectedNodes(parts[i], nodes, i==0);
95    };
96    return nodes;
97};
98
99function getSelectedNodes(select, elements, includeRoot) {
100    var element, result, node, nodes = [];
101    var classname = (/\.([a-z0-9_-]+)/i).exec(select);
102    if(classname) {classname_re = new RegExp('\\b' + classname[1] + '\\b');};
103    var identify = (/\#([a-z0-9_-]+)/i).exec(select);
104    var tagName = (/^[a-z0-9]+/i).exec(select.toUpperCase()) || '*',
105        ell = elements.length;
106    for(var i=0; i<ell; i++) {
107        element = elements[i]
108        if(includeRoot)
109          if((tagName == '*') || (element.nodeName == tagName)) result = [element]
110          else result = []
111        else
112            if (tagName == '*' && IE5) result = elements[i].all
113            else result = elements[i].getElementsByTagName(tagName)
114        rel = result.length;
115        for(var j=0; j<rel; j++) {
116            node = result[j];
117            if((identify && node.id != identify[1]) || (classname && !(classname_re.exec(node.className))) || arrayHasElement(nodes, node)) continue;
118            nodes[nodes.length] = node;
119        };
120    };
121    return nodes;
122};
123
124function arrayHasElement(a, el) {
125    var l = a.length;
126    for(var i=0; i<l; i++) if(a[i] == el) return true;
127    return false;
128};
129
130function getElementsByClassName(className, parentElement) {
131    var result = new Array(),
132        list = (parentElement || doc.body).getElementsByTagName('*');
133    if(list.length == 0) list = (parentElement || doc.body).all;
134    var class_re = new RegExp('(^|\\s)' + className + '(\\s|$)');
135    for(var i = 0; i < list.length; i++) if(list[i].className.match(class_re)) result[result.length] = list[i];
136    return result;
137};
138
139function startTimer() {timer = new Date().getTime();};
140function stopTimer(message) {return (new Date().getTime()) - timer;};
141
142</script>
Note: See TracBrowser for help on using the repository browser.