UNPKG

2.86 kBJavaScriptView Raw
1/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22// config.xml wrapper (non-node ConfigParser analogue)
23var config;
24function 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
37function 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 /* file:// */) {
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 * Reads a preference value from config.xml.
77 * Returns preference value or undefined if it does not exist.
78 * @param {String} preferenceName Preference name to read */
79Config.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
89exports.readConfig = readConfig;