Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * Handles filtering of items on download center page
3
 *
4
 * @module        local_downloadcenter/modfilter
5
 * @author        Simeon Naydenov (moniNaydenov@gmail.com)
6
 * @copyright     2022 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
7
 * @license       http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
8
 */
9
 
10
/**
11
 * @module local_downloadcenter/modfilter
12
 */
13
define(['jquery', 'core/str', 'core/url'], function($, Str, url) {
14
 
15
    const ModFilter = function(modnames) {
16
 
17
        const instance = this;
18
        this.modnames = modnames;
19
        this.strings = {};
20
        this.formid = null;
21
        this.currentlyshown = false;
22
        this.modlist = null;
23
 
24
        Str.get_strings([
25
            {key: 'all', component: 'moodle'},
26
            {key: 'none', component: 'moodle'},
27
            {key: 'select', component: 'moodle'},
28
            {key: 'showtypes', component: 'backup'},
29
            {key: 'hidetypes', component: 'backup'}
30
        ]).done(function(strs) {
31
            // Init strings.. new moodle super cool way...
32
            instance.strings['all'] = strs[0];
33
            instance.strings['none'] = strs[1];
34
            instance.strings['select'] = strs[2];
35
            instance.strings['showtypes'] = strs[3];
36
            instance.strings['hidetypes'] = strs[4];
37
 
38
            const firstsection = $('div[role="main"] > form .card.block').first();
39
            instance.formid = firstsection.closest('form').prop('id');
40
 
41
            // Add global select all/none options.
42
            const showTypeOptionsLink = '<span class="font-weight-bold ml-3 text-nowrap">' +
43
                '(<a id="downloadcenter-bytype" href="#">' + instance.strings['showtypes'] + '</a>)' + '</span>';
44
            let html = instance.html_generator('included', instance.strings['select']);
45
            let links = $(document.createElement('div'));
46
            links.addClass('grouped_settings section_level block card');
47
            links.html(html);
48
            links.find('.downloadcenter_selector .col-md-9').append(showTypeOptionsLink);
49
 
50
            links.insertBefore(firstsection);
51
 
52
            // For each module type on the course, add hidden select all/none options.
53
            instance.modlist = $(document.createElement('div'));
54
            instance.modlist.prop('id', 'mod_select_links');
55
            instance.modlist.prop('class', 'm-l-2');
56
            instance.modlist.appendTo(links);
57
            instance.modlist.hide();
58
 
59
            for (let mod in instance.modnames) {
60
                // Only include actual values from the list..
61
                if (!instance.modnames.hasOwnProperty(mod)) {
62
                    continue;
63
                }
64
 
65
                const img = '<img src="' + url.imageUrl('icon', 'mod_' + mod) + '" class="activityicon" />';
66
                html = instance.html_generator('mod_' + mod, img + instance.modnames[mod]);
67
                const modlinks = $(document.createElement('div'));
68
                modlinks.addClass('grouped_settings section_level');
69
                modlinks.html(html);
70
                modlinks.appendTo(instance.modlist);
71
                instance.initlinks(modlinks, mod);
72
            }
73
 
74
            // Attach events to links!
75
            $('#downloadcenter-all-included').click(function(e) { instance.helper(e, true,  'item_'); });
76
            $('#downloadcenter-none-included').click(function(e) { instance.helper(e, false, 'item_'); });
77
            $('#downloadcenter-bytype').click(function(e) { e.preventDefault(); instance.toggletypes(); });
78
            // Attach event to checkboxes!
79
            $('input.form-check-input').click(function() { instance.checkboxhandler($(this)); instance.updateFormState(); });
80
        });
81
 
82
    };
83
 
84
    ModFilter.prototype.checkboxhandler = function($checkbox) {
85
        const prefix = 'item_topic';
86
        const shortprefix = 'item_';
87
        const name = $checkbox.prop('name');
88
        const checked = $checkbox.prop('checked');
89
        if (name.substring(0, shortprefix.length) === shortprefix) {
90
            const $parent = $checkbox.parentsUntil('form', '.card');
91
            if (name.substring(0, prefix.length) === prefix) {
92
                $parent.find('input.form-check-input').prop('checked', checked);
93
            } else {
94
                if (checked) {
95
                    $parent.find('input.form-check-input[name^="item_topic"]').prop('checked', true);
96
                }
97
            }
98
        }
99
    };
100
 
101
    ModFilter.prototype.updateFormState = function() {
102
        // At this point, we really need to persuade the form we are part of to
103
        // update all of its disabledIf rules. However, as far as I can see,
104
        // given the way that lib/form/form.js is written, that is impossible.
105
        if (this.formid && M.form && M.form.updateFormState) {
106
            M.form.updateFormState(this.formid);
107
        }
108
    };
109
 
110
    // Toggles the display of the hidden module select all/none links.
111
    ModFilter.prototype.toggletypes = function() {
112
        // Change text of type toggle link.
113
        const link = $('#downloadcenter-bytype');
114
        if (this.currentlyshown) {
115
            link.text(this.strings['showtypes']);
116
        } else {
117
            link.text(this.strings['hidetypes']);
118
        }
119
        this.modlist.animate({height: 'toggle' }, 500, 'swing');
120
 
121
        this.currentlyshown = !this.currentlyshown;
122
 
123
    };
124
 
125
    ModFilter.prototype.initlinks = function(links, mod) {
126
        const instance = this;
127
        $('#downloadcenter-all-mod_' + mod).click(function(e) { instance.helper(e, true, 'item_', mod); });
128
        $('#downloadcenter-none-mod_' + mod).click(function(e) { instance.helper(e, false, 'item_', mod); });
129
 
130
    };
131
 
132
    ModFilter.prototype.helper = function(e, check, type, mod) {
133
        e.preventDefault();
134
        let prefix = '';
135
        if (typeof mod !== 'undefined') {
136
            prefix = 'item_' + mod + '_';
137
        }
138
 
139
        const len = type.length;
140
 
141
        $('input[type="checkbox"]').each(function(i, checkbox) {
142
            checkbox = $(checkbox);
143
            const name = checkbox.prop('name');
144
 
145
            // If a prefix has been set, ignore checkboxes which don't have that prefix.
146
            if (prefix && name.substring(0, prefix.length) !== prefix) {
147
                return;
148
            }
149
            if (name.substring(0, len) === type) {
150
                checkbox.prop('checked', check);
151
            }
152
            if (check) {
153
                checkbox.closest('.card.block').find('.form-group:first-child input').prop('checked', check);
154
            }
155
        });
156
 
157
        this.updateFormState();
158
    };
159
 
160
    ModFilter.prototype.html_generator = function(idtype, heading) {
161
        let links = '<a id="downloadcenter-all-' + idtype + '" href="#">' + this.strings['all'] + '</a> / ';
162
        links += '<a id="downloadcenter-none-' + idtype + '" href="#">' + this.strings['none'] + '</a>';
163
        return this.row_generator(heading, links);
164
    };
165
 
166
    ModFilter.prototype.row_generator = function(heading, content) {
167
        let ret = '<div class="form-group row fitem downloadcenter_selector">';
168
        ret += '<div class="col-md-3"></div>';
169
        ret += '<div class="col-md-9">';
170
        ret += '<label><span class="itemtitle">' + heading + '</span></label>';
171
        ret += '<span class="text-nowrap">' + content + '</span>';
172
        ret += '</div>';
173
        ret += '</div>';
174
        return ret;
175
    };
176
 
177
    return {
178
        init: function(modnames) {
179
            return new ModFilter(modnames);
180
        }
181
    };
182
});