MDL-21887 fixed borked JS, but still the groups ajax needs a major cleanup
[moodle.git] / group / clientlib.js
CommitLineData
3cdc1e28 1/**
2 * Client-side JavaScript for group management interface.
6f5e0852 3 * @author vy-shane AT moodle.com
3cdc1e28 4 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
5 * @package groups
6 */
7
8
9/**
10 * Class UpdatableGroupsCombo
11 */
12function UpdatableGroupsCombo(wwwRoot, courseId) {
13 this.wwwRoot = wwwRoot;
14 this.courseId = courseId;
15
ac433d41 16 this.connectCallback = {
3cdc1e28 17
95bea1ed 18 success: function(o) {
f1303e92 19 if (o.responseText !== undefined) {
3cdc1e28 20 var groupsComboEl = document.getElementById("groups");
21 var membersComboEl = document.getElementById("members");
3cdc1e28 22 if (membersComboEl) {
e254aa34 23 // Clear the members list box.
3cdc1e28 24 while (membersComboEl.firstChild) {
25 membersComboEl.removeChild(membersComboEl.firstChild);
26 }
27 }
28
f1303e92 29 if (groupsComboEl && o.responseText) {
30 var groups = eval("("+o.responseText+")");
29848126 31
f1303e92 32 // Populate the groups list box.
29848126 33 for (var i=0; i<groups.length; i++) {
34 var optionEl = document.createElement("option");
35 optionEl.setAttribute("value", groups[i].id);
eb674f8b 36 optionEl.title = groups[i].name;
29848126 37 optionEl.innerHTML = groups[i].name;
38 groupsComboEl.appendChild(optionEl);
3cdc1e28 39 }
3cdc1e28 40 }
f1303e92 41 }
42 // Remove the loader gif image.
43 removeLoaderImgs("groupsloader", "groupslabel");
29848126 44 },
45
46 failure: function(o) {
47 removeLoaderImgs("membersloader", "memberslabel");
95bea1ed 48 this.currentTransId = null;
3cdc1e28 49 }
95bea1ed 50
ac433d41 51 };
52
e254aa34 53 // Add onchange event to groups list box.
95bea1ed 54 // Okay, this is not working in IE. The onchange is never fired...
55 // I'm hard coding the onchange in ../index.php. Not ideal, but it works
56 // then. vyshane AT moodle DOT com.
57 /*
29848126 58 groupsComboEl = document.getElementById("groups");
59 if (groupsComboEl) {
60 groupsComboEl.setAttribute("onchange", "membersCombo.refreshMembers(this.options[this.selectedIndex].value);");
61 }
95bea1ed 62 */
63
3cdc1e28 64 // Hide the updategroups input since AJAX will take care of this.
77ea3330 65 YAHOO.util.Dom.setStyle("updategroups", "display", "none");
3cdc1e28 66}
67
3cdc1e28 68
69/**
70 * Class UpdatableMembersCombo
71 */
72function UpdatableMembersCombo(wwwRoot, courseId) {
73 this.wwwRoot = wwwRoot;
74 this.courseId = courseId;
75
ac433d41 76 this.connectCallback = {
3cdc1e28 77 success: function(o) {
78
f1303e92 79 if (o.responseText !== undefined) {
3cdc1e28 80 var selectEl = document.getElementById("members");
f1303e92 81 if (selectEl && o.responseText) {
e254aa34 82 var roles = eval("("+o.responseText+")");
29848126 83
e254aa34 84 // Clear the members list box.
53b16b2b 85 if (selectEl) {
86 while (selectEl.firstChild) {
87 selectEl.removeChild(selectEl.firstChild);
88 }
89 }
e254aa34 90 // Populate the members list box.
91 for (var i=0; i<roles.length; i++) {
92 var optgroupEl = document.createElement("optgroup");
93 optgroupEl.setAttribute("label",roles[i].name);
94
95 for(var j=0; j<roles[i].users.length; j++) {
96 var optionEl = document.createElement("option");
97 optionEl.setAttribute("value", roles[i].users[j].id);
98 optionEl.title = roles[i].users[j].name;
99 optionEl.innerHTML = roles[i].users[j].name;
100 optgroupEl.appendChild(optionEl);
101 }
102 selectEl.appendChild(optgroupEl);
3cdc1e28 103 }
3cdc1e28 104 }
f1303e92 105 }
106 // Remove the loader gif image.
107 removeLoaderImgs("membersloader", "memberslabel");
29848126 108 },
109
110 failure: function(o) {
111 removeLoaderImgs("membersloader", "memberslabel");
3cdc1e28 112 }
95bea1ed 113
ac433d41 114 };
115
3cdc1e28 116 // Hide the updatemembers input since AJAX will take care of this.
77ea3330 117 YAHOO.util.Dom.setStyle("updatemembers", "display", "none");
3cdc1e28 118}
119
120/**
121 * When a group is selected, we need to update the members.
fa2d60c5 122 * The Add/Remove Users button also needs to be disabled/enabled
123 * depending on whether or not a group is selected
3cdc1e28 124 */
f6eece19 125UpdatableMembersCombo.prototype.refreshMembers = function () {
126
127 // Get group selector and check selection type
128 var selectEl = document.getElementById("groups");
129 var selectionCount=0,groupId=0;
130 if( selectEl ) {
131 for (var i = 0; i < selectEl.options.length; i++) {
132 if(selectEl.options[i].selected) {
133 selectionCount++;
134 if(!groupId) {
135 groupId=selectEl.options[i].value;
136 }
137 }
138 }
139 }
140 var singleSelection=selectionCount == 1;
141
142 // Add the loader gif image (we only load for single selections)
143 if(singleSelection) {
144 createLoaderImg("membersloader", "memberslabel", this.wwwRoot);
145 }
6f5e0852 146
eb674f8b 147 // Update the label.
eb674f8b 148 var spanEl = document.getElementById("thegroup");
f6eece19 149 if (singleSelection) {
eb674f8b 150 spanEl.innerHTML = selectEl.options[selectEl.selectedIndex].title;
f6eece19 151 } else {
152 spanEl.innerHTML = '&nbsp;';
eb674f8b 153 }
8bfa86fd 154
e254aa34 155 // Clear the members list box.
eb674f8b 156 selectEl = document.getElementById("members");
29848126 157 if (selectEl) {
158 while (selectEl.firstChild) {
159 selectEl.removeChild(selectEl.firstChild);
160 }
161 }
6f5e0852 162
f6eece19 163 document.getElementById("showaddmembersform").disabled = !singleSelection;
164 document.getElementById("showeditgroupsettingsform").disabled = !singleSelection;
165 document.getElementById("deletegroup").disabled = selectionCount == 0;
166
167 if(singleSelection) {
168 var sUrl = this.wwwRoot+"/group/index.php?id="+this.courseId+"&group="+groupId+"&act_ajax_getmembersingroup";
169 YAHOO.util.Connect.asyncRequest("GET", sUrl, this.connectCallback, null);
170 }
ac433d41 171};
3cdc1e28 172
173
174
ac433d41 175var createLoaderImg = function (elClass, parentId, wwwRoot) {
176 var parentEl = document.getElementById(parentId);
177 if (!parentEl) {
178 return false;
179 }
95bea1ed 180 if (document.getElementById("loaderImg")) {
181 // A loader image already exists.
ac433d41 182 return false;
183 }
8bfa86fd 184 var loadingImg = document.createElement("img");
185
95fd023e 186 loadingImg.setAttribute("src", M.util.image_url('moodle', '/i/ajaxloader'));
ac433d41 187 loadingImg.setAttribute("class", elClass);
8bfa86fd 188 loadingImg.setAttribute("alt", "Loading");
95bea1ed 189 loadingImg.setAttribute("id", "loaderImg");
ac433d41 190 parentEl.appendChild(loadingImg);
191
192 return true;
193};
8bfa86fd 194
195
ac433d41 196var removeLoaderImgs = function (elClass, parentId) {
197 var parentEl = document.getElementById(parentId);
ac433d41 198 if (parentEl) {
95bea1ed 199 var loader = document.getElementById("loaderImg");
200 parentEl.removeChild(loader);
ac433d41 201 }
202};
97873016 203
bf1ac6d9
AD
204function updateUserSummary() {
205 var selectEl = document.getElementById('addselect');
206 var summaryDiv = document.getElementById('group-usersummary');
207 var length = selectEl.length;
208 var selectCnt = 0;
209 var selectIdx = -1;
210
211 for(i=0;i<length;i++) {
212 if (selectEl.options[i].selected) {
213 selectCnt++;
214 selectIdx = i;
215 }
216 }
217
218 if (selectCnt == 1 && userSummaries[selectIdx]) {
219 summaryDiv.innerHTML = userSummaries[selectIdx];
220 } else {
221 summaryDiv.innerHTML = '';
222 }
223
224 return(true);
225}
226
97873016 227function init_add_remove_members_page() {
228 var addselect = user_selector.get('addselect');
229 document.getElementById('add').disabled = addselect.is_selection_empty();
230 addselect.subscribe('selectionchanged', function(isempty) {
231 document.getElementById('add').disabled = isempty;
232 });
233
234 var removeselect = user_selector.get('removeselect');
235 document.getElementById('remove').disabled = removeselect.is_selection_empty();
236 removeselect.subscribe('selectionchanged', function(isempty) {
237 document.getElementById('remove').disabled = isempty;
238 });
bf1ac6d9
AD
239
240 addselect = document.getElementById('addselect');
241 addselect.onchange = updateUserSummary;
97873016 242}