Skip to content

Commit

Permalink
Bux fix for onload and teardown of pages on refresh and ajax requests.
Browse files Browse the repository at this point in the history
Signed-Off-By: Piyush Vijay <piyushvijay.1997@gmail.com>
  • Loading branch information
Piyush3079 committed Jul 25, 2018
1 parent 5402f43 commit 177e101
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
7 changes: 7 additions & 0 deletions js/src/ajax.js
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions js/src/index.js
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
}
14 changes: 8 additions & 6 deletions libraries/classes/Scripts.php
Expand Up @@ -195,18 +195,20 @@ public function getDisplay()

$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 .= '");';
Expand Down

0 comments on commit 177e101

Please sign in to comment.