2017-11-06 17:17:11 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-05-22 13:31:52 +02:00
|
|
|
import { Injectable, SimpleChange } from '@angular/core';
|
2018-01-29 10:05:20 +01:00
|
|
|
import {
|
|
|
|
LoadingController, Loading, ToastController, Toast, AlertController, Alert, Platform, Content,
|
|
|
|
ModalController
|
|
|
|
} from 'ionic-angular';
|
2017-11-06 17:17:11 +01:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
import { CoreTextUtilsProvider } from './text';
|
|
|
|
import { CoreAppProvider } from '../app';
|
|
|
|
import { CoreConfigProvider } from '../config';
|
|
|
|
import { CoreUrlUtilsProvider } from './url';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { CoreConstants } from '@core/constants';
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* "Utils" service with helper functions for UI, DOM elements and HTML code.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class CoreDomUtilsProvider {
|
2018-01-12 14:28:46 +01:00
|
|
|
// List of input types that support keyboard.
|
|
|
|
protected INPUT_SUPPORT_KEYBOARD = ['date', 'datetime', 'datetime-local', 'email', 'month', 'number', 'password',
|
2018-01-29 10:05:20 +01:00
|
|
|
'search', 'tel', 'text', 'time', 'url', 'week'];
|
2018-02-05 12:31:48 +01:00
|
|
|
protected INSTANCE_ID_ATTR_NAME = 'core-instance-id';
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-01-12 14:28:46 +01:00
|
|
|
protected element = document.createElement('div'); // Fake element to use in some functions, to prevent creating it each time.
|
|
|
|
protected matchesFn: string; // Name of the "matches" function to use when simulating a closest call.
|
2018-02-05 12:31:48 +01:00
|
|
|
protected instances: {[id: string]: any} = {}; // Store component/directive instances by id.
|
|
|
|
protected lastInstanceId = 0;
|
2018-01-12 14:28:46 +01:00
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
constructor(private translate: TranslateService, private loadingCtrl: LoadingController, private toastCtrl: ToastController,
|
2018-01-29 10:05:20 +01:00
|
|
|
private alertCtrl: AlertController, private textUtils: CoreTextUtilsProvider, private appProvider: CoreAppProvider,
|
|
|
|
private platform: Platform, private configProvider: CoreConfigProvider, private urlUtils: CoreUrlUtilsProvider,
|
|
|
|
private modalCtrl: ModalController) { }
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Equivalent to element.closest(). If the browser doesn't support element.closest, it will
|
|
|
|
* traverse the parents to achieve the same functionality.
|
|
|
|
* Returns the closest ancestor of the current element (or the current element itself) which matches the selector.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element DOM Element.
|
|
|
|
* @param {string} selector Selector to search.
|
|
|
|
* @return {Element} Closest ancestor.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
closest(element: HTMLElement, selector: string): Element {
|
2017-11-06 17:17:11 +01:00
|
|
|
// Try to use closest if the browser supports it.
|
|
|
|
if (typeof element.closest == 'function') {
|
|
|
|
return element.closest(selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.matchesFn) {
|
|
|
|
// Find the matches function supported by the browser.
|
2018-01-29 10:05:20 +01:00
|
|
|
['matches', 'webkitMatchesSelector', 'mozMatchesSelector', 'msMatchesSelector', 'oMatchesSelector'].some((fn) => {
|
2017-11-06 17:17:11 +01:00
|
|
|
if (typeof document.body[fn] == 'function') {
|
|
|
|
this.matchesFn = fn;
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!this.matchesFn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse parents.
|
|
|
|
while (element) {
|
|
|
|
if (element[this.matchesFn](selector)) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
element = element.parentElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the download size is higher than a certain threshold shows a confirm dialog.
|
|
|
|
*
|
|
|
|
* @param {any} size Object containing size to download and a boolean to indicate if its totally or partialy calculated.
|
2017-12-08 15:53:27 +01:00
|
|
|
* @param {string} [message] Code of the message to show. Default: 'core.course.confirmdownload'.
|
2017-11-06 17:17:11 +01:00
|
|
|
* @param {string} [unknownMessage] ID of the message to show if size is unknown.
|
|
|
|
* @param {number} [wifiThreshold] Threshold to show confirm in WiFi connection. Default: CoreWifiDownloadThreshold.
|
|
|
|
* @param {number} [limitedThreshold] Threshold to show confirm in limited connection. Default: CoreDownloadThreshold.
|
2018-01-17 12:12:23 +01:00
|
|
|
* @param {boolean} [alwaysConfirm] True to show a confirm even if the size isn't high, false otherwise.
|
2017-11-06 17:17:11 +01:00
|
|
|
* @return {Promise<void>} Promise resolved when the user confirms or if no confirm needed.
|
|
|
|
*/
|
2018-01-17 12:12:23 +01:00
|
|
|
confirmDownloadSize(size: any, message?: string, unknownMessage?: string, wifiThreshold?: number, limitedThreshold?: number,
|
2018-01-29 10:05:20 +01:00
|
|
|
alwaysConfirm?: boolean): Promise<void> {
|
2018-01-12 14:28:46 +01:00
|
|
|
wifiThreshold = typeof wifiThreshold == 'undefined' ? CoreConstants.WIFI_DOWNLOAD_THRESHOLD : wifiThreshold;
|
|
|
|
limitedThreshold = typeof limitedThreshold == 'undefined' ? CoreConstants.DOWNLOAD_THRESHOLD : limitedThreshold;
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
if (size.size < 0 || (size.size == 0 && !size.total)) {
|
|
|
|
// Seems size was unable to be calculated. Show a warning.
|
2018-01-29 10:05:20 +01:00
|
|
|
unknownMessage = unknownMessage || 'core.course.confirmdownloadunknownsize';
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
return this.showConfirm(this.translate.instant(unknownMessage));
|
|
|
|
} else if (!size.total) {
|
|
|
|
// Filesize is only partial.
|
2018-01-29 10:05:20 +01:00
|
|
|
const readableSize = this.textUtils.bytesToSize(size.size, 2);
|
|
|
|
|
|
|
|
return this.showConfirm(this.translate.instant('core.course.confirmpartialdownloadsize', { size: readableSize }));
|
2017-11-06 17:17:11 +01:00
|
|
|
} else if (size.size >= wifiThreshold || (this.appProvider.isNetworkAccessLimited() && size.size >= limitedThreshold)) {
|
2018-01-29 10:05:20 +01:00
|
|
|
message = message || 'core.course.confirmdownload';
|
|
|
|
const readableSize = this.textUtils.bytesToSize(size.size, 2);
|
|
|
|
|
|
|
|
return this.showConfirm(this.translate.instant(message, { size: readableSize }));
|
2018-01-17 12:12:23 +01:00
|
|
|
} else if (alwaysConfirm) {
|
|
|
|
return this.showConfirm(this.translate.instant('core.areyousure'));
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2018-04-06 08:58:23 +02:00
|
|
|
/**
|
|
|
|
* Create a "cancelled" error. These errors won't display an error message in showErrorModal functions.
|
|
|
|
*
|
|
|
|
* @return {any} The error object.
|
|
|
|
*/
|
|
|
|
createCanceledError(): any {
|
|
|
|
return {coreCanceled: true};
|
|
|
|
}
|
|
|
|
|
2018-05-22 13:31:52 +02:00
|
|
|
/**
|
|
|
|
* Given a list of changes for a component input detected by a KeyValueDiffers, create an object similar to the one
|
|
|
|
* passed to the ngOnChanges functions.
|
|
|
|
*
|
|
|
|
* @param {any} changes Changes detected by KeyValueDiffer.
|
|
|
|
* @return {{[name: string]: SimpleChange}} Changes in a format like ngOnChanges.
|
|
|
|
*/
|
|
|
|
createChangesFromKeyValueDiff(changes: any): { [name: string]: SimpleChange } {
|
|
|
|
const newChanges: { [name: string]: SimpleChange } = {};
|
|
|
|
|
|
|
|
// Added items are considered first change.
|
|
|
|
changes.forEachAddedItem((item) => {
|
|
|
|
newChanges[item.key] = new SimpleChange(item.previousValue, item.currentValue, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Changed or removed items aren't first change.
|
|
|
|
changes.forEachChangedItem((item) => {
|
|
|
|
newChanges[item.key] = new SimpleChange(item.previousValue, item.currentValue, false);
|
|
|
|
});
|
|
|
|
changes.forEachRemovedItem((item) => {
|
|
|
|
newChanges[item.key] = new SimpleChange(item.previousValue, item.currentValue, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
return newChanges;
|
|
|
|
}
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
/**
|
|
|
|
* Extract the downloadable URLs from an HTML code.
|
|
|
|
*
|
|
|
|
* @param {string} html HTML code.
|
|
|
|
* @return {string[]} List of file urls.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
extractDownloadableFilesFromHtml(html: string): string[] {
|
|
|
|
const urls = [];
|
|
|
|
let elements;
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
this.element.innerHTML = html;
|
|
|
|
elements = this.element.querySelectorAll('a, img, audio, video, source, track');
|
|
|
|
|
2018-02-22 11:58:08 +01:00
|
|
|
for (let i = 0; i < elements.length; i++) {
|
2018-01-29 10:05:20 +01:00
|
|
|
const element = elements[i];
|
|
|
|
let url = element.tagName === 'A' ? element.href : element.src;
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
if (url && this.urlUtils.isDownloadableUrl(url) && urls.indexOf(url) == -1) {
|
|
|
|
urls.push(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Treat video poster.
|
|
|
|
if (element.tagName == 'VIDEO' && element.getAttribute('poster')) {
|
|
|
|
url = element.getAttribute('poster');
|
|
|
|
if (url && this.urlUtils.isDownloadableUrl(url) && urls.indexOf(url) == -1) {
|
|
|
|
urls.push(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the downloadable URLs from an HTML code and returns them in fake file objects.
|
|
|
|
*
|
|
|
|
* @param {string} html HTML code.
|
|
|
|
* @return {any[]} List of fake file objects with file URLs.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
extractDownloadableFilesFromHtmlAsFakeFileObjects(html: string): any[] {
|
|
|
|
const urls = this.extractDownloadableFilesFromHtml(html);
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
// Convert them to fake file objects.
|
|
|
|
return urls.map((url) => {
|
|
|
|
return {
|
|
|
|
fileurl: url
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search all the URLs in a CSS file content.
|
|
|
|
*
|
|
|
|
* @param {string} code CSS code.
|
|
|
|
* @return {string[]} List of URLs.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
extractUrlsFromCSS(code: string): string[] {
|
2017-11-06 17:17:11 +01:00
|
|
|
// First of all, search all the url(...) occurrences that don't include "data:".
|
2018-01-29 10:05:20 +01:00
|
|
|
const urls = [],
|
2017-11-06 17:17:11 +01:00
|
|
|
matches = code.match(/url\(\s*["']?(?!data:)([^)]+)\)/igm);
|
|
|
|
|
2018-03-13 10:21:36 +01:00
|
|
|
if (!matches) {
|
|
|
|
return urls;
|
|
|
|
}
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
// Extract the URL form each match.
|
|
|
|
matches.forEach((match) => {
|
2018-01-29 10:05:20 +01:00
|
|
|
const submatches = match.match(/url\(\s*['"]?([^'"]*)['"]?\s*\)/im);
|
2017-11-06 17:17:11 +01:00
|
|
|
if (submatches && submatches[1]) {
|
|
|
|
urls.push(submatches[1]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Focus an element and open keyboard.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} el HTML element to focus.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
focusElement(el: HTMLElement): void {
|
2017-11-06 17:17:11 +01:00
|
|
|
if (el && el.focus) {
|
|
|
|
el.focus();
|
|
|
|
if (this.platform.is('android') && this.supportsInputKeyboard(el)) {
|
|
|
|
// On some Android versions the keyboard doesn't open automatically.
|
|
|
|
this.appProvider.openKeyboard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a size to be used as width/height of an element.
|
|
|
|
* If the size is already valid (like '500px' or '50%') it won't be modified.
|
|
|
|
* Returned size will have a format like '500px'.
|
|
|
|
*
|
|
|
|
* @param {any} size Size to format.
|
|
|
|
* @return {string} Formatted size. If size is not valid, returns an empty string.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
formatPixelsSize(size: any): string {
|
2017-11-06 17:17:11 +01:00
|
|
|
if (typeof size == 'string' && (size.indexOf('px') > -1 || size.indexOf('%') > -1)) {
|
|
|
|
// It seems to be a valid size.
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = parseInt(size, 10);
|
|
|
|
if (!isNaN(size)) {
|
|
|
|
return size + 'px';
|
|
|
|
}
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the contents of a certain selection in a DOM element.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element DOM element to search in.
|
|
|
|
* @param {string} selector Selector to search.
|
|
|
|
* @return {string} Selection contents. Undefined if not found.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
getContentsOfElement(element: HTMLElement, selector: string): string {
|
2017-11-06 17:17:11 +01:00
|
|
|
if (element) {
|
2018-01-29 10:05:20 +01:00
|
|
|
const selected = element.querySelector(selector);
|
2017-11-06 17:17:11 +01:00
|
|
|
if (selected) {
|
|
|
|
return selected.innerHTML;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:35:53 +01:00
|
|
|
/**
|
|
|
|
* Get the data from a form. It will only collect elements that have a name.
|
|
|
|
*
|
|
|
|
* @param {HTMLFormElement} form The form to get the data from.
|
|
|
|
* @return {any} Object with the data. The keys are the names of the inputs.
|
|
|
|
*/
|
|
|
|
getDataFromForm(form: HTMLFormElement): any {
|
|
|
|
if (!form || !form.elements) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = {};
|
|
|
|
|
|
|
|
for (let i = 0; i < form.elements.length; i++) {
|
|
|
|
const element: any = form.elements[i],
|
|
|
|
name = element.name || '';
|
|
|
|
|
|
|
|
// Ignore submit inputs.
|
|
|
|
if (!name || element.type == 'submit' || element.tagName == 'BUTTON') {
|
2018-04-16 13:04:41 +02:00
|
|
|
continue;
|
2018-02-20 15:35:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value.
|
|
|
|
if (element.type == 'checkbox') {
|
|
|
|
data[name] = !!element.checked;
|
|
|
|
} else if (element.type == 'radio') {
|
|
|
|
if (element.checked) {
|
|
|
|
data[name] = element.value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data[name] = element.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
/**
|
|
|
|
* Returns height of an element.
|
|
|
|
*
|
|
|
|
* @param {any} element DOM element to measure.
|
|
|
|
* @param {boolean} [usePadding] Whether to use padding to calculate the measure.
|
|
|
|
* @param {boolean} [useMargin] Whether to use margin to calculate the measure.
|
|
|
|
* @param {boolean} [useBorder] Whether to use borders to calculate the measure.
|
|
|
|
* @param {boolean} [innerMeasure] If inner measure is needed: padding, margin or borders will be substracted.
|
|
|
|
* @return {number} Height in pixels.
|
|
|
|
*/
|
|
|
|
getElementHeight(element: any, usePadding?: boolean, useMargin?: boolean, useBorder?: boolean,
|
2018-01-29 10:05:20 +01:00
|
|
|
innerMeasure?: boolean): number {
|
2017-11-06 17:17:11 +01:00
|
|
|
return this.getElementMeasure(element, false, usePadding, useMargin, useBorder, innerMeasure);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns height or width of an element.
|
|
|
|
*
|
|
|
|
* @param {any} element DOM element to measure.
|
2018-01-23 13:00:00 +01:00
|
|
|
* @param {boolean} [getWidth] Whether to get width or height.
|
2017-11-06 17:17:11 +01:00
|
|
|
* @param {boolean} [usePadding] Whether to use padding to calculate the measure.
|
|
|
|
* @param {boolean} [useMargin] Whether to use margin to calculate the measure.
|
|
|
|
* @param {boolean} [useBorder] Whether to use borders to calculate the measure.
|
|
|
|
* @param {boolean} [innerMeasure] If inner measure is needed: padding, margin or borders will be substracted.
|
|
|
|
* @return {number} Measure in pixels.
|
|
|
|
*/
|
|
|
|
getElementMeasure(element: any, getWidth?: boolean, usePadding?: boolean, useMargin?: boolean, useBorder?: boolean,
|
2018-01-29 10:05:20 +01:00
|
|
|
innerMeasure?: boolean): number {
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
const offsetMeasure = getWidth ? 'offsetWidth' : 'offsetHeight',
|
2017-11-06 17:17:11 +01:00
|
|
|
measureName = getWidth ? 'width' : 'height',
|
|
|
|
clientMeasure = getWidth ? 'clientWidth' : 'clientHeight',
|
|
|
|
priorSide = getWidth ? 'Left' : 'Top',
|
2018-01-29 10:05:20 +01:00
|
|
|
afterSide = getWidth ? 'Right' : 'Bottom';
|
|
|
|
let measure = element[offsetMeasure] || element[measureName] || element[clientMeasure] || 0;
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
// Measure not correctly taken.
|
|
|
|
if (measure <= 0) {
|
2018-01-29 10:05:20 +01:00
|
|
|
const style = getComputedStyle(element);
|
2017-11-06 17:17:11 +01:00
|
|
|
if (style && style.display == '') {
|
|
|
|
element.style.display = 'inline-block';
|
|
|
|
measure = element[offsetMeasure] || element[measureName] || element[clientMeasure] || 0;
|
|
|
|
element.style.display = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usePadding || useMargin || useBorder) {
|
2018-01-29 10:05:20 +01:00
|
|
|
const computedStyle = getComputedStyle(element);
|
|
|
|
let surround = 0;
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
if (usePadding) {
|
2018-05-29 16:56:47 +02:00
|
|
|
surround += this.getComputedStyleMeasure(computedStyle, 'padding' + priorSide) +
|
|
|
|
this.getComputedStyleMeasure(computedStyle, 'padding' + afterSide);
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
if (useMargin) {
|
2018-05-29 16:56:47 +02:00
|
|
|
surround += this.getComputedStyleMeasure(computedStyle, 'margin' + priorSide) +
|
|
|
|
this.getComputedStyleMeasure(computedStyle, 'margin' + afterSide);
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
if (useBorder) {
|
2018-05-29 16:56:47 +02:00
|
|
|
surround += this.getComputedStyleMeasure(computedStyle, 'border' + priorSide + 'Width') +
|
|
|
|
this.getComputedStyleMeasure(computedStyle, 'border' + afterSide + 'Width');
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
if (innerMeasure) {
|
|
|
|
measure = measure > surround ? measure - surround : 0;
|
|
|
|
} else {
|
|
|
|
measure += surround;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return measure;
|
2018-05-29 16:56:47 +02:00
|
|
|
}
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-05-29 16:56:47 +02:00
|
|
|
/**
|
|
|
|
* Returns the computed style measure or 0 if not found or NaN.
|
|
|
|
*
|
|
|
|
* @param {any} style Style from getComputedStyle.
|
|
|
|
* @param {string} measure Measure to get.
|
|
|
|
* @return {number} Result of the measure.
|
|
|
|
*/
|
|
|
|
getComputedStyleMeasure(style: any, measure: string): number {
|
|
|
|
return parseInt(style[measure], 10) || 0;
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns width of an element.
|
|
|
|
*
|
|
|
|
* @param {any} element DOM element to measure.
|
|
|
|
* @param {boolean} [usePadding] Whether to use padding to calculate the measure.
|
|
|
|
* @param {boolean} [useMargin] Whether to use margin to calculate the measure.
|
|
|
|
* @param {boolean} [useBorder] Whether to use borders to calculate the measure.
|
|
|
|
* @param {boolean} [innerMeasure] If inner measure is needed: padding, margin or borders will be substracted.
|
|
|
|
* @return {number} Width in pixels.
|
|
|
|
*/
|
|
|
|
getElementWidth(element: any, usePadding?: boolean, useMargin?: boolean, useBorder?: boolean,
|
2018-01-29 10:05:20 +01:00
|
|
|
innerMeasure?: boolean): number {
|
2017-11-06 17:17:11 +01:00
|
|
|
return this.getElementMeasure(element, true, usePadding, useMargin, useBorder, innerMeasure);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the position of a element relative to another element.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} container Element to search in.
|
|
|
|
* @param {string} [selector] Selector to find the element to gets the position.
|
|
|
|
* @param {string} [positionParentClass] Parent Class where to stop calculating the position. Default scroll-content.
|
|
|
|
* @return {number[]} positionLeft, positionTop of the element relative to.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
getElementXY(container: HTMLElement, selector?: string, positionParentClass?: string): number[] {
|
2017-11-06 17:17:11 +01:00
|
|
|
let element: HTMLElement = <HTMLElement> (selector ? container.querySelector(selector) : container),
|
|
|
|
offsetElement,
|
|
|
|
positionTop = 0,
|
|
|
|
positionLeft = 0;
|
|
|
|
|
|
|
|
if (!positionParentClass) {
|
|
|
|
positionParentClass = 'scroll-content';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!element) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (element) {
|
|
|
|
positionLeft += (element.offsetLeft - element.scrollLeft + element.clientLeft);
|
|
|
|
positionTop += (element.offsetTop - element.scrollTop + element.clientTop);
|
|
|
|
|
|
|
|
offsetElement = element.offsetParent;
|
|
|
|
element = element.parentElement;
|
|
|
|
|
|
|
|
// Every parent class has to be checked but the position has to be got form offsetParent.
|
|
|
|
while (offsetElement != element && element) {
|
|
|
|
// If positionParentClass element is reached, stop adding tops.
|
|
|
|
if (element.className.indexOf(positionParentClass) != -1) {
|
|
|
|
element = null;
|
|
|
|
} else {
|
|
|
|
element = element.parentElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, check again.
|
2017-11-29 16:03:47 +01:00
|
|
|
if (element && element.className.indexOf(positionParentClass) != -1) {
|
2017-11-06 17:17:11 +01:00
|
|
|
element = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [positionLeft, positionTop];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an error message, return a suitable error title.
|
|
|
|
*
|
|
|
|
* @param {string} message The error message.
|
|
|
|
* @return {string} Title.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
private getErrorTitle(message: string): string {
|
2017-12-08 15:53:27 +01:00
|
|
|
if (message == this.translate.instant('core.networkerrormsg') ||
|
2018-01-29 10:05:20 +01:00
|
|
|
message == this.translate.instant('core.fileuploader.errormustbeonlinetoupload')) {
|
2017-12-29 18:05:52 +01:00
|
|
|
return '<span class="core-icon-with-badge"><i class="icon ion-wifi"></i>\
|
|
|
|
<i class="icon ion-alert-circled core-icon-badge"></i></span>';
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2017-12-08 15:53:27 +01:00
|
|
|
return this.textUtils.decodeHTML(this.translate.instant('core.error'));
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
2018-02-05 12:31:48 +01:00
|
|
|
/**
|
|
|
|
* Retrieve component/directive instance.
|
|
|
|
* Please use this function only if you cannot retrieve the instance using parent/child methods: ViewChild (or similar)
|
|
|
|
* or Angular's injection.
|
|
|
|
*
|
|
|
|
* @param {Element} element The root element of the component/directive.
|
|
|
|
* @return {any} The instance, undefined if not found.
|
|
|
|
*/
|
|
|
|
getInstanceByElement(element: Element): any {
|
|
|
|
const id = element.getAttribute(this.INSTANCE_ID_ATTR_NAME);
|
|
|
|
|
|
|
|
return this.instances[id];
|
|
|
|
}
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
/**
|
|
|
|
* Check if an element is outside of screen (viewport).
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} scrollEl The element that must be scrolled.
|
|
|
|
* @param {HTMLElement} element DOM element to check.
|
|
|
|
* @return {boolean} Whether the element is outside of the viewport.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
isElementOutsideOfScreen(scrollEl: HTMLElement, element: HTMLElement): boolean {
|
|
|
|
const elementRect = element.getBoundingClientRect();
|
|
|
|
let elementMidPoint,
|
2017-11-06 17:17:11 +01:00
|
|
|
scrollElRect,
|
|
|
|
scrollTopPos = 0;
|
|
|
|
|
|
|
|
if (!elementRect) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
elementMidPoint = Math.round((elementRect.bottom + elementRect.top) / 2);
|
|
|
|
|
|
|
|
scrollElRect = scrollEl.getBoundingClientRect();
|
2018-01-29 10:05:20 +01:00
|
|
|
scrollTopPos = (scrollElRect && scrollElRect.top) || 0;
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
return elementMidPoint > window.innerHeight || elementMidPoint < scrollTopPos;
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if rich text editor is enabled.
|
|
|
|
*
|
|
|
|
* @return {Promise<boolean>} Promise resolved with boolean: true if enabled, false otherwise.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
isRichTextEditorEnabled(): Promise<boolean> {
|
2017-11-06 17:17:11 +01:00
|
|
|
if (this.isRichTextEditorSupported()) {
|
2018-05-30 11:38:38 +02:00
|
|
|
return this.configProvider.get(CoreConstants.SETTINGS_RICH_TEXT_EDITOR, true).then((enabled) => {
|
|
|
|
return !!enabled;
|
|
|
|
});
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if rich text editor is supported in the platform.
|
|
|
|
*
|
|
|
|
* @return {boolean} Whether it's supported.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
isRichTextEditorSupported(): boolean {
|
2018-06-18 16:24:27 +02:00
|
|
|
return true;
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 08:50:27 +01:00
|
|
|
/**
|
|
|
|
* Move children from one HTMLElement to another.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} oldParent The old parent.
|
|
|
|
* @param {HTMLElement} newParent The new parent.
|
2018-02-22 09:27:49 +01:00
|
|
|
* @return {Node[]} List of moved children.
|
2017-11-24 08:50:27 +01:00
|
|
|
*/
|
2018-02-22 09:27:49 +01:00
|
|
|
moveChildren(oldParent: HTMLElement, newParent: HTMLElement): Node[] {
|
|
|
|
const movedChildren: Node[] = [];
|
|
|
|
|
2017-11-24 08:50:27 +01:00
|
|
|
while (oldParent.childNodes.length > 0) {
|
2018-02-22 09:27:49 +01:00
|
|
|
const child = oldParent.childNodes[0];
|
|
|
|
movedChildren.push(child);
|
|
|
|
|
|
|
|
newParent.appendChild(child);
|
2017-11-24 08:50:27 +01:00
|
|
|
}
|
2018-02-22 09:27:49 +01:00
|
|
|
|
|
|
|
return movedChildren;
|
2017-11-24 08:50:27 +01:00
|
|
|
}
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
/**
|
|
|
|
* Search and remove a certain element from inside another element.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element DOM element to search in.
|
|
|
|
* @param {string} selector Selector to search.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
removeElement(element: HTMLElement, selector: string): void {
|
2017-11-06 17:17:11 +01:00
|
|
|
if (element) {
|
2018-01-29 10:05:20 +01:00
|
|
|
const selected = element.querySelector(selector);
|
2017-11-06 17:17:11 +01:00
|
|
|
if (selected) {
|
|
|
|
selected.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search and remove a certain element from an HTML code.
|
|
|
|
*
|
|
|
|
* @param {string} html HTML code to change.
|
|
|
|
* @param {string} selector Selector to search.
|
|
|
|
* @param {boolean} [removeAll] True if it should remove all matches found, false if it should only remove the first one.
|
|
|
|
* @return {string} HTML without the element.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
removeElementFromHtml(html: string, selector: string, removeAll?: boolean): string {
|
2017-11-06 17:17:11 +01:00
|
|
|
let selected;
|
|
|
|
|
|
|
|
this.element.innerHTML = html;
|
|
|
|
|
|
|
|
if (removeAll) {
|
|
|
|
selected = this.element.querySelectorAll(selector);
|
2018-02-22 11:58:08 +01:00
|
|
|
for (let i = 0; i < selected.length; i++) {
|
2017-11-06 17:17:11 +01:00
|
|
|
selected[i].remove();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
selected = this.element.querySelector(selector);
|
|
|
|
if (selected) {
|
|
|
|
selected.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.element.innerHTML;
|
|
|
|
}
|
|
|
|
|
2018-02-05 12:31:48 +01:00
|
|
|
/**
|
|
|
|
* Remove a component/directive instance using the DOM Element.
|
|
|
|
*
|
|
|
|
* @param {Element} element The root element of the component/directive.
|
|
|
|
*/
|
|
|
|
removeInstanceByElement(element: Element): void {
|
|
|
|
const id = element.getAttribute(this.INSTANCE_ID_ATTR_NAME);
|
|
|
|
delete this.instances[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a component/directive instance using the ID.
|
|
|
|
*
|
|
|
|
* @param {string} id The ID to remove.
|
|
|
|
*/
|
|
|
|
removeInstanceById(id: string): void {
|
|
|
|
delete this.instances[id];
|
|
|
|
}
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
/**
|
|
|
|
* Search for certain classes in an element contents and replace them with the specified new values.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element DOM element.
|
|
|
|
* @param {any} map Mapping of the classes to replace. Keys must be the value to replace, values must be
|
2017-12-29 18:05:52 +01:00
|
|
|
* the new class name. Example: {'correct': 'core-question-answer-correct'}.
|
2017-11-06 17:17:11 +01:00
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
replaceClassesInElement(element: HTMLElement, map: any): void {
|
|
|
|
for (const key in map) {
|
|
|
|
const foundElements = element.querySelectorAll('.' + key);
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-02-22 11:58:08 +01:00
|
|
|
for (let i = 0; i < foundElements.length; i++) {
|
2018-01-29 10:05:20 +01:00
|
|
|
const foundElement = foundElements[i];
|
2017-11-06 17:17:11 +01:00
|
|
|
foundElement.className = foundElement.className.replace(key, map[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an HTML, search all links and media and tries to restore original sources using the paths object.
|
|
|
|
*
|
|
|
|
* @param {string} html HTML code.
|
|
|
|
* @param {object} paths Object linking URLs in the html code with the real URLs to use.
|
|
|
|
* @param {Function} [anchorFn] Function to call with each anchor. Optional.
|
|
|
|
* @return {string} Treated HTML code.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
restoreSourcesInHtml(html: string, paths: object, anchorFn?: Function): string {
|
2017-11-06 17:17:11 +01:00
|
|
|
let media,
|
|
|
|
anchors;
|
|
|
|
|
|
|
|
this.element.innerHTML = html;
|
|
|
|
|
|
|
|
// Treat elements with src (img, audio, video, ...).
|
|
|
|
media = this.element.querySelectorAll('img, video, audio, source, track');
|
2018-02-02 08:11:29 +01:00
|
|
|
media.forEach((media: HTMLElement) => {
|
|
|
|
let newSrc = paths[this.textUtils.decodeURIComponent(media.getAttribute('src'))];
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
if (typeof newSrc != 'undefined') {
|
2018-02-02 08:11:29 +01:00
|
|
|
media.setAttribute('src', newSrc);
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Treat video posters.
|
2018-02-02 08:11:29 +01:00
|
|
|
if (media.tagName == 'VIDEO' && media.getAttribute('poster')) {
|
|
|
|
newSrc = paths[this.textUtils.decodeURIComponent(media.getAttribute('poster'))];
|
2017-11-06 17:17:11 +01:00
|
|
|
if (typeof newSrc !== 'undefined') {
|
2018-02-02 08:11:29 +01:00
|
|
|
media.setAttribute('poster', newSrc);
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-02 08:11:29 +01:00
|
|
|
});
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
// Now treat links.
|
|
|
|
anchors = this.element.querySelectorAll('a');
|
2018-02-02 08:11:29 +01:00
|
|
|
anchors.forEach((anchor: HTMLElement) => {
|
|
|
|
const href = this.textUtils.decodeURIComponent(anchor.getAttribute('href')),
|
2017-11-06 17:17:11 +01:00
|
|
|
newUrl = paths[href];
|
|
|
|
|
|
|
|
if (typeof newUrl != 'undefined') {
|
|
|
|
anchor.setAttribute('href', newUrl);
|
|
|
|
|
|
|
|
if (typeof anchorFn == 'function') {
|
|
|
|
anchorFn(anchor, href);
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 08:11:29 +01:00
|
|
|
});
|
2017-11-06 17:17:11 +01:00
|
|
|
|
|
|
|
return this.element.innerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-31 11:37:42 +01:00
|
|
|
* Scroll to a certain element.
|
2017-11-06 17:17:11 +01:00
|
|
|
*
|
2018-01-31 11:37:42 +01:00
|
|
|
* @param {Content} content The content that must be scrolled.
|
|
|
|
* @param {HTMLElement} element The element to scroll to.
|
2017-11-06 17:17:11 +01:00
|
|
|
* @param {string} [scrollParentClass] Parent class where to stop calculating the position. Default scroll-content.
|
|
|
|
* @return {boolean} True if the element is found, false otherwise.
|
|
|
|
*/
|
2018-01-31 11:37:42 +01:00
|
|
|
scrollToElement(content: Content, element: HTMLElement, scrollParentClass?: string): boolean {
|
|
|
|
const position = this.getElementXY(element, undefined, scrollParentClass);
|
2017-11-06 17:17:11 +01:00
|
|
|
if (!position) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-31 11:37:42 +01:00
|
|
|
content.scrollTo(position[0], position[1]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scroll to a certain element using a selector to find it.
|
|
|
|
*
|
|
|
|
* @param {Content} content The content that must be scrolled.
|
|
|
|
* @param {string} selector Selector to find the element to scroll to.
|
|
|
|
* @param {string} [scrollParentClass] Parent class where to stop calculating the position. Default scroll-content.
|
|
|
|
* @return {boolean} True if the element is found, false otherwise.
|
|
|
|
*/
|
|
|
|
scrollToElementBySelector(content: Content, selector: string, scrollParentClass?: string): boolean {
|
|
|
|
const position = this.getElementXY(content.getScrollElement(), selector, scrollParentClass);
|
|
|
|
if (!position) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
content.scrollTo(position[0], position[1]);
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-29 18:05:52 +01:00
|
|
|
* Search for an input with error (core-input-error directive) and scrolls to it if found.
|
2017-11-06 17:17:11 +01:00
|
|
|
*
|
2018-01-31 11:37:42 +01:00
|
|
|
* @param {Content} content The content that must be scrolled.
|
2017-11-06 17:17:11 +01:00
|
|
|
* @param [scrollParentClass] Parent class where to stop calculating the position. Default scroll-content.
|
|
|
|
* @return {boolean} True if the element is found, false otherwise.
|
|
|
|
*/
|
2018-01-31 11:37:42 +01:00
|
|
|
scrollToInputError(content: Content, scrollParentClass?: string): boolean {
|
|
|
|
if (!content) {
|
2017-11-29 16:03:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-31 11:37:42 +01:00
|
|
|
return this.scrollToElementBySelector(content, '.core-input-error', scrollParentClass);
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show an alert modal with a button to close it.
|
|
|
|
*
|
|
|
|
* @param {string} title Title to show.
|
|
|
|
* @param {string} message Message to show.
|
|
|
|
* @param {string} [buttonText] Text of the button.
|
|
|
|
* @param {number} [autocloseTime] Number of milliseconds to wait to close the modal. If not defined, modal won't be closed.
|
2018-06-14 15:01:11 +02:00
|
|
|
* @return {Promise<Alert>} Promise resolved with the alert modal.
|
2017-11-06 17:17:11 +01:00
|
|
|
*/
|
2018-06-14 15:01:11 +02:00
|
|
|
showAlert(title: string, message: string, buttonText?: string, autocloseTime?: number): Promise<Alert> {
|
|
|
|
const hasHTMLTags = this.textUtils.hasHTMLTags(message);
|
|
|
|
let promise;
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-06-14 15:01:11 +02:00
|
|
|
if (hasHTMLTags) {
|
|
|
|
// Format the text.
|
|
|
|
promise = this.textUtils.formatText(message);
|
|
|
|
} else {
|
|
|
|
promise = Promise.resolve(message);
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
2018-06-14 15:01:11 +02:00
|
|
|
return promise.then((message) => {
|
|
|
|
|
|
|
|
const alert = this.alertCtrl.create({
|
|
|
|
title: title,
|
|
|
|
message: message,
|
|
|
|
buttons: [buttonText || this.translate.instant('core.ok')]
|
|
|
|
});
|
|
|
|
|
|
|
|
alert.present().then(() => {
|
|
|
|
if (hasHTMLTags) {
|
|
|
|
// Treat all anchors so they don't override the app.
|
|
|
|
const alertMessageEl: HTMLElement = alert.pageRef().nativeElement.querySelector('.alert-message');
|
|
|
|
this.treatAnchors(alertMessageEl);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (autocloseTime > 0) {
|
|
|
|
setTimeout(() => {
|
|
|
|
alert.dismiss();
|
|
|
|
}, autocloseTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
return alert;
|
|
|
|
});
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show an alert modal with a button to close it, translating the values supplied.
|
|
|
|
*
|
|
|
|
* @param {string} title Title to show.
|
|
|
|
* @param {string} message Message to show.
|
|
|
|
* @param {string} [buttonText] Text of the button.
|
|
|
|
* @param {number} [autocloseTime] Number of milliseconds to wait to close the modal. If not defined, modal won't be closed.
|
2018-06-14 15:01:11 +02:00
|
|
|
* @return {Promise<Alert>} Promise resolved with the alert modal.
|
2017-11-06 17:17:11 +01:00
|
|
|
*/
|
2018-06-14 15:01:11 +02:00
|
|
|
showAlertTranslated(title: string, message: string, buttonText?: string, autocloseTime?: number): Promise<Alert> {
|
2017-11-06 17:17:11 +01:00
|
|
|
title = title ? this.translate.instant(title) : title;
|
|
|
|
message = message ? this.translate.instant(message) : message;
|
|
|
|
buttonText = buttonText ? this.translate.instant(buttonText) : buttonText;
|
|
|
|
|
|
|
|
return this.showAlert(title, message, buttonText, autocloseTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a confirm modal.
|
|
|
|
*
|
|
|
|
* @param {string} message Message to show in the modal body.
|
|
|
|
* @param {string} [title] Title of the modal.
|
|
|
|
* @param {string} [okText] Text of the OK button.
|
|
|
|
* @param {string} [cancelText] Text of the Cancel button.
|
|
|
|
* @param {any} [options] More options. See https://ionicframework.com/docs/api/components/alert/AlertController/
|
2018-04-06 08:58:23 +02:00
|
|
|
* @return {Promise<void>} Promise resolved if the user confirms and rejected with a canceled error if he cancels.
|
2017-11-06 17:17:11 +01:00
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
showConfirm(message: string, title?: string, okText?: string, cancelText?: string, options?: any): Promise<void> {
|
|
|
|
return new Promise<void>((resolve, reject): void => {
|
2018-06-14 15:01:11 +02:00
|
|
|
const hasHTMLTags = this.textUtils.hasHTMLTags(message);
|
|
|
|
let promise;
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-06-14 15:01:11 +02:00
|
|
|
if (hasHTMLTags) {
|
|
|
|
// Format the text.
|
|
|
|
promise = this.textUtils.formatText(message);
|
|
|
|
} else {
|
|
|
|
promise = Promise.resolve(message);
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
2018-06-14 15:01:11 +02:00
|
|
|
|
|
|
|
promise.then((message) => {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
options.message = message;
|
|
|
|
options.title = title;
|
|
|
|
if (!title) {
|
|
|
|
options.cssClass = 'core-nohead';
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
2018-06-14 15:01:11 +02:00
|
|
|
options.buttons = [
|
|
|
|
{
|
|
|
|
text: cancelText || this.translate.instant('core.cancel'),
|
|
|
|
role: 'cancel',
|
|
|
|
handler: (): void => {
|
|
|
|
reject(this.createCanceledError());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: okText || this.translate.instant('core.ok'),
|
|
|
|
handler: (): void => {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const alert = this.alertCtrl.create(options);
|
2017-11-06 17:17:11 +01:00
|
|
|
|
2018-06-14 15:01:11 +02:00
|
|
|
alert.present().then(() => {
|
|
|
|
if (hasHTMLTags) {
|
|
|
|
// Treat all anchors so they don't override the app.
|
|
|
|
const alertMessageEl: HTMLElement = alert.pageRef().nativeElement.querySelector('.alert-message');
|
|
|
|
this.treatAnchors(alertMessageEl);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-11-06 17:17:11 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show an alert modal with an error message.
|
|
|
|
*
|
|
|
|
* @param {any} error Message to show.
|
|
|
|
* @param {boolean} [needsTranslate] Whether the error needs to be translated.
|
|
|
|
* @param {number} [autocloseTime] Number of milliseconds to wait to close the modal. If not defined, modal won't be closed.
|
2018-06-14 15:01:11 +02:00
|
|
|
* @return {Promise<Alert>} Promise resolved with the alert modal.
|
2017-11-06 17:17:11 +01:00
|
|
|
*/
|
2018-06-14 15:01:11 +02:00
|
|
|
showErrorModal(error: any, needsTranslate?: boolean, autocloseTime?: number): Promise<Alert> {
|
2017-11-06 17:17:11 +01:00
|
|
|
if (typeof error == 'object') {
|
|
|
|
// We received an object instead of a string. Search for common properties.
|
2018-04-06 08:58:23 +02:00
|
|
|
if (error.coreCanceled) {
|
|
|
|
// It's a canceled error, don't display an error.
|
|
|
|
return;
|
|
|
|
} else if (typeof error.content != 'undefined') {
|
2017-11-06 17:17:11 +01:00
|
|
|
error = error.content;
|
|
|
|
} else if (typeof error.body != 'undefined') {
|
|
|
|
error = error.body;
|
|
|
|
} else if (typeof error.message != 'undefined') {
|
|
|
|
error = error.message;
|
|
|
|
} else if (typeof error.error != 'undefined') {
|
|
|
|
error = error.error;
|
|
|
|
} else {
|
|
|
|
// No common properties found, just stringify it.
|
|
|
|
error = JSON.stringify(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to remove tokens from the contents.
|
2018-01-29 10:05:20 +01:00
|
|
|
const matches = error.match(/token"?[=|:]"?(\w*)/, '');
|
2017-11-06 17:17:11 +01:00
|
|
|
if (matches && matches[1]) {
|
|
|
|
error = error.replace(new RegExp(matches[1], 'g'), 'secret');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 08:58:23 +02:00
|
|
|
if (error == CoreConstants.DONT_SHOW_ERROR) {
|
|
|
|
// The error shouldn't be shown, stop.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
const message = this.textUtils.decodeHTML(needsTranslate ? this.translate.instant(error) : error);
|
|
|
|
|
2017-12-18 10:22:04 +01:00
|
|
|
return this.showAlert(this.getErrorTitle(message), message, undefined, autocloseTime);
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show an alert modal with an error message. It uses a default message if error is not a string.
|
|
|
|
*
|
|
|
|
* @param {any} error Message to show.
|
|
|
|
* @param {any} [defaultError] Message to show if the error is not a string.
|
|
|
|
* @param {boolean} [needsTranslate] Whether the error needs to be translated.
|
|
|
|
* @param {number} [autocloseTime] Number of milliseconds to wait to close the modal. If not defined, modal won't be closed.
|
2018-06-14 15:01:11 +02:00
|
|
|
* @return {Promise<Alert>} Promise resolved with the alert modal.
|
2017-11-06 17:17:11 +01:00
|
|
|
*/
|
2018-06-14 15:01:11 +02:00
|
|
|
showErrorModalDefault(error: any, defaultError: any, needsTranslate?: boolean, autocloseTime?: number): Promise<Alert> {
|
2018-04-06 08:58:23 +02:00
|
|
|
if (error && error.coreCanceled) {
|
|
|
|
// It's a canceled error, don't display an error.
|
|
|
|
return;
|
|
|
|
}
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2018-04-06 08:58:23 +02:00
|
|
|
if (error && typeof error != 'string') {
|
|
|
|
error = error.message || error.error || error.content || error.body;
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
2018-04-06 08:58:23 +02:00
|
|
|
|
|
|
|
error = typeof error == 'string' ? error : defaultError;
|
|
|
|
|
|
|
|
return this.showErrorModal(error, needsTranslate, autocloseTime);
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
|
|
|
|
2018-01-18 16:38:41 +01:00
|
|
|
/**
|
|
|
|
* Show an alert modal with the first warning error message. It uses a default message if error is not a string.
|
|
|
|
*
|
|
|
|
* @param {any} warnings Warnings returned.
|
|
|
|
* @param {any} [defaultError] Message to show if the error is not a string.
|
|
|
|
* @param {boolean} [needsTranslate] Whether the error needs to be translated.
|
|
|
|
* @param {number} [autocloseTime] Number of milliseconds to wait to close the modal. If not defined, modal won't be closed.
|
2018-06-14 15:01:11 +02:00
|
|
|
* @return {Promise<Alert>} Promise resolved with the alert modal.
|
2018-01-18 16:38:41 +01:00
|
|
|
*/
|
2018-06-14 15:01:11 +02:00
|
|
|
showErrorModalFirstWarning(warnings: any, defaultError: any, needsTranslate?: boolean, autocloseTime?: number): Promise<Alert> {
|
2018-01-29 10:05:20 +01:00
|
|
|
const error = warnings && warnings.length && warnings[0].message;
|
|
|
|
|
2018-01-18 16:38:41 +01:00
|
|
|
return this.showErrorModalDefault(error, defaultError, needsTranslate, autocloseTime);
|
|
|
|
}
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
/**
|
|
|
|
* Displays a loading modal window.
|
|
|
|
*
|
2017-12-08 15:53:27 +01:00
|
|
|
* @param {string} [text] The text of the modal window. Default: core.loading.
|
2017-11-06 17:17:11 +01:00
|
|
|
* @param {boolean} [needsTranslate] Whether the 'text' needs to be translated.
|
|
|
|
* @return {Loading} Loading modal instance.
|
|
|
|
* @description
|
|
|
|
* Usage:
|
|
|
|
* let modal = domUtils.showModalLoading(myText);
|
|
|
|
* ...
|
|
|
|
* modal.dismiss();
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
showModalLoading(text?: string, needsTranslate?: boolean): Loading {
|
2017-11-06 17:17:11 +01:00
|
|
|
if (!text) {
|
2017-12-08 15:53:27 +01:00
|
|
|
text = this.translate.instant('core.loading');
|
2017-11-06 17:17:11 +01:00
|
|
|
} else if (needsTranslate) {
|
|
|
|
text = this.translate.instant(text);
|
|
|
|
}
|
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
const loader = this.loadingCtrl.create({
|
2017-11-06 17:17:11 +01:00
|
|
|
content: text
|
|
|
|
});
|
|
|
|
|
|
|
|
loader.present();
|
|
|
|
|
|
|
|
return loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a prompt modal to input some data.
|
|
|
|
*
|
|
|
|
* @param {string} message Modal message.
|
|
|
|
* @param {string} [title] Modal title.
|
|
|
|
* @param {string} [placeholder] Placeholder of the input element. By default, "Password".
|
|
|
|
* @param {string} [type] Type of the input element. By default, password.
|
|
|
|
* @return {Promise<any>} Promise resolved with the input data if the user clicks OK, rejected if cancels.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
showPrompt(message: string, title?: string, placeholder?: string, type: string = 'password'): Promise<any> {
|
|
|
|
return new Promise((resolve, reject): void => {
|
2018-06-14 15:01:11 +02:00
|
|
|
const hasHTMLTags = this.textUtils.hasHTMLTags(message);
|
|
|
|
let promise;
|
|
|
|
|
|
|
|
if (hasHTMLTags) {
|
|
|
|
// Format the text.
|
|
|
|
promise = this.textUtils.formatText(message);
|
|
|
|
} else {
|
|
|
|
promise = Promise.resolve(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.then((message) => {
|
|
|
|
const alert = this.alertCtrl.create({
|
|
|
|
message: message,
|
|
|
|
title: title,
|
|
|
|
inputs: [
|
|
|
|
{
|
|
|
|
name: 'promptinput',
|
|
|
|
placeholder: placeholder || this.translate.instant('core.login.password'),
|
|
|
|
type: type
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
2018-06-14 15:01:11 +02:00
|
|
|
],
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
text: this.translate.instant('core.cancel'),
|
|
|
|
role: 'cancel',
|
|
|
|
handler: (): void => {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: this.translate.instant('core.ok'),
|
|
|
|
handler: (data): void => {
|
|
|
|
resolve(data.promptinput);
|
|
|
|
}
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
2018-06-14 15:01:11 +02:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
alert.present().then(() => {
|
|
|
|
if (hasHTMLTags) {
|
|
|
|
// Treat all anchors so they don't override the app.
|
|
|
|
const alertMessageEl: HTMLElement = alert.pageRef().nativeElement.querySelector('.alert-message');
|
|
|
|
this.treatAnchors(alertMessageEl);
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
2018-06-14 15:01:11 +02:00
|
|
|
});
|
|
|
|
});
|
2017-11-06 17:17:11 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays an autodimissable toast modal window.
|
|
|
|
*
|
|
|
|
* @param {string} text The text of the toast.
|
|
|
|
* @param {boolean} [needsTranslate] Whether the 'text' needs to be translated.
|
|
|
|
* @param {number} [duration=2000] Duration in ms of the dimissable toast.
|
2018-01-24 15:39:05 +01:00
|
|
|
* @param {string} [cssClass=""] Class to add to the toast.
|
2017-11-06 17:17:11 +01:00
|
|
|
* @return {Toast} Toast instance.
|
|
|
|
*/
|
2018-01-24 15:39:05 +01:00
|
|
|
showToast(text: string, needsTranslate?: boolean, duration: number = 2000, cssClass: string = ''): Toast {
|
2017-11-06 17:17:11 +01:00
|
|
|
if (needsTranslate) {
|
|
|
|
text = this.translate.instant(text);
|
|
|
|
}
|
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
const loader = this.toastCtrl.create({
|
2017-11-06 17:17:11 +01:00
|
|
|
message: text,
|
|
|
|
duration: duration,
|
|
|
|
position: 'bottom',
|
2018-01-24 15:39:05 +01:00
|
|
|
cssClass: cssClass,
|
2017-11-06 17:17:11 +01:00
|
|
|
dismissOnPageChange: true
|
|
|
|
});
|
|
|
|
|
|
|
|
loader.present();
|
|
|
|
|
|
|
|
return loader;
|
|
|
|
}
|
|
|
|
|
2018-02-05 12:31:48 +01:00
|
|
|
/**
|
|
|
|
* Stores a component/directive instance.
|
|
|
|
*
|
|
|
|
* @param {Element} element The root element of the component/directive.
|
|
|
|
* @param {any} instance The instance to store.
|
|
|
|
* @return {string} ID to identify the instance.
|
|
|
|
*/
|
|
|
|
storeInstanceByElement(element: Element, instance: any): string {
|
|
|
|
const id = String(this.lastInstanceId++);
|
|
|
|
|
|
|
|
element.setAttribute(this.INSTANCE_ID_ATTR_NAME, id);
|
|
|
|
this.instances[id] = instance;
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2017-11-06 17:17:11 +01:00
|
|
|
/**
|
|
|
|
* Check if an element supports input via keyboard.
|
|
|
|
*
|
|
|
|
* @param {any} el HTML element to check.
|
|
|
|
* @return {boolean} Whether it supports input using keyboard.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
supportsInputKeyboard(el: any): boolean {
|
2017-11-06 17:17:11 +01:00
|
|
|
return el && !el.disabled && (el.tagName.toLowerCase() == 'textarea' ||
|
2018-01-12 14:28:46 +01:00
|
|
|
(el.tagName.toLowerCase() == 'input' && this.INPUT_SUPPORT_KEYBOARD.indexOf(el.type) != -1));
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|
2018-01-02 08:47:21 +01:00
|
|
|
|
2018-01-31 09:12:59 +01:00
|
|
|
/**
|
|
|
|
* Converts HTML formatted text to DOM element.
|
|
|
|
* @param {string} text HTML text.
|
|
|
|
* @return {HTMLCollection} Same text converted to HTMLCollection.
|
|
|
|
*/
|
|
|
|
toDom(text: string): HTMLCollection {
|
|
|
|
const element = document.createElement('div');
|
|
|
|
element.innerHTML = text;
|
|
|
|
|
|
|
|
return element.children;
|
|
|
|
}
|
|
|
|
|
2018-06-14 15:01:11 +02:00
|
|
|
/**
|
|
|
|
* Treat anchors inside alert/modals.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} container The HTMLElement that can contain anchors.
|
|
|
|
*/
|
|
|
|
protected treatAnchors(container: HTMLElement): void {
|
|
|
|
const anchors = Array.from(container.querySelectorAll('a'));
|
|
|
|
|
|
|
|
anchors.forEach((anchor) => {
|
|
|
|
anchor.addEventListener('click', (event) => {
|
|
|
|
if (event.defaultPrevented) {
|
|
|
|
// Stop.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const href = anchor.getAttribute('href');
|
|
|
|
if (href) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
// We cannot use CoreDomUtilsProvider.openInBrowser due to circular dependencies.
|
|
|
|
if (this.appProvider.isDesktop()) {
|
|
|
|
// It's a desktop app, use Electron shell library to open the browser.
|
|
|
|
const shell = require('electron').shell;
|
|
|
|
if (!shell.openExternal(href)) {
|
|
|
|
// Open browser failed, open a new window in the app.
|
|
|
|
window.open(href, '_system');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
window.open(href, '_system');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-03 10:36:07 +01:00
|
|
|
/**
|
|
|
|
* View an image in a new page or modal.
|
|
|
|
*
|
|
|
|
* @param {string} image URL of the image.
|
|
|
|
* @param {string} title Title of the page or modal.
|
|
|
|
* @param {string} [component] Component to link the image to if needed.
|
|
|
|
* @param {string|number} [componentId] An ID to use in conjunction with the component.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
viewImage(image: string, title?: string, component?: string, componentId?: string | number): void {
|
2018-01-03 10:36:07 +01:00
|
|
|
if (image) {
|
2018-01-29 10:05:20 +01:00
|
|
|
const params: any = {
|
|
|
|
title: title,
|
|
|
|
image: image,
|
|
|
|
component: component,
|
|
|
|
componentId: componentId
|
|
|
|
},
|
|
|
|
modal = this.modalCtrl.create('CoreViewerImagePage', params);
|
2018-01-23 13:00:00 +01:00
|
|
|
modal.present();
|
2018-01-03 10:36:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-02 08:47:21 +01:00
|
|
|
/**
|
|
|
|
* Wrap an HTMLElement with another element.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} el The element to wrap.
|
|
|
|
* @param {HTMLElement} wrapper Wrapper.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
wrapElement(el: HTMLElement, wrapper: HTMLElement): void {
|
2018-01-02 08:47:21 +01:00
|
|
|
// Insert the wrapper before the element.
|
|
|
|
el.parentNode.insertBefore(wrapper, el);
|
|
|
|
// Now move the element into the wrapper.
|
|
|
|
wrapper.appendChild(el);
|
|
|
|
}
|
2017-11-06 17:17:11 +01:00
|
|
|
}
|