Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"add_random_form.min.js","sources":["../src/add_random_form.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 * JavaScript for the add_random_form class.\n *\n * @module    mod_quiz/add_random_form\n * @copyright 2018 Ryan Wyllie <ryan@moodle.com>\n * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(\n    [\n        'jquery',\n        'mod_quiz/random_question_form_preview'\n    ],\n    function(\n        $,\n        RandomQuestionFormPreview\n    ) {\n\n    // Wait 2 seconds before reloading the question set just in case\n    // the user is still changing the criteria.\n    var RELOAD_DELAY = 2000;\n    var SELECTORS = {\n        PREVIEW_CONTAINER: '[data-region=\"random-question-preview-container\"]',\n        CATEGORY_FORM_ELEMENT: '[name=\"category\"]',\n        SUBCATEGORY_FORM_ELEMENT: '[name=\"includesubcategories\"]',\n        TAG_IDS_FORM_ELEMENT: '[name=\"fromtags[]\"]'\n    };\n\n    /**\n     * Get the selected category value from the form.\n     *\n     * @param {jquery} form The form element.\n     * @return {string} The category value.\n     */\n    var getCategorySelectValue = function(form) {\n        return form.find(SELECTORS.CATEGORY_FORM_ELEMENT).val();\n    };\n\n    /**\n     * Get the category id from the form.\n     *\n     * @param {jquery} form The form element.\n     * @return {string} The category id.\n     */\n    var getCategoryId = function(form) {\n        // The value string is the category id and category context id joined\n        // by a comma.\n        var valueString = getCategorySelectValue(form);\n        // Split the two ids.\n        var values = valueString.split(',');\n        // Return just the category id.\n        return values[0];\n    };\n\n    /**\n     * Check if a top level category is selected in the form.\n     *\n     * @param {jquery} form The form element.\n     * @param {string[]} topCategories List of top category values (matching the select box values)\n     * @return {bool}\n     */\n    var isTopLevelCategorySelected = function(form, topCategories) {\n        var selectedValue = getCategorySelectValue(form);\n        return (topCategories.indexOf(selectedValue) > -1);\n    };\n\n    /**\n     * Check if the form indicates we should include include subcategories in\n     * the filter.\n     *\n     * @param {jquery} form The form element.\n     * @param {string[]} topCategories List of top category values (matching the select box values)\n     * @return {bool}\n     */\n    var shouldIncludeSubcategories = function(form, topCategories) {\n        if (isTopLevelCategorySelected(form, topCategories)) {\n            return true;\n        } else {\n            return form.find(SELECTORS.SUBCATEGORY_FORM_ELEMENT).is(':checked');\n        }\n    };\n\n    /**\n     * Get the tag ids for the selected tags in the form.\n     *\n     * @param {jquery} form The form element.\n     * @return {string[]} The tag ids.\n     */\n    var getTagIds = function(form) {\n        var values = form.find(SELECTORS.TAG_IDS_FORM_ELEMENT).val();\n        return values.map(function(value) {\n            // The tag element value is the tag id and tag name joined\n            // by a comma. So we need to split them to get the tag id.\n            var parts = value.split(',');\n            return parts[0];\n        });\n    };\n\n    /**\n     * Reload the preview section with a new set of filters.\n     *\n     * @param {jquery} form The form element.\n     * @param {int} contextId The current context id.\n     * @param {string[]} topCategories List of top category values (matching the select box values)\n     */\n    var reloadQuestionPreview = function(form, contextId, topCategories) {\n        var previewContainer = form.find(SELECTORS.PREVIEW_CONTAINER);\n        RandomQuestionFormPreview.reload(\n            previewContainer,\n            getCategoryId(form),\n            shouldIncludeSubcategories(form, topCategories),\n            getTagIds(form),\n            contextId\n        );\n    };\n\n    /**\n     * Is this an element we're interested in listening to changes on.\n     *\n     * @param {jquery} element The element to check.\n     * @return {bool}\n     */\n    var isInterestingElement = function(element) {\n        if (element.closest(SELECTORS.CATEGORY_FORM_ELEMENT).length > 0) {\n            return true;\n        }\n\n        if (element.closest(SELECTORS.SUBCATEGORY_FORM_ELEMENT).length > 0) {\n            return true;\n        }\n\n        if (element.closest(SELECTORS.TAG_IDS_FORM_ELEMENT).length > 0) {\n            return true;\n        }\n\n        return false;\n    };\n\n    /**\n     * Listen for changes to any of the interesting elements and reload the form\n     * preview with the new filter values if they are changed.\n     *\n     * The reload is delayed for a small amount of time (see RELOAD_DELAY) in case\n     * the user is actively editing the form. This allows us to avoid having to\n     * send multiple requests to the server on each change.\n     *\n     * Instead we can just send a single request when the user appears to have\n     * finished editing the form.\n     *\n     * @param {jquery} form The form element.\n     * @param {int} contextId The current context id.\n     * @param {string[]} topCategories List of top category values (matching the select box values)\n     */\n    var addEventListeners = function(form, contextId, topCategories) {\n        var reloadTimerId = null;\n        const tagsFilter = form.find(SELECTORS.TAG_IDS_FORM_ELEMENT);\n\n        form.add(tagsFilter).on('change', function(e) {\n            // Only reload the preview when elements that will change the result\n            // are modified.\n            if (!isInterestingElement($(e.target))) {\n                return;\n            }\n\n            // Show the loading icon to let the user know that the preview\n            // will be updated after their actions.\n            RandomQuestionFormPreview.showLoadingIcon(form);\n\n            if (reloadTimerId) {\n                // Reset the timer each time the form is modified.\n                clearTimeout(reloadTimerId);\n            }\n\n            // Don't immediately reload the question preview section just\n            // in case the user is still modifying the form. We don't want to\n            // spam reload requests.\n            reloadTimerId = setTimeout(function() {\n                reloadQuestionPreview(form, contextId, topCategories);\n            }, RELOAD_DELAY);\n        });\n    };\n\n    /**\n     * Trigger the first load of the preview section and then listen for modifications\n     * to the form to reload the preview with new filter values.\n     *\n     * @param {jquery} formId The form element id.\n     * @param {int} contextId The current context id.\n     * @param {string[]} topCategories List of top category values (matching the select box values)\n     * @param {bool} isTagsEnabled Whether tags feature is enabled or not.\n     */\n    var init = function(formId, contextId, topCategories, isTagsEnabled) {\n         if (isTagsEnabled == true) {\n             var form = $('#' + formId);\n             reloadQuestionPreview(form, contextId, topCategories, isTagsEnabled);\n             addEventListeners(form, contextId, topCategories, isTagsEnabled);\n         }\n    };\n\n    return {\n        init: init\n    };\n});\n"],"names":["define","$","RandomQuestionFormPreview","SELECTORS","getCategorySelectValue","form","find","val","shouldIncludeSubcategories","topCategories","selectedValue","indexOf","isTopLevelCategorySelected","is","reloadQuestionPreview","contextId","previewContainer","reload","split","getCategoryId","map","value","getTagIds","addEventListeners","reloadTimerId","tagsFilter","add","on","e","element","target","closest","length","showLoadingIcon","clearTimeout","setTimeout","init","formId","isTagsEnabled"],"mappings":";;;;;;;AAsBAA,kCACI,CACI,SACA,0CAEJ,SACIC,EACAC,+BAMAC,4BACmB,oDADnBA,gCAEuB,oBAFvBA,mCAG0B,gCAH1BA,+BAIsB,sBAStBC,uBAAyB,SAASC,aAC3BA,KAAKC,KAAKH,iCAAiCI,OAuClDC,2BAA6B,SAASH,KAAMI,uBAbf,SAASJ,KAAMI,mBACxCC,cAAgBN,uBAAuBC,aACnCI,cAAcE,QAAQD,gBAAkB,EAY5CE,CAA2BP,KAAMI,gBAG1BJ,KAAKC,KAAKH,oCAAoCU,GAAG,aA2B5DC,sBAAwB,SAAST,KAAMU,UAAWN,mBAC9CO,iBAAmBX,KAAKC,KAAKH,6BACjCD,0BAA0Be,OACtBD,iBAhEY,SAASX,aAGPD,uBAAuBC,MAEhBa,MAAM,KAEjB,GA0DVC,CAAcd,MACdG,2BAA2BH,KAAMI,eAtBzB,SAASJ,aACRA,KAAKC,KAAKH,gCAAgCI,MACzCa,KAAI,SAASC,cAGXA,MAAMH,MAAM,KACX,MAiBbI,CAAUjB,MACVU,YAyCJQ,kBAAoB,SAASlB,KAAMU,UAAWN,mBAC1Ce,cAAgB,WACdC,WAAapB,KAAKC,KAAKH,gCAE7BE,KAAKqB,IAAID,YAAYE,GAAG,UAAU,SAASC,GAnCpB,IAASC,UAAAA,QAsCF5B,EAAE2B,EAAEE,SArCtBC,QAAQ5B,iCAAiC6B,OAAS,GAI1DH,QAAQE,QAAQ5B,oCAAoC6B,OAAS,GAI7DH,QAAQE,QAAQ5B,gCAAgC6B,OAAS,KAmCzD9B,0BAA0B+B,gBAAgB5B,MAEtCmB,eAEAU,aAAaV,eAMjBA,cAAgBW,YAAW,WACvBrB,sBAAsBT,KAAMU,UAAWN,iBA7JhC,gBAmLZ,CACH2B,KATO,SAASC,OAAQtB,UAAWN,cAAe6B,kBAC5B,GAAjBA,cAAuB,KACnBjC,KAAOJ,EAAE,IAAMoC,QACnBvB,sBAAsBT,KAAMU,UAAWN,eACvCc,kBAAkBlB,KAAMU,UAAWN"}