summaryrefslogtreecommitdiff
path: root/public/assets/js
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/js')
-rw-r--r--public/assets/js/panel/admin_petitions.js184
1 files changed, 184 insertions, 0 deletions
diff --git a/public/assets/js/panel/admin_petitions.js b/public/assets/js/panel/admin_petitions.js
new file mode 100644
index 0000000..7b65f10
--- /dev/null
+++ b/public/assets/js/panel/admin_petitions.js
@@ -0,0 +1,184 @@
+// 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(id, signature) {
+
+ var del, edit, show;
+
+ del = "";
+ edit = [
+ '<button type="button" class="btn btn-xs btn-primary"',
+ 'data-bs-toggle="modal" data-bs-target="#managePetition"',
+ 'data-id="'+ id +'">',
+ '<i class="fa-solid fa-stamp"></i>',
+ '</button>'
+ ].join('\n');
+
+ show = [
+ '<a type="button" target="_blank" class="btn btn-xs btn-warning"',
+ 'href="/petition/signature/'+ signature +'">',
+ '<i class="fa-regular fa-circle-down"></i>',
+ '</a>'
+ ].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,
+ 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: 'datepost' },
+ { data: 'actions', width: '96px' }
+ ]
+ });
+
+ })
+ .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
+ });
+
+ });
+
+});