PageRenderTime 245ms CodeModel.GetById 121ms app.highlight 18ms RepoModel.GetById 103ms app.codeStats 0ms

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