/
cocoon.js
245 lines (210 loc) · 7.64 KB
/
cocoon.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
(function() {
/**
* The "Cocoon" object holds all the Cocoon Extensions and other stuff needed for the Cocoon environment.
* @namespace Cocoon
*/
var Cocoon = window.Cocoon || (window.Cocoon = {});
if (window.cordova && typeof module !== 'undefined') {
module.exports = Cocoon;
}
/**
* @property {string} version Current version of the library.
* @memberOf Cocoon
* @example
* console.log(Cocoon.version);
*/
Cocoon.version = "1.0";
/**
* This utility function allows to create an object oriented like hierarchy between two functions using their prototypes.
* This function adds a "superclass" and a "__super" attributes to the subclass and it's functions to reference the super class.
* @memberof Cocoon
* @private
* @static
* @param {function} subc The subclass function.
* @param {function} superc The superclass function.
*/
Cocoon.extend = function(subc, superc) {
var subcp = subc.prototype;
var CocoonJSExtendHierarchyChainClass = function() {};
CocoonJSExtendHierarchyChainClass.prototype = superc.prototype;
subc.prototype = new CocoonJSExtendHierarchyChainClass();
subc.superclass = superc.prototype;
subc.prototype.constructor = subc;
if (superc.prototype.constructor === Object.prototype.constructor) {
superc.prototype.constructor = superc;
}
for (var method in subcp) {
if (subcp.hasOwnProperty(method)) {
subc.prototype[method] = subcp[method];
}
}
};
/**
* Bridge function to call native functions from JavaScript
* @static
* @private
* @param {string} serviceName The name of native extension service
* @param {string} functionName The name of the function to be called inside the native extension object.
* @param {array} args The arguments of the function to be called
*/
Cocoon.exec = function(serviceName, functionName, args, succeesCallback, failCallback) {
if (window.cordova) {
window.cordova.exec(succeesCallback, failCallback, serviceName, functionName, args);
} else {
console.error("window.cordova not found");
}
};
/**
* This function is used to create extensions in the global namespace of the "Cocoon" object.
* @memberof Cocoon
* @private
* @static
* @param {string} namespace The extensions namespace, ex: Cocoon.App.Settings.
* @param {object} callback The callback which holds the declaration of the new extension.
* @example
* Cocoon.define("Cocoon.namespace" , function(extension){
* "use strict";
*
* return extension;
* });
*/
Cocoon.define = function(extName, ext, exportToModule) {
var namespace = (extName.substring(0, 7) == "Cocoon.") ? extName.substr(7) : extName;
var base = window.Cocoon;
var parts = namespace.split(".");
var object = base;
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (!object[part]) {
console.log("Created namespace: " + extName);
} else {
console.log("Updated namespace: - " + extName);
}
object = object[part] = (i == (parts.length - 1)) ? ext((object[part] || {})) : {};
if (!object) {
throw "Unable to create class " + extName;
}
}
if (arguments.length < 2) {
exportToModule = true;
}
if (exportToModule && typeof module !== 'undefined') {
module.exports = object;
}
return true;
};
/**
* This constructor creates a new Signal that holds and emits different events that are specified inside each extension.
* @constructor Cocoon.Signal
* @memberof Cocoon
*/
Cocoon.Signal = function() {
this.signals = {};
};
Cocoon.Signal.prototype = {
on: function(eventName, handler) {
if (!eventName || !handler) {
throw new Error("Can't create signal " + (eventName || ""));
}
var listeners = this.signals[eventName];
if (!listeners) {
listeners = [];
this.signals[eventName] = listeners;
}
listeners.push(handler);
},
emit: function(eventName, functionName, args) {
var listeners = this.signals[eventName];
if (!listeners) {
return;
}
for (var i = 0; i < listeners.length; ++i) {
var func = listeners[i];
if (functionName) {
func = func[functionName];
}
if (func) {
func.apply(null, args || []);
}
}
},
remove: function(eventName, handler) {
var listeners = this.signals[eventName];
if (!listeners) {
return;
}
if (!handler) {
listeners.lenght = 0;
} else {
for (var i = 0; i < listeners.lenght; ++i) {
if (listeners[i] === handler) {
listeners.splice(i, 1);
--i;
}
}
}
},
expose: function() {
return this.on.bind(this);
}
};
/**
* Defines the platform where the app is running.
* @name Cocoon.PlatformType
* @memberOf Cocoon
* @property {string} ANDROID Android.
* @property {string} IOS iOS.
* @property {string} AMAZON Amazon.
* @property {string} WINDOWS_PHONE Windows phone.
* @property {string} BLACKBERRY Blackberry.
* @property {string} OTHER Other.
*/
Cocoon.PlatformType = {
ANDROID: "android",
IOS: "ios",
AMAZON: "amazon",
WINDOWS_PHONE: "wp",
BLACKBERRY: "blackberry",
OTHER: "other"
};
var cachedPlatform;
/**
* Returns the platform where the app is running.
* @function getPlatform
* @memberOf Cocoon
* @returns {Cocoon.PlatformType} The platform where the app is running.
* @example
* var platform = Cocoon.getPlatform();
*/
Cocoon.getPlatform = function() {
if (cachedPlatform) {
return cachedPlatform;
}
var ua = navigator.userAgent;
if (navigator.isCocoonJS) {
if (/ios/ig.test(ua)) {
cachedPlatform = Cocoon.PlatformType.IOS;
} else {
cachedPlatform = Cocoon.PlatformType.ANDROID;
}
return cachedPlatform;
}
if (/(iPad|iPhone|iPod)/g.test(ua)) {
cachedPlatform = Cocoon.PlatformType.IOS;
} else if (/Kindle/i.test(ua) || /Silk/i.test(ua) || /KFTT/i.test(ua) || /KFOT/i.test(ua) ||
/KFJWA/i.test(ua) || /KFJWI/i.test(ua) || /KFSOWI/i.test(ua) || /KFTHWA/i.test(ua) ||
/KFTHWI/i.test(ua) || /KFAPWA/i.test(ua) || /KFAPWI/i.test(ua)) {
cachedPlatform = Cocoon.PlatformType.AMAZON;
} else if (/Android/i.test(ua)) {
cachedPlatform = Cocoon.PlatformType.ANDROID;
} else if (/BlackBerry/i.test(navigator.userAgent)) {
cachedPlatform = Cocoon.PlatformType.BLACKBERRY;
} else if (/IEMobile/i.test(navigator.userAgent)) {
cachedPlatform = Cocoon.PlatformType.WINDOWS_PHONE;
} else {
cachedPlatform = Cocoon.PlatformType.OTHER;
}
return cachedPlatform;
};
console.log("Created namespace: Cocoon");
})();