-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
105 lines (93 loc) · 3.61 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function extend(subclass, superclass){
subclass.prototype.__proto__ = superclass.prototype;
/*
Don't trust super! It's only for use in trivial things.
If you have a method A which calls this.__super.prototype.someFunc.call(this),
and method B inherits from A's object and calls this.__super.prototype.A.call(this),
__super within method A will then be bound to B's object, rather than A's. I think.
Instead of using this.__super.prototype, point to the class directly (B.prototype.A.call(this)).
You should know what you're inheriting from anyway.
*/
subclass.prototype.__super = superclass;
}
// Determine if a reference is defined
function defined(o) {
return (typeof(o)!="undefined");
}
//returns an array of strings with all the classes on the object
function getClasses(obj) {
return formatClassString(obj.className).split(" ");
}
//remove multiple spaces and leading/trailing whitespace
function formatClassString(str) {
return str.replace(/\s{2,}/gi, ' ').replace(/^\s|\s$/gi, '');
}
// Determine if an object or class string contains a given class.
// If given no className, it will return true if the element has any class
function hasClass(obj,className) {
if (!defined(obj) || obj==null || !RegExp) return false;
if(!defined(className)) return (typeof obj == "string" ? obj : obj.className ? obj.className : '').replace(/\s/gi, '') != '';
var re = new RegExp("(^|\\s)" + className + "(\\s|$)");
if (typeof(obj)=="string")
return re.test(obj);
else if (typeof(obj)=="object" && obj.className)
return re.test(obj.className);
return false;
}
// Add a class to an object
function addClass(obj,className) {
if (typeof(obj)!="object" || obj==null || !defined(obj.className)) return false;
if (!hasClass(obj)) {
obj.className = formatClassString(className);
return true;
}
if (hasClass(obj,className)) return true;
obj.className = formatClassString(obj.className + " " + className);
return true;
}
// Remove a class from an object
function removeClass(obj,className) {
if (typeof(obj)!="object" || obj==null || !defined(obj.className) || obj.className==null) return false;
if (!hasClass(obj,className)) return false;
var re = new RegExp("(^|\\s+)" + className + "(\\s+|$)");
obj.className = formatClassString(obj.className.replace(re,' '));
return true;
}
// Fully replace a class with a new one
function replaceClass(obj,className,newClassName) {
if (typeof(obj)!="object" || obj==null || !defined(obj.className) || obj.className==null) return false;
removeClass(obj,className);
addClass(obj,newClassName);
return true;
}
function getParentByClassName(el, name, depth) {
if(typeof depth != "number") depth = -1;
if(depth == 0) return null;
var p = el.parentNode;
if(!p || p.tagName == 'HTML') return null;
else if(hasClass(p, name)) return p;
else return getParentByClassName(p, name, depth-1);
}
function fixHelpDivs() {
var helps = document.getElementsByClassName("help");
for(var i=0, len=helps.length; i<len; i++) {
helps[i].style.display = "block";
helps[i].style.display = null;
}
}
/*
Given some element, adjust its max-height so that the height of ancestor
is less than or equal to the height of ancestor.parentNode.
*/
function expandToFit(element, ancestor) {
var elementHeight = element.offsetHeight;
var ancestorHeight = ancestor.offsetHeight;
var ancestorParentHeight = ancestor.parentNode.offsetHeight;
var desiredHeight = elementHeight + (ancestorParentHeight - ancestorHeight);
element.style.height = desiredHeight + "px";
}
function resize(parent) {
var evt = document.createEvent('HTMLEvents');
evt.initEvent("resize", true, false);
return parent.dispatchEvent(evt);
}