// Globals
// -----------------------------------------------------------------------------
var petitions = [];
var table;
var types = [ '', 'Αίτηση', 'Ποινολόγιο' ];
// Supplamentary functions
// -----------------------------------------------------------------------------
// array.indexOf polyfill
// --- -- -- - - -
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
// return petitions record by petition-id
// --- -- -- - - -
function petition_record(id) {
var record = false;
petitions.forEach(el => { if (el.id == id) record = el; });
return record;
}
// create actions html (edit and delete buttons)
// --- -- -- - - -
function create_actions_html(id, signature) {
var del, edit, show;
del = "";
edit = [
''
].join('\n');
show = [
'',
'',
''
].join('\n');
return show +' '+ edit +' '+ del;
}
$(document).ready(function() {
// When document is ready
// -------------------------------------------------------------------------
/** init_table
* --- -- -- - - -
* get categories
* constuct categories array
* render dataTable
*/
function init_table() {
// get all categories from server (ajax)
$.getJSON( "/admin/get/petitions")
.done(function( json ) {
// then ...
console.log(json);
// reset categories
petitions.length = 0;
// construct petitions data
json.data.forEach( el => {
petitions.push({
id: parseInt(el.id),
subject: el.subject,
protocol: el.protocol,
username: el.UserName,
type: types[el.type_id],
signature: el.signature,
datepost: el.creation_date,
actions: create_actions_html(el.id, el.signature)
});
});
// destroy previous datatable table instances
$('#dt-petitions').dataTable().fnClearTable();
$('#dt-petitions').dataTable().fnDestroy();
// (re-)create categories datatable
table = $('#dt-petitions').DataTable({
language: { url: '/assets/js/DataTables/localization/Greek.json' },
data: petitions,
// ordering: false,
columns: [
{ data: 'subject', width: '42%' },
{ data: 'protocol' },
{ data: 'username' },
// { data: 'signature' },
{ data: 'type' },
{ data: 'datepost' },
{ data: 'actions', class: 'dt-actions' }
]
});
})
.fail(function( jqxhr, textStatus, error ) {
console.log( "Request Failed (" + error +")" );
});
}
init_table(); // init table on first run
/** When modal show-up
* -------------------------------------------------------------------------
* ... prepare the manage-category form
* ... update select2 with possible parents
*/
$('#managePetition').on('show.bs.modal', event => {
var button = $(event.relatedTarget); // Button that triggered the modal
var id = parseInt( button.data('id') ); // category_id from data-id
// var modal = $(this); // modal object
var petition = petition_record(id)
$('#managePetition form input[name=protocol]').val(
petition.protocol ? petition.protocol : ''
);
$('#managePetition form input[name=id]').val( id ); // category id (hidden)
})
// When form is submited
// -------------------------------------------------------------------------
$('#managePetition form').submit( event => {
event.preventDefault();
// prepare data to POST
var data = {
id: parseInt($('#managePetition form input[name=id]').val()),
protocol: $('#managePetition form input[name=protocol]').val() };
// select action url (add or update)
var request = '/admin/set/protocol';
// send POST request
$.post(request, data)
.done(function( data ) {
$('#managePetition').modal('hide'); // when done, close modal
init_table(); // reload table of categories
});
});
});