/** * Created by Marco Glietsch on 03.12.2018. */ $(document).ready(function (){ var is_ajax_fire = 0; var itemsPerPage = $('#itemsPerPage').find(":selected").val(); var page = 1; var current_page = 1; var total_page = 0; var orderColumn = ''; var orderDirection = ''; var searchText = ''; var p_orderColumn = ''; var p_orderDirection = ''; var p_searchText = ''; var wizardStates = { CLOSED: 0, FORWARD_TO_STEP_FORM: 1, STEP_FORM: 2, FORWARD_TO_STEP_FILES: 3, BACK_TO_STEP_FORM: 4, STEP_FILES: 5, FORWARD_TO_STEP_PUBLISH: 6, BACK_TO_STEP_FILES: 7, STEP_PUBLISH: 8, SAVE: 9, CANCEL: 10, CLOSE: 11, }; var wizardState = wizardStates.CLOSED; var wizardButtonStates = { NONE: 0, BUTTON_BACK: 1, BUTTON_NEXT: 2, BUTTON_SAVE: 3, BUTTON_CANCEL: 4, }; toastr.options = { 'newestOnTop' : false, 'timeOut' : 5000, }; reloadPagination(); function reloadPagination(){ var type = $('.pagination-sm').data('type'); if(type == undefined) { type = $('#ajax-container').data('type'); } var p_orderColumn = ''; var p_orderDirection = ''; var p_searchText = ''; if(orderColumn != '') { p_orderColumn = '&orderBy='+orderColumn; } if(orderDirection != '') { p_orderDirection= '&orderDirection='+orderDirection; } if(searchText != '') { p_searchText = '&search='+searchText; } $.ajax({ url: '/admin/' + type + '-index-ajax?page='+page+p_orderColumn+p_orderDirection+p_searchText, data: '', success: function (data){ if ((data.status.error == false)){ if(data.content.models) { total_page = data.content.models.last_page; current_page = data.content.models.current_page; } else { total_page = 1; current_page = 1; } if($('.pagination-sm').data('twbsPagination')) { $('.pagination-sm').twbsPagination('destroy'); } $('.pagination-sm').twbsPagination({ totalPages: total_page, visiblePages: 10, startPage: current_page, first: '<<', prev: '<', next: '>', last: '>>', nextClass: 'btn btn-outline-primary', prevClass: 'btn btn-outline-primary', lastClass: 'btn btn-outline-primary', firstClass: 'btn btn-outline-primary', pageClass: 'btn btn-outline-primary', activeClass: 'active', disabledClass: 'disabled', anchorClass: '', onPageClick: function (event, pageL){ page = pageL; if (is_ajax_fire != 0){ getPageData(type); } } }); $('#ajax-container').html(data.content.html); is_ajax_fire = 1; } } }); } /* Get Page Data*/ function getPageData(type){ //////////////////////// // Fallunterscheidung // //////////////////////// // Es handelt sich um den Adminbereich: Die Liste mit den Posts wird per Ajax neu geladen if($('#ajax-container').length) { var accordion = $('#ajax-container #accordion .collapse.show').data('page'); if(accordion !== undefined) { page = accordion; } $.ajax({ url: '/admin/' + type + '-index-ajax', dataType: 'json', data: { page: page, orderBy: orderColumn, orderDirection: orderDirection, itemsPerPage: itemsPerPage, search: searchText, }, success: function (data){ if ((data.status.error == false)){ $('#ajax-container').html(data.content.html); } } }); } // Es handelt sich um ein Inline-Edit: Es wird die ganze Seite neu geladen else { location.reload(); } } $(document).on('click', 'tr', function(e){ // e.preventDefault(); navigateTableElement(this); }); function updateWizardForm(data, wizardButtonState = wizardButtonStates.NONE){ var id = $('#crudForm input[name=id]').val(); var type = $('#crudForm').data('type'); if(wizardButtonState == wizardButtonStates.CANCEL) { wizardState = wizardStates.CANCEL; } switch (wizardState){ // Der Dialog wird noch nicht angezeigt case wizardStates.CLOSED: // Formulardaten laden $('#modalDialogContainer').html(data.content.html); // Navigationstabs verstecken und den nächsten Tab anzeigen $('.nav-tabs').hide(); $('.nav-tabs a[href="#general-details"]').tab('show'); var tabcount = $('#modal-tab-header-container li').length; // Nur benötigte Schaltflächen anzeigen $('.btn.wizard_step_back').hide(); if(tabcount < 1) { $('.btn.wizard_step_forward').hide(); $('.btn.save').hide(); } else if(tabcount == 1) { $('.btn.wizard_step_forward').hide(); $('.btn.save').show(); } else { $('.btn.wizard_step_forward').show(); $('.btn.save').hide(); } // Schaltflächen-Events registrieren $('.modal-footer').on('click', '.wizard_step_back', function (e){ e.preventDefault(); updateWizardForm(null, wizardButtonStates.BUTTON_BACK); }); $('.modal-footer').on('click', '.wizard_step_forward', function (e){ e.preventDefault(); updateWizardForm(null, wizardButtonStates.BUTTON_NEXT); }); $('.modal-footer').on('click', '.save', function (e){ e.preventDefault(); updateWizardForm(null, wizardButtonStates.BUTTON_SAVE); }); $('.modal-footer').on('click', '.cancel', function (e){ e.preventDefault(); updateWizardForm(null, wizardButtonStates.BUTTON_CANCEL); }); // Dropzone registrieren Dropzone.discover(); // Dialog anzeigen $('#modalDialogContainer').modal('show'); $('#modalDialogContainer').on('hidden.bs.modal', function(e){ e.stopImmediatePropagation(); if(wizardState != wizardStates.CLOSED) { wizardState = wizardStates.CANCEL; updateWizardForm(null); } }); wizardState = wizardStates.STEP_FORM; break; case wizardStates.FORWARD_TO_STEP_FORM: break; case wizardStates.STEP_FORM: // Überprüfe, welche Schaltfläche betätigt wurde switch(wizardButtonState) { case wizardButtonStates.BUTTON_NEXT: // Überprüfe die Formulareingabe $.ajax({ type: 'PUT', url: '/admin/' + type + '/' + id + '/check?wizard=wizard', data: $('#crudForm').serialize() + "&wizard=wizard", success: function (data){ var validatedForm = $(data.content.html).find('#crudForm').html(); $('#crudForm').html(validatedForm); var publishForm = $(data.content.html).find('#publish-details').html(); $('#publish-details').html(publishForm); if ((data.status.error == true)){ // Es sind Eingabefehler im Formular vorhanden showMessages(data.messages); } else{ // Eingabe war in Ordnung wizardState = wizardStates.FORWARD_TO_STEP_FILES; updateWizardForm(null); } }, }); break; case wizardButtonStates.BUTTON_SAVE: wizardState = wizardStates.SAVE; updateWizardForm(null); break; } break; case wizardStates.FORWARD_TO_STEP_FILES: // Tab "Dateien" anzeigen $('.nav-tabs a[href="#files-details"]').tab('show'); // Nur benötigte Schaltflächen anzeigen $('.btn.wizard_step_back').show(); if($(".nav-item a[href='#files-details']").length) { $('.btn.wizard_step_forward').show(); $('.btn.save').hide(); wizardState = wizardStates.STEP_FILES; } else { wizardState = wizardStates.FORWARD_TO_STEP_PUBLISH; updateWizardForm(null); } break; case wizardStates.BACK_TO_STEP_FORM: // Tab "Allgemein" anzeigen $('.nav-tabs a[href="#general-details"]').tab('show'); // Nur benötigte Schaltflächen anzeigen $('.btn.wizard_step_back').hide(); $('.btn.wizard_step_forward').show(); $('.btn.save').hide(); wizardState = wizardStates.STEP_FORM; break; case wizardStates.STEP_FILES: // Überprüfe, welche Schaltfläche betätigt wurde switch(wizardButtonState) { case wizardButtonStates.BUTTON_BACK: wizardState = wizardStates.BACK_TO_STEP_FORM; updateWizardForm(null); break; case wizardButtonStates.BUTTON_NEXT: wizardState = wizardStates.FORWARD_TO_STEP_PUBLISH; updateWizardForm(null); break; case wizardButtonStates.BUTTON_SAVE: wizardState = wizardStates.SAVE; updateWizardForm(null); break; } break; break; case wizardStates.FORWARD_TO_STEP_PUBLISH: // Tab "Veröffentlichen" anzeigen $('.nav-tabs a[href="#publish-details"]').tab('show'); // Nur benötigte Schaltflächen anzeigen $('.btn.wizard_step_back').show(); $('.btn.wizard_step_forward').hide(); $('.btn.save').show(); wizardState = wizardStates.STEP_PUBLISH; break; case wizardStates.BACK_TO_STEP_FILES: // Tab "Veröffentlichen" anzeigen $('.nav-tabs a[href="#files-details"]').tab('show'); // Nur benötigte Schaltflächen anzeigen $('.btn.wizard_step_back').show(); $('.btn.wizard_step_forward').show(); $('.btn.save').hide(); wizardState = wizardStates.STEP_FILES; break; case wizardStates.STEP_PUBLISH: // Überprüfe, welche Schaltfläche betätigt wurde switch(wizardButtonState) { case wizardButtonStates.BUTTON_BACK: wizardState = wizardStates.BACK_TO_STEP_FILES; updateWizardForm(null); break; case wizardButtonStates.BUTTON_SAVE: wizardState = wizardStates.SAVE; updateWizardForm(null); break; } break; case wizardStates.SAVE: // Daten speichern und Dialog schließen // Überprüfe die Formulareingabe $.ajax({ type: 'PUT', url: $('#crudForm').attr('action') + '?wizard=wizard', data: createFormData(true), success: function (data){ if ((data.status.error == true)){ // Es sind Eingabefehler im Formular vorhanden var validatedForm = $(data.content.html).find('#crudForm').html(); $('#crudForm').html(validatedForm); showMessages(data.messages); wizardStates.FORWARD_TO_STEP_FILES updateWizardForm(null); } else{ // Eingabe war in Ordnung showMessages(data.messages); wizardState = wizardStates.CLOSE; updateWizardForm(null); } }, }); break; case wizardStates.CANCEL: if(wizardState != wizardStates.CLOSED) { $.ajax({ type: 'DELETE', url: '/admin/' + type + '/' + id, data: { '_token': $('input[name=_token]').val(), '_method': 'DELETE', 'wizard': true }, success: function (data) { if ((data.status.error == true)){ } else{ } } }); } // Kein break case wizardStates.CLOSE: wizardState = wizardStates.CLOSED; getPageData(type); closeDialog(); break; } } function closeDialog() { $('.modal').modal('hide'); $('.modal-backdrop').remove(); } function registerEditForm(data, allreadyOpened = false){ if (allreadyOpened == false){ $('.modal-backdrop').remove(); } $('#modalDialogContainer').html(data.content.html); Dropzone.discover(); $('#modalDialogContainer').modal('show'); $('#crudForm').on('submit', function (e){ e.preventDefault(); submitEditForm(); }); $('.modal-footer').on('click', '.save', function (e){ submitEditForm(); }); } function submitEditForm(){ var type = $('#ajax-container').data('type'); if(type == undefined) { var type = $('#crudForm').data('type'); } $.ajax({ type: 'PUT', url: $('#crudForm').attr('action'), data: createFormData(), success: function (data){ if ((data.status.error == true)){ setTimeout(function (){ registerEditForm(data); showMessages(data.messages); }, 500); } else{ getPageData(type); showMessages(data.messages); $('#modalDialogContainer').modal('hide'); } } }); } function showMessages(messages) { $.each(messages, function(key, message){ if(message.title && message.message) { switch(message.type.toUpperCase()) { case 'SUCCESS': toastr.success(message.message, message.title); break; case 'INFO': toastr.info(message.message, message.title); break; case 'WARNING': toastr.warning(message.message, message.title); break; case 'ERROR': toastr.error(message.message, message.title); break; } } }) } function createFormData(wizard = false) { var element; element = $('#publish_website'); var publish_website = ''; if(element.length) { publish_website = '&publish_website=' + (element.is(':checked') ? 'true' : 'false'); } element = $('#publish_facebook'); var publish_facebook = ''; if(element.length) { publish_facebook = '&publish_facebook=' + (element.is(':checked') ? 'true' : 'false'); } element = $('#publish_twitter'); var publish_twitter = ''; if(element.length) { publish_twitter = '&publish_twitter=' + (element.is(':checked') ? 'true' : 'false'); } element = $('#publish_stadtanzeiger'); var publish_stadtanzeiger = ''; if(element.length) { publish_stadtanzeiger = '&publish_stadtanzeiger=' + (element.is(':checked') ? 'true' : 'false'); } var is_wizard = ''; if(wizard == true) { is_wizard = (wizard == true) ? '&wizard=wizard' : ''; } var data = $('#crudForm').serialize() + publish_website + publish_facebook + publish_twitter + publish_stadtanzeiger + is_wizard; return data; } /**/ // add a new post $(document).on('click', '.add-modal', function (e){ e.preventDefault(); var type = $(this).data('type'); var title = $(this).data('title'); $('.modal-title').text(title + ' erstellen'); $.ajax({ type: 'GET', url: '/admin/' + type + '/erstellen', success: function (data){ if (data.status.error == false){ wizardState = wizardStates.CLOSED; updateWizardForm(data); } else{ toastr.error('Fehler beim Erstellen', "Fehler"); } }, }); }); // Show a post $(document).on('click', '.show-modal', function (e){ e.preventDefault(); var id = $(this).data('id'); var type = $(this).data('type'); var title = $(this).data('title'); showPostDetails(id, type); }); function showPostDetails(id, type) { $.ajax({ type: 'GET', url: '/admin/' + type + '/' + id + '/details', success: function (data){ if (data.status.error == false){ $('.modal-backdrop').remove(); $('#modalDialogContainer').html(data.content.html); $('#modalDialogContainer').modal('show'); } else{ toastr.error('Fehler beim öffnen', "Fehler"); } }, }); } $(document).on('click', '.move-up', function (e){ e.preventDefault(); var id = $(this).data('id'); var type = $(this).data('type'); $.ajax({ type: 'GET', url: '/admin/' + type + '/' + id + '/auf', success: function (data){ if (data.status.error == false){ getPageData(type); } else{ toastr.error('Fehler beim bearbeiten', "Fehler"); } }, }); }); $(document).on('click', '.move-down', function (e){ e.preventDefault(); var id = $(this).data('id'); var type = $(this).data('type'); $.ajax({ type: 'GET', url: '/admin/' + type + '/' + id + '/ab', success: function (data){ if (data.status.error == false){ getPageData(type); } else{ toastr.error('Fehler beim bearbeiten', "Fehler"); } }, }); }); // Edit a post $(document).on('click', '.edit-modal', function (e){ e.preventDefault(); var id = $(this).data('id'); var type = $(this).data('type'); editPost(id, type); }); function editPost(id, type) { $.ajax({ type: 'GET', url: '/admin/' + type + '/' + id + '/bearbeiten', success: function (data){ if (data.status.error == false){ registerEditForm(data); } else{ toastr.error('Fehler beim bearbeiten', "Fehler"); } }, }); } $('.modal-footer').on('click', '.edit', function (e){ submitEditForm(); }); // Restore a post $(document).off('click', '.restore-modal'); $(document).on('click', '.restore-modal', function (e){ e.preventDefault(); var id = $(this).data('id'); var type = $(this).data('type'); $.ajax({ type: 'PUT', url: '/admin/' + type + '/' + id, data: { '_token': $('input[name=_token]').val(), '_method': 'PUT', 'restore': true, }, success: function (data){ if ((data.status.error == true)){ setTimeout(function (){ showMessages(data.messages); }, 500); } else{ getPageData(type); showMessages(data.messages); } } }); }); // delete a post $(document).on('click', '.delete-modal', function (e){ var id = $(this).data('id'); var type = $(this).data('type'); var title = $(this).data('title'); deletePost(id, type, title); }); function deletePost(id, type, title) { $('.modal-title').text(title + ' löschen'); $('.modal-body').text('Wirklich löschen?') .data('id', id) .data('type', type) .data('title', title); $('#deleteModal').modal('show'); } $('.modal-footer').on('click', '.delete', function (){ var id = $('.modal-body').data('id'); var type = $('.modal-body').data('type'); var title = $('.modal-body').data('title'); $.ajax({ type: 'DELETE', url: '/admin/' + type + '/' + id, data: { '_token': $('input[name=_token]').val(), '_method': 'DELETE', }, success: function (data){ if ((data.status.error == true)){ setTimeout(function (){ showMessages(data.messages); }, 500); } else{ getPageData(type); showMessages(data.messages); $('#editModal').modal('hide'); } } }); }); $(document).on('click', '.publish-modal', function (e){ e.preventDefault(); checked = $(this).data('value') == 1 ? true : false; checked = !checked; var id = $(this).data('id'); var type = $(this).data('type'); $.ajax({ type: 'PUT', url: '/admin/' + type + '/' + id, data: { '_token': $('input[name=_token]').val(), '_method': 'PUT', 'publish_website_action': checked, }, success: function (data){ if ((data.status.error == true)){ setTimeout(function (){ showMessages(data.messages); }, 500); } else{ getPageData(type); showMessages(data.messages); } } }); }); /**/ $(document).on('change', '#itemsPerPage', function(e){ itemsPerPage = $(this).find(":selected").val(); page = 1; $('.pagination-sm').twbsPagination('show', 1); }); // Auswerten von Tasteneingaben $(document).keydown(function(e){ //console.log(e.keyCode); switch(e.keyCode) { // Enter case 13: if(nothingHasFocus()){ var currentRow = $('.table .table-info'); // Ist eine Zeile markiert? if (currentRow.length){ // Ist der Details-Dialog bereits geöffnet? if (isModalDialogActive()){ } else{ var id = currentRow.data('id'); var type = currentRow.data('type'); editPost(id, type); } } } break; // ESC case 27: if(nothingHasFocus()){ e.preventDefault(); // Ist der Details-Dialog bereits geöffnet? if (isModalDialogActive()){ closeDialog(); } } break; // Leertaste case 32: if(nothingHasFocus()){ e.preventDefault(); var currentRow = $('.table .table-info'); // Ist eine Zeile markiert? if (currentRow.length){ // Ist der Details-Dialog bereits geöffnet? if (isModalDialogActive()){ closeDialog(); } else{ var id = currentRow.data('id'); var type = currentRow.data('type'); showPostDetails(id, type); } } } break; // Ende case 35: if(nothingHasFocus()){ e.preventDefault(); if (!isModalDialogActive()){ page = total_page; $('.pagination-sm').twbsPagination('show', page); } } break; // Position 1 case 36: if(nothingHasFocus()){ e.preventDefault(); if (!isModalDialogActive()){ page = 1; $('.pagination-sm').twbsPagination('show', page); } } break; // Pfeiltaste links case 37: if(nothingHasFocus()){ e.preventDefault(); if (!isModalDialogActive()){ if (page > 1){ page--; $('.pagination-sm').twbsPagination('show', page); } } // Details-Dialog else if($('#crudModal').data('action') == 'details') { $('#crudModal .nav-item .active').parent().prev().find('.nav-link').tab('show'); } } break; // Pfeiltaste auf case 38: if(nothingHasFocus()){ e.preventDefault(); if (!isModalDialogActive()){ navigateTable(-1); } } break; // Pfeiltaste rechts case 39: if(nothingHasFocus()){ e.preventDefault(); if(!isModalDialogActive()){ if (page < total_page){ page++; $('.pagination-sm').twbsPagination('show', page); } } // Details-Dialog else if($('#crudModal').data('action') == 'details') { $('#crudModal .nav-item .active').parent().next().find('.nav-link').tab('show'); } } break; // Pfeiltaste ab case 40: if(nothingHasFocus()){ e.preventDefault(); if (!isModalDialogActive()){ navigateTable(1); } } break; // Entfernen case 46: if(nothingHasFocus()) { e.preventDefault(); var currentRow = $('.table .table-info'); // Ist eine Zeile markiert? if(currentRow.length) { // Ist der Details-Dialog bereits geöffnet? if(isModalDialogActive()) { } else { var id = currentRow.data('id'); var type = currentRow.data('type'); var title = currentRow.data('title'); deletePost(id, type, title); } } } break; } }); function nothingHasFocus() { return !hasSomethingFocus(); } function hasSomethingFocus() { return $(':focus').length > 0 ? true : false; } function isModalDialogActive() { return $('.modal').is(':visible'); } function navigateTableElement(element) { var index; if($(element).is('tr')) { index = $(element).index(); } else { index = $(element).parents('tr').index(); } navigateTableIndex(index); } function navigateTableIndex(index) { var currentRow = $('.table .table-info'); var rows = $('.table tbody tr'); // Aktuelle Zeile nicht mehr markieren currentRow.removeClass('table-info'); rows.eq(index).addClass('table-info'); } function navigateTable(step = 1) { // Suche nach einer Tabellenzeile mit der Klasse table-info var currentRow = $('.table .table-info'); var rows = $('.table tbody tr'); var rowCount = rows.length; if(currentRow.length) { // Hole den Index der aktuellen Zeile var index = currentRow.index(); // Verrechne den index mit dem Schritt index += step; // Korrigiere ggfs. den index if(index < 0) { index = rowCount - 1; } else if(index >= rowCount) { index = 0; } navigateTableIndex(index); } // Es existiert noch keine markierte Zeile. else { if(step < 0) { navigateTableIndex(rowCount - 1); } else { navigateTableIndex(0); } } } $(document).on('click', '.column-sortable', function (e){ page = 1; var clickedColumn = $(this).data('name'); // Initialisierung, wenn noch keine Sortierspalte und -richtung festgelegt wurde if(orderDirection == '') { orderDirection = $(this).data('sort-direction'); } if(orderColumn != clickedColumn) { orderColumn = clickedColumn; orderDirection = 'ASC'; } else { if(orderDirection == 'ASC') { orderDirection = 'DESC'; } else { orderDirection = 'ASC'; } } page = 1; $('.pagination-sm').twbsPagination('show', page); // reloadPagination(); }); $(document).on('submit', '#searchForm', function(e){ e.preventDefault(); $('#searchForm input[type="text"]').blur(); reloadPagination(); }); $('#searchForm input[type="text"]').on('input propertyChange', function(){ var visible = Boolean($(this).val()); $(this).siblings('#searchClear').toggleClass('d-none', !visible); searchText = $(this).val(); }); $(document).on('click', '#searchClear', function(){ $(this).siblings('input[type="text"]').val('') .trigger('propertyChange') .focus(); $('#searchForm').submit(); }); // Workaround, um in einem modalen Bootstrap-Dialog im TinyMCE Editor Links, Bilder und Videos bearbeiten zu können $(document).on('focusin', function(e){ if($(e.target).closest('.mce-window').length){ e.stopImmediatePropagation(); } }); $('.quickedit.hidden').parent().hover( function(){ $(this).find('.quickedit').eq(0).slideDown(200); }, function(){ $(this).find('.quickedit').eq(0).slideUp(200); } ); $('.quickedit.hidden').parent().on('touchstart', function(e){ var element = $(this).find('.quickedit').eq(0); if(element.is(':visible')) { if($(e.touches[0].target).closest('.quickedit').length == 0) { element.slideUp(200); } } else { element.slideDown(200); } }); });