Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"prefetch.min.js","sources":["../src/prefetch.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 * Prefetch module to help lazily load content for use on the current page.\n *\n * @module     core/prefetch\n * @copyright  2020 Andrew Nicols <andrew@nicols.co.uk>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n *\n * @example <caption>Pre-fetching a set of strings to use later</caption>\n *\n * import prefetch from 'core/prefetch';\n *\n * // A single string prefetch.\n * prefetch.prefetchString('error', 'cannotfindteacher');\n *\n * // Prefetch multiple strings in the same component.\n * prefetch.prefetchStrings('core', [\n *     'yes',\n *     'no',\n * ]);\n *\n * // Use the strings.\n * import {getString, getStrings} from 'core/str';\n * getString('cannotfindteacher', 'error')\n * .then(str => {\n *     window.console.log(str); // Cannot find teacher\n * })\n * .catch();\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 * .catch();\n */\nimport Config from 'core/config';\n\n// Keep track of whether the initial prefetch has occurred.\nlet initialPrefetchComplete = false;\n\n// Prefetch templates.\nlet templateList = [];\n\n// Prefetch strings.\nlet stringList = {};\n\nlet prefetchTimer;\n\n/**\n * Fetch all queued items in the queue.\n *\n * Should only be called via processQueue.\n * @private\n */\nconst fetchQueue = () => {\n    // Prefetch templates.\n    if (templateList) {\n        const templatesToLoad = templateList.slice();\n        templateList = [];\n        import('core/templates')\n        .then(Templates => Templates.prefetchTemplates(templatesToLoad))\n        .catch();\n    }\n\n    // Prefetch strings.\n    const mappedStringsToFetch = stringList;\n    stringList = {};\n\n    const stringsToFetch = [];\n    Object.keys(mappedStringsToFetch).forEach(component => {\n        stringsToFetch.push(...mappedStringsToFetch[component].map(key => {\n            return {component, key};\n        }));\n    });\n\n    if (stringsToFetch) {\n        import('core/str')\n        .then(Str => Str.get_strings(stringsToFetch))\n        .catch();\n    }\n};\n\n/**\n * Process the prefetch queues as required.\n *\n * The initial call will queue the first fetch after a delay.\n * Subsequent fetches are immediate.\n *\n * @private\n */\nconst processQueue = () => {\n    if (prefetchTimer) {\n        // There is a live prefetch timer. The initial prefetch has been scheduled but is not complete.\n        return;\n    }\n\n    // The initial prefetch has compelted. Just queue as normal.\n    if (initialPrefetchComplete) {\n        fetchQueue();\n\n        return;\n    }\n\n    // Queue the initial prefetch in a short while.\n    prefetchTimer = setTimeout(() => {\n        initialPrefetchComplete = true;\n        prefetchTimer = null;\n\n        // Ensure that the icon system is loaded.\n        // This can be quite slow and delay UI interactions if it is loaded on demand.\n        import(Config.iconsystemmodule)\n        .then(IconSystem => {\n            const iconSystem = new IconSystem();\n            prefetchTemplate(iconSystem.getTemplateName());\n\n            return iconSystem;\n        })\n        .then(iconSystem => {\n            fetchQueue();\n            iconSystem.init();\n\n            return;\n        })\n        .catch();\n    }, 500);\n};\n\n/**\n * Add a set of templates to the prefetch queue.\n *\n * @param {Array} templatesNames A list of the template names to fetch\n * @static\n */\nconst prefetchTemplates = templatesNames => {\n    templateList = templateList.concat(templatesNames);\n\n    processQueue();\n};\n\n/**\n * Add a single template to the prefetch queue.\n *\n * @param {String} templateName The template names to fetch\n * @static\n */\nconst prefetchTemplate = templateName => {\n    prefetchTemplates([templateName]);\n};\n\n/**\n * Add a set of strings from the same component to the prefetch queue.\n *\n * @param {String} component The component that all of the strings belongs to\n * @param {String[]} keys An array of string identifiers.\n * @static\n */\nconst prefetchStrings = (component, keys) => {\n    if (!stringList[component]) {\n        stringList[component] = [];\n    }\n\n    stringList[component] = stringList[component].concat(keys);\n\n    processQueue();\n};\n\n/**\n * Add a single string to the prefetch queue.\n *\n * @param {String} component The component that the string belongs to\n * @param {String} key The string identifier\n * @static\n */\nconst prefetchString = (component, key) => {\n    if (!stringList[component]) {\n        stringList[component] = [];\n    }\n\n    stringList[component].push(key);\n\n    processQueue();\n};\n\n// Prefetch some commonly-used templates.\nprefetchTemplates([].concat(\n    ['core/loading'],\n    ['core/modal'],\n    ['core/modal_backdrop'],\n));\n\n// And some commonly used strings.\nprefetchStrings('core', [\n    'cancel',\n    'closebuttontitle',\n    'loading',\n    'savechanges',\n]);\nprefetchStrings('core_form', [\n    'showless',\n    'showmore',\n]);\n\nexport default {\n    prefetchTemplate,\n    prefetchTemplates,\n    prefetchString,\n    prefetchStrings,\n};\n"],"names":["prefetchTimer","initialPrefetchComplete","templateList","stringList","fetchQueue","templatesToLoad","slice","then","Templates","prefetchTemplates","catch","mappedStringsToFetch","stringsToFetch","Object","keys","forEach","component","push","map","key","Str","get_strings","processQueue","setTimeout","Config","iconsystemmodule","IconSystem","iconSystem","prefetchTemplate","getTemplateName","init","templatesNames","concat","templateName","prefetchStrings","prefetchString"],"mappings":"mXA0EIA,cARAC,yBAA0B,EAG1BC,aAAe,GAGfC,WAAa,SAUXC,WAAa,QAEXF,aAAc,OACRG,gBAAkBH,aAAaI,QACrCJ,aAAe,gmBAEdK,MAAKC,WAAaA,UAAUC,kBAAkBJ,mBAC9CK,cAICC,qBAAuBR,WAC7BA,WAAa,SAEPS,eAAiB,GACvBC,OAAOC,KAAKH,sBAAsBI,SAAQC,YACtCJ,eAAeK,QAAQN,qBAAqBK,WAAWE,KAAIC,MAChD,CAACH,UAAAA,UAAWG,IAAAA,YAIvBP,2lBAECL,MAAKa,KAAOA,IAAIC,YAAYT,kBAC5BF,SAYHY,aAAe,KACbtB,gBAMAC,wBACAG,aAMJJ,cAAgBuB,YAAW,KACvBtB,yBAA0B,EAC1BD,cAAgB,sNAITwB,gBAAOC,4SAAPD,gBAAP,4EAAOA,gBAAOC,oBACblB,MAAKmB,mBACIC,WAAa,IAAID,kBACvBE,iBAAiBD,WAAWE,mBAErBF,cAEVpB,MAAKoB,aACFvB,aACAuB,WAAWG,UAIdpB,UACF,OASDD,kBAAoBsB,iBACtB7B,aAAeA,aAAa8B,OAAOD,gBAEnCT,gBASEM,iBAAmBK,eACrBxB,kBAAkB,CAACwB,gBAUjBC,gBAAkB,CAAClB,UAAWF,QAC3BX,WAAWa,aACZb,WAAWa,WAAa,IAG5Bb,WAAWa,WAAab,WAAWa,WAAWgB,OAAOlB,MAErDQ,gBAqBJb,kBAAkB,GAAGuB,OACjB,CAAC,gBACD,CAAC,cACD,CAAC,yBAILE,gBAAgB,OAAQ,CACpB,SACA,mBACA,UACA,gBAEJA,gBAAgB,YAAa,CACzB,WACA,0BAGW,CACXN,iBAAAA,iBACAnB,kBAAAA,kBACA0B,eAhCmB,CAACnB,UAAWG,OAC1BhB,WAAWa,aACZb,WAAWa,WAAa,IAG5Bb,WAAWa,WAAWC,KAAKE,KAE3BG,gBA0BAY,gBAAAA"}