From e70176b6e6f539754974117625eeab06c4c5dd01 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Tue, 14 Mar 2023 00:23:55 +0200 Subject: main admin and front-end scripts --- html/assets/js/manage-posts.js | 177 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 html/assets/js/manage-posts.js (limited to 'html/assets/js/manage-posts.js') diff --git a/html/assets/js/manage-posts.js b/html/assets/js/manage-posts.js new file mode 100644 index 0000000..028c773 --- /dev/null +++ b/html/assets/js/manage-posts.js @@ -0,0 +1,177 @@ +// Globals +// ----------------------------------------------------------------------------- +var categories = []; +var posts = []; +var table; + +var pageUrl = new URL(window.location.href); + + +// Supplamentary functions +// ----------------------------------------------------------------------------- + +// function for sorting an array by 'breadcrumb' attribute +// --- -- -- - - - +function compare_breadcrumb (a,b) { + if ( a.breadcrumb < b.breadcrumb ) { return -1; } + if ( a.breadcrumb > b.breadcrumb ) { return 1; } + return 0; +} + +// return category record by category-id +// --- -- -- - - - +function category_record(id) { + var record = 0; + categories.forEach(el => { if (el.id == id) record = el; }); + + return record; +} + +// create actions html (edit and delete buttons) +// --- -- -- - - - +function create_actions_html(id) { + + var copy, edit; + + edit = [ + '' + ].join('\n'); + + copy = [ + '' + ].join('\n'); + + return copy +' '+ edit; +} + + + +$(document).ready(function() { + + // When document is ready + // ------------------------------------------------------------------------- + + /** init_table for 1st time + * --- -- -- - - - + * get posts + categories + * constuct posts + categories array + * render dataTable + */ + // function init_table() { + + $.when( // when loaded posts and categories + + $.getJSON( "/wiki/ajax", { action: "all_posts_props" } ), + + $.getJSON( "/wiki/ajax", { action: "get_categories_array", exclude: '0' } ) + + ).done( function( posts_response, categories_response ) { + + // each response is an array of 3 items: + // [0] => data (array) + // [1] => [success|fail] + // [2] => xhr object + + // construct categories data + Object.keys(categories_response[0]).forEach( key => { + cat = categories_response[0][key]; + categories.push({ + id: cat.rec.id, + breadcrumb: cat.breadcrumb.replaceAll('\t', ' / ') + }); + }); + + // construct posts data + Object.keys(posts_response[0]).forEach( key => { + po = posts_response[0][key]; + posts.push({ + id: po.id, + title: po.title, + category: category_record(po.category_id).breadcrumb, + updated: po.update_date, + status: ((po.status == 1) ? '' : ''), + actions: create_actions_html(po.id) + }); + }); + + // (re-)create categories datatable + table = $('#dt-posts').DataTable({ + language: { url: '/libs/DataTables/localization/Greek.json' }, + data: posts, + order: [[2, 'desc']], + columns: [ + { data: 'title' }, + { data: 'category' }, + { // update date + data: 'updated', + render: { + _: function (data, row, full) { return data; }, + display: function (data, row, full) { return mini_date(data); } + } + }, + { data: 'status' }, + { data: 'actions', width: '64px' } + ], + columnDefs: [{ + 'targets': [3,4], + 'orderable': false + }] + }); + + }); + + + function mini_date(d){ + let yy = d.substring(2,4); // year 2digit + let mo = d.substring(5,7); // month + let dd = d.substring(8,10); // dat + let h = d.substring(11,13); // hour + let m = d.substring(14,16); // minute + return dd +'/'+ mo +'/'+ yy +' '+ h +':'+ m; + } + + + + /** When user ckicks button + * ------------------------------------------------------------------------- + */ + + + /** on(click, .edit-btn) + * --- -- -- - - - + * We need to attach the event handler to some element + * higher up in the DOM tree that will remain present; + * so the handled attached on #dt-posts + * + * get post-id ; load editpost + */ + $('#dt-posts').on('click', 'button.js-edit', function (e) { + + var id = $(this).data('id'); + window.location.href = '/wiki/admin?action=editpost&id=' + id + + }); + + + $('#dt-posts').on('click', 'button.js-copy', function (e) { + var postUrl = pageUrl.origin +'/wiki/post?id=' + $(this).data('id'); + + navigator.clipboard + .writeText(postUrl) + .then(() => { + $(this).addClass('copied'); + setTimeout( () => { $(this).removeClass('copied'); }, 700); + }) + .catch(() => { + console.log("error on coping text"); + }); + }); + + +}); -- cgit v1.2.3