MDL-57197 js: fix AMD module warnings
[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
ce7149b1 44 * @param {String} themename The naeme of the theme.
274d79c9 45 */
a11c74d1 46 var refreshSearch = function(themename) {
274d79c9
DW
47 var componentStr = $('[data-field="component"]').val();
48 var searchStr = $('[data-field="search"]').val();
49
50 // Trigger the search.
207136f2
DW
51 document.location.hash = searchStr;
52
274d79c9 53 ajax.call([
9f5f3dcc 54 {methodname: 'tool_templatelibrary_list_templates',
a11c74d1 55 args: {component: componentStr, search: searchStr, themename: themename},
274d79c9 56 done: reloadListTemplate,
9f5f3dcc 57 fail: notification.exception}
ba224fb4 58 ], true, false);
274d79c9
DW
59 };
60
61 var throttle = null;
62
63 /**
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.
66 *
67 * @method queueRefresh
c96f55e6
DP
68 * @param {function} callback
69 * @param {Number} delay The time in milliseconds to delay.
274d79c9
DW
70 */
71 var queueRefresh = function(callback, delay) {
72 if (throttle !== null) {
73 window.clearTimeout(throttle);
74 }
75
76 throttle = window.setTimeout(function() {
77 callback();
78 throttle = null;
79 }, delay);
80 };
81
82 var changeHandler = function() {
a11c74d1 83 queueRefresh(refreshSearch.bind(this, config.theme), 400);
274d79c9
DW
84 };
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);
88
207136f2 89 $('[data-field="search"]').val(document.location.hash.replace('#', ''));
a11c74d1 90 refreshSearch(config.theme);
274d79c9
DW
91 return {};
92});