MDL-69454 core_search: consistent template library search
[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 47 var componentStr = $('[data-field="component"]').val();
661d6d7d 48 var searchStr = $('[data-region="list-templates"] [data-region="input"]').val();
274d79c9 49
661d6d7d
BB
50 if (searchStr !== '') {
51 $('[data-region="list-templates"] [data-action="clearsearch"]').removeClass('d-none');
52 } else {
53 $('[data-region="list-templates"] [data-action="clearsearch"]').addClass('d-none');
54 }
274d79c9 55 // Trigger the search.
207136f2
DW
56 document.location.hash = searchStr;
57
274d79c9 58 ajax.call([
9f5f3dcc 59 {methodname: 'tool_templatelibrary_list_templates',
a11c74d1 60 args: {component: componentStr, search: searchStr, themename: themename},
274d79c9 61 done: reloadListTemplate,
9f5f3dcc 62 fail: notification.exception}
ba224fb4 63 ], true, false);
274d79c9
DW
64 };
65
66 var throttle = null;
67
68 /**
69 * Call the specified function after a delay. If this function is called again before the function is executed,
70 * the function will only be executed once.
71 *
72 * @method queueRefresh
c96f55e6
DP
73 * @param {function} callback
74 * @param {Number} delay The time in milliseconds to delay.
274d79c9
DW
75 */
76 var queueRefresh = function(callback, delay) {
77 if (throttle !== null) {
78 window.clearTimeout(throttle);
79 }
80
81 throttle = window.setTimeout(function() {
82 callback();
83 throttle = null;
84 }, delay);
85 };
86
87 var changeHandler = function() {
a11c74d1 88 queueRefresh(refreshSearch.bind(this, config.theme), 400);
274d79c9
DW
89 };
90 // Add change handlers to refresh the list.
91 $('[data-region="list-templates"]').on('change', '[data-field="component"]', changeHandler);
661d6d7d
BB
92 $('[data-region="list-templates"]').on('input', '[data-region="input"]', changeHandler);
93 $('[data-action="clearsearch"]').on('click', function() {
94 $('[data-region="input"]').val('');
95 refreshSearch(config.theme);
96 $(this).addClass('d-none');
97 });
274d79c9 98
661d6d7d 99 $('[data-region="input"]').val(document.location.hash.replace('#', ''));
a11c74d1 100 refreshSearch(config.theme);
274d79c9
DW
101 return {};
102});