2
function WebApp(appInfo, callbacks) {
3
this._init(appInfo, callbacks);
6
WebApp.match = { FIRST: 0, ANY: 1, ALL: 2 };
8
_init: function (appInfo, callbacks) {
9
this._appInfo = appInfo;
10
this._totalWeight = 0.0;
11
this._validItems = [];
12
this._callbacks = callbacks;
16
_unityLoaded: function () {
18
var i, nodeFunction = function () {
19
return document.evaluate("(" + this.node + ")[" + this.index + "]",
22
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
23
null).snapshotItem(0);
26
for (i = 0; i < this._validItems.length; i++) {
27
var item = this._validItems[i];
28
if (item.install !== undefined) {
29
item.install(nodeFunction.bind(item));
33
console.log("Exception attempting item install = " + e);
36
var indicatorsController = new Indicators(function () {
37
return this._unityCallback();
40
if (this._callbacks.loaded !== undefined) {
41
this._reportInfo("calling loaded callback");
42
this._callbacks.loaded(indicatorsController);
46
setupPage: function () {
47
function nodeValue(node) {
48
if (typeof node === 'string') {
49
var resultSet = document.evaluate(node,
52
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
54
var i, arrayized = [];
55
for (i = 0; i < resultSet.snapshotLength; i++) {
56
arrayized.push(resultSet.snapshotItem(i));
59
} else if (typeof node === 'function') {
67
if (this._totalWeight > 0.0) {
68
if (this._appInfo.validator(this._totalWeight)) {
71
this._totalWeight = 0.0;
76
var i = 0, j = 0, k = 0;
79
// Always collect login if available
80
if (this._appInfo.login !== undefined) {
81
var loginTests = this._appInfo.login;
82
for (i = 0; i < loginTests.nodes.length; i++) {
84
name: loginTests.name,
85
query: loginTests.nodes[i],
86
validator: loginTests.validator,
87
fragment: loginTests.fragment,
88
value: loginTests.value
91
var loginNode = validatedNode(nodeTest);
92
if (loginNode !== null) {
93
this._login = validatedNodeValue(nodeTest, loginNode);
96
if (this._login === undefined) {
97
if (this._shouldRetry()) {
98
this._reportWarning("Failed to find login will retry");
101
this._reportError("Unable to obtain login information");
105
this._reportInfo("Found login: " + this._login);
110
var itemsTests = this._appInfo.items;
111
var valueFunction = function () {
112
var result = document.evaluate("(" + this.node + ")[" + this.index + "]",
115
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
117
return this.nativeValue(result);
120
for (i = 0; i < itemsTests.length; i++) {
122
var item = itemsTests[i];
124
for (j = 0; j < item.nodes.length; j++) {
125
var nodeSet = nodeValue(item.nodes[j]);
127
for (k = 0; k < nodeSet.length; k++) {
128
var node = nodeSet[k];
130
if (item.validator(node)) {
132
this._totalWeight += item.weight;
141
if (item.value !== undefined) {
142
itemData.nativeValue = item.value;
143
itemData.value = valueFunction;
146
if (item.install !== undefined) {
147
itemData.install = item.install;
151
this._validItems.push(itemData);
152
this._reportInfo("Found item - " + item.name);
155
if (item.match === WebApp.match.FIRST || item.match === WebApp.match.ANY) {
166
if (item.fragment !== undefined) {
167
var fragmentOk, resultSet, testNode = document.createElement("div");
168
testNode.innerHTML = item.fragment;
169
resultSet = document.evaluate(item.nodes[0], testNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
170
fragmentOk = resultSet.snapshotLength > 0 && item.validator(resultSet.snapshotItem(0));
171
this._reportWarning("Item not found " + item.name + " fragment check " + (fragmentOk ? "OK" : "failed"));
173
this._reportWarning("Item not found: " + item.name);
178
// Should we continue?
179
if (!this._appInfo.validator(this._totalWeight)) {
180
if (this._shouldRetry()) {
181
this._reportWarning("Did not retrieve enough information to instantiate app, will retry");
184
this._reportFailure(this._totalWeght);
189
// Build Pagedata for Unity.init()
190
pageData.name = this._valueFromField(this._appInfo.name);
191
pageData.iconUrl = this._valueFromField(this._appInfo.iconUrl);
192
pageData.homepage = this._valueFromField(this._appInfo.homepage);
193
pageData.domain = this._valueFromField(this._appInfo.domain);
194
pageData.login = this._login;
195
pageData.onInit = function () { this._unityLoaded(); }.bind(this);
197
if (this._callbacks.success !== undefined) {
198
this._reportInfo("calling success callback");
199
this._callbacks.success(pageData);
205
_unityCallback: function () {
206
// Collect all values
210
for (i = 0; i < this._validItems.length; i++) {
211
var item = this._validItems[i];
213
if (item.value !== undefined) {
214
indicators.push(item.value());
221
_valueFromField: function (value) {
222
// Field can either be a value or a function to compute the value
223
if (typeof value === 'function') {
229
_reportInfo: function (msg) {
230
if (this._callbacks.report) {
231
this._callbacks.report("REPORT: INFO: " + msg);
235
_reportWarning: function (msg) {
236
if (this._callbacks.report) {
237
this._callbacks.report("REPORT: WARNING: " + msg);
241
_reportError: function (msg) {
242
if (this._callbacks.report) {
243
this._callbacks.report("REPORT: ERROR: " + msg);
247
_reportFailure: function (weight) {
248
if (this._callbacks.report) {
249
this._callbacks.report("REPORT: ERROR: Failed to pass sufficient tests to continue " + weight);
253
_shouldRetry: function () {
254
if (this._appInfo.maxRetries !== undefined) {
255
return this._retries++ < this._appInfo.maxRetries;