Merge branch 'wip-MDL-54986-master' of https://github.com/Beedell/moodle
[moodle.git] / admin / tool / templatelibrary / amd / src / search.js
CommitLineData
274d79c9
DW
1// This file is part of Moodle - http://moodle.org/
2//
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.
7//
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.
12//
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/>.
15
16/**
17 * This module adds ajax search functions to the template library page.
18 *
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
23 */
a11c74d1
DW
24define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates', 'core/config'],
25 function($, ajax, log, notification, templates, config) {
274d79c9
DW
26
27 /**
28 * The ajax call has returned with a new list of templates.
29 *
30 * @method reloadListTemplate
c96f55e6 31 * @param {String[]} templateList List of template ids.
274d79c9
DW
32 */
33 var reloadListTemplate = function(templateList) {
9f5f3dcc 34 templates.render('tool_templatelibrary/search_results', {templates: templateList})
35be5826 35 .done(function(result, js) {
28de7771 36 templates.replaceNode($('[data-region="searchresults"]'), result, js);
274d79c9
DW
37 }).fail(notification.exception);
38 };
39
40 /**
41 * Get the current values for the form inputs and refresh the list of matching templates.
42 *
43 * @method refreshSearch
44 */
a11c74d1 45 var refreshSearch = function(themename) {
274d79c9
DW
46 var componentStr = $('[data-field="component"]').val();
47 var searchStr = $('[data-field="search"]').val();
48
49 // Trigger the search.
274d79c9 50 ajax.call([
9f5f3dcc 51 {methodname: 'tool_templatelibrary_list_templates',
a11c74d1 52 args: {component: componentStr, search: searchStr, themename: themename},
274d79c9 53 done: reloadListTemplate,
9f5f3dcc 54 fail: notification.exception}
ba224fb4 55 ], true, false);
274d79c9
DW
56 };
57
58 var throttle = null;
59
60 /**
61 * Call the specified function after a delay. If this function is called again before the function is executed,
62 * the function will only be executed once.
63 *
64 * @method queueRefresh
c96f55e6
DP
65 * @param {function} callback
66 * @param {Number} delay The time in milliseconds to delay.
274d79c9
DW
67 */
68 var queueRefresh = function(callback, delay) {
69 if (throttle !== null) {
70 window.clearTimeout(throttle);
71 }
72
73 throttle = window.setTimeout(function() {
74 callback();
75 throttle = null;
76 }, delay);
77 };
78
79 var changeHandler = function() {
a11c74d1 80 queueRefresh(refreshSearch.bind(this, config.theme), 400);
274d79c9
DW
81 };
82 // Add change handlers to refresh the list.
83 $('[data-region="list-templates"]').on('change', '[data-field="component"]', changeHandler);
84 $('[data-region="list-templates"]').on('input', '[data-field="search"]', changeHandler);
85
a11c74d1 86 refreshSearch(config.theme);
274d79c9
DW
87 return {};
88});