diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-04-25 03:40:09 +0300 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-04-25 03:40:09 +0300 |
| commit | 65d07ba64d45c77d2db16272710ec5702b34d641 (patch) | |
| tree | 4578793e288cd1a6dc08f87ab44d0e3568904c12 /public/assets/js/admin/lessons.js | |
| parent | 13753d58d6aceaea8f246040518d50f2eb06a8d9 (diff) | |
| download | classroom-65d07ba64d45c77d2db16272710ec5702b34d641.tar.gz classroom-65d07ba64d45c77d2db16272710ec5702b34d641.tar.bz2 classroom-65d07ba64d45c77d2db16272710ec5702b34d641.zip | |
front-end: putting everything together
Diffstat (limited to 'public/assets/js/admin/lessons.js')
| -rw-r--r-- | public/assets/js/admin/lessons.js | 158 |
1 files changed, 68 insertions, 90 deletions
diff --git a/public/assets/js/admin/lessons.js b/public/assets/js/admin/lessons.js index 8391f4d..1a08875 100644 --- a/public/assets/js/admin/lessons.js +++ b/public/assets/js/admin/lessons.js @@ -1,7 +1,7 @@ // Globals // ----------------------------------------------------------------------------- var categories = []; -var privileges = []; +var privileges = [ { id: 0, label: 'Δημόσιο' } ]; var lessons = []; var table; @@ -36,9 +36,6 @@ if (!Array.prototype.indexOf) } -// Supplamentary functions -// ----------------------------------------------------------------------------- - // function for sorting an array by 'label' attribute // --- -- -- - - - function compare_label (a,b) { @@ -65,25 +62,45 @@ function lesson_record(id) { return record; } +/** course(id) + * label of course with + * @param id (int) + */ +function course_(id) { + let reply = false; + categories.forEach( cat => { if (cat.id == id) reply = cat.label; }) + return reply; +} -// return children of parent -// --- -- -- - - - -function childs_of_parents(list) { - // if 'list' is integer then make it an one-item list - parents = (Number.isInteger(list)) ? [ list ] : list; - - var childs = []; - - if (parents.length == 0) { return []; } - - parents.forEach( pid => { - categories.forEach( el => { if (el.parent == pid) childs.push(el.id)}); - }); +/** 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; +} - var grandchilds = childs_of_parents(childs); - return childs.concat(grandchilds); -} +//// return children of parent +//// --- -- -- - - - +//function childs_of_parents(list) { +// // if 'list' is integer then make it an one-item list +// parents = (Number.isInteger(list)) ? [ list ] : list; +// +// var childs = []; +// +// if (parents.length == 0) { return []; } +// +// parents.forEach( pid => { +// categories.forEach( el => { if (el.parent == pid) childs.push(el.id)}); +// }); +// +// var grandchilds = childs_of_parents(childs); +// +// return childs.concat(grandchilds); +//} // create actions html (edit and delete buttons) @@ -132,6 +149,7 @@ $(document).ready(function() { // ... $("#privilege_id").select2({ placeholder: 'Επίπεδο Πρόσβασης', + dropdownParent: $('#manageLessons'), width: '100%' }); @@ -168,7 +186,9 @@ $(document).ready(function() { id: parseInt(el.id), title: el.title, course_id: el.course_id, + course: course_(el.course_id), privilege_id: el.privilege_id, + privilege: privilege_(el.privilege_id), actions: create_actions_html(el) }); }); @@ -188,8 +208,8 @@ $(document).ready(function() { columns: [ { data: 'id' }, { data: 'title' }, - { data: 'course_id' }, - { data: 'privilege_id' }, + { data: 'course' }, + { data: 'privilege' }, { data: 'actions' } ] }); @@ -269,89 +289,47 @@ $(document).ready(function() { * ... prepare the manage-category form * ... update select2 with possible parents */ - $('#manageCategory').on('show.bs.modal', event => { + $('#manageLesson').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=label]').val( // category label - (id) ? category_record(id).label : "" - ); - prepare_parents_element(id); // prepare <select> for parents + //// update modal literature + //// --- + //$('#manageCategory .modal-title').html( // modal title + // (id) ? 'Ενημέρωση Κατηγορίας' : 'Δημιουργία Κατηγορίας' + //); + //$('#manageCategory form input[name=id]').val( id ); // category id (hidden) + //$('#manageCategory form input[name=label]').val( // category label + // (id) ? category_record(id).label : "" + //); + //prepare_parents_element(id); // prepare <select> for parents }) -// /** prepare_parents_element() -// * --- -// * @param id (int) : id of selected category -// * -> Get all posible parents -// * -> call set_parent_options() to render the options -// */ -// function prepare_parents_element(id) { -// var parents = []; -// -// if (id) { // get all possible parents -// -// // id-category and subcategories are fobiden parents -// var forbiden = childs_of_parents(id); -// forbiden.push(id); -// -// // filter catogories adn prepare parents -// categories.forEach( el => { -// if (!forbiden.includes(el.id)) { -// parents.push({ -// id: el.id, -// breadcrumb: el.breadcrumb, -// }); -// } -// }); -// set_parent_options(parents, category_record(id).parent); -// -// -// } else { // new record; all categories are possible parrents -// categories.forEach( el => { -// parents.push({ -// id: el.id, -// breadcrumb: el.breadcrumb, -// }); -// }) -// set_parent_options(parents, -1); -// } -// } /** set_parent_options() + * * -> create options for parents <select> * -> choose selected parent + * + * NOTE: + * the targer <select> should be a select2 control + * * @param parents (array): array of parents * @param selected (int): selected parent */ - function set_select_options(parents, selected) { - // sort parents by breadcrumb - parents.sort( compare_breadcrumb ); + function set_parent_options(selector, list, selected_value = false) { - // remove any olded options from select2 - $('#parent_id option').each(function() { $(this).remove(); }); - - // first option is the root category - $('#parent_id').append('<option value="0">Root</option>') - - // add all other parent options - parents.forEach( i => { - $('#parent_id').append(`<option value="${i.id}">${i.breadcrumb}</option>`); - }); - - if (selected == -1) { // new category; remove any parent pre-selection - $('#parent_id').val(null).trigger('change'); - - } else { // category exists; choose selected parent - $('#parent_id').val(selected).trigger('change'); + list.forEach( li => { // attach options into <select> control + selector.append(`<option value="${li.id}">${li.label}</option>`); + }); + + if (selected_value !== false) { // mark selected option(s) + selector.val(selected_value).trigger('change'); + } + else { selector.val(null).trigger('change'); } } - } + |
