-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwm3.js
305 lines (261 loc) · 12.2 KB
/
wm3.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
(function ($) {
$.bifrost = $.bifrost || {};
var inherits = $.bifrost.inherits = function (to, from) {
function F() {}
F.prototype = from.prototype;
to.prototype = new F();
to.prototype.constructor = to;
to.prototype.superClass_ = from.prototype;
}
function AssertionFailed(msg) {
Error.call(this, msg);
}
inherits(AssertionFailed, Error);
var assert = $.bifrost.assert = function (condition, msg) {
if (!condition) {
throw new AssertionFailed("Assertion failed in: " + arguments.callee + (msg || ""));
}
}
function isNumber(num) { return typeof num == "number"; }
function isInteger(num) { return isNumber(num) && /^\d+$/.test(num.toString()); }
function isNonNegative(num) { return isInteger(num) && num >= 0; }
function isPositive(num) { return isInteger(num) && num > 0; }
function isBetween(num, min, max) { return num >= min && num < max; }
function isHtmlElem(obj) { return $(obj).get(0) instanceof HtmlElement; }
function px(num) { assert(isPositiveInteger(num)); return num + "px"; }
function Counter(startValue) {
assert(!startValue || isInteger(startValue));
var counter = startValue || 0;
this.next = function () { return counter++; }
}
$.bifrost.sum = function (array, initial) {
assert($.isArray(array) && isBetween(arguments.length, 1, 3));
var accum = arguments.length <= 1 ? 0 : initial;
for (var i = 0; i < array.length; ++i) {
if (i in array) {
accum += array[i];
}
}
return accum;
}
function Dimensions(left, top, width, height) {
assert(isNonNegative(left) && isNonNegative(top) &&
isPositive(width) && isPositive(height));
this.left = function () { return left; }; this.top = function () { return top; };
this.width = function () { return width; }; this.height = function () { return height; };
}
$.extend(Dimensions.prototype, {
apply: function(elem) {
$(elem).css("position", "absolute").css("left", px(this.left())).css("top", px(this.top()))
.css("width", px(this.width())).css("height", px(this.height()));
},
addWidth: function (width) { assert(isPositive(width + this.width()));
return new Dimensions(this.left(), this.top(),
this.width() + width, this.height()); },
addHeight: function (height) { assert(isPositive(height + this.height()));
return new Dimensions(this.left(), this.top(),
this.width(), this.height() + height);},
addLeft: function (left) { assert(isNonNegative(this.left() + left));
return new Dimensions(this.left() + left, this.top(),
this.width(), this.height()); },
addTop: function (top) { assert(isNonNegative(this.top() + top));
return new Dimensions(this.left(), this.top() + top,
this.width(), this.height()); },
setLeft: function (left) { assert(isNonNegative(left));
return new Dimensions(left, this.top(),
this.width(), this.height()); },
setTop: function (top) { assert(isNonNegative(top));
return new Dimensions(this.left(), top,
this.width(), this.height()); },
setWidth: function (width) { assert(isPositive(width));
return new Dimensions(this.left(), this.top(), width, this.height()); },
setHeight: function (height) { assert(isPositive(height));
return new Dimensions(this.left(), this.top(), this.width(), height); },
splitHorizontally: function () {
var secondWidth = Math.floor(this.width() / 2);
var firstWidth = this.width() - secondWidth;
return [new Dimensions(this.left(), this.top(), firstWidth, this.height()),
new Dimensions(this.left() + firstWidth, this.top(), secondWidth(), this.height())]
},
splitVertically: function () {
var secondHeight = Math.floor(this.height() / 2);
var firstHeight = this.height() - secondHeight;
return [new Dimensions(this.left(), this.top(), this.width(), firstHeight),
new Dimensions(this.left(), this.top() + firstHeight, this.width(), secondHeight)];
}
});
$.fn.setDimensions = function (dims) {
assert(dims instanceof Dimensions);
dims.apply($(this));
};
function Buffer(domContent, dimensions) {
assert(isHtmlElem(domContent) && dimensions instanceof Dimensions);
this.getDomContent = function () { return domContent; };
this.getDimensions = function () { return dimensions; };
}
$.extend(Buffer.prototype, {
render: function () { $(this.getDomContent()).setDimensions(this.getDimensions()); },
hide: function () { $(this.getDomContent()).hide(); },
setDimensions: function (dims) { assert(dims instanceof Dimensions);
return new Buffer(this.getDomContent(), dims); }
});
function isNode(node) { return node instanceof AbstractNode; }
function isNonRootNode(node) { return isNode(node) && !(node instanceof RootNode); }
nodeIdCounter = new Counter();
function AbstractNode(parent, dims) {
assert((parent === null || parent instanceof AbstractNode) &&
dims instanceof Dimensions)
this.getParent = function () { return parent; };
this.getDimensions = function () { return dims; };
var id = nodeIdCounter.next();
this.getId = function () { return id; }
}
$.extend(AbstractNode.prototype, {
isLeft: function () {
return this.getParent() instanceof HorizontalSplitNode &&
this.getParent.getFirstChild() == this;
},
isRight: function () {
return this.getParent() instanceof HorizontalSplitNode &&
this.getParent.getSecondChild() == this;
},
isBottom: function () {
return this.getParent() instanceof VerticalSplitNode &&
this.getParent().getSecondChild() == this;
},
isTop: function () {
return this.getParent() instanceof VerticalSplitNode &&
this.getParent().getFirstChild() == this;
}
});
function repairTree(node) {
function resizeLeft(node, offset) {
if (node instanceof RootNode)
return node;
if (node instanceof HorizontalNode) {
var firstChild = node.getFirstChild(), secondChild = node.getSecondChild();
return repairTree(node.getParent().substituteNode(
node.getId(), new HorizontalNode(firstChild, firstChild.getDimensions().addWidth(offset).addLeft(-offset),
secondChild, secondChild.getDimensions().addWidth(offset).addLeft(-offset),
node.getParent())));
}
return node.getParent().substituteNode(node, offset);
}
function RootNode(child, dims) {
assert((child === null || child instanceof Buffer || isNonRootNode(child)) &&
dims instanceof Dimensions);
AbstractNode.call(this, null, dims);
child = this._makeChildNode(node);
this.getChild = function () { return child; }
}
inherits(RootNode, AbstractNode);
$.extend(RootNode.prototype, {
_makeChildNode: function (node) {
assert(node === null || node instanceof Buffer || isNonRootNode(node));
return node ? node instanceof Buffer ? new LeafNode(node, this, dims) :
node.setDimensions(this.getDimensions()) : this._defaultNode();
},
_defaultNode: function () {
return this._makeChildNode(new Buffer(document.createElement("div"), this.getDimensions()));
},
substituteNode: function (id, newNode) {
assert(isNonRootNode(newNode) && isNumber(id));
return this.getChild().getId() == id ? new RootNode(newNode, this.getDimensions()) : this;
},
render: function () { this.getChild().render(); },
setDimensions: function (dims) { return new RootNode(this.getChild(), dims); }
});
function LeafNode(buffer, parent, dims) {
assert((parent === null || isNode(parent)) && buffer instanceof Buffer && dims instanceof Dimensions);
AbstractNode.call(this, parent, dims);
buffer = buffer.setDimensions(dims);
this.getBuffer = function () { return buffer; };
}
inherits(LeafNode, AbstractNode);
$.extend(LeafNode.prototype, {
render: function () { this.getBuffer().render(); },
hide: function () { this.getBuffer().hide(); },
setDimensions: function (dims) { assert(dims instanceof Dimensions);
return new LeafNode(this.getBuffer(), this.getParent(), dims); },
setParent: function (parent) { assert(isNode(parent));
return new LeafNode(this.getBuffer(), parent, this.getDimensions()); }
});
function AbstractSplitNode(node1, node2, parent, dims) {
assert(isNonRootNode(node1) && isNonRootNode(node2) && dims instanceof Dimensions);
AbstractNode.call(this, parent, dims);
node1 = node1.setParent(this); node2 = node2.setParent(this);
this.getFirstChild = function () { return node1; };
this.getSecondChild = function () { return node2; };
}
inherits(AbstractSplitNode, AbstractNode);
$.extend(AbstractSplitNode.prototype, {
render: function () { this.getFirstChild().render(); this.getSecondChild().render(); },
hide: function () { this.getFirstChild().hide(); this.getSecondChild().hide(); },
substituteNode: function (id, newNode) {
assert(isInteger(id) && isNonRootNode(newNode));
if (this.getFirstChild().getId() === id) {
return new this.constructor(newNode, this.getFirstChild().getDimensions(),
this.getSecondChild(), this.getSecondChild().getDimensions(),
this.getParent());
} else if (this.getSecondChild().getId() == id){
return this.constructor(this.getFirstChild(), this.getFirstChild.getDimensions(),
newNode, this.getSecondChild().getDimensions(),
this.getParent());
} else {
return this;
}
},
setParent: function (parent) {
assert(isNode(parent));
return new this.constructor(this.getFirstChild(), this.getFirstChild().getDimensions(),
this.getSecondChild(), this.getSecondChild().getDimensions(), parent);
}
});
function HorizontalSplitNode(node1, node1dims, node2, node2dims, parent) {
assert(isNonRootNode(node1) && isNonRootNode(node2) &&
(parent === null || isNode(parent)) &&
dims instanceof Dimensions &&
(node1dims instanceof Dimensions && node2dims instanceof Dimensions &&
node1dims.left() + node1dims.width() === node2dims.left() &&
node1dims.height() == node2dims.height() && node1dims.top() == node2dims.top()));
node1 = node1.setDimensions(node1dims); node2 = node2.setDimensions(node2dims);
var dims = new Dimensions(node1dims.left(), node1dims.top(),
node1dims.width() + node2dims.width(), node1dims.height());
AbstractSplitNode.call(this, node1, node2, parent, dims);
}
inherits(HorizontalSplitNode, AbstractSplitNode);
$.extend(HorizontalSplitNode.prototype, {
setDimensions: function (dims) {
assert(dims instanceof Dimensions);
var secondWidth = Math.floor(
dims.width() * this.getSecondChild().getDimensions().width() / this.getDimensions().width()),
firsthWidth = dims.width() - secondWidth,
node1dims = new Dimensions(dims.left(), dims.top(), firstWidth, dims.height()),
node2dims = new Dimensions(dims.left() + firstWidth, dims.top(), secondWidth, dims.height());
return new this.constructor(node1, node1dims, node2, node2dims, parent);
}
});
function VerticalSplitNode(node1, node1dims, node2, node2dims, parent) {
assert(isNonRootNode(node1) && isNonRootNode(node2) &&
(parent === null || isNode(parent)) &&
(node1dims instanceof Dimensions && node2dims instanceof Dimensions &&
node1dims.left() === node2dims.left() && node1dims.width() === node2dims.width() &&
node1dims.top() + node1dims.height() === node2dims.top()));
node1 = node1.setDimensions(node1dims); node2 = node2.setDimensions(node2dims);
var dims = new Dimensions(node1dims.left(), node1dims.top(),
node1dims.width(), node1dims.height() + node2dims.height());
AbstractSplitNode.call(this, node1, node2, parent, dims);
}
inherits(VerticalSplitNode, AbstractSplitNode);
$.extend(VerticalSplitNode.prototype, {
setDimensions: function (dims) {
assert(dims instanceof Dimensions);
var secondHeight = Math.floor(
dims.width() * this.getSecondChild().getDimensions.height() / this.getDimensions().height()),
firstHeight = dims.height() - secondHeight,
node1dims = new Dimensions(dims.left(), dims.top(), dims.width(), firstHeight),
node2dims = new Dimensions(dims.left(), dims.top() + firstHeight, dims.width(), secondHeight);
return new this.constructor(node1, node1dims, node2, node2dims, parent);
}
});
})(jQuery);