-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAPI.js
363 lines (332 loc) · 16 KB
/
API.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Copyright (c) 2014 by Center Open Middleware. All Rights Reserved.
* Titanium Appcelerator 3.3.0GA
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*
* API Team
*
* Alejandro F.Carrera
* Carlos Blanco Vallejo
* Santiago Blanco Ventas
* Alejandro Vera De Juan
*
*/
'use strict';
/** YaaST API.
* @version 1.0.0
* @alias API
* @namespace
*/
/**
* Yaast API.
* @class
*/
/**
* @constructs API
* @param initVector Initialization vector. It is an object that indicates the APIs that must be loaded. For example { SW : { Utils: true, Map: true } }
*/
var API = function(initVector) {
//TODO: only for debug
var eventcounter = {};
var self = {
SW: {},
HW: {}
};
if(initVector && initVector.HW) {
if (initVector.HW.System) self.HW.System = require('lib/API.System');
if (initVector.HW.UI) self.HW.UI = require('lib/API.UI')(self);
if (initVector.HW.Network) self.HW.Network = require('lib/API.Network')(self);
if (initVector.HW.Media) self.HW.Media = require('lib/API.Media')(self);
if (initVector.HW.Acceloremeter) self.HW.Acceloremeter = require('lib/API.Accelerometer');
if (initVector.HW.Battery) self.HW.Battery = require('lib/API.Battery');
if (initVector.HW.Camera) self.HW.Camera = require('lib/API.Camera')(self);
if (initVector.HW.GeoLocation) self.HW.GeoLocation = '';
if (initVector.HW.Gesture) self.HW.Gesture = '';
}
if(initVector && initVector.SW) {
if (initVector.SW.FileSystem) self.SW.FileSystem = require('lib/API.FileSystem');
if (initVector.SW.Contacts) self.SW.Contacts = require('lib/API.Contacts')(self);
if (initVector.SW.Utils) self.SW.Utils = require('lib/API.Utils')(self);
if (initVector.SW.Notification) self.SW.Notification = require('lib/API.Notification')(self);
if (initVector.SW.Calendar) self.SW.Calendar = '';
if (initVector.SW.DataBase) self.SW.DataBase = '';
if (initVector.SW.Map) self.SW.Map = require('lib/API.Map');
if (initVector.SW.Social) self.SW.Social = '';
}
var events = {
activeHandlers: {},
availableEvents: {},
publicEvents: {}
};
var initEvents = function initEvents() {
var key, eventId, eventData, enventId;
/* TODO new class eventData
* API.Example.events = {
* 'publicEventName': {
* event: 'titaniumeventname',
* listener: Ti.thePathToTitModule
* }
* };
*/
for (key in self.SW) {
if (self.SW[key].events) {
for (eventId in self.SW[key].events) {
eventData = self.SW[key].events[eventId];
if (typeof eventData.source === 'undefined') {
Ti.API.info('[API._initEvents] Error. event source undefined. self.SW.' + key + '.events.' + eventId + ' = ' + JSON.stringify(eventData));
continue;
} else {
if (eventData.dummy) {
events.availableEvents[eventId] = {
keylist: eventData.keylist,
event: eventData.event,
listener: self.SW[key],
source: eventData.source,
dummy: true
};
} else {
events.availableEvents[eventId] = {
keylist: eventData.keylist,
event: eventData.event,
listener: eventData.listener,
source: eventData.source
};
}
//events.publicEvents[eventData.event] = eventId;
if (events.publicEvents[eventData.source] == null) {
events.publicEvents[eventData.source] = {};
}
events.publicEvents[eventData.source][eventData.event] = eventId;
}
}
}
}
for (key in self.HW) {
if (self.HW[key].events) {
for (eventId in self.HW[key].events) {
eventData = self.HW[key].events[eventId];
if (typeof eventData.source === 'undefined') {
Ti.API.info('[API._initEvents] Error. event source undefined. self.HW.' + key + '.events.' + eventId + ' = ' + JSON.stringify(eventData));
continue;
} else {
if (eventData.dummy) {
events.availableEvents[eventId] = {
keylist: eventData.keylist,
event: eventData.event,
listener: self.HW[key],
source: eventData.source,
dummy: true
};
} else {
events.availableEvents[eventId] = {
keylist: eventData.keylist,
event: eventData.event,
listener: eventData.listener,
source: eventData.source
};
}
//events.publicEvents[eventData.event] = eventId;
if (events.publicEvents[eventData.source] == null) {
events.publicEvents[eventData.source] = {};
}
events.publicEvents[eventData.source][eventData.event] = eventId;
}
}
}
}
};
/** Transport to html a internal event
* @param: {eventName} the name of the Titanium event.
* @param: {data} event data.
* @return : Bool true if success or false if error*/
events.fireHTMLEvents = function fireHTMLEvents(data) {
var key, viewId, result;
if (events.activeHandlers[data.publicEvent]) {
/*
TODO: other way... maybe more efficient
for (key in events.activeHandlers[publicEvent].views) {
viewId = events.activeHandlers[publicEvent].views[key];
// This event must be send to each view
evaljs...
}*/
eventcounter[data.publicEvent] ++;
// for all events with timestamp
if (data.timestamp != null && (data.timestamp - eventControl[data.publicEvent]) < 500){
// discart event to improve
} else {
// TODO: only for debug
console.log('[API]---------> new event: ' + data.publicEvent + ' counter: ' + eventcounter[data.publicEvent] + '; data: ' + JSON.stringify(data));
Ti.App.fireEvent(data.publicEvent, data);
console.log('[API] event fired');
}
result = true;
} else {
result = false;
}
return result;
};
// Create new handler
var eventHandler = function(e) {
var i;
var data = {};
var publicEvent = events.publicEvents[e.source.apiName][e.type];
data['publicEvent'] = publicEvent;
for (i = 0; i < events.availableEvents[publicEvent].keylist.length; i++) {
data[events.availableEvents[publicEvent].keylist[i]] = e[events.availableEvents[publicEvent].keylist[i]];
}
events.fireHTMLEvents(data);
};
/** AddEventListener from a HTML view (througth APICommons.js TODO)
* @param: {publicEvent} the public html name for the event.
* @param: {viewId} the id of the interested html view.
* @return : Bool true if success or false if error*/
events.addEventListener = function addEventListener(publicEvent, viewId, entityId, dummy) {
var eventData;
eventData = events.availableEvents[publicEvent];
if (!eventData) {
// Public event doesn't exist
return false;
}
if (typeof events.activeHandlers[publicEvent] !== 'undefined') {
// Main listener active yet
events.activeHandlers[publicEvent].nlisteners += 1;
if (events.activeHandlers[publicEvent].views[viewId] !== 0) {
events.activeHandlers[publicEvent].views[viewId] = 0;
}
events.activeHandlers[publicEvent].views[viewId] += 1;
} else {
// Inicialize handler for this event
events.activeHandlers[publicEvent] = {
nlisteners: 1,
views: {}
};
Ti.API.info('[API.addEventListener] event: ' + publicEvent + ', privateEvent: ' + events.availableEvents[publicEvent].event, ', entityId: ' + entityId + ', dummy: ' + dummy + ', isDummy?: ' + events.availableEvents[publicEvent].dummy);
eventcounter[publicEvent] = 0;
if (events.availableEvents[publicEvent].listener === 'accelerometer') {
// Accelerometer is special.
Ti.Accelerometer.addEventListener(events.availableEvents[publicEvent].event, eventHandler);
} else if (events.availableEvents[publicEvent].dummy) {
// Special Dummy events. This events depends of Titnaium Objects
Ti.API.info('[API.addEventListener] special event for dummy: ' + publicEvent + ', privateEvent: ' + events.availableEvents[publicEvent].event, ', entityId: ' + entityId + ', dummy: ' + dummy);
events.availableEvents[publicEvent].listener.addEventListener(events.availableEvents[publicEvent].event, eventHandler, entityId, dummy);
} else {
events.availableEvents[publicEvent].listener.addEventListener(events.availableEvents[publicEvent].event, eventHandler);
}
events.activeHandlers[publicEvent].views[viewId] = 1;
}
};
/** RemoveEventListener from a HTML view (througth APICommons.js TODO)
* @param: {publicEvent} the public html name for the event.
* @param: {viewId} the id of the interested html view.
* @return : Bool true if success or false if error*/
events.removeEventListener = function removeEventListener(publicEvent, viewId, entityId, dummy) {
var eventData;
eventData = events.availableEvents[publicEvent];
if (!eventData || !events.activeHandlers[publicEvent]) {
// Public event doesn't exist
return false;
}
events.activeHandlers[publicEvent].nlisteners -= 1;
events.activeHandlers[publicEvent].views[viewId] -= 1 ;
if (events.availableEvents[publicEvent].listener === 'accelerometer') {
// Accelerometer is special.
Ti.Accelerometer.removeEventListener(events.availableEvents[publicEvent].event, eventHandler);
} else {
Ti.API.info('[API.removeEventListener] special event: ' + publicEvent + ', privateEvent' + events.availableEvents[publicEvent].event + ', entityId: ' + entityId + ', dummy: ' + dummy);
events.availableEvents[publicEvent].listener.removeEventListener(events.availableEvents[publicEvent].event, eventHandler, entityId, dummy);
}
if (events.activeHandlers[publicEvent].nlisteners == 0) {
delete events.activeHandlers[publicEvent];
}
};
/** Handler for the events from a HTML view througth APICommons.js
* @param: {data} data = {
* 'action': 'addEventListener'/'removeEventListener',
* 'viewId': viewId,
* 'entityId': entityId optional param. the Titanium Object ID.
* 'dummy': dummy name optional param.
* }
**/
events.APIEventHandler = function APIEventHandler(data) {
Ti.API.info('[API.APIEventHandler]: entityId: ' + data.entityId, ', viewId: ' + data.viewId + ', event: ' + data.event + ', dummy: ' + data.dummy);
if (data.action === 'addEventListener') {
events.addEventListener(data.event, data.viewId, data.entityId, data.dummy);
} else if (data.action === 'removeEventListener') {
events.removeEventListener(data.event, data.viewId, data.entityId, data.dummy);
}
};
/** Handler for the API methods invocations from a HTML view througth APICommons.js
* @param: {data} data = {
* 'method': 'API.Example.getBlabla',
* 'params': [param],
* 'options': {},
* 'eventName': eventName
* }
**/
events.APIMethodHandler = function APIMethodHandler(data) {
var result;
Ti.API.info('[API.APIMethodHandler] Method event recieved: ' + JSON.stringify(data));
Ti.API.info('[API.APIMethodHandler] Params:' + JSON.stringify(data.params));
Ti.API.info('[API.APIMethodHandler] Options:' + JSON.stringify(data.options));
if (data.method !== null && data.params == null && data.options == null) {
try {
result = self[data.method.type][data.method.subapi][data.method.name]();
} catch (e) {
// TODO
Ti.API.info('[----- ¡¡API METHOD ERROR!!] ' + e);
}
} else if (data.method !== null && data.params !== null) {
if (data.options !== null) {
data.params.push(data.options);
}
try {
result = self[data.method.type][data.method.subapi][data.method.name].apply(null, data.params);
} catch (e) {
// TODO
Ti.API.info('[----- ¡¡API METHOD ERROR!!] ' + e);
}
} else {
// Error. Method doesn't exist
result = "Error. Unknown API method";
}
Ti.API.info('[API.APIMethodHandler] Sending method event from API to Bridge: ' + data.method.eventName + '_' + data.viewId + '_' + data.callId);
Ti.API.info('[API.APIMethodHandler] data: ' + JSON.stringify(result));
Ti.App.fireEvent(data.method.eventName + '_' + data.viewId + '_' + data.callId, {'returnedData': result});
};
events.APIMethodAsyncHandler = function APIMethodAsyncHandler(data) {
var result;
Ti.API.info('[API.APIMethodAsyncHandler] Async Method event recived: ' + JSON.stringify(data));
Ti.API.info('[API.APIMethodAsyncHandler] Params:' + JSON.stringify(data.params));
Ti.API.info('[API.APIMethodAsyncHandler] Options:' + JSON.stringify(data.options));
var genericHandler = function(initialData, result) {
Ti.App.fireEvent(data.method.eventName + '_' + data.viewId + '_' + data.callId, result);
};
if (data.method !== null && data.params.length === 0) {
self[data.method.type][data.method.subapi][data.method.name](function(result) {
Ti.API.info('[API.APIMethodAsyncHandler] Setting method event from aPI to Bridge: :' + data.method.eventName + '_' + data.viewId + '_' + data.callId);
Ti.App.fireEvent(data.method.eventName + '_' + data.viewId + '_' + data.callId, result);
});
} else if (data.method !== null) {
if (data.options !== null) {
data.params.push(data.options);
}
self[data.method.type][data.method.subapi][data.method.name](function(result) {
Ti.API.info('[API.APIMethodAsyncHandler] Setting method event from aPI to Bridge: :' + data.method.eventName + '_' + data.viewId + '_' + data.callId);
Ti.App.fireEvent(data.method.eventName + '_' + data.viewId + '_' + data.callId, result);
}, data.params);
} else {
// Error. Method doesn't exist
result = "Error. Unknown API method";
}
};
var init = function init() {
initEvents();
Ti.App.addEventListener('APIEvent', events.APIEventHandler);
Ti.App.addEventListener('APIMethod', events.APIMethodHandler);
Ti.App.addEventListener('APIMethodAsync', events.APIMethodAsyncHandler);
};
init();
return self;
};
module.exports = API;