@enact/webos
Version:
webOS support library
135 lines (127 loc) • 3.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.platformBack = exports.fetchAppRootPath = exports.fetchAppInfo = exports.fetchAppId = void 0;
/* eslint-disable no-console */
/**
* Provides a collection of functions for fetching application metadata.
*
* @module webos/application
* @exports fetchAppId,
* @exports fetchAppInfo,
* @exports fetchAppRootPath,
* @exports platformBack
*/
var appInfo = {};
/**
* Fetches the appID of the caller app.
*
* @function
* @returns {String} AppID of the app
* @memberof webos/application
* @public
*/
var fetchAppId = exports.fetchAppId = function fetchAppId() {
var _window$webOSSystem;
var webOSSystem = (_window$webOSSystem = window.webOSSystem) !== null && _window$webOSSystem !== void 0 ? _window$webOSSystem : window.PalmSystem;
if (webOSSystem && webOSSystem.identifier) {
// webOSSystem.identifier: <appid> <processid>
return webOSSystem.identifier.split(' ')[0];
}
};
/**
* The callback signature for `fetchAppInfo`
*
* @callback appInfoCallback
* @param {?object} info - JSON data object read from the app's *appinfo.json* file. `undefined` if not found.
* @memberof webos/application
*/
/**
* Fetches the *appinfo.json* data of the caller app.
*
* @function
* @param {webos/application~appInfoCallback} callback - Called upon completion
* @param {String} [path] - Optional relative filepath to a specific *appinfo.json* to read
* @returns {undefined}
* @memberof webos/application
* @public
*/
var fetchAppInfo = exports.fetchAppInfo = function fetchAppInfo(callback, path) {
if (Object.keys(appInfo).length === 0) {
var parseInfo = function parseInfo(err, info) {
if (!err && info) {
try {
appInfo = JSON.parse(info);
if (callback) callback(appInfo);
} catch (e) {
console.error('Unable to parse appinfo.json file for ' + fetchAppId);
if (callback) callback();
}
} else if (callback) {
callback();
}
};
var req = new window.XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status >= 200 && req.status < 300 || req.status === 0) {
parseInfo(null, req.responseText);
} else {
parseInfo({
status: 404
});
}
}
};
try {
req.open('GET', path || 'appinfo.json', true);
req.send(null);
} catch (e) {
parseInfo({
status: 404
});
}
} else if (callback) {
callback(appInfo);
}
};
/**
* Fetches the full root URI (path) of the caller app.
*
* @function
* @returns {String} App's URI (application install path)
* @memberof webos/application
* @public
*/
var fetchAppRootPath = exports.fetchAppRootPath = function fetchAppRootPath() {
var base = window.location.href;
if ('baseURI' in window.document) {
base = window.document.baseURI;
} else {
var baseTags = window.document.getElementsByTagName('base');
if (baseTags.length > 0) {
base = baseTags[0].href;
}
}
var match = base.match(new RegExp('.*://[^#]*/'));
if (match) {
return match[0];
}
return '';
};
/**
* Emulate the remote *back* key.
*
* @function
* @returns {undefined}
* @memberof webos/application
* @public
*/
var platformBack = exports.platformBack = function platformBack() {
var _window$webOSSystem2;
var webOSSystem = (_window$webOSSystem2 = window.webOSSystem) !== null && _window$webOSSystem2 !== void 0 ? _window$webOSSystem2 : window.PalmSystem;
if (webOSSystem && webOSSystem.platformBack) {
return webOSSystem.platformBack();
}
};