summaryrefslogtreecommitdiff
path: root/public/assets/js/admin/files.js
diff options
context:
space:
mode:
authorGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-04-27 03:47:30 +0300
committerGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-04-27 03:47:30 +0300
commit059e0d95d0c28bc5060e87e146eaf7411f51bf90 (patch)
tree7c21ac432f16dde2329a0d5954d70711eceb48e6 /public/assets/js/admin/files.js
parent26cd8ee99659ef1926c96a049c93645ffc9b169d (diff)
downloadgyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.tar.gz
gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.tar.bz2
gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.zip
skeleton commit; based on an anom project
Diffstat (limited to 'public/assets/js/admin/files.js')
-rw-r--r--public/assets/js/admin/files.js302
1 files changed, 302 insertions, 0 deletions
diff --git a/public/assets/js/admin/files.js b/public/assets/js/admin/files.js
new file mode 100644
index 0000000..e100b30
--- /dev/null
+++ b/public/assets/js/admin/files.js
@@ -0,0 +1,302 @@
+// Globals
+// -----------------------------------------------------------------------------
+var privileges = [ { id: 0, label: 'Δημόσιο' } ];
+var files = [];
+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;
+ };
+}
+
+
+// function for sorting an array by 'label' attribute
+// --- -- -- - - -
+function compare_label (a,b) {
+ if ( a.label < b.label ) { return -1; }
+ if ( a.label > b.label ) { return 1; }
+ return 0;
+}
+
+
+// return file record by file-id
+// --- -- -- - - -
+function privilege_record(id) {
+ var record = 0;
+ files.forEach(el => { if (el.id == id) record = el; });
+ return record;
+}
+
+
+/** privilege(id)
+ * label of privilege with
+ * @param id (int)
+ */
+function privilege_(id) {
+ let reply = false;
+ privileges.forEach( pri => { if (pri.id == id) reply = pri.label; });
+ return reply;
+}
+
+
+// create actions html (edit and delete buttons)
+// --- -- -- - - -
+function create_actions_html(id) {
+
+ var del, edit;
+
+ del = "";
+
+ edit = [
+ '<button type="button" class="btn btn-sm btn-warning"',
+ 'data-bs-toggle="modal" data-bs-target="#manageFiles"',
+ 'data-id="'+ id +'">',
+ '<i class="fas fa-pencil-alt"></i>',
+ '</button>'
+ ].join('\n');
+
+ return edit +' '+ del;
+}
+
+
+
+$(document).ready(function() {
+
+ // When document is ready
+ // -------------------------------------------------------------------------
+
+
+ /** init selec2 element (used to specify parent-category)
+ * --- -- -- - - -
+ */
+ $("#privilege_id").select2({
+ placeholder: 'Επίπεδο πρόσβασης',
+ dropdownParent: $('#manageFiles'),
+ width: '100%',
+ });
+
+
+ /** init_table
+ * --- -- -- - - -
+ * get categories
+ * constuct categories array
+ * render dataTable
+ */
+ function init_table() {
+
+ // get all categories from server (ajax)
+ $.getJSON( "/admin/api/files")
+ .done(function( json ) {
+ // then ...
+
+ // reset categories
+ files.length = 0;
+
+ // construct (new) categories data
+ Object.keys(json).forEach( key => {
+ el = json[key];
+ files.push({
+ id: el.id,
+ label: el.label,
+ path: el.path,
+ type: el.type,
+ privilege_id: ((el.privilege_id == null) ? 0 : el.privilege_id),
+ privilege: privilege_( ((el.privilege_id == null) ? 0 : el.privilege_id) ),
+ actions: create_actions_html(el.id)
+ });
+ });
+
+ // sort by breadcrumb
+ // categories.sort( compare_breadcrumb );
+
+ // destroy previous datatable table instances
+ $('#dt-files').dataTable().fnClearTable();
+ $('#dt-files').dataTable().fnDestroy();
+
+ // (re-)create categories datatable
+ table = $('#dt-files').DataTable({
+ language: { url: '/libs/DataTables/localization/Greek.json' },
+ data: files,
+ ordering: false,
+ columns: [
+ { data: 'id' },
+ { data: 'label' },
+ { data: 'path' },
+ { data: 'type' },
+ { data: 'privilege' },
+ { data: 'actions' }
+ ]
+ });
+
+ })
+ .fail(function( jqxhr, textStatus, error ) {
+ console.log( "Request Failed (" + error +")" );
+ });
+
+ }
+
+
+
+ $.getJSON( "/admin/api/privileges" )
+ .done( function( response ) {
+
+ // construct provileges data
+ // ---
+ response.forEach( el => {
+ privileges.push({
+ id: el.id,
+ label: el.label
+ });
+ });
+ privileges.sort( compare_label ); // sort categories by breadcrumb
+
+ init_table(); // init table on first run
+ });
+
+
+
+ /** When modal show-up
+ * -------------------------------------------------------------------------
+ * ... prepare the manage-category form
+ * ... update select2 with possible parents
+ */
+ $('#managePrivileges').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
+ // ---
+ $('#managePrivileges .modal-title').html( // modal title
+ (id) ? 'Ενημέρωση Επιπέδου Πρόσβασης' : 'Δημιουργία Επιπέδου Πρόσβασης'
+ );
+ $('#managePrivileges form input[name=id]').val( id ); // category id (hidden)
+ $('#managePrivileges form input[name=label]').val( // category label
+ (id) ? privilege_record(id).label : ""
+ );
+ prepare_included_element(id); // prepare <select> for parents
+ })
+
+ /** prepare_included_element()
+ * ---
+ * @param id (int) : id of selected privilege
+ * -> Get all posible included privileges
+ * -> call set_included_options() to render the options
+ */
+ function prepare_included_element(id) {
+ var may_included = [];
+
+ if (id) { // get all possible parents
+
+ // TODO:
+ // specify forbiden privilege IDs for being included
+
+ // filter privileges adn prepare possible included privileges
+ privileges.forEach( el => {
+ if (id != el.id) {
+ may_included.push({
+ id: el.id,
+ label: el.label
+ });
+ }
+ });
+ console.log(may_included);
+ set_included_options(may_included, privilege_record(id).included_id);
+
+
+ } else { // new record; all categories are possible parrents
+ privileges.forEach( el => {
+ may_included.push({
+ id: el.id,
+ label: el.label,
+ });
+ })
+ set_included_options(may_included, -1);
+ }
+ }
+
+ /** set_included_options()
+ * -> create options for parents <select>
+ * -> choose selected parent
+ * @param may_included (array): array of possible included privileges
+ * @param selected (int): selected included privilege
+ */
+ function set_included_options(may_included, selected) {
+ // sort parents by breadcrumb
+ // parents.sort( compare_breadcrumb );
+
+ // remove any olded options from select2
+ $('#included_id option').each(function() { $(this).remove(); });
+
+ // first option is the root category
+ $('#included_id').append('<option value="0">— κανένα —</option>')
+
+ // add all other parent options
+ may_included.forEach( i => {
+ $('#included_id').append(`<option value="${i.id}">${i.label}</option>`);
+ });
+
+ if (selected == -1) { // new category; remove any parent pre-selection
+ $('#included_id').val(0).trigger('change');
+
+ } else { // category exists; choose selected parent
+ $('#included_id').val(selected).trigger('change');
+ }
+ }
+
+
+
+
+ // When form is submited
+ // -------------------------------------------------------------------------
+
+ $('#manageCategory form').submit( event => {
+ event.preventDefault();
+
+ // prepare data to POST
+ var data = {
+ id: parseInt($('#manageCategory form input[name=id]').val()),
+ label: $('#manageCategory form input[name=label]').val(),
+ parent_id: parseInt($('#parent_id').select2('data')[0].id)
+ };
+
+ // select action url (add or update)
+ var request = '/admin/api/category/' + ((data.id == 0) ? 'add' : 'update');
+
+ // send POST request
+ $.post(request, data)
+ .done(function( data ) {
+
+ $('#manageCategory').modal('hide'); // when done, close modal
+
+ init_table(); // reload table of categories
+ });
+
+ });
+
+});