1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * This module adds ajax search functions to the template library page.
19 * @module tool_templatelibrary/search
20 * @package tool_templatelibrary
21 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates', 'core/config'],
25 function($, ajax, log, notification, templates, config) {
28 * The ajax call has returned with a new list of templates.
30 * @method reloadListTemplate
31 * @param {String[]} templateList List of template ids.
33 var reloadListTemplate = function(templateList) {
34 templates.render('tool_templatelibrary/search_results', {templates: templateList})
35 .done(function(result, js) {
36 templates.replaceNode($('[data-region="searchresults"]'), result, js);
37 }).fail(notification.exception);
41 * Get the current values for the form inputs and refresh the list of matching templates.
43 * @method refreshSearch
44 * @param {String} themename The naeme of the theme.
46 var refreshSearch = function(themename) {
47 var componentStr = $('[data-field="component"]').val();
48 var searchStr = $('[data-field="search"]').val();
50 // Trigger the search.
51 document.location.hash = searchStr;
54 {methodname: 'tool_templatelibrary_list_templates',
55 args: {component: componentStr, search: searchStr, themename: themename},
56 done: reloadListTemplate,
57 fail: notification.exception}
64 * Call the specified function after a delay. If this function is called again before the function is executed,
65 * the function will only be executed once.
67 * @method queueRefresh
68 * @param {function} callback
69 * @param {Number} delay The time in milliseconds to delay.
71 var queueRefresh = function(callback, delay) {
72 if (throttle !== null) {
73 window.clearTimeout(throttle);
76 throttle = window.setTimeout(function() {
82 var changeHandler = function() {
83 queueRefresh(refreshSearch.bind(this, config.theme), 400);
85 // Add change handlers to refresh the list.
86 $('[data-region="list-templates"]').on('change', '[data-field="component"]', changeHandler);
87 $('[data-region="list-templates"]').on('input', '[data-field="search"]', changeHandler);
89 $('[data-field="search"]').val(document.location.hash.replace('#', ''));
90 refreshSearch(config.theme);