@ -6,135 +6,136 @@ define(function() {
Users . init = function ( ) {
Users . init = function ( ) {
var yourid = ajaxify . variables . get ( 'yourid' ) ;
var yourid = ajaxify . variables . get ( 'yourid' ) ;
function isUserAdmin ( element ) {
$ ( '#users-container' ) . on ( 'click' , '.select' , function ( ) {
var parent = $ ( element ) . parents ( '.users-box' ) ;
var userBox = $ ( this ) . parents ( '.users-box' ) ;
return ( parent . attr ( 'data-admin' ) !== "0" ) ;
var isSelected = userBox . hasClass ( 'selected' ) ;
}
userBox . toggleClass ( 'selected' , ! isSelected ) ;
$ ( this ) . toggleClass ( 'fa-square-o' , isSelected ) ;
function isUserBanned ( element ) {
$ ( this ) . toggleClass ( 'fa-check-square-o' , ! isSelected ) ;
var parent = $ ( element ) . parents ( '.users-box' ) ;
} ) ;
return ( parent . attr ( 'data-banned' ) !== "" && parent . attr ( 'data-banned' ) !== "0" ) ;
}
function getUID ( element ) {
function getSelectedUids ( ) {
var parent = $ ( element ) . parents ( '.users-box' ) ;
var uids = [ ] ;
return parent . attr ( 'data-uid' ) ;
$ ( '#users-container .users-box.selected' ) . each ( function ( ) {
uids . push ( $ ( this ) . attr ( 'data-uid' ) ) ;
} ) ;
return uids ;
}
}
function updateUserBanButtons ( elements ) {
function update ( className , state ) {
elements . each ( function ( index , element ) {
$ ( '#users-container .users-box.selected ' + className + '.label' ) . each ( function ( ) {
var banBtn = $ ( element ) ;
$ ( this ) . toggleClass ( 'hide' , ! state ) ;
var uid = getUID ( banBtn ) ;
banBtn . toggleClass ( 'disabled' , isUserAdmin ( banBtn ) || uid === yourid ) ;
banBtn . toggleClass ( 'btn-warning' , isUserBanned ( banBtn ) ) ;
} ) ;
} ) ;
}
}
function updateUserAdminButtons ( elements ) {
function unselectAll ( ) {
elements . each ( function ( index , element ) {
$ ( '#users-container .users-box.selected' ) . removeClass ( 'selected' )
var adminBtn = $ ( element ) ;
. find ( '.select' ) . toggleClass ( 'fa-square-o' , true ) . toggleClass ( 'fa-check-square-o' , false ) ;
var uid = getUID ( adminBtn ) ;
adminBtn . toggleClass ( 'disabled' , ( isUserAdmin ( adminBtn ) && uid === yourid ) || isUserBanned ( adminBtn ) ) ;
adminBtn . toggleClass ( 'btn-success' , isUserAdmin ( adminBtn ) ) ;
} ) ;
}
}
function updateButtons ( ) {
function removeSelected ( ) {
updateUserBanButtons ( $ ( '.ban-btn' ) ) ;
$ ( '#users-container .users-box.selected' ) . remove ( ) ;
updateUserAdminButtons ( $ ( '.admin-btn' ) ) ;
}
}
$ ( '#users-container' ) . on ( 'click' , '.ban-btn' , function ( ) {
$ ( '.ban-user' ) . on ( 'click' , function ( ) {
var banBtn = $ ( this ) ;
var uids = getSelectedUids ( ) ;
var parent = banBtn . parents ( '.users-box' ) ;
if ( ! uids . length ) {
var uid = getUID ( banBtn ) ;
return ;
}
if ( ! isUserAdmin ( banBtn ) ) {
bootbox . confirm ( 'Do you really want to ban?' , function ( confirm ) {
if ( isUserBanned( banBtn ) ) {
if ( confirm ) {
socket . emit ( 'admin.user. un banUser', uid , function ( err ) {
socket . emit ( 'admin.user. banUsers ', uid s , function ( err ) {
if ( err ) {
if ( err ) {
return app . alertError ( err . message ) ;
return app . alertError ( err . message ) ;
}
}
app . alertSuccess ( 'This user is unbanned!' ) ;
app . alertSuccess ( 'User(s) banned!' ) ;
} ) ;
banBtn . removeClass ( 'btn-warning' ) ;
parent . attr ( 'data-banned' , 0 ) ;
updateUserAdminButtons ( $ ( '.admin-btn' ) ) ;
} else {
bootbox . confirm ( 'Do you really want to ban "' + parent . attr ( 'data-username' ) + '"?' , function ( confirm ) {
if ( confirm ) {
socket . emit ( 'admin.user.banUser' , uid , function ( err ) {
if ( err ) {
return app . alertError ( err . message ) ;
}
app . alertSuccess ( 'This user is banned!' ) ;
} ) ;
banBtn . addClass ( 'btn-warning' ) ;
parent . attr ( 'data-banned' , 1 ) ;
updateUserAdminButtons ( $ ( '.admin-btn' ) ) ;
}
} ) ;
} ) ;
update ( '.ban' , true ) ;
unselectAll ( ) ;
}
}
} ) ;
} ) ;
$ ( '.unban-user' ) . on ( 'click' , function ( ) {
var uids = getSelectedUids ( ) ;
if ( ! uids . length ) {
return ;
}
}
return false ;
socket . emit ( 'admin.user.unbanUsers' , uids , function ( err ) {
if ( err ) {
return app . alertError ( err . message ) ;
}
app . alertSuccess ( 'User(s) unbanned!' ) ;
} ) ;
update ( '.ban' , false ) ;
unselectAll ( ) ;
} ) ;
} ) ;
$ ( '#users-container' ) . on ( 'click' , '.admin-btn' , function ( ) {
$ ( '.admin-user' ) . on ( 'click' , function ( ) {
var adminBtn = $ ( this ) ;
var uids = getSelectedUids ( ) ;
var parent = adminBtn . parents ( '.users-box' ) ;
if ( ! uids . length ) {
var uid = getUID ( adminBtn ) ;
return ;
}
if ( uid === yourid ) {
app . alert ( {
if ( uids . indexOf ( yourid ) !== - 1 ) {
title : 'Error' ,
app . alertError ( 'You can\'t remove yourself as Administrator!' ) ;
message : 'You can\'t remove yourself as Administrator!' ,
} else {
type : 'danger' ,
socket . emit ( 'admin.user.makeAdmins' , uids , function ( err ) {
timeout : 5000
} ) ;
} else if ( ! isUserAdmin ( adminBtn ) ) {
socket . emit ( 'admin.user.makeAdmin' , uid , function ( err ) {
if ( err ) {
if ( err ) {
return app . alertError ( err . message ) ;
return app . alertError ( err . message ) ;
}
}
app . alertSuccess ( ' This user is now an administrator .') ;
app . alertSuccess ( 'User(s) are now administrators.' ) ;
} ) ;
} ) ;
parent . attr ( 'data-admin' , 1 ) ;
updateUserBanButtons ( $ ( '.ban-btn' ) ) ;
update ( '.administrator' , true ) ;
updateUserAdminButtons ( $ ( '.admin-btn' ) ) ;
unselectAll ( ) ;
} else if ( uid !== yourid ) {
}
bootbox . confirm ( 'Do you really want to remove this user as admin "' + parent . attr ( 'data-username' ) + '"?' , function ( confirm ) {
} ) ;
$ ( '.remove-admin-user' ) . on ( 'click' , function ( ) {
var uids = getSelectedUids ( ) ;
if ( ! uids . length ) {
return ;
}
if ( uids . indexOf ( yourid ) !== - 1 ) {
app . alertError ( 'You can\'t remove yourself as Administrator!' ) ;
} else {
bootbox . confirm ( 'Do you really want to remove admins?' , function ( confirm ) {
if ( confirm ) {
if ( confirm ) {
socket . emit ( 'admin.user.removeAdmin' , uid , function ( err ) {
socket . emit ( 'admin.user.removeAdmin s ', uid s , function ( err ) {
if ( err ) {
if ( err ) {
return app . alertError ( err . message ) ;
return app . alertError ( err . message ) ;
}
}
app . alertSuccess ( 'This user is no longer an administrator.' ) ;
app . alertSuccess ( ' User(s) are no longer administrators .') ;
} ) ;
} ) ;
parent . attr ( 'data-admin' , 0 ) ;
updateUserBanButtons ( $ ( '.ban-btn' ) ) ;
update ( '.administrator' , false ) ;
updateUserAdminButtons ( $ ( '.admin-btn' ) ) ;
u nselectAll( ) ;
}
}
} ) ;
} ) ;
}
}
return false ;
} ) ;
} ) ;
$ ( '#users-container' ) . on ( 'click' , '.delete-btn' , function ( ) {
$ ( '.delete-user' ) . on ( 'click' , function ( ) {
var deleteBtn = $ ( this ) ;
var uids = getSelectedUids ( ) ;
var parent = deleteBtn . parents ( '.users-box' ) ;
if ( ! uids . length ) {
var uid = getUID ( deleteBtn ) ;
return ;
bootbox . confirm ( '<b>Warning!</b><br/>Do you really want to delete this user "' + parent . attr ( 'data-username' ) + '"?<br/> This action is not reversable, all user data and content will be erased!' , function ( confirm ) {
}
bootbox . confirm ( '<b>Warning!</b><br/>Do you really want to delete user(s)?<br/> This action is not reversable, all user data and content will be erased!' , function ( confirm ) {
if ( confirm ) {
if ( confirm ) {
socket . emit ( 'admin.user.deleteUser' , uid , function ( err ) {
socket . emit ( 'admin.user.deleteUser s ', uid s , function ( err ) {
if ( err ) {
if ( err ) {
return app . alertError ( err . message ) ;
return app . alertError ( err . message ) ;
}
}
parent . remove ( ) ;
app . alertSuccess ( 'User Deleted!' ) ;
app . alertSuccess ( 'User(s) Deleted!' ) ;
removeSelected ( ) ;
unselectAll ( ) ;
} ) ;
} ) ;
}
}
} ) ;
} ) ;
@ -179,7 +180,6 @@ define(function() {
} ) ;
} ) ;
}
}
$ ( 'document' ) . ready ( function ( ) {
$ ( 'document' ) . ready ( function ( ) {
var timeoutId = 0 ,
var timeoutId = 0 ,
@ -230,23 +230,17 @@ define(function() {
. addClass ( 'label-success' )
. addClass ( 'label-success' )
. removeClass ( 'label-danger' ) ;
. removeClass ( 'label-danger' ) ;
}
}
updateButtons ( ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} , 250 ) ;
} , 250 ) ;
} ) ;
} ) ;
updateButtons ( ) ;
handleUserCreate ( ) ;
handleUserCreate ( ) ;
function onUsersLoaded ( users ) {
function onUsersLoaded ( users ) {
ajaxify . loadTemplate ( 'admin/users' , function ( adminUsers ) {
ajaxify . loadTemplate ( 'admin/users' , function ( adminUsers ) {
var html = $ ( templates . parse ( templates . getBlock ( adminUsers , 'users' ) , { users : users } ) ) ;
var html = $ ( templates . parse ( templates . getBlock ( adminUsers , 'users' ) , { users : users } ) ) ;
$ ( '#users-container' ) . append ( html ) ;
$ ( '#users-container' ) . append ( html ) ;
updateUserBanButtons ( html . find ( '.ban-btn' ) ) ;
updateUserAdminButtons ( html . find ( '.admin-btn' ) ) ;
} ) ;
} ) ;
}
}