1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var config;
|
24 | function Config(xhr) {
|
25 | function loadPreferences(xhr) {
|
26 | var parser = new DOMParser();
|
27 | var doc = parser.parseFromString(xhr.responseText, "application/xml");
|
28 |
|
29 | var preferences = doc.getElementsByTagName("preference");
|
30 | return Array.prototype.slice.call(preferences);
|
31 | }
|
32 |
|
33 | this.xhr = xhr;
|
34 | this.preferences = loadPreferences(this.xhr);
|
35 | }
|
36 |
|
37 | function readConfig(success, error) {
|
38 | var xhr;
|
39 |
|
40 | if (typeof config != 'undefined') {
|
41 | success(config);
|
42 | }
|
43 |
|
44 | function fail(msg) {
|
45 | console.error(msg);
|
46 |
|
47 | if (error) {
|
48 | error(msg);
|
49 | }
|
50 | }
|
51 |
|
52 | var xhrStatusChangeHandler = function () {
|
53 | if (xhr.readyState == 4) {
|
54 | if (xhr.status == 200 || xhr.status == 304 || xhr.status == 0 ) {
|
55 | config = new Config(xhr);
|
56 | success(config);
|
57 | }
|
58 | else {
|
59 | fail('[Windows][cordova.js][xhrStatusChangeHandler] Could not XHR config.xml: ' + xhr.statusText);
|
60 | }
|
61 | }
|
62 | };
|
63 |
|
64 | xhr = new XMLHttpRequest();
|
65 | xhr.addEventListener("load", xhrStatusChangeHandler);
|
66 |
|
67 | try {
|
68 | xhr.open("get", "/config.xml", true);
|
69 | xhr.send();
|
70 | } catch (e) {
|
71 | fail('[Windows][cordova.js][readConfig] Could not XHR config.xml: ' + JSON.stringify(e));
|
72 | }
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | Config.prototype.getPreferenceValue = function getPreferenceValue(preferenceName) {
|
80 | var preferenceItem = this.preferences && this.preferences.filter(function (item) {
|
81 | return item.attributes['name'].value === preferenceName;
|
82 | });
|
83 |
|
84 | if (preferenceItem && preferenceItem[0] && preferenceItem[0].attributes && preferenceItem[0].attributes['value']) {
|
85 | return preferenceItem[0].attributes['value'].value;
|
86 | }
|
87 | }
|
88 |
|
89 | exports.readConfig = readConfig;
|