Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / group / lib / clientlib.js
blob968c3fce6e4e9252ab7be9f65b68cd0214800751
1 /**
2 * Client-side JavaScript for group management interface.
3 * @author vy-shane AT moodle.com
4 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
5 * @package groups
6 */
9 /**
10 * Class UpdatableGroupsCombo
12 function UpdatableGroupsCombo(wwwRoot, courseId) {
13 this.wwwRoot = wwwRoot;
14 this.courseId = courseId;
16 this.connectCallback = {
18 success: function(o) {
19 if (o.responseText !== undefined) {
20 var groupsComboEl = document.getElementById("groups");
21 var membersComboEl = document.getElementById("members");
23 if (membersComboEl) {
24 // Clear the members combo box.
25 while (membersComboEl.firstChild) {
26 membersComboEl.removeChild(membersComboEl.firstChild);
30 if (groupsComboEl && o.responseText) {
31 var groups = eval("("+o.responseText+")");
33 // Populate the groups combo box.
34 for (var i=0; i<groups.length; i++) {
35 var optionEl = document.createElement("option");
36 optionEl.setAttribute("value", groups[i].id);
37 optionEl.innerHTML = groups[i].name;
38 groupsComboEl.appendChild(optionEl);
42 // Remove the loader gif image.
43 removeLoaderImgs("groupsloader", "groupslabel");
46 failure: function(o) {
47 removeLoaderImgs("membersloader", "memberslabel");
48 this.currentTransId = null;
53 // Add onchange event to groups combo box.
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.
58 groupsComboEl = document.getElementById("groups");
59 if (groupsComboEl) {
60 groupsComboEl.setAttribute("onchange", "membersCombo.refreshMembers(this.options[this.selectedIndex].value);");
64 // Hide the updategroups input since AJAX will take care of this.
65 var updateGroupsButton = document.getElementById("updategroups");
66 updateGroupsButton.setAttribute("style", "display:none;");
69 /**
70 * When a grouping is selected, we need to update the groups.
72 UpdatableGroupsCombo.prototype.refreshGroups = function (groupingId) {
73 // Add the loader gif image.
74 createLoaderImg("groupsloader", "groupslabel", this.wwwRoot);
76 // Clear the groups combo box.
77 var selectEl = document.getElementById("groups");
78 if (selectEl) {
79 while (selectEl.firstChild) {
80 selectEl.removeChild(selectEl.firstChild);
84 var sUrl = this.wwwRoot+"/group/index.php?id="+this.courseId+"&grouping="+groupingId+"&act_ajax_getgroupsingrouping";
85 YAHOO.util.Connect.asyncRequest('GET', sUrl, this.connectCallback, null);
90 /**
91 * Class UpdatableMembersCombo
93 function UpdatableMembersCombo(wwwRoot, courseId) {
94 this.wwwRoot = wwwRoot;
95 this.courseId = courseId;
97 this.connectCallback = {
98 success: function(o) {
100 if (o.responseText !== undefined) {
101 var selectEl = document.getElementById("members");
103 if (selectEl && o.responseText) {
104 var members = eval("("+o.responseText+")");
106 // Populate the members combo box.
107 for (var i=0; i<members.length; i++) {
108 var optionEl = document.createElement("option");
109 optionEl.setAttribute("value", members[i].id);
110 optionEl.innerHTML = members[i].firstname+" "+members[i].lastname;
111 selectEl.appendChild(optionEl);
115 // Remove the loader gif image.
116 removeLoaderImgs("membersloader", "memberslabel");
119 failure: function(o) {
120 removeLoaderImgs("membersloader", "memberslabel");
125 // Hide the updatemembers input since AJAX will take care of this.
126 var updateMembersButton = document.getElementById("updatemembers");
127 updateMembersButton.setAttribute("style", "display:none;");
131 * When a group is selected, we need to update the members.
133 UpdatableMembersCombo.prototype.refreshMembers = function (groupId) {
134 // Add the loader gif image.
135 createLoaderImg("membersloader", "memberslabel", this.wwwRoot);
137 // Clear the members combo box.
138 var selectEl = document.getElementById("members");
139 if (selectEl) {
140 while (selectEl.firstChild) {
141 selectEl.removeChild(selectEl.firstChild);
145 var sUrl = this.wwwRoot+"/group/index.php?id="+this.courseId+"&group="+groupId+"&act_ajax_getmembersingroup";
146 YAHOO.util.Connect.asyncRequest("GET", sUrl, this.connectCallback, null);
151 var createLoaderImg = function (elClass, parentId, wwwRoot) {
152 var parentEl = document.getElementById(parentId);
153 if (!parentEl) {
154 return false;
156 if (document.getElementById("loaderImg")) {
157 // A loader image already exists.
158 return false;
160 var loadingImg = document.createElement("img");
162 loadingImg.setAttribute("src", wwwRoot+"/pix/i/ajaxloader.gif");
163 loadingImg.setAttribute("class", elClass);
164 loadingImg.setAttribute("alt", "Loading");
165 loadingImg.setAttribute("id", "loaderImg");
166 parentEl.appendChild(loadingImg);
168 return true;
172 var removeLoaderImgs = function (elClass, parentId) {
173 var parentEl = document.getElementById(parentId);
174 if (parentEl) {
175 var loader = document.getElementById("loaderImg");
176 parentEl.removeChild(loader);