This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amcharts.js
131 lines (121 loc) · 3.77 KB
/
amcharts.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
const WebView = require("sf-core/ui/webview");
const File = require('sf-core/io/file');
var EventEmitter = require('wolfy87-eventemitter');
const extend = require("js-base/core/extend");
const AMCharts = new extend(EventEmitter)(function AMCharts(_super, params) {
_super(this);
const me = this;
params = params || {};
var webView = params.webView || new WebView();
var amChartsPath = "assets://amcharts/index.html";
var chartData;
var amChartScriptNames = [];
var ready = false;
var readyWaiting = [];
Object.defineProperties(this, {
"amChartsPath": {
get: function() {
return amChartsPath;
},
set: function(value) {
amChartsPath = value.slice(-1) === '/' ? value + "index.html" : value + "/index.html";
},
enumerable: true
},
"webView": {
value: webView,
writable: false,
enumerable: true
},
"refresh": {
value: function() {
webView.loadFile(new File({ path: amChartsPath }));
},
enumerable: true
},
"data": {
get: function() {
return chartData;
},
set: function(value) {
chartData = value;
},
enumerable: true
},
"loadScritsByName": {
enumerable: true,
writable: false,
value: loadScritsByName
},
"ready": {
enumerable: true,
writable: false,
value: readyFunction
},
"evaluateJS": {
enumerable: true,
writable: false,
value: webView.evaluateJS.bind(webView)
}
});
function loadScritsByName() {
var scriptNames = [].concat(
arguments.length === 1 ?
arguments[0] :
arguments
);
scriptNames = scriptNames.filter((item) => { return amChartScriptNames.indexOf(item) === -1; });
return new Promise((resolve, reject) => {
if (scriptNames.length === 0) {
reject();
return;
}
var myScript = "window.addScript('" + JSON.stringify(scriptNames) + "');";
me.once("scriptLoaded", (scriptLoadStatus) => {
for (let s in scriptLoadStatus) {
scriptLoadStatus[s] && amChartScriptNames.push(s);
}
resolve(scriptLoadStatus);
});
webView.evaluateJS(myScript);
});
}
webView.onChangedURL = function(event) {
if (event.url.indexOf('amcharts://') != -1) {
var eventObject = { event: null };
try {
eventObject = JSON.parse(decodeURIComponent(event.url.replace('amcharts://', '')));
}
catch (ex) {}
if (eventObject.event) {
// console.log(`Event captured: ${eventObject.event} - ${JSON.stringify(eventObject.data)}`);
me.emit(eventObject.event, eventObject.data);
}
return false;
}
return true;
};
webView.onLoad = function(event) {
me.once("window.onload", () => {
ready = true;
readyWaiting.forEach(item => {
item();
});
readyWaiting.length = 0;
});
};
function readyFunction() {
return new Promise((resolve, reject) => {
if (ready)
resolve();
else
readyWaiting.push(() => {
resolve();
});
});
}
webView.bounceEnabled = false;
// after constructing
this.refresh();
});
module.exports = AMCharts;