-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathCollectionView.js
83 lines (70 loc) · 2.41 KB
/
CollectionView.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
function createCollectionView(options) {
if( OS_IOS )
{
return require("ti.collectionview").createCollectionView(options);
}
var templates = options.templates;
for (var binding in templates) {
var currentTemplate = templates[binding];
//process template
processTemplate(currentTemplate);
//process child templates
processChildTemplates(currentTemplate);
}
Ti.API.info( JSON.stringify(options) );
var listView = require("ti.collectionview").createCollectionView(options);
return listView;
}
//Create ListItemProxy, add events, then store it in 'tiProxy' property
function processTemplate(properties) {
var cellProxy = require("ti.collectionview").createCollectionItem();
properties.tiProxy = cellProxy;
var events = properties.events;
addEventListeners(events, cellProxy);
}
//Recursive function that process childTemplates and append corresponding proxies to
//property 'tiProxy'. I.e: type: "Titanium.UI.Label" -> tiProxy: LabelProxy object
function processChildTemplates(properties) {
if (!properties.hasOwnProperty('childTemplates')) return;
var childProperties = properties.childTemplates;
if (childProperties === void 0 || childProperties === null) return;
for (var i = 0; i < childProperties.length; i++) {
var child = childProperties[i];
var proxyType = child.type;
if (proxyType !== void 0) {
var creationProperties = child.properties;
var creationFunction = lookup(proxyType);
var childProxy;
//create the proxy
if (creationProperties !== void 0) {
childProxy = creationFunction(creationProperties);
} else {
childProxy = creationFunction();
}
//add event listeners
var events = child.events;
addEventListeners(events, childProxy);
//append proxy to tiProxy property
child.tiProxy = childProxy;
}
processChildTemplates(child);
}
}
//add event listeners
function addEventListeners(events, proxy) {
if (events !== void 0) {
for (var eventName in events) {
proxy.addEventListener(eventName, events[eventName]);
}
}
}
//convert name of UI elements into a constructor function.
//I.e: lookup("Titanium.UI.Label") returns Titanium.UI.createLabel function
function lookup(name) {
var lastDotIndex = name.lastIndexOf('.');
var proxy = eval(name.substring(0, lastDotIndex));
if (typeof(proxy) == undefined) return;
var proxyName = name.slice(lastDotIndex + 1);
return proxy['create' + proxyName];
}
exports.createCollectionView = createCollectionView;