-
Notifications
You must be signed in to change notification settings - Fork 13
/
ampersand-view-switcher.js
132 lines (117 loc) · 3.27 KB
/
ampersand-view-switcher.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
/*$AMPERSAND_VERSION*/
function ViewSwitcher(options) {
options || (options = {});
this.el = options.el;
this.config = {
hide: null,
show: null,
empty: null,
prepend: false,
waitForRemove: false,
autoRender: true
};
for (var item in options) {
if (this.config.hasOwnProperty(item)) {
this.config[item] = options[item];
}
}
this._setCurrent(options.view);
if (this.config.autoRender) this.render();
}
ViewSwitcher.prototype.set = function (view) {
var self = this;
var prev = this.previous = this.current;
if (prev === view) {
return;
}
if (this.config.waitForRemove) {
this.next = view;
this._hide(prev, function () {
if (self.next === view) {
delete self.next;
self._show(view);
}
});
} else {
this._hide(prev);
this._show(view);
}
return this;
};
ViewSwitcher.prototype._setCurrent = function (view) {
this.current = view;
if (view) this._registerRemoveListener(view);
var emptyCb = this.config.empty;
if (emptyCb && !this.current) {
emptyCb();
}
return view;
};
ViewSwitcher.prototype.clear = function (cb) {
this._hide(this.current, cb);
return this;
};
// If the view switcher itself is removed, remove its child to avoid memory leaks
ViewSwitcher.prototype.remove = function () {
if (this.current) this.current.remove();
if (this.previous) this.previous.remove();
if (this.el && this.el.parentNode) this.el.parentNode.removeChild(this.el);
return this;
};
ViewSwitcher.prototype._show = function (view) {
var customShow = this.config.show;
this._setCurrent(view);
this._render(view);
if (customShow) customShow(view);
};
ViewSwitcher.prototype._registerRemoveListener = function (view) {
if (view && view.once) view.once('remove', this._onViewRemove, this);
};
ViewSwitcher.prototype._onViewRemove = function (view) {
var emptyCb = this.config.empty;
if (this.current === view) {
this.current = null;
}
if (emptyCb && !this.current) {
emptyCb();
}
};
ViewSwitcher.prototype._render = function (view) {
if (!this.el) return;
if (!view.rendered) view.render({containerEl: this.el});
if (!view.insertSelf) {
if (this.config.prepend) {
this.el.insertBefore(view.el, this.el.firstChild);
} else {
this.el.appendChild(view.el);
}
}
};
ViewSwitcher.prototype.render = function () {
if (this.current && !this._rendered) {
this._render(this.current);
}
//set rendered, el exists even if a current view was not inserted/appended
this._rendered = true;
return this;
};
ViewSwitcher.prototype._hide = function (view, cb) {
var customHide = this.config.hide;
if (!view) return cb && cb();
if (customHide) {
if (customHide.length === 2) {
customHide(view, function () {
view.remove();
if (cb) cb();
});
} else {
customHide(view);
view.remove();
if (cb) cb();
}
} else {
view.remove();
if (cb) cb();
}
};
module.exports = ViewSwitcher;