diff --git a/lib/amd/build/str.min.js b/lib/amd/build/str.min.js index 5d25aba70d1..eb98b89cadc 100644 --- a/lib/amd/build/str.min.js +++ b/lib/amd/build/str.min.js @@ -7,6 +7,6 @@ define("core/str",["exports","jquery","core/ajax","core/config","core/localstora * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since 2.9 * - */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.get_strings=_exports.get_string=_exports.getStrings=_exports.getString=_exports.cache_strings=void 0,_jquery=_interopRequireDefault(_jquery),_ajax=_interopRequireDefault(_ajax),_config=_interopRequireDefault(_config),_localstorage=_interopRequireDefault(_localstorage);let promiseCache=[];_exports.get_string=(key,component,param,lang)=>get_strings([{key:key,component:component,param:param,lang:lang}]).then((results=>results[0]));_exports.getString=(key,component,param,lang)=>getRequestedStrings([{key:key,component:component,param:param,lang:lang}])[0];_exports.getStrings=requests=>Promise.all(getRequestedStrings(requests));const getRequestedStrings=requests=>{let requestData=[];const pageLang=_config.default.language,stringPromises=requests.map((request=>{let{component:component,key:key,param:param,lang:lang=pageLang}=request;component||(component="core");const cacheKey=(_ref=>{let{key:key,component:component,lang:lang=pageLang}=_ref;return"core_str/".concat(key,"/").concat(component,"/").concat(lang)})({key:key,component:component,lang:lang}),buildReturn=promise=>(promiseCache[cacheKey]=promise,promise);if(component in M.str&&key in M.str[component])return buildReturn(new Promise((resolve=>{resolve(M.util.get_string(key,component,param,lang))})));const cached=_localstorage.default.get(cacheKey);return cached?(M.str[component]={...M.str[component],[key]:cached},buildReturn(new Promise((resolve=>{resolve(M.util.get_string(key,component,param,lang))})))):cacheKey in promiseCache?buildReturn(promiseCache[cacheKey]).then((()=>M.util.get_string(key,component,param,lang))):buildReturn(new Promise(((resolve,reject)=>{requestData.push({methodname:"core_get_string",args:{stringid:key,stringparams:[],component:component,lang:lang},done:str=>{M.str[component]={...M.str[component],[key]:str},_localstorage.default.set(cacheKey,str),resolve(M.util.get_string(key,component,param,lang))},fail:reject})})))}));return requestData.length&&_ajax.default.call(requestData,!0,!1,!1,0,M.cfg.langrev),stringPromises},get_strings=requests=>_jquery.default.when.apply(_jquery.default,getRequestedStrings(requests)).then((function(){for(var _len=arguments.length,strings=new Array(_len),_key=0;_key<_len;_key++)strings[_key]=arguments[_key];return strings}));_exports.get_strings=get_strings;_exports.cache_strings=strings=>{strings.forEach((_ref2=>{let{key:key,component:component,value:value,lang:lang=_config.default.language}=_ref2;const cacheKey=["core_str",key,component,lang].join("/");component in M.str&&key in M.str[component]||(component in M.str||(M.str[component]={}),M.str[component][key]=value),_localstorage.default.get(cacheKey)||_localstorage.default.set(cacheKey,value),cacheKey in promiseCache||(promiseCache[cacheKey]=_jquery.default.Deferred().resolve(value).promise())}))}})); + */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.get_strings=_exports.get_string=_exports.getStrings=_exports.getString=_exports.cache_strings=void 0,_jquery=_interopRequireDefault(_jquery),_ajax=_interopRequireDefault(_ajax),_config=_interopRequireDefault(_config),_localstorage=_interopRequireDefault(_localstorage);let promiseCache=[];_exports.get_string=(key,component,param,lang)=>get_strings([{key:key,component:component,param:param,lang:lang}]).then((results=>results[0]));_exports.getString=(key,component,param,lang)=>getRequestedStrings([{key:key,component:component,param:param,lang:lang}])[0];_exports.getStrings=requests=>Promise.all(getRequestedStrings(requests));const getRequestedStrings=requests=>{let requestData=[];const pageLang=_config.default.language,stringPromises=requests.map((request=>{let{component:component,key:key,param:param,lang:lang=pageLang}=request;component||(component="core");const cacheKey=(_ref=>{let{key:key,component:component,lang:lang=pageLang}=_ref;return"core_str/".concat(key,"/").concat(component,"/").concat(lang)})({key:key,component:component,lang:lang}),buildReturn=promise=>(promiseCache[cacheKey]=promise,promise);if(component in M.str&&key in M.str[component])return buildReturn(new Promise((resolve=>{resolve(M.util.get_string(key,component,param))})));const cached=_localstorage.default.get(cacheKey);return cached?(M.str[component]={...M.str[component],[key]:cached},buildReturn(new Promise((resolve=>{resolve(M.util.get_string(key,component,param))})))):cacheKey in promiseCache?buildReturn(promiseCache[cacheKey]).then((()=>M.util.get_string(key,component,param))):buildReturn(new Promise(((resolve,reject)=>{requestData.push({methodname:"core_get_string",args:{stringid:key,stringparams:[],component:component,lang:lang},done:str=>{M.str[component]={...M.str[component],[key]:str},_localstorage.default.set(cacheKey,str),resolve(M.util.get_string(key,component,param))},fail:reject})})))}));return requestData.length&&_ajax.default.call(requestData,!0,!1,!1,0,M.cfg.langrev),stringPromises},get_strings=requests=>_jquery.default.when.apply(_jquery.default,getRequestedStrings(requests)).then((function(){for(var _len=arguments.length,strings=new Array(_len),_key=0;_key<_len;_key++)strings[_key]=arguments[_key];return strings}));_exports.get_strings=get_strings;_exports.cache_strings=strings=>{strings.forEach((_ref2=>{let{key:key,component:component,value:value,lang:lang=_config.default.language}=_ref2;const cacheKey=["core_str",key,component,lang].join("/");component in M.str&&key in M.str[component]||(component in M.str||(M.str[component]={}),M.str[component][key]=value),_localstorage.default.get(cacheKey)||_localstorage.default.set(cacheKey,value),cacheKey in promiseCache||(promiseCache[cacheKey]=_jquery.default.Deferred().resolve(value).promise())}))}})); //# sourceMappingURL=str.min.js.map \ No newline at end of file diff --git a/lib/amd/build/str.min.js.map b/lib/amd/build/str.min.js.map index c5c649fac1f..3929280a5dd 100644 --- a/lib/amd/build/str.min.js.map +++ b/lib/amd/build/str.min.js.map @@ -1 +1 @@ -{"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 .\n\n/**\n * Fetch and return language strings.\n *\n * @module core/str\n * @copyright 2015 Damyon Wiese \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 Config from 'core/config';\nimport LocalStorage from 'core/localstorage';\n\n/**\n * @typedef StringRequest\n * @type {object}\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 */\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 * @param {string} key The language string key\n * @param {string} [component='core'] The language string component\n * @param {object|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 {jQuery.Promise} A jQuery Promise containing the translated string\n *\n * @example Fetching a string\n *\n * import {getString} from 'core/str';\n * get_string('cannotfindteacher', 'error')\n * .then((str) => window.console.log(str)); // Cannot find teacher\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 * 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 * @param {string} key The language string key\n * @param {string} [component='core'] The language string component\n * @param {object|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} A native Promise containing the translated string\n *\n * @example Fetching a string\n *\n * import {getString} from 'core/str';\n *\n * getString('cannotfindteacher', 'error')\n * .then((str) => window.console.log(str)); // Cannot find teacher\n */\nexport const getString = (key, component, param, lang) =>\n getRequestedStrings([{key, component, param, lang}])[0];\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 * @param {Array.} requests List of strings to fetch\n * @return {Promise} A native promise containing an array of the translated strings\n *\n * @example Fetching a set of strings\n *\n * import {getStrings} from 'core/str';\n * getStrings([\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 */\nexport const getStrings = (requests) => Promise.all(getRequestedStrings(requests));\n\n/**\n * Internal function to perform the string requests.\n *\n * @param {Array.} requests List of strings to fetch\n * @returns {Promise[]}\n */\nconst getRequestedStrings = (requests) => {\n let requestData = [];\n const pageLang = Config.language;\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 const cacheKey = getCacheKey({key, component, lang});\n\n // Helper function to add the promise to cache.\n const buildReturn = (promise) => {\n // Make sure the promise cache contains our promise.\n promiseCache[cacheKey] = promise;\n return promise;\n };\n\n // Check if we can serve the string straight from M.str.\n if (component in M.str && key in M.str[component]) {\n return buildReturn(new Promise((resolve) => {\n resolve(M.util.get_string(key, component, param, lang));\n }));\n }\n\n // Check if the string is in the browser's local storage.\n const cached = LocalStorage.get(cacheKey);\n if (cached) {\n M.str[component] = {...M.str[component], [key]: cached};\n return buildReturn(new Promise((resolve) => {\n resolve(M.util.get_string(key, component, param, lang));\n }));\n }\n\n // Check if we've already loaded this string from the server.\n if (cacheKey in promiseCache) {\n return buildReturn(promiseCache[cacheKey]).then(() => {\n return M.util.get_string(key, component, param, lang);\n });\n } else {\n // We're going to have to ask the server for the string so\n // add this string to the list of requests to be sent.\n return buildReturn(new Promise((resolve, reject) => {\n requestData.push({\n methodname: 'core_get_string',\n args: {\n stringid: key,\n stringparams: [],\n component,\n lang,\n },\n done: (str) => {\n // When we get the response from the server\n // we should update M.str and the browser's\n // local storage before resolving this promise.\n M.str[component] = {...M.str[component], [key]: str};\n LocalStorage.set(cacheKey, str);\n resolve(M.util.get_string(key, component, param, lang));\n },\n fail: reject\n });\n }));\n }\n });\n\n if (requestData.length) {\n // If we need to load any strings from the server then send\n // off the request.\n Ajax.call(requestData, true, false, false, 0, M.cfg.langrev);\n }\n\n return stringPromises;\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 * @param {Array.} requests List of strings to fetch\n * @return {jquery.Promise} A jquery promise containing an array of the translated strings\n *\n * @example Fetching a set of strings\n *\n * import {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 */\n// eslint-disable-next-line camelcase\nexport const get_strings = (requests) => {\n // We need to use jQuery here because some calling code uses the\n // .done handler instead of the .then handler.\n return $.when.apply($, getRequestedStrings(requests))\n .then((...strings) => strings);\n};\n\n/**\n * Add a list of strings to the caches.\n *\n * This function should typically only be called from core APIs to pre-cache values.\n *\n * @method cache_strings\n * @protected\n * @param {Object[]} strings List of strings to fetch\n * @param {string} strings.key The string identifer to fetch\n * @param {string} strings.value The string value\n * @param {string} [strings.component='core'] The componet to fetch from\n * @param {string} [strings.lang=Config.language] The language to fetch a string for. Defaults to current page language.\n */\n// eslint-disable-next-line camelcase\nexport const cache_strings = (strings) => {\n strings.forEach(({key, component, value, lang = Config.language}) => {\n const cacheKey = ['core_str', key, component, lang].join('/');\n\n // Check M.str caching.\n if (!(component in M.str) || !(key in M.str[component])) {\n if (!(component in M.str)) {\n M.str[component] = {};\n }\n\n M.str[component][key] = value;\n }\n\n // Check local storage.\n if (!LocalStorage.get(cacheKey)) {\n LocalStorage.set(cacheKey, value);\n }\n\n // Check the promises cache.\n if (!(cacheKey in promiseCache)) {\n promiseCache[cacheKey] = $.Deferred().resolve(value).promise();\n }\n });\n};\n/* eslint-enable no-restricted-properties */\n"],"names":["promiseCache","key","component","param","lang","get_strings","then","results","getRequestedStrings","requests","Promise","all","requestData","pageLang","Config","language","stringPromises","map","request","cacheKey","_ref","getCacheKey","buildReturn","promise","M","str","resolve","util","get_string","cached","LocalStorage","get","reject","push","methodname","args","stringid","stringparams","done","set","fail","length","call","cfg","langrev","$","when","apply","strings","forEach","_ref2","value","join","Deferred"],"mappings":";;;;;;;;;uVAwCIA,aAAe,uBAuBO,CAACC,IAAKC,UAAWC,MAAOC,OACvCC,YAAY,CAAC,CAACJ,IAAAA,IAAKC,UAAAA,UAAWC,MAAAA,MAAOC,KAAAA,QACvCE,MAAKC,SAAWA,QAAQ,wBAsBR,CAACN,IAAKC,UAAWC,MAAOC,OAC7CI,oBAAoB,CAAC,CAACP,IAAAA,IAAKC,UAAAA,UAAWC,MAAAA,MAAOC,KAAAA,QAAQ,uBAkC9BK,UAAaC,QAAQC,IAAIH,oBAAoBC,iBAQlED,oBAAuBC,eACrBG,YAAc,SACZC,SAAWC,gBAAOC,SAKlBC,eAAiBP,SAASQ,KAAKC,cAC7BhB,UAACA,UAADD,IAAYA,IAAZE,MAAiBA,MAAjBC,KAAwBA,KAAOS,UAAYK,QAC1ChB,YACDA,UAAY,cAGViB,SARUC,CAAAA,WAACnB,IAACA,IAADC,UAAMA,UAANE,KAAiBA,KAAOS,wCAA0BZ,gBAAOC,sBAAaE,OAQtEiB,CAAY,CAACpB,IAAAA,IAAKC,UAAAA,UAAWE,KAAAA,OAGxCkB,YAAeC,UAEjBvB,aAAamB,UAAYI,QAClBA,YAIPrB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EAAEC,IAAIvB,WAAa,IAGvBsB,EAAEC,IAAIvB,WAAWD,KAAOkD,OAIvBrB,sBAAaC,IAAIZ,iCACLoB,IAAIpB,SAAUgC,OAIzBhC,YAAYnB,eACdA,aAAamB,UAAY0B,gBAAEQ,WAAW3B,QAAQyB,OAAO5B"} \ No newline at end of file +{"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 .\n\n/**\n * Fetch and return language strings.\n *\n * @module core/str\n * @copyright 2015 Damyon Wiese \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 Config from 'core/config';\nimport LocalStorage from 'core/localstorage';\n\n/**\n * @typedef StringRequest\n * @type {object}\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 */\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 * @param {string} key The language string key\n * @param {string} [component='core'] The language string component\n * @param {object|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 {jQuery.Promise} A jQuery Promise containing the translated string\n *\n * @example Fetching a string\n *\n * import {getString} from 'core/str';\n * get_string('cannotfindteacher', 'error')\n * .then((str) => window.console.log(str)); // Cannot find teacher\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 * 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 * @param {string} key The language string key\n * @param {string} [component='core'] The language string component\n * @param {object|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} A native Promise containing the translated string\n *\n * @example Fetching a string\n *\n * import {getString} from 'core/str';\n *\n * getString('cannotfindteacher', 'error')\n * .then((str) => window.console.log(str)); // Cannot find teacher\n */\nexport const getString = (key, component, param, lang) =>\n getRequestedStrings([{key, component, param, lang}])[0];\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 * @param {Array.} requests List of strings to fetch\n * @return {Promise} A native promise containing an array of the translated strings\n *\n * @example Fetching a set of strings\n *\n * import {getStrings} from 'core/str';\n * getStrings([\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 */\nexport const getStrings = (requests) => Promise.all(getRequestedStrings(requests));\n\n/**\n * Internal function to perform the string requests.\n *\n * @param {Array.} requests List of strings to fetch\n * @returns {Promise[]}\n */\nconst getRequestedStrings = (requests) => {\n let requestData = [];\n const pageLang = Config.language;\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 const cacheKey = getCacheKey({key, component, lang});\n\n // Helper function to add the promise to cache.\n const buildReturn = (promise) => {\n // Make sure the promise cache contains our promise.\n promiseCache[cacheKey] = promise;\n return promise;\n };\n\n // Check if we can serve the string straight from M.str.\n if (component in M.str && key in M.str[component]) {\n return buildReturn(new Promise((resolve) => {\n resolve(M.util.get_string(key, component, param));\n }));\n }\n\n // Check if the string is in the browser's local storage.\n const cached = LocalStorage.get(cacheKey);\n if (cached) {\n M.str[component] = {...M.str[component], [key]: cached};\n return buildReturn(new Promise((resolve) => {\n resolve(M.util.get_string(key, component, param));\n }));\n }\n\n // Check if we've already loaded this string from the server.\n if (cacheKey in promiseCache) {\n return buildReturn(promiseCache[cacheKey]).then(() => {\n return M.util.get_string(key, component, param);\n });\n } else {\n // We're going to have to ask the server for the string so\n // add this string to the list of requests to be sent.\n return buildReturn(new Promise((resolve, reject) => {\n requestData.push({\n methodname: 'core_get_string',\n args: {\n stringid: key,\n stringparams: [],\n component,\n lang,\n },\n done: (str) => {\n // When we get the response from the server\n // we should update M.str and the browser's\n // local storage before resolving this promise.\n M.str[component] = {...M.str[component], [key]: str};\n LocalStorage.set(cacheKey, str);\n resolve(M.util.get_string(key, component, param));\n },\n fail: reject\n });\n }));\n }\n });\n\n if (requestData.length) {\n // If we need to load any strings from the server then send\n // off the request.\n Ajax.call(requestData, true, false, false, 0, M.cfg.langrev);\n }\n\n return stringPromises;\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 * @param {Array.} requests List of strings to fetch\n * @return {jquery.Promise} A jquery promise containing an array of the translated strings\n *\n * @example Fetching a set of strings\n *\n * import {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 */\n// eslint-disable-next-line camelcase\nexport const get_strings = (requests) => {\n // We need to use jQuery here because some calling code uses the\n // .done handler instead of the .then handler.\n return $.when.apply($, getRequestedStrings(requests))\n .then((...strings) => strings);\n};\n\n/**\n * Add a list of strings to the caches.\n *\n * This function should typically only be called from core APIs to pre-cache values.\n *\n * @method cache_strings\n * @protected\n * @param {Object[]} strings List of strings to fetch\n * @param {string} strings.key The string identifer to fetch\n * @param {string} strings.value The string value\n * @param {string} [strings.component='core'] The componet to fetch from\n * @param {string} [strings.lang=Config.language] The language to fetch a string for. Defaults to current page language.\n */\n// eslint-disable-next-line camelcase\nexport const cache_strings = (strings) => {\n strings.forEach(({key, component, value, lang = Config.language}) => {\n const cacheKey = ['core_str', key, component, lang].join('/');\n\n // Check M.str caching.\n if (!(component in M.str) || !(key in M.str[component])) {\n if (!(component in M.str)) {\n M.str[component] = {};\n }\n\n M.str[component][key] = value;\n }\n\n // Check local storage.\n if (!LocalStorage.get(cacheKey)) {\n LocalStorage.set(cacheKey, value);\n }\n\n // Check the promises cache.\n if (!(cacheKey in promiseCache)) {\n promiseCache[cacheKey] = $.Deferred().resolve(value).promise();\n }\n });\n};\n/* eslint-enable no-restricted-properties */\n"],"names":["promiseCache","key","component","param","lang","get_strings","then","results","getRequestedStrings","requests","Promise","all","requestData","pageLang","Config","language","stringPromises","map","request","cacheKey","_ref","getCacheKey","buildReturn","promise","M","str","resolve","util","get_string","cached","LocalStorage","get","reject","push","methodname","args","stringid","stringparams","done","set","fail","length","call","cfg","langrev","$","when","apply","strings","forEach","_ref2","value","join","Deferred"],"mappings":";;;;;;;;;uVAwCIA,aAAe,uBAuBO,CAACC,IAAKC,UAAWC,MAAOC,OACvCC,YAAY,CAAC,CAACJ,IAAAA,IAAKC,UAAAA,UAAWC,MAAAA,MAAOC,KAAAA,QACvCE,M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wB,2BACnCc,IAAIpB,SAAUM,KAC3BC,QAAQF,EAAEG,KAAKC,WAAW3B,IAAKC,UAAWC,SAE9CqC,KAAMR,uBAMlBpB,YAAY6B,sBAGPC,KAAK9B,aAAa,GAAM,GAAO,EAAO,EAAGY,EAAEmB,IAAIC,SAGjD5B,gBAoCEX,YAAeI,UAGjBoC,gBAAEC,KAAKC,MAAMF,gBAAGrC,oBAAoBC,WACtCH,MAAK,yCAAI0C,gDAAAA,qCAAYA,mEAiBAA,UAC1BA,QAAQC,SAAQC,YAACjD,IAACA,IAADC,UAAMA,UAANiD,MAAiBA,MAAjB/C,KAAwBA,KAAOU,gBAAOC,sBAC7CI,SAAW,CAAC,WAAYlB,IAAKC,UAAWE,MAAMgD,KAAK,KAGnDlD,aAAasB,EAAEC,KAAUxB,OAAOuB,EAAEC,IAAIvB,aAClCA,aAAasB,EAAEC,MACjBD,EAAEC,IAAIvB,WAAa,IAGvBsB,EAAEC,IAAIvB,WAAWD,KAAOkD,OAIvBrB,sBAAaC,IAAIZ,iCACLoB,IAAIpB,SAAUgC,OAIzBhC,YAAYnB,eACdA,aAAamB,UAAY0B,gBAAEQ,WAAW3B,QAAQyB,OAAO5B"} \ No newline at end of file diff --git a/lib/amd/src/str.js b/lib/amd/src/str.js index a09dcd00be6..fd89c83c83e 100644 --- a/lib/amd/src/str.js +++ b/lib/amd/src/str.js @@ -153,7 +153,7 @@ const getRequestedStrings = (requests) => { // Check if we can serve the string straight from M.str. if (component in M.str && key in M.str[component]) { return buildReturn(new Promise((resolve) => { - resolve(M.util.get_string(key, component, param, lang)); + resolve(M.util.get_string(key, component, param)); })); } @@ -162,14 +162,14 @@ const getRequestedStrings = (requests) => { if (cached) { M.str[component] = {...M.str[component], [key]: cached}; return buildReturn(new Promise((resolve) => { - resolve(M.util.get_string(key, component, param, lang)); + resolve(M.util.get_string(key, component, param)); })); } // Check if we've already loaded this string from the server. if (cacheKey in promiseCache) { return buildReturn(promiseCache[cacheKey]).then(() => { - return M.util.get_string(key, component, param, lang); + return M.util.get_string(key, component, param); }); } else { // We're going to have to ask the server for the string so @@ -189,7 +189,7 @@ const getRequestedStrings = (requests) => { // local storage before resolving this promise. M.str[component] = {...M.str[component], [key]: str}; LocalStorage.set(cacheKey, str); - resolve(M.util.get_string(key, component, param, lang)); + resolve(M.util.get_string(key, component, param)); }, fail: reject });