// Globals
// -----------------------------------------------------------------------------
var petitions = [];
var table;
// 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(signature) {
var del, edit, show;
del = "";
edit = "";
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( "/user/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,
signature: el.signature,
datepost: el.creation_date,
actions: create_actions_html(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: '55%' },
{ data: 'protocol' },
// { data: 'signature' },
{ data: 'datepost' },
{ data: '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
*/
$('#manageCategory').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
// update modal literature
// ---
$('#manageCategory .modal-title').html( // modal title
(id) ? 'Ενημέρωση Κατηγορίας' : 'Δημιουργία Κατηγορίας'
);
$('#manageCategory form input[name=id]').val( id ); // category id (hidden)
$('#manageCategory form input[name=title]').val( // category title
(id) ? category_record(id).title : ""
);
prepare_parents_element(id); // prepare