Skip to content

Instantly share code, notes, and snippets.

@daserge
Created December 1, 2016 06:53
Show Gist options
  • Select an option

  • Save daserge/66c7ec88be72e32df005c63108eddcbb to your computer and use it in GitHub Desktop.

Select an option

Save daserge/66c7ec88be72e32df005c63108eddcbb to your computer and use it in GitHub Desktop.
// file: F:/coho/cordova-windows/cordova-js-src/confighelper.js
define("cordova/confighelper", function(require, exports, module) {
// config.xml wrapper (non-node ConfigParser analogue)
var config, doc;
var utils = require("cordova/utils");
var isPhone = (cordova.platformId == 'windows') && WinJS.Utilities.isPhone;
var isWin10UWP = navigator.appVersion.indexOf('MSAppHost/3.0') !== -1;
var manifestFilePath = isWin10UWP ? "/package.windows10.appxmanifest" : (isPhone ? "/package.phone.appxmanifest" : "/package.windows.appxmanifest");
var isHosted = window.location.protocol.indexOf('http') === 0;
var isMsAppxWeb = window.location.protocol.indexOf('ms-appx-web') === 0;
var schema = (isHosted || isWin10UWP && isMsAppxWeb) ? 'ms-appx-web' : 'ms-appx';
manifestFilePath = 'ms-appx:///www' + manifestFilePath;//schema + '://' + manifestFilePath;
function XmlFile(text) {
this.text = text;
}
XmlFile.prototype.loadTags = function (tagName) {
var parser;
if (!this.doc) {
parser = new DOMParser();
this.doc = parser.parseFromString(this.text, "application/xml");
}
var tags = this.doc.getElementsByTagName(tagName);
return Array.prototype.slice.call(tags);
}
function Config(text) {
XmlFile.apply(this, arguments);
this.preferences = this.loadTags("preference");
}
function Manifest(text) {
XmlFile.apply(this, arguments);
this.splashScreen = this.loadTags("SplashScreen")[0];
}
utils.extend(Config, XmlFile);
utils.extend(Manifest, XmlFile);
function requestFile(filePath, 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:// */) {
success(xhr);
}
else {
fail('[Windows][cordova.js][xhrStatusChangeHandler] Could not XHR ' + filePath + ': ' + xhr.statusText);
}
}
};
xhr = new XMLHttpRequest();
xhr.addEventListener("load", xhrStatusChangeHandler);
try {
xhr.open("get", filePath, true);
xhr.send();
} catch (e) {
fail('[Windows][cordova.js][xhrFile] Could not XHR ' + filePath + ': ' + JSON.stringify(e));
}
}
function readConfig(success, error) {
requestFile("/config.xml", function (xhr) {
success(new Config(xhr.responseText));
}, error);
}
function readManifest(success, error) {
function getFileContentAsync(filePath) {
var uri = new Windows.Foundation.Uri(filePath);
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (file) {
return Windows.Storage.FileIO.readTextAsync(file);
});
}
getFileContentAsync(manifestFilePath).then(function (text) {
success(new Manifest(text));
}, error);
}
/**
* 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 (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;
}
}
/**
* Reads SplashScreen image path
*/
Manifest.prototype.getSplashScreenImagePath = function () {
return this.splashScreen.attributes['Image'].value;
}
exports.readConfig = readConfig;
exports.readManifest = readManifest;
});
// usage:
var manifest;
e.setPromise(makePromise(configHelper.readManifest).then(function (manifestTmp) {
manifest = manifestTmp;
return makePromise(configHelper.readConfig);
})
.then(function (config) {
splashscreen.firstShow(config, manifest, e);
}).then(function () {
...
//
// Update splashscreen image path to match application manifest
splashImageSrc = schema + ':///' + manifest.getSplashScreenImagePath().replace(/\\/g, '/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment