From 17d243371c43847e9dbdeb21395afc0e8f5437ae Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Mon, 24 Apr 2023 04:36:57 +0300 Subject: edit lesson: putting all together; media with privile level --- public/assets/js/admin/edit_lesson.js | 198 +++++++++---------------- public/assets/js/admin/files.js | 264 ++++++++++++++++++++++++++++++++++ public/assets/js/admin/panel.js | 1 + 3 files changed, 336 insertions(+), 127 deletions(-) create mode 100644 public/assets/js/admin/files.js create mode 100644 public/assets/js/admin/panel.js (limited to 'public/assets/js') diff --git a/public/assets/js/admin/edit_lesson.js b/public/assets/js/admin/edit_lesson.js index ba31470..c5b0501 100644 --- a/public/assets/js/admin/edit_lesson.js +++ b/public/assets/js/admin/edit_lesson.js @@ -25,7 +25,6 @@ var files = []; // array of { id:.., title:.., type:.., path:.. } records const files_root = '/media/'; // files root firectory -var pageUrl = new URL(window.location.href); // an alternative--id when a lesson_id not exists (needed for file-upload) // ... consist of a 6-digit date string + random number up to 999 @@ -89,22 +88,9 @@ const upload_file_form = () => { '', '', - '
', - - '
', - '', - '', - '
', - - '
', - '', - '', - '
', - + '
', + '', + '', '
', '
', @@ -172,6 +158,13 @@ $(document).ready(function() { width: '100%' }); + // inti category (select2) + // ... + $("#privilege_id").select2({ + placeholder: 'Επίπεδο Πρόσβασης', + width: '100%' + }); + // // init tags (select2) // // ... @@ -200,33 +193,25 @@ $(document).ready(function() { */ if (id) { // if an `id` is given, load post .. then init_form - $.getJSON( "/admin/api/", { action: "get_post", id: id } ) + $.getJSON( "/admin/api/lesson/" + id ) .done( function (data) { - post = data; // keep post + lesson = data; // keep post // update fields' values - $('input[name=id]').val(post.id); // id - $('input[name=title]').val(post.title); // title - $('textarea[name=intro]').val(post.intro); // intro - $('textarea[name=body]').val(post.body); // body + $('input[name=id]').val(lesson.id); // id + $('input[name=title]').val(lesson.title); // title + $('textarea[name=intro]').val(lesson.intro); // intro + $('textarea[name=body]').val(lesson.body); // body $('input[name=status]').prop( // status 'checked', - ((post.status==1)? true : false)) + ((lesson.status==1)? true : false)) .trigger('change'); - $('#post-info').html('Δημιουργία: '+post.creation_date+'
Τελευταία Ενημέρωση: '+ post.update_date); - - $('#delete').prop( "disabled", false ); // enable delete button - - // if intro exist, show intro (optinal) field - if ((post.intro != null) && (post.intro != '')) { - $('.optional label[for=intro]').addClass('show'); // unhide section - } + // $('#post-info').html('Δημιουργία: '+post.creation_date+'
Τελευταία Ενημέρωση: '+ post.update_date); // parse media files; then draw - if (post.medias_json != null) { - files = JSON.parse(post.medias_json); - $('.optional label[for=media]').addClass('show'); // unhide section + if (lesson.medias_json != null) { + files = JSON.parse(lesson.medias_json); } draw_files_list(); // then draw the files list @@ -234,7 +219,6 @@ $(document).ready(function() { }); } else { // else ... just init_form - $('#delete').prop( "disabled", true ); // disable delete button for new post attempt init_form_values(); } @@ -265,15 +249,15 @@ $(document).ready(function() { }); categories.sort( compare_label ); // sort categories by breadcrumb - // // construct tags data - // // --- - // Object.keys(tags_response[0]).forEach( key => { - // tag = tags_response[0][key]; - // tags.push({ - // id: tag.id, - // label: tag.name - // }); - // }); + // construct tags data + // --- + /// Object.keys(tags_response[0]).forEach( key => { + /// tag = tags_response[0][key]; + /// tags.push({ + /// id: tag.id, + /// label: tag.name + /// }); + /// }); // construct provileges data // --- @@ -294,20 +278,20 @@ $(document).ready(function() { } else { // setup category_id options; select post's chosen category-id - set_parent_options( $('#category_id'), categories, post.category_id ); + set_parent_options( $('#course_id'), categories, lesson.course_id ); // parse selected tags // --- - var selected_tag_ids = []; - if (post.tags_json != null) { - var selected_tags = JSON.parse(post.tags_json); - selected_tags.forEach( itag => { selected_tag_ids.push(itag.id); }) - } + /// var selected_tag_ids = []; + /// if (post.tags_json != null) { + /// var selected_tags = JSON.parse(post.tags_json); + /// selected_tags.forEach( itag => { selected_tag_ids.push(itag.id); }) + /// } // setup tags options; select post's chosen tags - set_parent_options( $('#tags'), tags, selected_tag_ids ); + set_parent_options( $('#privilege_id'), privileges, lesson.privilege_id ); // ALSO: if tags selectd, show tags field - if (selected_tag_ids.length != 0) { $('.optional label[for=tags]').addClass('show'); } + // if (selected_tag_ids.length != 0) { $('.optional label[for=tags]').addClass('show'); } } }); @@ -347,28 +331,15 @@ $(document).ready(function() { var ref_icon, ref_class; files.forEach( item => { - // preapre [show|hide]-file as reference - if (item.reference == 1) { - ref_icon = ''; - ref_class = 'btn-default'; - - } else { - ref_icon = ''; - ref_class = 'btn-disabled'; - } - var copy = [ // copy button '' ].join(''); - var ref = [ // show|hide (is_reference) - '' - ].join(''); + var del = [ // delete 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), + 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_id' }, + { 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 + */ + $('#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 + * -> 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('') + + // add all other parent options + may_included.forEach( i => { + $('#included_id').append(``); + }); + + 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 + }); + + }); + +}); diff --git a/public/assets/js/admin/panel.js b/public/assets/js/admin/panel.js new file mode 100644 index 0000000..24b465e --- /dev/null +++ b/public/assets/js/admin/panel.js @@ -0,0 +1 @@ +console.log('Admin Panel!'); \ No newline at end of file -- cgit v1.2.3