-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlib.js
103 lines (93 loc) · 3.06 KB
/
lib.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
UiHooks = {};
UiHooks.defaults = {
insert: function(node, next, container) {
if (next) {
$(node).insertBefore(next);
} else {
$(container).append(node);
}
},
move: function(node, next, container) {
UiHooks.defaults.remove(node);
UiHooks.defaults.insert(node, next, container);
},
remove: function(node) {
$(node).remove();
}
};
var allFalse = function(array) {
var hasOnlyFalse = true;
array.forEach(function(item) {
if (item) return hasOnlyFalse = false;
});
return hasOnlyFalse;
};
var makeHook = function(hooks, defaultCallback) {
return function() {
var args = arguments;
var hooksResults = [];
var self = this;
hooks.forEach(function(hook) {
hooksResults.push(hook.apply(self, args));
});
if (!defaultCallback) return;
if (allFalse(hooksResults)) {
return defaultCallback.apply(self, args);
}
}
};
Template.prototype.uihooks = function(hooksAll) {
var tpl = this;
tpl.onRendered(function() {
if (!tpl.viewName) return;
tpl = this;
var tplContainer = $(tpl.firstNode.parentNode);
for (var selector in hooksAll) {
var hooks = hooksAll[selector];
var declaredContainer = hooks.container && $(hooks.container);
var itemsContainer = tpl.$(selector).length && tpl.$(selector).first().parent();
var container = declaredContainer || itemsContainer || tplContainer;
$(container).each(function() {
if (!this._alluihooks) {
this._alluihooks = {
insert: [],
move: [],
remove: []
};
if (this._uihooks) {
if (this._uihooks.insertElement) this._alluihooks.insert.push(this._uihooks.insertElement);
if (this._uihooks.moveElement) this._alluihooks.move.push(this._uihooks.moveElement);
if (this._uihooks.removeElement) this._alluihooks.remove.push(this._uihooks.removeElement);
}
}
this._alluihooks.insert.push(function(node, next) {
if (!$(node).is(selector)) return false;
hooks.insert && hooks.insert.apply(this, [node, next, tpl]);
return true;
});
this._alluihooks.move.push(function(node, next) {
if (!$(node).is(selector)) return false;
hooks.move && hooks.move.apply(this, [node, next, tpl]);
return true;
});
this._alluihooks.remove.push(function(node) {
if (!$(node).is(selector)) return false;
hooks.remove && hooks.remove.apply(this, [node, tpl]);
return true;
});
var self = this;
this._uihooks = {
insertElement: makeHook(this._alluihooks.insert, function(node, next) {
return UiHooks.defaults.insert(node, next, self);
}),
moveElement: makeHook(this._alluihooks.move, function(node, next) {
return UiHooks.defaults.move(node, next, self);
}),
removeElement: makeHook(this._alluihooks.remove, function(node) {
return UiHooks.defaults.remove(node);
})
};
});
}
});
};