Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * @copyright  2015 Damyon Wiese <damyon@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates', 'core/config'],
24
       function($, ajax, log, notification, templates, config) {
25
 
26
    /**
27
     * The ajax call has returned with a new list of templates.
28
     *
29
     * @method reloadListTemplate
30
     * @param {String[]} templateList List of template ids.
31
     */
32
    var reloadListTemplate = function(templateList) {
33
        templates.render('tool_templatelibrary/search_results', {templates: templateList})
34
            .done(function(result, js) {
35
                templates.replaceNode($('[data-region="searchresults"]'), result, js);
36
            }).fail(notification.exception);
37
    };
38
 
39
    /**
40
     * Get the current values for the form inputs and refresh the list of matching templates.
41
     *
42
     * @method refreshSearch
43
     * @param {String} themename The naeme of the theme.
44
     */
45
    var refreshSearch = function(themename) {
46
        var componentStr = $('[data-field="component"]').val();
47
        var searchStr = $('[data-region="list-templates"] [data-region="input"]').val();
48
 
49
        if (searchStr !== '') {
50
            $('[data-region="list-templates"] [data-action="clearsearch"]').removeClass('d-none');
51
        } else {
52
            $('[data-region="list-templates"] [data-action="clearsearch"]').addClass('d-none');
53
        }
54
 
55
        // Trigger the search.
56
        ajax.call([
57
            {methodname: 'tool_templatelibrary_list_templates',
58
              args: {component: componentStr, search: searchStr, themename: themename},
59
              done: reloadListTemplate,
60
              fail: notification.exception}
61
        ], true, false);
62
    };
63
 
64
    var throttle = null;
65
 
66
    /**
67
     * Call the specified function after a delay. If this function is called again before the function is executed,
68
     * the function will only be executed once.
69
     *
70
     * @method queueRefresh
71
     * @param {function} callback
72
     * @param {Number} delay The time in milliseconds to delay.
73
     */
74
    var queueRefresh = function(callback, delay) {
75
        if (throttle !== null) {
76
            window.clearTimeout(throttle);
77
        }
78
 
79
        throttle = window.setTimeout(function() {
80
            callback();
81
            throttle = null;
82
        }, delay);
83
    };
84
 
85
    var changeHandler = function() {
86
        queueRefresh(refreshSearch.bind(this, config.theme), 400);
87
    };
88
    // Add change handlers to refresh the list.
89
    $('[data-region="list-templates"]').on('change', '[data-field="component"]', changeHandler);
90
    $('[data-region="list-templates"]').on('input', '[data-region="input"]', changeHandler);
91
    $('[data-action="clearsearch"]').on('click', function() {
92
        $('[data-region="input"]').val('');
93
        refreshSearch(config.theme);
94
        $(this).addClass('d-none');
95
    });
96
 
97
    refreshSearch(config.theme);
98
    return {};
99
});