docbrown/app/assets/javascripts/initializers/runtime.js
Michael Kohl 5406b0576e
Split Settings::Authentication from SiteConfig (#13095)
* Split Settings::Authentication from SiteConfig

* Move specs

* Sort fields

* Update settings usages

* Update recaptcha usages

* Add data update script

* Update spec

* Rename SiteConfigParams concern

* Fixes, new route, new controller

* Controller and service refactoring

* More controller and service updates

* Spec updates

* More spec fixes

* Move file

* Fix FeedbackMessagesController

* Update admin/configs_spec

* Fix remaining specs in admin/configs_spec

* Fix configs API

* Formatting

* Clean up old service object

* Various fixes

* Update DUS

* Add model argument to admin_config_label

* Fix key name

* Fix specs

* Add distinct request caches for settings classes

* Fix e2e tests

* Fix remaining system spec

* Make DUS idempotent

* Move routes block

* Cleanup

* Switch to ActiveSupport::CurrentAttributes

* Pinned rails-settings-cached

* Update e2e test

* Update lib/data_update_scripts/20210316091354_move_authentication_settings.rb

Co-authored-by: rhymes <rhymes@hey.com>

* Add guard to DUS

* Temporarily re-add two SiteConfig fields

* Fix config show view

Co-authored-by: rhymes <rhymes@hey.com>
2021-04-12 09:41:09 +02:00

136 lines
4.2 KiB
JavaScript

/**
* This class helps managing native feature support. Can easily be referenced
* from anywhere in JavaScript with:
*
* if (Runtime.isNativeiOS('video')) { ... }
*
* if (Runtime.isNativeAndroid('podcastMessage')) { ... }
*/
class Runtime {
/**
* This function returns a string combining the current Medium and OS
* that represents the current Context where the app is running.
*
* @returns {String} "Medium-OS", for example "Browser-Android"
*/
static currentContext() {
return `${Runtime.currentMedium()}-${Runtime.currentOS()}`;
}
/**
* This function returns a string that represents the current Medium where
* the app is currently running. The currently supported mediums are Browser,
* ForemWebView and PWA.
*
* @returns {String} One of the supported Mediums or 'Unsupported'
*/
static currentMedium() {
const pwaButtons = document.getElementById('pwa-nav-buttons');
if (/ForemWebView/i.test(navigator.userAgent)) {
return 'ForemWebView';
} else if (pwaButtons.classList.contains('pwa-nav-buttons--showing')) {
return 'PWA';
} else {
return 'Browser';
}
}
/**
* This function returns a string that represents the current OS where the app
* is currently running. The currently supported Operating Systems are
* Windows, Linux, macOS, Android and iOS.
*
* @returns {String} One of the supported Operating Systems or 'Unsupported'
*/
static currentOS() {
const macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'];
const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
const iosPlatforms = ['iPhone', 'iPad', 'iPod'];
if (macosPlatforms.includes(window.navigator.platform)) {
return 'macOS';
} else if (iosPlatforms.includes(window.navigator.platform)) {
return 'iOS';
} else if (windowsPlatforms.includes(window.navigator.platform)) {
return 'Windows';
} else if (/Android/i.test(window.navigator.userAgent)) {
return 'Android';
} else if (/Linux/i.test(window.navigator.platform)) {
return 'Linux';
}
return 'Unsupported';
}
/**
* Checks the device for iOS (webkit) native feature support
*
* @function isNativeIOS
* @param {string} namespace Specifies support for a specific feature
* (i.e. video, podcast, etc)
* @returns {boolean} true if current environment support native features
*/
static isNativeIOS(namespace = null) {
const nativeCheck =
/DEV-Native-ios|ForemWebView/i.test(navigator.userAgent) &&
window &&
window.webkit &&
window.webkit.messageHandlers;
let namespaceCheck = true;
if (nativeCheck && namespace) {
namespaceCheck = window.webkit.messageHandlers[namespace] != undefined;
}
return nativeCheck && namespaceCheck;
}
/**
* Checks the device for Android native feature support
*
* @function isNativeAndroid
* @param {string} namespace Specifies support for a specific feature
* (i.e. videoMessage, podcastMessage, etc)
* @returns {boolean} true if current environment support native features
*/
static isNativeAndroid(namespace = null) {
const nativeCheck =
/DEV-Native-android|ForemWebView/i.test(navigator.userAgent) &&
typeof AndroidBridge !== 'undefined';
let namespaceCheck = true;
if (nativeCheck && namespace) {
namespaceCheck = AndroidBridge[namespace] != undefined;
}
return nativeCheck && namespaceCheck;
}
/**
* This function copies text to clipboard taking in consideration all
* supported platforms.
*
* @param {string} text to be copied to the clipboard
*
* @returns {Promise} Resolves when succesful in copying to clipboard
*/
static copyToClipboard(text) {
return new Promise((resolve, reject) => {
if (Runtime.isNativeAndroid('copyToClipboard')) {
AndroidBridge.copyToClipboard(text);
resolve();
} else if (navigator.clipboard != null) {
navigator.clipboard
.writeText(text)
.then(() => {
resolve();
})
.catch((e) => {
reject(e);
});
} else {
reject('Unsupported device unable to copy to clipboard');
}
});
}
}