2018-01-26 10:19:43 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import { parseStandardURIString } from '../base/util';
|
|
|
|
|
2018-04-28 01:43:11 +00:00
|
|
|
declare var interfaceConfig: Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for web. Indicates if the setting section is enabled.
|
|
|
|
*
|
|
|
|
* @param {string} settingName - The name of the setting section as defined in
|
|
|
|
* interface_config.js and SettingsMenu.js.
|
|
|
|
* @returns {boolean} True to indicate that the given setting section
|
|
|
|
* is enabled, false otherwise.
|
|
|
|
*/
|
|
|
|
export function isSettingEnabled(settingName: string) {
|
|
|
|
return interfaceConfig.SETTINGS_SECTIONS.includes(settingName);
|
|
|
|
}
|
|
|
|
|
2018-01-26 10:19:43 +00:00
|
|
|
/**
|
|
|
|
* Normalizes a URL entered by the user.
|
|
|
|
* FIXME: Consider adding this to base/util/uri.
|
|
|
|
*
|
|
|
|
* @param {string} url - The URL to validate.
|
|
|
|
* @returns {string|null} - The normalized URL, or null if the URL is invalid.
|
|
|
|
*/
|
|
|
|
export function normalizeUserInputURL(url: string) {
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
url = url.replace(/\s/g, '').toLowerCase();
|
2018-02-26 16:14:46 +00:00
|
|
|
|
2018-01-26 10:19:43 +00:00
|
|
|
const urlRegExp = new RegExp('^(\\w+://)?(.+)$');
|
|
|
|
const urlComponents = urlRegExp.exec(url);
|
|
|
|
|
|
|
|
if (!urlComponents[1] || !urlComponents[1].startsWith('http')) {
|
|
|
|
url = `https://${urlComponents[2]}`;
|
|
|
|
}
|
|
|
|
|
2018-02-26 16:14:46 +00:00
|
|
|
const parsedURI = parseStandardURIString(url);
|
2018-01-26 10:19:43 +00:00
|
|
|
|
|
|
|
if (!parsedURI.host) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedURI.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
|
|
|
/* eslint-enable no-param-reassign */
|
|
|
|
}
|
2018-04-28 01:43:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for web. Returns whether or not only Device Selection is configured to
|
|
|
|
* display as a setting.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function shouldShowOnlyDeviceSelection() {
|
|
|
|
return interfaceConfig.SETTINGS_SECTIONS.length === 1
|
|
|
|
&& isSettingEnabled('devices');
|
|
|
|
}
|