diff --git a/assets/js/app.js b/assets/js/app.js index c6e21cec..a2db074e 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -45,6 +45,8 @@ require('jquery-form'); window.$ = window.jQuery = require("jquery"); require('bootstrap-treeview/src/js/bootstrap-treeview'); +require('./datatables.js'); + require('../ts_src/ajax_ui'); import {ajaxUI} from "../ts_src/ajax_ui"; diff --git a/public/helper/datatables.js b/assets/js/datatables.js similarity index 97% rename from public/helper/datatables.js rename to assets/js/datatables.js index 0269ed13..90a8164c 100644 --- a/public/helper/datatables.js +++ b/assets/js/datatables.js @@ -13,6 +13,10 @@ * Initializes the datatable dynamically. */ $.fn.initDataTables = function(config, options) { + + //Update default used url, so it reflects the current location (useful on single side apps) + $.fn.initDataTables.defaults.url = window.location.origin + window.location.pathname; + var root = this, config = $.extend({}, $.fn.initDataTables.defaults, config), state = '' diff --git a/assets/ts_src/ajax_ui.ts b/assets/ts_src/ajax_ui.ts index 7ed18ea1..43aca54e 100644 --- a/assets/ts_src/ajax_ui.ts +++ b/assets/ts_src/ajax_ui.ts @@ -75,9 +75,16 @@ class AjaxUI { this.BASE = $("body").data("base-url") + "/"; console.info("Base path is " + this.BASE); + //Show flash messages + $(".toast").toast('show'); + this.registerLinks(); this.registerForm(); this.fillTrees(); + + + this.initDataTables(); + } /** @@ -198,7 +205,8 @@ class AjaxUI { */ public registerLinks() { - $('a').not(".link-external, [data-no-ajax]").click(function (event) { + // Unbind all old handlers, so the things are not executed multiple times. + $('a').not(".link-external, [data-no-ajax]").unbind('click').click(function (event) { let a = $(this); let href = $.trim(a.attr("href")); //Ignore links without href attr and nav links ('they only have a #) @@ -232,6 +240,9 @@ class AjaxUI { } + /** + * Show the progressbar + */ public showProgressBar() { //Blur content background @@ -245,6 +256,9 @@ class AjaxUI { }); } + /** + * Hides the progressbar. + */ public hideProgressBar() { // @ts-ignore @@ -322,7 +336,7 @@ class AjaxUI { ajaxUI.hideProgressBar(); //Parse response to DOM structure - let dom = $.parseHTML(responseText); + let dom = $.parseHTML(responseText, document, true); //And replace the content container $("#content").replaceWith($("#content", dom)); //Replace login menu too (so everything is up to date) @@ -332,6 +346,10 @@ class AjaxUI { $("#message-container").replaceWith($('#message-container', dom)); $(".toast").toast('show'); + //Inject the local scripts + $("#script-reloading").replaceWith($('#script-reloading', dom)); + + //Set new title let title = extractTitle(responseText); document.title = title; @@ -342,14 +360,38 @@ class AjaxUI { history.pushState(null, title, this.url); } else { //Clear pop state - ajaxUI.statePopped; + ajaxUI.statePopped = true; } //Do things on the new dom ajaxUI.registerLinks(); ajaxUI.registerForm(); + ajaxUI.initDataTables(); + } + /** + * Init all datatables marked with data-datatable based on their data-settings attribute. + */ + protected initDataTables() + { + //Find all datatables and init it. + let $tables = $('[data-datatable]'); + $.each($tables, function(index, table) { + let $table = $(table); + let settings = $table.data('settings'); + //@ts-ignore + var promise = $('#part_list').initDataTables(settings, + { + "fixedHeader": { header: $(window).width() >= 768, //Only enable fixedHeaders on devices with big screen. Fixes scrolling issues on smartphones. + headerOffset: $("#navbar").height()} + }); + + //Register links. + promise.then(ajaxUI.registerLinks); + }); + + console.debug('Datatables inited.'); } } diff --git a/templates/base.html.twig b/templates/base.html.twig index 9cce8001..fc847050 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -105,7 +105,7 @@
{% trans %}user.language_select{% endtrans %} + app.request.attributes.get('_route_params')|merge({'_locale': 'en'})) }}"> {% trans %}language.english{% endtrans %} @@ -268,34 +268,9 @@ {% block javascripts %} {{ encore_entry_script_tags('app') }} - - - {% endblock %} - - - {% block scripts %} {% endblock %} diff --git a/templates/parts_list.html.twig b/templates/parts_list.html.twig index 6cb6e9ae..9a0af43c 100644 --- a/templates/parts_list.html.twig +++ b/templates/parts_list.html.twig @@ -6,7 +6,7 @@