2 * This class filters the rows of a table like the one on the define or
3 * override roles pages. It adds a search box just above the table, and if
4 * content is typed into that box, it hides any rows in the table where the
5 * capability name does not contain that text.
15 * @param {string} tableid
16 * @param {int} contextid
18 M.core_role.init_cap_table_filter = function(Y, tableid, contextid) {
20 var CapTableFilter = function(tableid) {
21 this.tableid = tableid;
22 this.context = contextid;
25 CapTableFilter.prototype = {
26 tableid : null, // ID of the cap table
27 context : null, // Context ID associated with what ever we are looking at
29 searchdelay : 100, // milliseconds
36 * Initialises the CapTableFilter object.
37 * This is called initializer so that a move to convert this to a proper
38 * YUI module will be easier.
40 initializer : function() {
41 // Get any existing filter value
42 var filtervalue = this.getFilterCookieValue();
44 // Find the form controls.
45 this.table = Y.one('#'+this.tableid);
47 // Create a div to hold the search UI.
48 this.div = Y.Node.create('<div class="capabilitysearchui form-inline"></div>').setStyles({
49 width : this.table.get('offsetWidth'),
53 // Create the capability search input.
54 this.input = Y.Node.create('<input class="form-control mx-1" type="text"' +
55 ' id="'+this.table.get('id')+'capabilitysearch" value="'+Y.Escape.html(filtervalue)+'" />');
56 // Create a label for the search input.
57 this.label = Y.Node.create('<label for="' + this.input.get('id') + '">' +
58 M.util.get_string('filter', 'moodle') + ' </label>');
59 // Create a clear button to clear the input.
60 this.button = Y.Node.create('<input type="button" class="btn btn-primary"' +
61 ' value="'+M.util.get_string('clear', 'moodle')+'" />').set('disabled', filtervalue=='');
63 // Tie it all together
64 this.div.append(this.label).append(this.input).append(this.button);
66 // Insert it into the div
67 this.table.ancestor().insert(this.div, this.table);
69 // Wire the events so it actually does something
70 this.input.on('keyup', this.change, this);
71 this.button.on('click', this.clear, this);
73 if (filtervalue != '') {
78 * Sets a cookie that describes the filter value.
79 * The cookie stores the context, and the time it was created and upon
80 * retrieval is checked to ensure that the cookie is for the correct
81 * context and is no more than an hour old.
83 setFilterCookieValue : function(value) {
85 fltcontext : this.context,
86 flttime : new Date().getTime(),
89 Y.Cookie.setSubs("captblflt", cookie);
92 * Gets the existing filter value if there is one.
93 * The cookie stores the context, and the time it was created and upon
94 * retrieval is checked to ensure that the cookie is for the correct
95 * context and is no more than an hour old.
97 getFilterCookieValue : function() {
98 var cookie = Y.Cookie.getSubs('captblflt');
99 if (cookie!=null && cookie.fltcontext && cookie.fltcontext == this.context && parseInt(cookie.flttime) > new Date().getTime()-(60*60*1000)) {
100 return cookie.fltvalue;
105 * Clears the filter value.
108 this.input.set('value', '');
109 if (this.delayhandle != -1) {
110 clearTimeout(this.delayhandle);
111 this.delayhandle = -1;
116 * Event callback for when the filter value changes
118 change : function() {
120 var handle = setTimeout(function(){self.filter();}, this.searchdelay);
121 if (this.delayhandle != -1) {
122 clearTimeout(this.delayhandle);
124 this.delayhandle = handle;
127 * Marks a row as visible or hidden
129 setVisible : function(row, visible) {
131 row.removeClass('hiddenrow');
133 row.addClass('hiddenrow');
137 * Filters the capability table
139 filter : function() {
140 var filtertext = this.input.get('value').toLowerCase(),
143 this.setFilterCookieValue(filtertext);
145 this.button.set('disabled', (filtertext == ''));
147 this.table.all('tr').each(function(row){
148 if (row.hasClass('rolecapheading')) {
149 this.setVisible(row, false);
152 if (row.hasClass('rolecap')) {
153 var capname = row.one('.cap-name').get('text') + '|' + row.one('.cap-desc a').get('text').toLowerCase();
154 if (capname.indexOf(filtertext) >= 0) {
155 this.setVisible(row, true);
157 this.setVisible(lastheading, true);
161 this.setVisible(row, false);
168 new CapTableFilter(tableid);
171 M.core_role.init_add_assign_page = function(Y) {
172 var add = Y.one('#add');
173 var addselect = M.core_user.get_user_selector('addselect');
174 add.set('disabled', addselect.is_selection_empty());
175 addselect.on('user_selector:selectionchanged', function(isempty) {
176 add.set('disabled', isempty);
179 var remove = Y.one('#remove');
180 var removeselect = M.core_user.get_user_selector('removeselect');
181 remove.set('disabled', removeselect.is_selection_empty());
182 removeselect.on('user_selector:selectionchanged', function(isempty) {
183 remove.set('disabled', isempty);