blob: e13ea1394ef2d3d1a00ad9c449063b0a3c200fd7 [file] [log] [blame]
avm9996304def3e2016-11-27 22:53:05 +01001var UI = {
2
3 show: function(element) {
4 if (UI.getStyle(element, 'display') == 'none') {
5 // Set display value to be defined by style sheet
6 var cssRules = window.getMatchedCSSRules(element, '', true);
7 var ruleLength = cssRules.length;
8 var display;
9 for (var i = ruleLength - 1; i >= 0 ; --i) {
10 display = cssRules[i].style.display;
11 if (display && display != 'none') {
12 element.style.display = display;
13 return;
14 }
15 }
16
17 // Set display value to be UA default value
18 var tmpElement = document.createElement(element.nodeName);
19 document.body.appendChild(tmpElement);
20 display = UI.getStyle(tmpElement, 'display');
21 document.body.removeChild(tmpElement);
22 element.style.display = display;
23 }
24 },
25
26 hide: function(element) {
27 element.style.display = 'none';
28 },
29
30 setStyle: function(element) {
31 var argLength = arguments.length;
32 var arg1 = arguments[1];
33 if (argLength == 2 && arg1.constructor == Object) {
34 for (var prop in arg1) {
35 var camelCasedProp = prop.replace(/-([a-z])/gi, function(n, letter) {
36 return letter.toUpperCase();
37 });
38 element.style[camelCasedProp] = arg1[prop];
39 }
40 } else if (argLength == 3)
41 element.style[arg1] = arguments[2];
42 },
43
44 getStyle: function(element, property) {
45 return window.getComputedStyle(element)[property];
46 },
47
48 addClass: function(element, className) {
49 var classes = element.className.split(' ');
50 classes.push(className);
51 element.className = classes.join(' ');
52 },
53
54 removeClass: function(element, className) {
55 var classes = element.className.split(' ');
56 var index = classes.indexOf(className);
57 if (index >= 0) {
58 classes.splice(index, 1);
59 element.className = classes.join(' ');
60 }
61 },
62
63 addStyleSheet: function(path) {
64 var link = document.createElement('link');
65 link.setAttribute('type', 'text/css');
66 link.setAttribute('rel', 'stylesheet');
67 link.setAttribute('href', path);
68 document.head.appendChild(link);
69 }
70};