/
default-rep.js
145 lines (115 loc) · 3.36 KB
/
default-rep.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function dump(obj) { print(require('test/jsdump').jsDump.parse(obj)) };
var DefaultRep = function() {};
exports.rep = new DefaultRep();
exports.extend = function(obj) {
var rep = function() {};
rep.prototype = exports.rep;
var rep = new rep();
for (var n in obj) rep[n] = obj[n];
return rep;
}
DefaultRep.prototype.toString = function()
{
return "[default-rep]";
}
DefaultRep.prototype.setTemplatePack = function(pack)
{
this.pack = pack;
};
DefaultRep.prototype.getRepTagForNode = function(node) {
var rep = this.getRepForNode(node);
if(!rep) return false;
return rep.tag;
}
DefaultRep.prototype.getRepForNode = function(node)
{
if(!this.pack) {
throw "No template pack set for rep: " + this.toString();
}
var id = node.getTemplateId();
if(id) {
return this.pack.getTemplate(id, true).rep;
}
return this.pack.seekTemplate(node, true).rep;
};
DefaultRep.prototype.util = new function() {
var self = this;
this.isLeftClick = function(event)
{
return event.button == 0 && self.noKeyModifiers(event);
};
this.noKeyModifiers = function(event)
{
return !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey;
};
this.getAncestorByClass = function(node, className)
{
for (var parent = node; parent; parent = parent.parentNode)
{
if (self.hasClass(parent, className))
return parent;
}
return null;
};
this.getElementByClass = function(node, className) // className, className, ...
{
var args = cloneArray(arguments); args.splice(0, 1);
for (var child = node.firstChild; child; child = child.nextSibling)
{
var args1 = cloneArray(args); args1.unshift(child);
if (self.hasClass.apply(null, args1))
return child;
else
{
var found = self.getElementByClass.apply(null, args1);
if (found)
return found;
}
}
return null;
};
this.setClass = function(node, name)
{
if (node && !self.hasClass(node, name))
node.className += " " + name;
};
this.hasClass = function(node, name) // className, className, ...
{
if (!node || node.nodeType != 1)
return false;
else
{
for (var i=1; i<arguments.length; ++i)
{
var name = arguments[i];
var re = new RegExp("(^|\\s)"+name+"($|\\s)");
if (!re.exec(node.getAttribute("class")))
return false;
}
return true;
}
};
this.removeClass = function(node, name)
{
if (node && node.className)
{
var index = node.className.indexOf(name);
if (index >= 0)
{
var size = name.length;
node.className = node.className.substr(0,index-1) + node.className.substr(index+size);
}
}
};
}();
function cloneArray(array, fn)
{
var newArray = [];
if (fn)
for (var i = 0; i < array.length; ++i)
newArray.push(fn(array[i]));
else
for (var i = 0; i < array.length; ++i)
newArray.push(array[i]);
return newArray;
}