-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssistantBase.js
More file actions
164 lines (163 loc) · 5.19 KB
/
AssistantBase.js
File metadata and controls
164 lines (163 loc) · 5.19 KB
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
/**
* AssistantBase v1.1 by Fourth Draft Software
*
* A simple base class to inherit from for assistants, to simplify usage.
* See README for more information.
*/
var AssistantBase = Class.create({
/**
* A hash of listeners added via addListener.
*/
listeners: {},
/**
* A hash of listeners added via addActivateListener.
*/
activateListeners: {},
/**
* The basic initialize function required of all classes. Sets defaults.
*/
initialize: function() {
this.listeners = {};
this.activateListeners = {};
},
/**
* When setup is called, it checks through every function in the class.
* If a function begins with setup (e.g. setupButton), it will call that
* function. If it begins with do (e.g. doSelectUser) it will bind the
* 'this' context to that user.
*
* In addition, it will add the 'pixi' class name to the body if the
* user is using a pixi. This way, a secondary CSS can be set up that
* checks body.pixi style and has new widths.
*
* setup occurs automatically the first time an assistant is called.
*/
setup: function() {
if(Mojo.Environment.DeviceInfo.touchableRows < 8) {
document.body.addClassName('pixi');
}
for (i in this) {
if (typeof this[i] == 'function') {
if (i.indexOf('setup') == 0 && i.length > 'setup'.length) {
this[i].call(this);
} else if (i.indexOf('do') == 0 && i.length > 'do'.length) {
this[i] = this[i].bind(this);
}
}
}
},
/**
* @param {Object} event
* The activate function is called every time the scene is made active.
*/
activate: function(event) {},
/**
* @param {Object} event
*
* Deactivate loops through the activateListeners that have been added and
* stops listening to them.
*
* The deactivate function is called every time the scene is deactivated.
*/
deactivate: function(event) {
for (i in this.activateListeners) {
var l = this.activateListeners[i];
Mojo.Event.stopListening(this.controller.get(i), l.event, l.func);
}
},
/**
* Cleanup loops through the listeners added via addListener and stops
* listening to them.
*
* Cleanup is called when the card is closing for the last time.
*/
cleanup: function() {
for (i in this.listeners) {
var l = this.listeners[i];
Mojo.Event.stopListening(this.controller.get(i), l.event, l.func);
}
},
/**
* @param {string} id
* @param {string} eventname
* @param {string|function} func
* @param {boolean} activateLevel
*
* addListener replaces the standard Mojo.Event.listen() function by
* binding the function to the 'this' context and keeping track of it,
* so that it can be stopped during cleanup.
*/
addListener: function(id, eventname, func, activateLevel) {
var f = null;
if (typeof func == 'function') {
func = func.bind(this);
f = func;
} else {
this[func] = this[func].bind(this);
f = this[func];
}
Mojo.Event.listen(this.controller.get(id), eventname, f);
if (activateLevel) {
this.activateListeners[id] = { event: eventname, func: f };
} else {
this.listeners[id] = { event: eventname, func: f };
}
},
/**
* @param {string} id
* @param {string} eventname
* @param {string|function} func
*
* as addListener, but specifically should be called when you want to listen
* on something during activate() events.
*/
addActivateListener: function(id, eventname, func) {
this.addListener(id, eventname, func, true);
},
/**
* @param {string} title
* @param {string} message
*
* Helper function to show a dialog box with an 'ok' button.
*/
showDialogBox: function(title, message){
this.controller.showAlertDialog({
onChoose: function(value){
},
title: title,
message: message,
choices: [{
label: 'OK',
value: 'OK',
type: 'color'
}]
});
},
/**
* @param {string} title
* @param {string} message
* @param {hash|function} eventTrigge
*
* Helper function to show an ok/cancel dialog; if 'ok' is selected,
* call onApprove.
*/
showOkCancelBox: function(title, message, onApprove) {
if (typeof eventTrigger == 'undefined') {
return false;
}
this.controller.showAlertDialog({
onChoose: function(value) {
Mojo.Log.info('selected a value, value is', value);
if (value == 'OK') {
onApprove();
}
},
title: title,
message: message,
choices: [
{ label: 'OK', value: 'OK', type: 'affirmative' },
{ label: 'Cancel', value: 'Cancel', type: 'negative' }
]
});
}
});