--- regenerate: true --- {% capture cache %} {% comment %} # ----------------------------------------------------------------------------- # ~/assets/themes/j1/core/js/adapter/webhook.js # Liquid template to create the Template Adapter for GH WebHooks # # Product/Info: # https://jekyll.one # Copyright (C) 2019 Juergen Adams # # J1 Template is licensed under the MIT License. # For details, see https://jekyll.one # # ----------------------------------------------------------------------------- # Test data: # {{ liquid_var | debug }} # # ----------------------------------------------------------------------------- {% endcomment %} /* # ----------------------------------------------------------------------------- # ~/assets/themes/j1/core/js/adapter/webhook.js # J1 Adapter for GH WebHooks # # Product/Info: # https://jekyll.one # # Copyright (C) 2019 Juergen Adams # # J1 Template is licensed under the MIT License. # For details, see https://jekyll.one # # ----------------------------------------------------------------------------- # Adapter generated: {{site.time}} # ----------------------------------------------------------------------------- */ 'use strict'; {% comment %} Load YML config data -------------------------------------------------------------------------------- {% endcomment %} {% assign environment = site.environment %} {% assign webhook_defaults = site.data.modules.defaults.webhook.defaults %} {% assign webhook_settings = site.data.modules.webhook.settings %} {% assign webhook_options = webhook_defaults | merge: webhook_settings %} j1.WebHook = (function (j1, window) { var environment = '{{environment}}'; // Set environment var state = 'not_started'; var logger; var logText; var json_data; return { // ------------------------------------------------------------------------- // Initialize WebHook Core // ------------------------------------------------------------------------- init: function () { // Config settings object var webhookConfig = {}; {% comment %} Set global variables -------------------------------------------------------------------------- {% endcomment %} logger = log4javascript.getLogger("j1.WebHook.adapter"); // Setup logger // Set|Log status state = 'initialize'; logger.info('state: ' + state); {% comment %} Load module config from yml data -------------------------------------------------------------------------- {% endcomment %} // Load module DEFAULTS|CONFIG webhookConfig = $.extend({}, {{webhook_options | replace: '=>', ':' }}); {% comment %} Deferred (AJAX) data load -------------------------------------------------------------------------- {% endcomment %} // Load color and font (json) data asychronously // See: https://stackoverflow.com/questions/3709597/wait-until-all-jquery-ajax-requests-are-done $.when( j1.WebHook.loadModals( webhookConfig ) ).done ( function( ) { // Run initializer j1webhook.init( webhookConfig ); j1.WebHook.modalEventHandler( webhookConfig ); }); state = 'finished'; logger.info('state: ' + state); // Set|Log status return true; }, // end init // ------------------------------------------------------------------------- // load the modals used by webhook // ------------------------------------------------------------------------- loadModals: function ( webhookOptions ) { // closure to pass additional data (#id) var cb_load_closure = function( id ) { return function ( responseTxt, statusTxt, xhr ) { if ( statusTxt == "success" ) { var logText = "WebHook Modals on ID " +id+ " loaded successfully" logger.info(logText); state = 'finished'; logger.info('state: ' + state); } if ( statusTxt == "error" ) { state = 'failed'; logger.error('state: ' + state); var logText = "WebHook Modals on ID " +id+ " loading failed. Error: " + xhr.status + ": " + xhr.statusText; logger.error(logText); } }; }; var id = "#" + webhookOptions.webhook_modal_id; var selector = $(id); if ( selector.length ) { var modal_html_data_path = webhookOptions.modal_html_data_path + " > *"; selector.load( modal_html_data_path, cb_load_closure( id ) ); } else { var logText = "WebHook Modals not loaded" logger.info(logText); // Set processing to 'finished' to display page state = 'finished'; logger.info('state: ' + state); } return true; }, // END loadModals // ------------------------------------------------------------------------- // EventHandler for J1 WebHook // Manage button click events for all BS Modals // See: https://www.nickang.com/add-event-listener-for-loop-problem-in-javascript/ // ------------------------------------------------------------------------- modalEventHandler: function ( options ) { var webHookOptions = options; var logText; logText = "modalEventHandler: Initialize button click events"; logger.info(logText); var modalsLoaded = setInterval(function() { if ( j1.WebHook.state() === 'finished' ) { var options = webHookOptions; var current_page = window.location.pathname; var modalButtons = document.querySelectorAll('a.btn'); var stopScrolling = true; //options.stop_scrolling; // Manage button click events for modal "fluidModalPullRepo" // ------------------------------------------------------------------- $(document).on('shown.bs.modal','#fluidModalPullRepo', function () { if ( stopScrolling ) { $('body').addClass('stop-scrolling'); } modalButtons.forEach(function(button, index) { button.addEventListener('click', function() { // acceptGitPullButton // ------------------------------------------------------------- if (this.id === 'acceptGitPullButton') { logger.info('User clicked acceptGitPullButton'); return true; } // declineGitPullButton // ------------------------------------------------------------- if (this.id === 'declineGitPullButton') { logger.info('User clicked declineGitPullButton'); return true; } }); }); }); // Manage post events on modal "fluidModalPullRepo" // ------------------------------------------------------------------- $(document).on('hide.bs.modal','#fluidModalPullRepo', function () { var response; logger.info('User closed fluidModalPullRepo'); response = j1.WebHook.getGitState(); json_data = JSON.stringify(response, undefined, 2); logText = 'Response received: ' + json_data; logger.info(logText); $('body').removeClass('stop-scrolling'); logger.info("Hey Du Kackarsch!!"); )}; // clear interval checking clearInterval(modalsLoaded); } // END j1.WebHook.state 'finished' }, 50); // END setInterval logText = "modalEventHandler: EventHandler initialized"; logger.info(logText); return true; }, // END modalEventHandler // ------------------------------------------------------------------------- // Returns the current user state collected from /status endpoint // if the web is detected as an app // See: https://stackoverflow.com/questions/3709597/wait-until-all-jquery-ajax-requests-are-done // ------------------------------------------------------------------------- getGitState: function (params, cbData) { var cbData = cbData; var parameters = params == '' ? 'all' : params; var url = 'http://localhost:13000/git'; $.when(ajax1().done (function (response) { return response; }); function ajax1() { return $.ajax({ url: url, success: function(data) { if (typeof data == 'string') { return JSON.parse(data); } if (typeof data == 'object') { return data; } } }); } }, // END getGitState // ------------------------------------------------------------------------- // returns the current (processing) state of the module // ------------------------------------------------------------------------- state: function () { return state; } // end state }; // end return })(j1, window); {% endcapture %} {{ cache | strip_empty_lines }} {% assign cache = nil %}