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