// (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. import { Injectable } from '@angular/core'; import { ModalController } from 'ionic-angular'; import { TranslateService } from '@ngx-translate/core'; import { CoreLangProvider } from '../lang'; /* * "Utils" service with helper functions for text. */ @Injectable() export class CoreTextUtilsProvider { // List of regular expressions to convert the old nomenclature to new nomenclature for disabled features. protected DISABLED_FEATURES_COMPAT_REGEXPS = [ {old: /\$mmLoginEmailSignup/g, new: 'CoreLoginEmailSignup'}, {old: /\$mmSideMenuDelegate/g, new: 'CoreMainMenuDelegate'}, {old: /\$mmCoursesDelegate/g, new: 'CoreCourseOptionsDelegate'}, {old: /\$mmUserDelegate/g, new: 'CoreUserDelegate'}, {old: /\$mmCourseDelegate/g, new: 'CoreCourseModuleDelegate'}, {old: /_mmCourses/g, new: '_CoreCourses'}, {old: /_mmaFrontpage/g, new: '_CoreSiteHome'}, {old: /_mmaGrades/g, new: '_CoreGrades'}, {old: /_mmaCompetency/g, new: '_AddonCompetency'}, {old: /_mmaNotifications/g, new: '_AddonNotifications'}, {old: /_mmaMessages/g, new: '_AddonMessages'}, {old: /_mmaCalendar/g, new: '_AddonCalendar'}, {old: /_mmaFiles/g, new: '_AddonFiles'}, {old: /_mmaParticipants/g, new: '_CoreUserParticipants'}, {old: /_mmaCourseCompletion/g, new: '_AddonCourseCompletion'}, {old: /_mmaNotes/g, new: '_AddonNotes'}, {old: /_mmaBadges/g, new: '_AddonBadges'}, {old: /files_privatefiles/g, new: 'AddonFilesPrivateFiles'}, {old: /files_sitefiles/g, new: 'AddonFilesSiteFiles'}, {old: /files_upload/g, new: 'AddonFilesUpload'}, {old: /_mmaModAssign/g, new: '_AddonModAssign'}, {old: /_mmaModBook/g, new: '_AddonModBook'}, {old: /_mmaModChat/g, new: '_AddonModChat'}, {old: /_mmaModChoice/g, new: '_AddonModChoice'}, {old: /_mmaModData/g, new: '_AddonModData'}, {old: /_mmaModFeedback/g, new: '_AddonModFeedback'}, {old: /_mmaModFolder/g, new: '_AddonModFolder'}, {old: /_mmaModForum/g, new: '_AddonModForum'}, {old: /_mmaModGlossary/g, new: '_AddonModGlossary'}, {old: /_mmaModImscp/g, new: '_AddonModImscp'}, {old: /_mmaModLabel/g, new: '_AddonModLabel'}, {old: /_mmaModLesson/g, new: '_AddonModLesson'}, {old: /_mmaModLti/g, new: '_AddonModLti'}, {old: /_mmaModPage/g, new: '_AddonModPage'}, {old: /_mmaModQuiz/g, new: '_AddonModQuiz'}, {old: /_mmaModResource/g, new: '_AddonModResource'}, {old: /_mmaModScorm/g, new: '_AddonModScorm'}, {old: /_mmaModSurvey/g, new: '_AddonModSurvey'}, {old: /_mmaModUrl/g, new: '_AddonModUrl'}, {old: /_mmaModWiki/g, new: '_AddonModWiki'}, {old: /_mmaModWorkshop/g, new: '_AddonModWorkshop'}, ]; protected parser = new DOMParser(); // Parser to treat HTML. constructor(private translate: TranslateService, private langProvider: CoreLangProvider, private modalCtrl: ModalController) { } /** * Given a list of sentences, build a message with all of them wrapped in
. * * @param {string[]} messages Messages to show. * @return {string} Message with all the messages. */ buildMessage(messages: string[]): string { let result = ''; messages.forEach((message) => { if (message) { result += `
${message}
`; } }); return result; } /** * Convert size in bytes into human readable format * * @param {number} bytes Number of bytes to convert. * @param {number} [precision=2] Number of digits after the decimal separator. * @return {string} Size in human readable format. */ bytesToSize(bytes: number, precision: number = 2): string { if (typeof bytes == 'undefined' || bytes < 0) { return this.translate.instant('core.notapplicable'); } if (precision < 0) { precision = 2; } const keys = ['core.sizeb', 'core.sizekb', 'core.sizemb', 'core.sizegb', 'core.sizetb'], units = this.translate.instant(keys); let pos = 0; if (bytes >= 1024) { while (bytes >= 1024) { pos++; bytes = bytes / 1024; } // Round to "precision" decimals if needed. bytes = Number(Math.round(parseFloat(bytes + 'e+' + precision)) + 'e-' + precision); } return this.translate.instant('core.humanreadablesize', { size: bytes, unit: units[keys[pos]] }); } /** * Clean HTML tags. * * @param {string} text The text to be cleaned. * @param {boolean} [singleLine] True if new lines should be removed (all the text in a single line). * @return {string} Clean text. */ cleanTags(text: string, singleLine?: boolean): string { if (typeof text == 'number') { return text; } if (!text) { return ''; } // First, we use a regexpr. text = text.replace(/(<([^>]+)>)/ig, ''); // Then, we rely on the browser. We need to wrap the text to be sure is HTML. const element = this.convertToElement(text); text = element.textContent; // Recover or remove new lines. text = this.replaceNewLines(text, singleLine ? ' ' : '