Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"mustache.min.js","sources":["../src/mustache.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 * Render mustache template examples within the component library.\n *\n * @module     tool_componentlibrary/mustache\n * @copyright  2021 Bas Brands <bas@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport selectors from 'tool_componentlibrary/selectors';\nimport Ajax from 'core/ajax';\nimport Config from 'core/config';\nimport Templates from 'core/templates';\nimport Log from 'core/log';\nimport Notification from 'core/notification';\n\n/**\n * Handle a template loaded response.\n *\n * @method\n * @private\n * @param {String} container The template container\n * @param {String} templateName The template name\n * @param {String} context Data for the template.\n */\nconst renderTemplate = async(container, templateName, context) => {\n    try {\n        context = JSON.parse(context);\n    } catch (e) {\n        Log.debug('Could not parse json example context for template.');\n        Log.debug(e);\n    }\n\n    const {html, js} = await Templates.renderForPromise(templateName, context);\n\n    const rendercontainer = container.querySelector(selectors.mustacherendered);\n\n    // Load the rendered content in the renderer tab.\n    await Templates.replaceNodeContents(rendercontainer, html, js);\n};\n\n/**\n * Load the a template source from Moodle.\n *\n * @method\n * @private\n * @param {String} container The template container\n */\nconst loadTemplate = container => {\n        const sourcecontainer = container.querySelector(selectors.mustachesource);\n        const contextcontainer = container.querySelector(selectors.mustachecontext);\n        const templateName = container.dataset.template;\n        let context = container.querySelector(selectors.mustacherawcontext).textContent;\n\n        const parts = templateName.split('/');\n        const component = parts.shift();\n        const name = parts.join('/');\n\n        const request = {\n            methodname: 'core_output_load_template',\n            args: {\n                component: component,\n                template: name,\n                themename: Config.theme,\n                includecomments: true\n            }\n        };\n\n        Ajax.call([request])[0]\n            .done((source) => {\n                // Load the source template in Template tab.\n                sourcecontainer.textContent = source;\n                if (!context) {\n                    const example = source.match(/Example context \\(json\\):([\\s\\S]+?)(}})/);\n                    context = example[1];\n                    // Load the variables in the Variables tab.\n                    const precontainer = document.createElement(\"pre\");\n                    precontainer.innerHTML = JSON.stringify(JSON.parse(context), null, 4);\n                    contextcontainer.parentNode.appendChild(precontainer);\n                    contextcontainer.classList.add('d-none');\n                }\n                renderTemplate(container, templateName, context);\n            })\n            .fail(Notification.exception);\n};\n\n/**\n * Initialize the module.\n *\n * @method\n */\nexport const mustache = () => {\n    document.querySelectorAll(selectors.mustachecode).forEach((container) => {\n        loadTemplate(container);\n    });\n};\n"],"names":["loadTemplate","container","sourcecontainer","querySelector","selectors","mustachesource","contextcontainer","mustachecontext","templateName","dataset","template","context","mustacherawcontext","textContent","parts","split","request","methodname","args","component","shift","join","themename","Config","theme","includecomments","call","done","source","example","match","precontainer","document","createElement","innerHTML","JSON","stringify","parse","parentNode","appendChild","classList","add","async","e","debug","html","js","Templates","renderForPromise","rendercontainer","mustacherendered","replaceNodeContents","renderTemplate","fail","Notification","exception","querySelectorAll","mustachecode","forEach"],"mappings":";;;;;;;0VA8DMA,aAAeC,kBACPC,gBAAkBD,UAAUE,cAAcC,mBAAUC,gBACpDC,iBAAmBL,UAAUE,cAAcC,mBAAUG,iBACrDC,aAAeP,UAAUQ,QAAQC,aACnCC,QAAUV,UAAUE,cAAcC,mBAAUQ,oBAAoBC,kBAE9DC,MAAQN,aAAaO,MAAM,KAI3BC,QAAU,CACZC,WAAY,4BACZC,KAAM,CACFC,UANUL,MAAMM,QAOhBV,SANKI,MAAMO,KAAK,KAOhBC,UAAWC,gBAAOC,MAClBC,iBAAiB,kBAIpBC,KAAK,CAACV,UAAU,GAChBW,MAAMC,YAEH1B,gBAAgBW,YAAce,QACzBjB,QAAS,OACJkB,QAAUD,OAAOE,MAAM,2CAC7BnB,QAAUkB,QAAQ,SAEZE,aAAeC,SAASC,cAAc,OAC5CF,aAAaG,UAAYC,KAAKC,UAAUD,KAAKE,MAAM1B,SAAU,KAAM,GACnEL,iBAAiBgC,WAAWC,YAAYR,cACxCzB,iBAAiBkC,UAAUC,IAAI,UAtD5BC,OAAMzC,UAAWO,aAAcG,eAE9CA,QAAUwB,KAAKE,MAAM1B,SACvB,MAAOgC,gBACDC,MAAM,mEACNA,MAAMD,SAGRE,KAACA,KAADC,GAAOA,UAAYC,mBAAUC,iBAAiBxC,aAAcG,SAE5DsC,gBAAkBhD,UAAUE,cAAcC,mBAAU8C,wBAGpDH,mBAAUI,oBAAoBF,gBAAiBJ,KAAMC,KA2C/CM,CAAenD,UAAWO,aAAcG,YAE3C0C,KAAKC,sBAAaC,8BAQP,KACpBvB,SAASwB,iBAAiBpD,mBAAUqD,cAAcC,SAASzD,YACvDD,aAAaC"}