1 |
efrain |
1 |
/**
|
|
|
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.
|
|
|
6 |
*/
|
|
|
7 |
|
|
|
8 |
/**
|
|
|
9 |
* Role namespace
|
|
|
10 |
*/
|
|
|
11 |
M.core_role = {};
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* @param {YUI} Y
|
|
|
15 |
* @param {string} tableid
|
|
|
16 |
* @param {int} contextid
|
|
|
17 |
*/
|
|
|
18 |
M.core_role.init_cap_table_filter = function(Y, tableid, contextid) {
|
|
|
19 |
|
|
|
20 |
var CapTableFilter = function(tableid) {
|
|
|
21 |
this.tableid = tableid;
|
|
|
22 |
this.context = contextid;
|
|
|
23 |
this.initializer();
|
|
|
24 |
};
|
|
|
25 |
CapTableFilter.prototype = {
|
|
|
26 |
tableid : null, // ID of the cap table
|
|
|
27 |
context : null, // Context ID associated with what ever we are looking at
|
|
|
28 |
delayhandle : -1,
|
|
|
29 |
searchdelay : 100, // milliseconds
|
|
|
30 |
table : null,
|
|
|
31 |
div : null,
|
|
|
32 |
input : null,
|
|
|
33 |
label : null,
|
|
|
34 |
button : null,
|
|
|
35 |
/**
|
|
|
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.
|
|
|
39 |
*/
|
|
|
40 |
initializer : function() {
|
|
|
41 |
// Get any existing filter value
|
|
|
42 |
var filtervalue = this.getFilterCookieValue();
|
|
|
43 |
|
|
|
44 |
// Find the form controls.
|
|
|
45 |
this.table = Y.one('#'+this.tableid);
|
|
|
46 |
|
|
|
47 |
// Create a div to hold the search UI.
|
|
|
48 |
this.div = Y.Node.create('<div class="capabilitysearchui d-flex flex-wrap align-items-center"></div>').setStyles({
|
|
|
49 |
width : this.table.get('offsetWidth'),
|
|
|
50 |
marginLeft : 'auto',
|
|
|
51 |
marginRight : 'auto'
|
|
|
52 |
});
|
|
|
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=='');
|
|
|
62 |
|
|
|
63 |
// Tie it all together
|
|
|
64 |
this.div.append(this.label).append(this.input).append(this.button);
|
|
|
65 |
|
|
|
66 |
// Insert it into the div
|
|
|
67 |
this.table.ancestor().insert(this.div, this.table);
|
|
|
68 |
|
|
|
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);
|
|
|
72 |
|
|
|
73 |
if (filtervalue != '') {
|
|
|
74 |
this.filter();
|
|
|
75 |
}
|
|
|
76 |
},
|
|
|
77 |
/**
|
|
|
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.
|
|
|
82 |
*/
|
|
|
83 |
setFilterCookieValue : function(value) {
|
|
|
84 |
var cookie = {
|
|
|
85 |
fltcontext : this.context,
|
|
|
86 |
flttime : new Date().getTime(),
|
|
|
87 |
fltvalue : value
|
|
|
88 |
}
|
|
|
89 |
Y.Cookie.setSubs("captblflt", cookie);
|
|
|
90 |
},
|
|
|
91 |
/**
|
|
|
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.
|
|
|
96 |
*/
|
|
|
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;
|
|
|
101 |
}
|
|
|
102 |
return '';
|
|
|
103 |
},
|
|
|
104 |
/**
|
|
|
105 |
* Clears the filter value.
|
|
|
106 |
*/
|
|
|
107 |
clear : function() {
|
|
|
108 |
this.input.set('value', '');
|
|
|
109 |
if (this.delayhandle != -1) {
|
|
|
110 |
clearTimeout(this.delayhandle);
|
|
|
111 |
this.delayhandle = -1;
|
|
|
112 |
}
|
|
|
113 |
this.filter();
|
|
|
114 |
},
|
|
|
115 |
/**
|
|
|
116 |
* Event callback for when the filter value changes
|
|
|
117 |
*/
|
|
|
118 |
change : function() {
|
|
|
119 |
var self = this;
|
|
|
120 |
var handle = setTimeout(function(){self.filter();}, this.searchdelay);
|
|
|
121 |
if (this.delayhandle != -1) {
|
|
|
122 |
clearTimeout(this.delayhandle);
|
|
|
123 |
}
|
|
|
124 |
this.delayhandle = handle;
|
|
|
125 |
},
|
|
|
126 |
/**
|
|
|
127 |
* Marks a row as visible or hidden
|
|
|
128 |
*/
|
|
|
129 |
setVisible : function(row, visible) {
|
|
|
130 |
if (visible) {
|
|
|
131 |
row.removeClass('hiddenrow');
|
|
|
132 |
} else {
|
|
|
133 |
row.addClass('hiddenrow');
|
|
|
134 |
}
|
|
|
135 |
},
|
|
|
136 |
/**
|
|
|
137 |
* Filters the capability table
|
|
|
138 |
*/
|
|
|
139 |
filter : function() {
|
|
|
140 |
var filtertext = this.input.get('value').toLowerCase(),
|
|
|
141 |
lastheading = null;
|
|
|
142 |
|
|
|
143 |
this.setFilterCookieValue(filtertext);
|
|
|
144 |
|
|
|
145 |
this.button.set('disabled', (filtertext == ''));
|
|
|
146 |
|
|
|
147 |
this.table.all('tr').each(function(row){
|
|
|
148 |
if (row.hasClass('rolecapheading')) {
|
|
|
149 |
this.setVisible(row, false);
|
|
|
150 |
lastheading = row;
|
|
|
151 |
}
|
|
|
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);
|
|
|
156 |
if (lastheading) {
|
|
|
157 |
this.setVisible(lastheading, true);
|
|
|
158 |
lastheading = null;
|
|
|
159 |
}
|
|
|
160 |
} else {
|
|
|
161 |
this.setVisible(row, false);
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
}, this);
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
new CapTableFilter(tableid);
|
|
|
169 |
};
|
|
|
170 |
|
|
|
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);
|
|
|
177 |
});
|
|
|
178 |
|
|
|
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);
|
|
|
184 |
});
|
|
|
185 |
};
|