forked from damiengarbarino/dojo-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RendererManager.js
213 lines (175 loc) · 4.85 KB
/
RendererManager.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
define([
"dcl/dcl",
"dojo/_base/html",
"dojo/dom-style",
"ibm-decor/Evented"
], function (
dcl,
html,
domStyle,
Evented
) {
return dcl(Evented, {
// summary:
// Factory for creating and caching released item renderers (LabelRenderer, VerticalRenderer, etc.)
// owner: Object
// The owner of the store manager: a view or a calendar widget.
owner: null,
// rendererPool: [protected] Array
// The stack of recycled renderers available.
rendererPool: null,
// rendererList: [protected] Array
// The list of used renderers
rendererList: null,
// itemToRenderer: [protected] Object
// The associated array item to renderer list.
itemToRenderer: null,
constructor: function (/*Object*/ args) {
this.rendererPool = {};
this.rendererList = [];
this.itemToRenderer = {};
if (args) {
for (var key in args) {
this[key] = args[key];
}
}
},
destroy: function () {
while (this.rendererList.length > 0) {
this.destroyRenderer(this.rendererList.pop());
}
for (var kind in this.rendererPool) {
var pool = this.rendererPool[kind];
if (pool) {
while (pool.length > 0) {
this.destroyRenderer(pool.pop());
}
}
}
},
recycleItemRenderers: function () {
// summary:
// Recycles all the item renderers.
// tags:
// protected
while (this.rendererList.length > 0) {
var ir = this.rendererList.pop();
this.recycleRenderer(ir);
}
this.itemToRenderer = {};
},
getRenderers: function (item) {
// summary:
// Returns the renderers that are currently used to display the specified item.
// Returns an array of objects that contains two properties:
// - container: The DOM node that contains the renderer.
// - renderer: The dcalendar/_RendererMixin instance.
// Do not keep references on the renderers are they are recycled and reused for other items.
// item: Object
// The data or render item.
// returns: Object[]
if (item == null || item.id == null) {
return null;
}
var list = this.itemToRenderer[item.id];
return list == null ? null : list.concat();
},
createRenderer: function (item, kind, RendererClass) {
// summary:
// Creates an item renderer of the specified kind.
// A renderer is an object with the "container" and "instance" properties.
// item: Object
// The data item.
// kind: String
// The kind of renderer.
// RendererClass: Object
// The class to instantiate to create the renderer.
// returns: Object
// tags:
// protected
if (item != null && kind != null && RendererClass != null) {
var res = null, renderer = null;
var pool = this.rendererPool[kind];
if (pool != null) {
res = pool.shift();
}
if (res == null) {
renderer = new RendererClass();
res = {
renderer: renderer,
container: renderer,
kind: kind
};
this.emit("renderer-created", {renderer: res, source: this.owner, item: item});
} else {
renderer = res.renderer;
this.emit("renderer-reused", {renderer: renderer, source: this.owner, item: item});
}
renderer.owner = this.owner;
renderer.rendererKind = kind;
renderer.item = item;
renderer.deliver();
var list = this.itemToRenderer[item.id];
if (list == null) {
this.itemToRenderer[item.id] = list = [];
}
list.push(res);
this.rendererList.push(res);
return res;
}
return null;
},
recycleRenderer: function (renderer) {
// summary:
// Recycles the item renderer to be reused in the future.
// renderer: dcalendar/_RendererMixin
// The item renderer to recycle.
// tags:
// protected
this.emit("renderer-recycled", {renderer: renderer, source: this.owner});
var pool = this.rendererPool[renderer.kind];
if (pool == null) {
this.rendererPool[renderer.kind] = [renderer];
} else {
pool.push(renderer);
}
renderer.container.parentNode.removeChild(renderer.container);
renderer.renderer.owner = null;
renderer.renderer.item = null;
},
destroyRenderer: function (renderer) {
// summary:
// Destroys the item renderer.
// renderer: dcalendar/_RendererMixin
// The item renderer to destroy.
// tags:
// protected
this.emit("renderer-destroyed", {renderer: renderer, source: this.owner});
var ir = renderer.renderer;
if (ir.destroy) {
ir.destroy();
}
html.destroy(renderer.container);
},
destroyRenderersByKind: function (kind) {
// tags:
// private
var list = [];
for (var i = 0; i < this.rendererList.length; i++) {
var ir = this.rendererList[i];
if (ir.kind == kind) {
this.destroyRenderer(ir);
} else {
list.push(ir);
}
}
this.rendererList = list;
var pool = this.rendererPool[kind];
if (pool) {
while (pool.length > 0) {
this.destroyRenderer(pool.pop());
}
}
}
});
});