nextcloud/admin/js/users.js

308 lines
8.4 KiB
JavaScript
Raw Normal View History

2011-04-16 19:49:57 +04:00
$(document).ready(function(){
2011-04-17 01:26:55 +04:00
// Vars we need
var uid = "";
var gid = "";
var togglepassword = "";
var togglegroup = "";
//#########################################################################
// Stuff I don't understand
//#########################################################################
function doToggleGroup( group ){
$("#changegroupgid").val(group);
// Serialize the data
var post = $( "#changegroupsform" ).serialize();
// Ajax foo
$.post( 'ajax/togglegroups.php', post, function(data){
if( data.status == "success" ){
var groups = [];
$("input[x-use='togglegroup']").each( function(index){
if( $(this).attr("checked")){
groups.push($(this).val());
}
});
$("#changegroups").prev().html( groups.join(", "));
}
else{
alert( "something went wrong! sorry!" );
}
});
return false;
}
//#########################################################################
// Functions for editing the dom after user manipulation
//#########################################################################
// Manipulating the page after crteating a user
function userCreated( username, groups ){
// Add user to table
var newrow = '<tr x-uid="'+username+'"><td x-use="username"><span x-use="usernamespan">'+username+'</span></td>';
newrow = newrow+'<td x-use="usergroups">'+groups+'</td>';
newrow = newrow+'<td><a href="" class="edituserbutton">edit</a> | <a class="removeuserbutton" href="">remove</a></td></tr>';
$("#usertable").append( newrow );
// Clear forms
$("input[x-use='createuserfield']").val( "" );
$("input[x-use='createusercheckbox']").attr( "checked", false );
}
function userRemoved( username ){
$( "tr[x-uid='"+username+"']" ).remove();
}
function groupCreated( groupname ){
var newrow = '<tr x-gid="'+groupname+'"><td>' + groupname + '</td>';
newrow = newrow + '<td><a class="removegroupbutton" href="">remove</a></td></tr>';
$("#grouptable").append( newrow );
// Delete form content
$("input[x-use='creategroupfield']").val( "" );
// Add group option to Create User and Edit User
var createuser = '<input x-gid="'+groupname+'" type="checkbox" name="groups[]" value="'+groupname+'" /><span x-gid="'+groupname+'">'+groupname+'<br /></span>';
$("#createusergroups").append( createuser );
var changeuser = '<input x-use="togglegroup" x-gid="'+groupname+'" type="checkbox" name="groups[]" value="'+groupname+'" /><span x-use="togglegroup" x-gid="'+groupname+'">'+groupname+'<br /></span>';
$("#changegroupsform").append( changeuser );
}
function groupRemoved( groupname ){
// Delete the options
$( "tr[x-gid='"+groupname+"']" ).remove();
$( "span[x-gid='"+groupname+"']" ).remove();
$( "input[x-gid='"+groupname+"']" ).remove();
// remove it from user list
$( "td[x-use='usergroups']" ).each(function(index){
var content = $(this).text();
var list = content.split( ", " );
var newlist = [];
for( var i = 0; i < list.length; i++ ){
var temp = list[i];
if( temp != groupname ){
newlist.push( temp );
}
2011-04-16 19:49:57 +04:00
}
var newstring = newlist.join( ", " );
$(this).html( newstring )
});
}
2011-04-16 19:49:57 +04:00
//#########################################################################
// Editing the users properties by clicking the cell
//#########################################################################
// Password (clicking on user name)
$("span[x-use='usernamespan']").live( "click", function(){
if( togglepassword == "" || $(this).parent().parent().attr("x-uid") != togglepassword ){
togglepassword = $(this).parent().parent().attr("x-uid");
// Set the username!
$("#changepassworduid").val(togglepassword);
$("#changepasswordpwd").val("");
$(this).parent().append( $('#changepassword') );
$('#changepassword').show();
}
else{
$('#changepassword').hide();
togglepassword = "";
}
});
$("#changepasswordbutton").click( function(){
// Serialize the data
var post = $( "#changepasswordform" ).serialize();
// Ajax foo
$.post( 'ajax/changepassword.php', post, function(data){
if( data.status == "success" ){
togglepassword = "";
$('#changepassword').hide();
}
else{
alert( "something went wrong! sorry!" );
}
});
return false;
});
// Groups
$("span[x-use='usergroupsspan']").live( "click", function(){
if( togglegroup == "" || $(this).parent().parent().attr("x-uid") != togglegroup){
togglegroup = $(this).parent().parent().attr("x-uid");
var groups = $(this).text();
groups = groups.split(", ");
$("input[x-use='togglegroup']").each( function(index){
var check = false;
// Group checked?
for( var i = 0; i < groups.length; i++ ){
if( $(this).val() == groups[i] ){
check = true;
}
}
// Check/uncheck
if( check ){
$(this).attr("checked","checked");
}
else{
$(this).removeAttr("checked");
}
});
$("#changegroupuid").val(togglegroup);
$(this).parent().append( $('#changegroups') );
$('#changegroups').show();
}
else{
var groups = [];
$("input[x-use='togglegroup']").each( function(index){
if( $(this).attr("checked")){
groups.push($(this).val());
}
});
$(this).html( groups.join(", "));
$('#changegroups').hide();
togglegroup = "";
}
});
$("span[x-use='togglegroup']").live( "click", function(){
if( $(this).prev().attr("checked")){
$(this).prev().removeAttr("checked")
}
else{
$(this).prev().attr("checked","checked")
}
doToggleGroup( $(this).attr("x-gid"));
});
$("input[x-use='togglegroup']").live( "click", function(){
doToggleGroup( $(this).attr("x-gid"));
});
//#########################################################################
// Clicking on buttons
//#########################################################################
// Show the create user form
$( "#createuseroptionbutton" )
.click(function(){
$( "#createuserform" ).toggle();
2011-04-16 19:49:57 +04:00
return false;
});
// Create a new user
$( "#createuserbutton" )
.click(function(){
// Create the post data
var post = $( "#createuserdata" ).serialize();
// Ajax call
$.post( 'ajax/createuser.php', post, function(data){
// If it says "success" then we are happy
if( data.status == "success" ){
userCreated( data.data.username, data.data.groups );
}
else{
alert( "Bug By Jakob (c)" );
}
});
return false;
});
$( ".removeuserbutton" ).live( 'click', function() {
uid = $( this ).parent().parent().attr( 'x-uid' );
$("#deleteuserusername").html(uid);
$("#deleteusernamefield").val(uid);
$("#removeuserform").dialog( "open" );
return false;
});
$( "#creategroupbutton" )
.click(function(){
// Serialize the data
var post = $( "#creategroupdata" ).serialize();
// Ajax foo
$.post( 'ajax/creategroup.php', post, function(data){
if( data.status == "success" ){
groupCreated( data.data.groupname );
}
else{
alert( "something went wrong! sorry!" );
}
});
return false;
});
$( ".removegroupbutton" ).live( 'click', function(){
gid = $( this ).parent().parent().attr( 'x-gid' );
$("#removegroupgroupname").html(gid);
$("#removegroupnamefield").val(gid);
$("#removegroupform").dialog( "open" );
return false;
});
//#########################################################################
// Dialogs
//#########################################################################
2011-04-17 03:04:23 +04:00
// Removing users
$( "#removeuserform" ).dialog({
2011-04-16 19:49:57 +04:00
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Remove user": function() {
2011-04-17 03:04:23 +04:00
var post = $( "#removeuserdata" ).serialize();
$.post( 'ajax/removeuser.php', post, function(data){
if( data.status == "success" ){
userRemoved( uid );
2011-04-17 03:04:23 +04:00
}
else{
alert( "Bug By Jakob (c)" );
}
});
2011-04-16 19:49:57 +04:00
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
2011-04-17 03:04:23 +04:00
true;
2011-04-16 19:49:57 +04:00
}
});
2011-04-17 03:04:23 +04:00
2011-04-16 19:49:57 +04:00
// Dialog for adding users
$( "#removegroupform" ).dialog({
2011-04-16 19:49:57 +04:00
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
2011-04-17 01:26:55 +04:00
"Remove group": function(){
2011-04-17 03:04:23 +04:00
var post = $( "#removegroupdata" ).serialize();
$.post( 'ajax/removegroup.php', post, function(data){
if( data.status == "success" ){
groupRemoved( gid );
2011-04-17 03:04:23 +04:00
}
else{
alert( "Bug By Jakob (c)" );
}
2011-04-17 01:26:55 +04:00
});
2011-04-16 19:49:57 +04:00
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
2011-04-17 01:26:55 +04:00
close: function(){
2011-04-17 03:04:23 +04:00
true;
2011-04-16 19:49:57 +04:00
}
});
} );