-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
87 lines (78 loc) · 1.96 KB
/
index.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
'use strict'
var YAJB = function(){
this.eventQueue = []
this.counter = 1
var options
this.isAndroid = false
this.isiOS = false
// get global options
if (window.javaInterface) {
options = JSON.parse(window.javaInterface.toString())
this.isAndroid = true
}else if (window.YAJB_INJECT){
options = window.YAJB_INJECT
this.isiOS = true
}
else {
throw new Error('No Global Inject Object detected, please use yajb-js in a YAJB WebView Environment.');
}
this.platform = options.platform
this.data = options.data
window.YAJB_INSTANCE = this
}
YAJB.prototype._emit = function(option){
var opt = JSON.parse(option)
// trigger event
//this.messageQueue.push(opt)
this.checkQueue(opt);
// this.events[option.name].apply({}, option.data)
}
YAJB.prototype.isMobile = function() {
if (window.javaInterface || window.YAJB_INJECT) {
return true
}else {
return false
}
}
YAJB.prototype._send = function(option) {
if (this.isAndroid) {
// window.alert(JSON.stringify(option))
window.location = "hybrid://" + option.event + ':' + option.id + '/'+ option.data
}else if (this.isiOS) {
// window.postMessage
}
}
YAJB.prototype.checkQueue = function(option){
// this.eventQueue.forEach(function(item){
// if(item.id === option.id){
// item.callback(option.data)
// }
// })
var event = this.eventQueue.find(function(item){
return item.id === option.id
})
event.callback(option.data)
}
// YAJB.prototype.on = function(event, callback) {
// //register event
// if(!YAJB.events[event]) {
// YAJB.events[event] = [];
// }
// YAJB.events[event].push(callback);
// }
YAJB.prototype.send = function(event, data) {
var that = this;
return new Promise(function(resolve, reject){
that.eventQueue.push({
event: event + "Resolved",
id : that.counter,
callback: function(value){
console.log("resolve")
resolve(value)
}
})
that._send({event:event,id:that.counter,data:data});
that.counter++
})
}
export default YAJB