/src/main/webapp/public/js/admin/users.js

http://thoughtsite.googlecode.com/ · JavaScript · 202 lines · 164 code · 10 blank · 28 comment · 45 complexity · b5df964618393e7ad888a95ecc0de983 MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. /**
  16. * file is responsible to handle all javascript tasks on Users Form on Admin Tab require
  17. * jquery 1.3.2
  18. */
  19. // onload set the user screen
  20. google.setOnLoadCallback(function() {
  21. ie.progressStart();
  22. // Call to the method that renders all the ideas
  23. user.loadUsers(0);
  24. });
  25. user = {
  26. }
  27. //Show all the Users.
  28. user.loadUsers = function(offset) {
  29. var url = '';
  30. url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'admin/users.json?startIndex=' + offset);
  31. $.get(
  32. url,
  33. {},
  34. function (handle) {
  35. // render ideas
  36. if(handle != ''){
  37. if(handle.viewStatus.status == ie.config.SUCCESS) {
  38. $('#userData').html('');
  39. var counter = 0;
  40. for (i in handle.viewStatus.data.users) {
  41. counter++;
  42. if(counter > ie.config.RECORD_PER_PAGE_ADMIN)
  43. break;
  44. $('#userData').append(createHtml(handle.viewStatus.data.users[i]));
  45. }
  46. // Handles pagination
  47. if(undefined != handle.viewStatus.data.paging)
  48. {
  49. $('#pagination').html(ie.Paging.getHTML(handle.viewStatus.data.paging, 'user.loadUsers'));
  50. }
  51. ie.progressEnd();
  52. }
  53. else {
  54. $('#userData').html(handle.viewStatus.messages.users);
  55. }
  56. }
  57. else {
  58. ie.globalError();
  59. }
  60. ie.progressEnd();
  61. },
  62. ie.config.RESPONSE_TYPE
  63. );
  64. }
  65. //HTML for rendering users
  66. function createHtml(jsonData) {
  67. var output = '';
  68. output += ' <div id="' + jsonData['userKey'] + '" class="ie-adm-lt-bar ie-clear">';
  69. output += ' <div class="ie-left ie-left-mar-5 ie-nm-blu">';
  70. output += ie.escapeHTML(jsonData['displayName']);
  71. output += ' </div>';
  72. output += ' <div class="ie-right ie-left-mar-10 ie-right-mar-0">';
  73. output += ' <a href="' + ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'users/profile/'+ jsonData['userKey']) + ' " onclick="window.open(this.href,\'window_name\',\'options\'); return false;" class="ie-nm-blu">View</a> ';
  74. output += ' </div>';
  75. output += ' <div id="banDiv' + jsonData['userKey'] + '" class="ie-right ie-left-mar-10 ie-right-mar-0">';
  76. if(ie.config.STATUS_ACTIVE == jsonData['status'] && ie.config.ADMIN_ROLE != jsonData['roleName']) {
  77. output += ' <a href="javascript:void(0);" onclick="adminReasonBan(\'' + jsonData['userKey'] + '\')" class="ie-nm-blu">Ban</a> |';
  78. }
  79. output += ' </div>';
  80. output += ' <div id="activateDiv' + jsonData['userKey'] + '" class="ie-right ie-left-mar-10 ie-right-mar-0">';
  81. if(ie.config.STATUS_BANNED == jsonData['status']) {
  82. output += ' <a href="javascript:void(0);" onclick="adminReasonActivate(\'' + jsonData['userKey'] + '\')" class="ie-nm-blu">Activate</a> |';
  83. }
  84. output += ' </div>';
  85. output += ' </div>';
  86. return output;
  87. }
  88. //Called when a user is banned
  89. function adminReasonBan(userKey)
  90. {
  91. var url = '';
  92. // Opens a dialog box that takes the reason for banning a user
  93. $('#dialog').html('<div class="hiddenInViewSource" style="padding-left: 70px; margin-top: 20px;">'
  94. + '<b>Enter reason to ban a user: </b><textarea id="inputVal"></textarea><br/>'
  95. + '</div>');
  96. $('#dialog').dialog('option', 'title', 'Input Reason!');
  97. $('#dialog').dialog('option', 'buttons', { "Submit": function() {
  98. if($.trim($('#inputVal').val()) != null && $.trim($('#inputVal').val()) != '')
  99. {
  100. url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'admin/banUser/' + userKey);
  101. $.post(
  102. url,
  103. {adminReason: $.trim($('#inputVal').val())}, //saved reason for ban
  104. function (handle) {
  105. if(ie.config.SUCCESS == handle.viewStatus.status) {
  106. if(handle.viewStatus.data.user.userKey == userKey && handle.viewStatus.data.user.status == "banned")
  107. {
  108. var main = document.getElementById(userKey);
  109. var banDiv = document.getElementById('banDiv' + userKey);
  110. var activateDiv = document.getElementById('activateDiv' + userKey);
  111. main.removeChild(banDiv);
  112. if(activateDiv != null)
  113. main.removeChild(activateDiv);
  114. var activateDiv = document.createElement('div');
  115. activateDiv.setAttribute('id','activateDiv' + userKey);
  116. activateDiv.innerHTML = '<a href="javascript:void(0);" onclick="adminReasonActivate(' + '\'' + userKey + '\'' + ')" class="ie-nm-blu">Activate</a> |';
  117. activateDiv.className = 'ie-right ie-left-mar-10 ie-right-mar-0';
  118. main.appendChild(activateDiv);
  119. }
  120. }
  121. else {
  122. $('#errDisplay').html(handle.viewStatus.messages.user);
  123. ie.progressEnd();
  124. }
  125. },
  126. ie.config.RESPONSE_TYPE
  127. );
  128. $(this).dialog("close");
  129. }
  130. else
  131. {
  132. alert('Please provide reason for action.');
  133. $('#dialog').dialog('option', 'buttons', { "Ok": function() { $(this).dialog("close"); } });
  134. }
  135. $(this).dialog("close");
  136. } });
  137. $('#dialog').dialog('option', 'modal', true);
  138. $('#dialog').dialog('open');
  139. }
  140. //Called when a user is activated
  141. function adminReasonActivate(userKey)
  142. {
  143. var url = '';
  144. // Opens a dialog box that takes the reason for activating a user
  145. $('#dialog').html('<div class="hiddenInViewSource" style="padding-left: 70px; margin-top: 20px;">'
  146. + '<b>Enter reason for activating: </b><textarea id="inputVal"></textarea><br/>'
  147. + '</div>');
  148. $('#dialog').dialog('option', 'title', 'Input Reason!');
  149. $('#dialog').dialog('option', 'buttons', { "Submit": function() {
  150. if($.trim($('#inputVal').val()) != null && $.trim($('#inputVal').val()) != '')
  151. {
  152. url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'admin/activateUser/' + userKey);
  153. $.post(
  154. url,
  155. {adminReason: $.trim($('#inputVal').val())},// saved reason for activation
  156. function (handle) {
  157. if(ie.config.SUCCESS == handle.viewStatus.status) {
  158. if(handle.viewStatus.data.user.userKey == userKey && handle.viewStatus.data.user.status == "active")
  159. {
  160. var main = document.getElementById(userKey);
  161. var activateDiv = document.getElementById('activateDiv' + userKey);
  162. main.removeChild(activateDiv);
  163. var banDiv = document.getElementById('banDiv' + userKey);
  164. if(banDiv != null)
  165. {
  166. main.removeChild(banDiv);
  167. }
  168. var banDiv = document.createElement('div');
  169. banDiv.setAttribute('id','banDiv' + userKey);
  170. banDiv.innerHTML = '<a href="javascript:void(0);" onclick="adminReasonBan(' + '\'' + userKey + '\'' + ')" class="ie-nm-blu">Ban</a> |';
  171. banDiv.className = 'ie-right ie-left-mar-10 ie-right-mar-0';
  172. main.appendChild(banDiv);
  173. }
  174. }
  175. else {
  176. $('#errDisplay').html(handle.viewStatus.messages.user);
  177. ie.progressEnd();
  178. }
  179. },
  180. ie.config.RESPONSE_TYPE
  181. );
  182. }
  183. else
  184. {
  185. alert('Please provide reason for action.');
  186. $('#dialog').dialog('option', 'buttons', { "Ok": function() { $(this).dialog("close"); } });
  187. }
  188. $(this).dialog("close");
  189. } });
  190. $('#dialog').dialog('option', 'modal', true);
  191. $('#dialog').dialog('open');
  192. }