{"version":3,"file":"str.min.js","sources":["../src/str.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Fetch and return language strings.\n *\n * @module core/str\n * @copyright 2015 Damyon Wiese <damyon@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 2.9\n *\n */\nimport $ from 'jquery';\nimport Ajax from 'core/ajax';\nimport LocalStorage from 'core/localstorage';\n\n// Module cache for the promises so that we don't make multiple\n// unnecessary requests.\nlet promiseCache = [];\n\n/* eslint-disable no-restricted-properties */\n\n/**\n * Return a Promise that resolves to a string.\n *\n * If the string has previously been cached, then the Promise will be resolved immediately, otherwise it will be fetched\n * from the server and resolved when available.\n *\n * @method get_string\n * @param {string} key The language string key\n * @param {string} component The language string component\n * @param {string} param The param for variable expansion in the string.\n * @param {string} lang The users language - if not passed it is deduced.\n * @return {Promise}\n *\n * @example <caption>Fetching a string</caption>\n *\n * import {get_string as getString} from 'core/str';\n * get_string('cannotfindteacher', 'error')\n * .then(str => {\n * window.console.log(str); // Cannot find teacher\n * })\n * .catch();\n */\n// eslint-disable-next-line camelcase\nexport const get_string = (key, component, param, lang) => {\n return get_strings([{key, component, param, lang}])\n .then(results => results[0]);\n};\n\n/**\n * Make a batch request to load a set of strings.\n *\n * Any missing string will be fetched from the server.\n * The Promise will only be resolved once all strings are available, or an attempt has been made to fetch them.\n *\n * @method get_strings\n * @param {Object[]} requests List of strings to fetch\n * @param {string} requests.key The string identifer to fetch\n * @param {string} [requests.component='core'] The componet to fetch from\n * @param {string} [requests.lang] The language to fetch a string for. Defaults to current page language.\n * @param {object|string} [requests.param] The param for variable expansion in the string.\n * @return {Promise[]}\n *\n * @example <caption>Fetching a set of strings</caption>\n *\n * import {get_strings as getStrings} from 'core/str';\n * get_strings([\n * {\n * key: 'cannotfindteacher',\n * component: 'error',\n * },\n * {\n * key: 'yes',\n * component: 'core',\n * },\n * {\n * key: 'no',\n * component: 'core',\n * },\n * ])\n * .then((cannotFindTeacher, yes, no) => {\n * window.console.log(cannotFindTeacher); // Cannot find teacher\n * window.console.log(yes); // Yes\n * window.console.log(no); // No\n * })\n * .catch();\n */\n// eslint-disable-next-line camelcase\nexport const get_strings = (requests) => {\n let requestData = [];\n const pageLang = $('html').attr('lang').replace(/-/g, '_');\n\n // Helper function to construct the cache key.\n const getCacheKey = ({key, component, lang = pageLang}) => `core_str/${key}/${component}/${lang}`;\n\n const stringPromises = requests.map((request) => {\n let {component, key, param, lang = pageLang} = request;\n if (!component) {\n component = 'core';\n }\n\n con