You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2015/03/18 15:22:48 UTC

cordova-browser git commit: CB-8223 Expose config.xml in the Browser platform

Repository: cordova-browser
Updated Branches:
  refs/heads/master ae9d599e9 -> 90b24d3f0


CB-8223 Expose config.xml in the Browser platform

Implemented config.xml wrapper via XHR


Project: http://git-wip-us.apache.org/repos/asf/cordova-browser/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-browser/commit/90b24d3f
Tree: http://git-wip-us.apache.org/repos/asf/cordova-browser/tree/90b24d3f
Diff: http://git-wip-us.apache.org/repos/asf/cordova-browser/diff/90b24d3f

Branch: refs/heads/master
Commit: 90b24d3f01c7f4d827151327e5d69701f36905f8
Parents: ae9d599
Author: daserge <da...@yandex.ru>
Authored: Thu Jan 15 14:31:31 2015 +0300
Committer: Vladimir Kotikov <v-...@microsoft.com>
Committed: Wed Mar 18 17:00:25 2015 +0300

----------------------------------------------------------------------
 cordova-lib/cordova.js | 79 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-browser/blob/90b24d3f/cordova-lib/cordova.js
----------------------------------------------------------------------
diff --git a/cordova-lib/cordova.js b/cordova-lib/cordova.js
index 3bdcea5..7fa7943 100644
--- a/cordova-lib/cordova.js
+++ b/cordova-lib/cordova.js
@@ -1373,6 +1373,85 @@ exports.makeAbsolute = function makeAbsolute(url) {
 
 });
 
+// config.xml wrapper (non-node ConfigParser analogue)
+define("cordova/confighelper", function(require, exports, module) {
+
+var config;
+function Config(xhr) {
+    function loadPreferences(xhr) {
+       var parser = new DOMParser();
+       var doc = parser.parseFromString(xhr.responseText, "application/xml");
+
+       var preferences = doc.getElementsByTagName("preference");
+       return Array.prototype.slice.call(preferences);
+    }
+
+    this.xhr = xhr;
+    this.preferences = loadPreferences(this.xhr);
+}
+
+function readConfig(success, error) {
+    var xhr;
+
+    if(typeof config != 'undefined') {
+        success(config);
+    }
+
+    function fail(msg) {
+        console.error(msg);
+
+        if(error) {
+            error(msg);
+        }
+    }
+
+    var xhrStatusChangeHandler = function() {
+        if (xhr.readyState == 4) {
+            if (xhr.status == 200 || xhr.status == 304 || xhr.status == 0 /* file:// */) {
+                config = new Config(xhr);
+                success(config);
+            }
+            else {
+                fail('[Browser][cordova.js][xhrStatusChangeHandler] Could not XHR config.xml: ' + xhr.statusText);
+            }
+        }
+    };
+
+    if ("ActiveXObject" in window) {
+        // Needed for XHR-ing via file:// protocol in IE
+        xhr = new ActiveXObject("MSXML2.XMLHTTP");
+        xhr.onreadystatechange = xhrStatusChangeHandler;
+    } else {
+        xhr = new XMLHttpRequest();
+        xhr.addEventListener("load", xhrStatusChangeHandler);
+    }
+
+    try {
+        xhr.open("get", "config.xml", true);
+        xhr.send();
+    } catch(e) {
+        fail('[Browser][cordova.js][readConfig] Could not XHR config.xml: ' + JSON.stringify(e));
+    }
+}
+
+/**
+ * Reads a preference value from config.xml.
+ * Returns preference value or undefined if it does not exist.
+ * @param {String} preferenceName Preference name to read */
+Config.prototype.getPreferenceValue = function getPreferenceValue(preferenceName) {
+    var preferenceItem = this.preferences && this.preferences.filter(function(item) { 
+        return item.attributes['name'].value === preferenceName; 
+    });
+
+    if(preferenceItem && preferenceItem[0] && preferenceItem[0].attributes && preferenceItem[0].attributes['value']) {
+        return preferenceItem[0].attributes['value'].value;
+    }
+}
+
+exports.readConfig = readConfig;
+
+});
+
 // file: src/common/utils.js
 define("cordova/utils", function(require, exports, module) {
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org