From 3e54d35fac3a2fd4ec07aaecc10a778d85fb09bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Tue, 26 Mar 2019 16:34:14 +0100 Subject: [PATCH] Added an simple event system and readded some functions from the legacy Part-DB. --- assets/js/app.js | 8 +- assets/ts_src/ajax_ui.ts | 33 ++++++- assets/ts_src/event_listeners.ts | 142 +++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 assets/ts_src/event_listeners.ts diff --git a/assets/js/app.js b/assets/js/app.js index a2db074e..99c9a05d 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -53,7 +53,13 @@ import {ajaxUI} from "../ts_src/ajax_ui"; window.ajaxUI = ajaxUI; -//Start AjaxUI +//Require all events; +require('../ts_src/event_listeners'); + + +//Start AjaxUI AFTER all event has been registered $(document).ready(ajaxUI.start()); + + //console.log('Hello Webpack Encore! Edit me in assets/js/app.js'); \ No newline at end of file diff --git a/assets/ts_src/ajax_ui.ts b/assets/ts_src/ajax_ui.ts index 43aca54e..e7d4e574 100644 --- a/assets/ts_src/ajax_ui.ts +++ b/assets/ts_src/ajax_ui.ts @@ -56,6 +56,10 @@ class AjaxUI { private statePopped : boolean = false; + //Listener savers; + private ajax_complete_listeners : Array<() => void> = []; + private start_listeners : Array<() => void> = []; + public constructor() { //Make back in the browser go back in history @@ -64,6 +68,27 @@ class AjaxUI { //$(document).ajaxComplete(this.onAjaxComplete.bind(this)); } + /** + * Register a function, which will be executed every time, a ajax request was successful. + * Should be used to register functions for elements in the #content div + * @param {() => void} func The function which should be registered. + */ + public addAjaxCompleteAction(func : ()=>void) + { + this.ajax_complete_listeners.push(func); + } + + /** + * Register a function, which will be called once, when start() is run. + * Should be used to register functions for elements outside the #content div. + * @param {() => void} func The function which should be registered. + */ + public addStartAction(func: ()=>void) + { + this.start_listeners.push(func); + } + + /** * Starts the ajax ui und execute handlers registered in addStartAction(). * Should be called in a document.ready, after handlers are set. @@ -82,9 +107,10 @@ class AjaxUI { this.registerForm(); this.fillTrees(); - this.initDataTables(); + //Trigger start event + $(document).trigger("ajaxUI:start"); } /** @@ -206,7 +232,7 @@ class AjaxUI { public registerLinks() { // Unbind all old handlers, so the things are not executed multiple times. - $('a').not(".link-external, [data-no-ajax]").unbind('click').click(function (event) { + $('a').not(".link-external, [data-no-ajax], .page-link").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 #) @@ -367,6 +393,9 @@ class AjaxUI { ajaxUI.registerLinks(); ajaxUI.registerForm(); ajaxUI.initDataTables(); + + //Trigger reload event + $(document).trigger("ajaxUI:reload"); } /** diff --git a/assets/ts_src/event_listeners.ts b/assets/ts_src/event_listeners.ts new file mode 100644 index 00000000..a0ebe799 --- /dev/null +++ b/assets/ts_src/event_listeners.ts @@ -0,0 +1,142 @@ +/* + * + * part-db version 0.1 + * Copyright (C) 2005 Christoph Lechner + * http://www.cl-projects.de/ + * + * part-db version 0.2+ + * Copyright (C) 2009 K. Jacobs and others (see authors.php) + * http://code.google.com/p/part-db/ + * + * Part-DB Version 0.4+ + * Copyright (C) 2016 - 2019 Jan Böhmer + * https://github.com/jbtronics + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * + */ + +/************************************ + * + * In this file all the functions that has to be called using AjaxUIoperation are registered. + * You can use AjaxUI:start and AjaxUI:reload events. + * + ***********************************/ + + +//Register greek input in search fields. +$(document).on("ajaxUI:start ajaxUI:reload", function() { + //@ts-ignore + $("input[type=text], textarea, input[type=search]").unbind("keydown").keydown(function (event : KeyboardEvent) { + let greek = event.altKey; + + let greek_char : string = ""; + if (greek){ + switch(event.key) { + case "w": //Omega + greek_char = '\u2126'; + break; + case "u": + case "m": //Micro + greek_char = "\u00B5"; + break; + case "p": //Phi + greek_char = "\u03C6"; + break; + case "a": //Alpha + greek_char = "\u03B1"; + break; + case "b": //Beta + greek_char = "\u03B2"; + break; + case "c": //Gamma + greek_char = "\u03B3"; + break; + case "d": //Delta + greek_char = "\u03B4"; + break; + case "l": //Pound + greek_char = "\u00A3"; + break; + case "y": //Yen + greek_char = "\u00A5"; + break; + case "o": //Yen + greek_char = "\u00A4"; + break; + case "1": //Sum symbol + greek_char = "\u2211"; + break; + case "2": //Integral + greek_char = "\u222B"; + break; + case "3": //Less-than or equal + greek_char = "\u2264"; + break; + case "4": //Greater than or equal + greek_char = "\u2265"; + break; + case "5": //PI + greek_char = "\u03c0"; + break; + case "q": //Copyright + greek_char = "\u00A9"; + break; + case "e": //Euro + greek_char = "\u20AC"; + break; + } + + if(greek_char=="") return; + + let $txt = $(this); + //@ts-ignore + let caretPos = $txt[0].selectionStart; + let textAreaTxt = $txt.val().toString(); + $txt.val(textAreaTxt.substring(0, caretPos) + greek_char + textAreaTxt.substring(caretPos) ); + + } + }); + //@ts-ignore + this.greek_once = true; +}); + +//Register bootstrap select picker +$(document).on("ajaxUI:reload", function () { + //@ts-ignore + $(".selectpicker").selectpicker(); +}); + +//Use bootstrap tooltips for the most tooltips +$(document).on("ajaxUI:start ajaxUI:reload", function () { + $(".tooltip").remove(); + $('a[title], button[title], span[title], h6[title]') + //@ts-ignore + .tooltip("hide").tooltip({container: "body", placement: "auto", boundary: 'window'}); +}); + + +//Need for proper body padding, with every navbar height +$(window).resize(function () { + let height : number = $('#navbar').height() + 10; + $('body').css('padding-top', height); + $('#fixed-sidebar').css('top', height); +}); + +$(window).on('load', function () { + let height : number = $('#navbar').height() + 10; + $('body').css('padding-top', height); + $('#fixed-sidebar').css('top', height); +}); \ No newline at end of file