-
Notifications
You must be signed in to change notification settings - Fork 10
/
alloyXL.js
113 lines (84 loc) · 3.36 KB
/
alloyXL.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
Alloy.App = _.clone(Backbone.Events);
// added to make webpack compatible
var createController = Alloy.createController;
var debug = true;
var touchTimeout = 750;
exports.options = (args) => {
debug = args.debug;
touchTimeout = args.touchTimeout;
};
Alloy.createController = function (name, args) {
function cleanUpController(controller) {
Alloy.Controllers[controller.id] = null;
if (controller.__views) {
_.each(controller.__views, function (value) {
cleanUpController(value);
});
}
if (controller.__iamalloy) {
controller.off();
controller.destroy();
}
controller = null;
}
// added to make webpack compatible
var controller = createController(name, args);
// original non-webpack implementation
// var controller = new (require("alloy/controllers/" + name).default)(args);
if (Object.keys(controller.__views).length > 0) {
var view = controller.getView();
Alloy.Controllers = Alloy.Controllers || {};
var path = name.split('/');
if (path.length > 0) name = path[path.length - 1];
if (Alloy.Controllers[name] && !controller.getView().id) {
console.warn('::AlloyXL:: The controller Alloy.Controllers.' + name + ' (' + controller.__controllerPath + ') exists, and will be overwriten because it\'s conflicting with another controller already instanciated with the same name. Please add a unique ID on the top parent view within that controller view so you can use this as the controller name within AlloyXL to avoid this.');
}
if (controller.getView().id) {
name = controller.getView().id;
}
Alloy.Controllers[name] = controller;
controller.once = function (eventName, callback) {
controller.on(eventName, function () {
controller.off(eventName);
callback();
});
return controller;
};
if (typeof view.addEventListener === 'function') {
if (typeof view.open === 'function') {
view.addEventListener('open', function open(e) {
view.removeEventListener('open', open);
controller.trigger('open', e);
if (ENV_DEV && debug) console.log('::AlloyXL:: controller ' + name + ' was opened');
});
view.addEventListener('close', function close() {
view.removeEventListener('close', close);
view = null;
cleanUpController(controller);
controller = null;
if (ENV_DEV && debug) console.log('::AlloyXL:: Controller ' + name + ' cleaned up!');
});
view.addEventListener('postlayout', function postlayout(e) {
view.removeEventListener('postlayout', postlayout);
controller.trigger('postlayout', e);
if (ENV_DEV && debug) console.log('::AlloyXL:: controller ' + name + ' layout finished');
});
} else {
view.addEventListener('postlayout', function postlayout(e) {
view.removeEventListener('postlayout', postlayout);
controller.trigger('postlayout', e);
if (ENV_DEV && debug) console.log('::AlloyXL:: controller ' + name + ' layout finished');
});
}
}
}
if (controller & controller.getView()) {
controller.getView().addEventListener('click', function clickBlocker(e) {
setTimeout(function () {
e.source.touchEnabled = false;
}, touchTimeout);
e.source.touchEnabled = true;
});
}
return controller;
};