sintonia/airtime_mvc/public/js/airtime/user/user.js

122 lines
4.3 KiB
JavaScript
Raw Normal View History

2011-02-09 19:03:46 +01:00
function populateForm(entries){
//$('#user_details').show();
$('.errors').remove();
$('.success').remove();
2011-02-09 19:03:46 +01:00
$('#user_id').val(entries.id);
$('#login').val(entries.login);
$('#first_name').val(entries.first_name);
$('#last_name').val(entries.last_name);
$('#type').val(entries.type);
$('#email').val(entries.email);
$('#cell_phone').val(entries.cell_phone);
$('#skype').val(entries.skype_contact);
$('#jabber').val(entries.jabber_contact);
2011-02-09 19:03:46 +01:00
if (entries.id.length != 0){
$('#login').attr('readonly', 'readonly');
$('#password').val("xxxxxx");
} else {
$('#login').removeAttr('readonly');
$('#password').val("");
}
}
function rowClickCallback(row_id){
$.ajax({ url: baseUrl+'/User/get-user-data/id/'+ row_id +'/format/json', dataType:"json", success:function(data){
2011-02-09 19:03:46 +01:00
populateForm(data.entries);
}});
}
function removeUserCallback(row_id, nRow){
$.ajax({ url: baseUrl+'/User/remove-user/id/'+ row_id +'/format/json', dataType:"text", success:function(data){
2011-02-09 19:03:46 +01:00
var o = $('#users_datatable').dataTable().fnDeleteRow(nRow);
}});
}
function rowCallback( nRow, aData, iDisplayIndex ){
$(nRow).click(function(){rowClickCallback(aData['id'])});
if( aData['delete'] != "self"){
$('td:eq(4)', nRow).append( '<span class="ui-icon ui-icon-closethick"></span>').children('span').click(function(e){e.stopPropagation(); removeUserCallback(aData['id'], nRow)});
}else{
$('td:eq(4)', nRow).empty().append( '<span class="ui-icon ui-icon-closethick"></span>').children('span').click(function(e){e.stopPropagation(); alert("Can't delete yourself!")});
}
if ( aData['type'] == "A" )
{
$('td:eq(3)', nRow).html( $.i18n._('Admin') );
} else if ( aData['type'] == "H" )
{
$('td:eq(3)', nRow).html( $.i18n._('DJ') );
} else if ( aData['type'] == "G" )
{
$('td:eq(3)', nRow).html( $.i18n._('Guest') );
} else if ( aData['type'] == "P" )
{
$('td:eq(3)', nRow).html( $.i18n._('Program Manager') );
}
2011-02-09 19:03:46 +01:00
return nRow;
}
function populateUserTable() {
$('#users_datatable').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": baseUrl+"/User/get-user-data-table-info/format/json",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
},
2011-02-09 19:03:46 +01:00
"fnRowCallback": rowCallback,
"aoColumns": [
/* Id */ { "sName": "id", "bSearchable": false, "bVisible": false, "mDataProp": "id" },
/* user name */ { "sName": "login", "mDataProp": "login" },
/* first name */ { "sName": "first_name", "mDataProp": "first_name" },
/* last name */ { "sName": "last_name", "mDataProp": "last_name" },
/* user type */ { "sName": "type", "bSearchable": false, "mDataProp": "type" },
/* del button */ { "sName": "null as delete", "bSearchable": false, "bSortable": false, "mDataProp": "delete"}
],
"bJQueryUI": true,
"bAutoWidth": false,
"bLengthChange": false,
"oLanguage": datatables_dict,
2012-05-18 16:00:36 +02:00
"sDom": '<"H"lf<"dt-process-rel"r>>t<"F"ip>',
});
}
$(document).ready(function() {
populateUserTable();
2011-02-09 19:03:46 +01:00
//$('#user_details').hide();
var newUser = {login:"", first_name:"", last_name:"", type:"G", id:""};
$('#add_user_button').live('click', function(){populateForm(newUser)});
$('#save_user').live('click', function(){
var data = $('#user_form').serialize();
var url = baseUrl+'/User/add-user';
$.post(url, {format: "json", data: data}, function(data){
var json = $.parseJSON(data);
if (json.valid === "true") {
$('#content').empty().append(json.html);
populateUserTable();
} else {
//if form is invalid we only need to redraw the form
$('#user_form').empty().append($(json.html).find('#user_form').children());
}
setTimeout(removeSuccessMsg, 5000);
});
});
2011-02-09 19:03:46 +01:00
});