Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"select_page.min.js","sources":["../src/select_page.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 * When returning to Moodle let the user select which course to add the resource to.\n *\n * @module     tool_moodlenet/select_page\n * @copyright  2020 Mathew May <mathew.solutions>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\ndefine([\n    'core/ajax',\n    'core/templates',\n    'tool_moodlenet/selectors',\n    'core/notification'\n], function(\n    Ajax,\n    Templates,\n    Selectors,\n    Notification\n) {\n    /**\n     * @var {string} The id corresponding to the import.\n     */\n    var importId;\n\n    /**\n     * Set up the page.\n     *\n     * @method init\n     * @param {string} importIdString the string ID of the import.\n     */\n    var init = function(importIdString) {\n        importId = importIdString;\n        var page = document.querySelector(Selectors.region.selectPage);\n        registerListenerEvents(page);\n        addCourses(page);\n    };\n\n    /**\n     * Renders the 'no-courses' template.\n     *\n     * @param {HTMLElement} areaReplace the DOM node to replace.\n     * @returns {Promise}\n     */\n    var renderNoCourses = function(areaReplace) {\n        return Templates.renderPix('courses', 'tool_moodlenet').then(function(img) {\n            return img;\n        }).then(function(img) {\n            var temp = document.createElement('div');\n            temp.innerHTML = img.trim();\n            return Templates.render('core_course/no-courses', {\n                nocoursesimg: temp.firstChild.src\n            });\n        }).then(function(html, js) {\n            Templates.replaceNodeContents(areaReplace, html, js);\n            areaReplace.classList.add('mx-auto');\n            areaReplace.classList.add('w-25');\n            return;\n        });\n    };\n\n    /**\n     * Render the course cards for those supplied courses.\n     *\n     * @param {HTMLElement} areaReplace the DOM node to replace.\n     * @param {Array<courses>} courses the courses to render.\n     * @returns {Promise}\n     */\n    var renderCourses = function(areaReplace, courses) {\n        return Templates.render('tool_moodlenet/view-cards', {\n            courses: courses\n        }).then(function(html, js) {\n            Templates.replaceNodeContents(areaReplace, html, js);\n            areaReplace.classList.remove('mx-auto');\n            areaReplace.classList.remove('w-25');\n            return;\n        });\n    };\n\n    /**\n     * For a given input, the page & what to replace fetch courses and manage icons too.\n     *\n     * @method searchCourses\n     * @param {string} inputValue What to search for\n     * @param {HTMLElement} page The whole page element for our page\n     * @param {HTMLElement} areaReplace The Element to replace the contents of\n     */\n    var searchCourses = function(inputValue, page, areaReplace) {\n        var searchIcon = page.querySelector(Selectors.region.searchIcon);\n        var clearIcon = page.querySelector(Selectors.region.clearIcon);\n\n        if (inputValue !== '') {\n            searchIcon.classList.add('d-none');\n            clearIcon.parentElement.classList.remove('d-none');\n        } else {\n            searchIcon.classList.remove('d-none');\n            clearIcon.parentElement.classList.add('d-none');\n        }\n        var args = {\n            searchvalue: inputValue,\n        };\n        Ajax.call([{\n            methodname: 'tool_moodlenet_search_courses',\n            args: args\n        }])[0].then(function(result) {\n            if (result.courses.length === 0) {\n                return renderNoCourses(areaReplace);\n            } else {\n                // Add the importId to the course link\n                result.courses.forEach(function(course) {\n                    course.viewurl += '&id=' + importId;\n                });\n                return renderCourses(areaReplace, result.courses);\n            }\n        }).catch(Notification.exception);\n    };\n\n    /**\n     * Add the event listeners to our page.\n     *\n     * @method registerListenerEvents\n     * @param {HTMLElement} page The whole page element for our page\n     */\n    var registerListenerEvents = function(page) {\n        var input = page.querySelector(Selectors.region.searchInput);\n        var courseArea = page.querySelector(Selectors.region.courses);\n        var clearIcon = page.querySelector(Selectors.region.clearIcon);\n        clearIcon.addEventListener('click', function() {\n            input.value = '';\n            searchCourses('', page, courseArea);\n        });\n\n        input.addEventListener('input', debounce(function() {\n            searchCourses(input.value, page, courseArea);\n        }, 300));\n    };\n\n    /**\n     * Fetch the courses to show the user. We use the same WS structure & template as the search for consistency.\n     *\n     * @method addCourses\n     * @param {HTMLElement} page The whole page element for our course page\n     */\n    var addCourses = function(page) {\n        var courseArea = page.querySelector(Selectors.region.courses);\n        searchCourses('', page, courseArea);\n    };\n\n    /**\n     * Define our own debounce function as Moodle 3.7 does not have it.\n     *\n     * @method debounce\n     * @from underscore.js\n     * @copyright 2009-2020 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors\n     * @licence MIT\n     * @param {function} func The function we want to keep calling\n     * @param {number} wait Our timeout\n     * @param {boolean} immediate Do we want to apply the function immediately\n     * @return {function}\n     */\n    var debounce = function(func, wait, immediate) {\n        var timeout;\n        return function() {\n            var context = this;\n            var args = arguments;\n            var later = function() {\n                timeout = null;\n                if (!immediate) {\n                    func.apply(context, args);\n                }\n            };\n            var callNow = immediate && !timeout;\n            clearTimeout(timeout);\n            timeout = setTimeout(later, wait);\n            if (callNow) {\n                func.apply(context, args);\n            }\n        };\n    };\n    return {\n        init: init,\n    };\n});\n"],"names":["define","Ajax","Templates","Selectors","Notification","importId","searchCourses","inputValue","page","areaReplace","searchIcon","querySelector","region","clearIcon","classList","add","parentElement","remove","args","searchvalue","call","methodname","then","result","courses","length","renderPix","img","temp","document","createElement","innerHTML","trim","render","nocoursesimg","firstChild","src","html","js","replaceNodeContents","renderNoCourses","forEach","course","viewurl","renderCourses","catch","exception","registerListenerEvents","input","searchInput","courseArea","addEventListener","value","debounce","addCourses","func","wait","immediate","timeout","context","this","arguments","later","apply","callNow","clearTimeout","setTimeout","init","importIdString","selectPage"],"mappings":";;;;;;;AAuBAA,oCAAO,CACH,YACA,iBACA,2BACA,sBACD,SACCC,KACAC,UACAC,UACAC,kBAKIC,SAgEAC,cAAgB,SAASC,WAAYC,KAAMC,iBACvCC,WAAaF,KAAKG,cAAcR,UAAUS,OAAOF,YACjDG,UAAYL,KAAKG,cAAcR,UAAUS,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