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"></div>').setStyles({
49 width : this.table.get('offsetWidth'),
53 // Create the capability search input.
54 this.input = Y.Node.create('<input type="text" id="'+this.table.get('id')+'capabilitysearch" value="'+filtervalue+'" />');
55 // Create a label for the search input.
56 this.label = Y.Node.create('<label for="'+this.input.get('id')+'">'+M.str.moodle.filter+' </label>');
57 // Create a clear button to clear the input.
58 this.button = Y.Node.create('<input type="button" value="'+M.str.moodle.clear+'" />').set('disabled', filtervalue=='');
60 // Tie it all together
61 this.div.append(this.label).append(this.input).append(this.button);
63 // Insert it into the div
64 this.table.ancestor().insert(this.div, this.table);
66 // Wire the events so it actually does something
67 this.input.on('keyup', this.change, this);
68 this.button.on('click', this.clear, this);
70 if (filtervalue != '') {
75 * Sets a cookie that describes the filter value.
76 * The cookie stores the context, and the time it was created and upon
77 * retrieval is checked to ensure that the cookie is for the correct
78 * context and is no more than an hour old.
80 setFilterCookieValue : function(value) {
82 fltcontext : this.context,
83 flttime : new Date().getTime(),
86 Y.Cookie.setSubs("captblflt", cookie);
89 * Gets the existing filter value if there is one.
90 * The cookie stores the context, and the time it was created and upon
91 * retrieval is checked to ensure that the cookie is for the correct
92 * context and is no more than an hour old.
94 getFilterCookieValue : function() {
95 var cookie = Y.Cookie.getSubs('captblflt');
96 if (cookie!=null && cookie.fltcontext && cookie.fltcontext == this.context && parseInt(cookie.flttime) > new Date().getTime()-(60*60*1000)) {
97 return cookie.fltvalue;
102 * Clears the filter value.
105 this.input.set('value', '');
106 if (this.delayhandle != -1) {
107 clearTimeout(this.delayhandle);
108 this.delayhandle = -1;
113 * Event callback for when the filter value changes
115 change : function() {
117 var handle = setTimeout(function(){self.filter();}, this.searchdelay);
118 if (this.delayhandle != -1) {
119 clearTimeout(this.delayhandle);
121 this.delayhandle = handle;
124 * Marks a row as visible or hidden
126 setVisible : function(row, visible) {
128 row.removeClass('hiddenrow');
130 row.addClass('hiddenrow');
134 * Filters the capability table
136 filter : function() {
137 var filtertext = this.input.get('value').toLowerCase(),
140 this.setFilterCookieValue(filtertext);
142 this.button.set('disabled', (filtertext == ''));
144 this.table.all('tr').each(function(row){
145 if (row.hasClass('rolecapheading')) {
146 this.setVisible(row, false);
149 if (row.hasClass('rolecap')) {
150 var capname = row.one('.cap-name').get('text') + '|' + row.one('.cap-desc a').get('text').toLowerCase();
151 if (capname.indexOf(filtertext) >= 0) {
152 this.setVisible(row, true);
154 this.setVisible(lastheading, true);
158 this.setVisible(row, false);
165 new CapTableFilter(tableid);
168 M.core_role.init_add_assign_page = function(Y) {
169 var add = Y.one('#add');
170 var addselect = M.core_user.get_user_selector('addselect');
171 add.set('disabled', addselect.is_selection_empty());
172 addselect.on('user_selector:selectionchanged', function(isempty) {
173 add.set('disabled', isempty);
176 var remove = Y.one('#remove');
177 var removeselect = M.core_user.get_user_selector('removeselect');
178 remove.set('disabled', removeselect.is_selection_empty());
179 removeselect.on('user_selector:selectionchanged', function(isempty) {
180 remove.set('disabled', isempty);