From 177e10137934fc949f221af7e7e28bbeeb6b4bdf Mon Sep 17 00:00:00 2001 From: Piyush Vijay Date: Tue, 24 Jul 2018 12:20:09 +0530 Subject: [PATCH] Bux fix for onload and teardown of pages on refresh and ajax requests. Signed-Off-By: Piyush Vijay --- js/src/ajax.js | 7 +++++++ js/src/index.js | 30 +++++++++++++++--------------- libraries/classes/Scripts.php | 14 ++++++++------ 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/js/src/ajax.js b/js/src/ajax.js index 5fe4947888..61e93a9852 100644 --- a/js/src/ajax.js +++ b/js/src/ajax.js @@ -744,6 +744,13 @@ export let AJAX = { } } +/** + * Attach a generic event handler to clicks + * on pages and submissions of forms + */ +$(document).on('click', 'a', AJAX.requestHandler); +$(document).on('submit', 'form', AJAX.requestHandler); + /** * @todo this is to be removed when complete code is modularised * Exporsing module to window for use with non modular code diff --git a/js/src/index.js b/js/src/index.js index b3019a997c..452a988f19 100644 --- a/js/src/index.js +++ b/js/src/index.js @@ -3,17 +3,6 @@ import './variables/import_variables'; import { jQuery as $ } from './utils/JqueryExtended'; import files from './consts/files'; -/** - * This block of code is for importing javascript files needed - * for the first time loading of the page. - */ -let firstPage = window.location.pathname.replace('/', '').replace('.php', ''); -if (typeof files[firstPage] !== 'undefined') { - for (let i in files[firstPage]) { - AJAX.scriptHandler.add(files[firstPage][i]); - } -} - /** * Page load event handler */ @@ -79,8 +68,19 @@ $(function () { }); /** - * Attach a generic event handler to clicks - * on pages and submissions of forms + * This block of code is for importing javascript files needed + * for the first time loading of the page. */ -$(document).on('click', 'a', AJAX.requestHandler); -$(document).on('submit', 'form', AJAX.requestHandler); +let firstPage = window.location.pathname.replace('/', '').replace('.php', ''); +let indexStart = window.location.search.indexOf('target') + 7; +let indexEnd = window.location.search.indexOf('.php'); +let indexPage = window.location.search.slice(indexStart, indexEnd); +if (typeof files[firstPage] !== 'undefined' && firstPage.toLocaleLowerCase() !== 'index') { + for (let i in files[firstPage]) { + AJAX.scriptHandler.add(files[firstPage][i], 1); + } +} else if (typeof files[indexPage] !== 'undefined' && firstPage.toLocaleLowerCase() === 'index') { + for (let i in files[indexPage]) { + AJAX.scriptHandler.add(files[indexPage][i], 1); + } +} diff --git a/libraries/classes/Scripts.php b/libraries/classes/Scripts.php index a8f87ca5b3..29125bb576 100644 --- a/libraries/classes/Scripts.php +++ b/libraries/classes/Scripts.php @@ -195,18 +195,20 @@ class Scripts $code = 'AJAX.scriptHandler'; foreach ($this->_files as $file) { - $code .= sprintf( - '.add("%s",%d)', - Sanitize::escapeJsString($file['filename']), - $file['has_onload'] ? 1 : 0 - ); + if (strpos($file['filename'], ".js") !== false) { + $code .= sprintf( + '.add("%s",%d)', + Sanitize::escapeJsString($file['filename']), + $file['has_onload'] ? 1 : 0 + ); + } } $code .= ';'; $this->addCode($code); $code = '$(function() {'; foreach ($this->_files as $file) { - if ($file['has_onload']) { + if ($file['has_onload'] && strpos($file['filename'], ".js") !== false) { $code .= 'AJAX.fireOnload("'; $code .= Sanitize::escapeJsString($file['filename']); $code .= '");';