// (C) Copyright 2015 Moodle Pty Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import { NavController } from '@ionic/angular'; import { CoreFileHelper } from '@services/file-helper'; import { CoreSites } from '@services/sites'; import { CoreUrlUtils } from '@services/utils/url'; import { CoreUtils } from '@services/utils/utils'; /** * Options for the open function. */ export type CoreWindowOpenOptions = { /** * NavController to use when opening the link in the app. * * @deprecated since 3.9.5 */ navCtrl?: NavController; }; /** * Singleton with helper functions for windows. */ export class CoreWindow { // Avoid creating singleton instances. private constructor() { // Nothing to do. } /** * "Safe" implementation of window.open. It will open the URL without overriding the app. * * @param url URL to open. * @param name Name of the browsing context into which to load the URL. * @param options Other options. * @return Promise resolved when done. */ static async open(url: string, name?: string, options?: CoreWindowOpenOptions): Promise { if (CoreUrlUtils.instance.isLocalFileUrl(url)) { const filename = url.substr(url.lastIndexOf('/') + 1); if (!CoreFileHelper.instance.isOpenableInApp({ filename })) { try { await CoreFileHelper.instance.showConfirmOpenUnsupportedFile(); } catch (error) { return; // Cancelled, stop. } } await CoreUtils.instance.openFile(url); } else { let treated = false; // eslint-disable-next-line @typescript-eslint/no-unused-vars options = options || {}; if (name != '_system') { // Check if it can be opened in the app. treated = false; // @todo await CoreContentLinksHelper.instance.handleLink(url, undefined, options.navCtrl, true, true); } if (!treated) { // Not opened in the app, open with browser. Check if we need to auto-login if (!CoreSites.instance.isLoggedIn()) { // Not logged in, cannot auto-login. CoreUtils.instance.openInBrowser(url); } else { await CoreSites.instance.getCurrentSite()!.openInBrowserWithAutoLoginIfSameSite(url); } } } } }