1
1
/**
2
2
* pjax.ts
3
- * Copyright(c) 2021 Aaron Hedges <[email protected] >
3
+ * Copyright(c) 2025 Aaron Hedges <[email protected] >
4
4
* MIT Licensed
5
5
*
6
6
* PJAX is a technique for speeding up webpages by automatically replacing links or
15
15
*
16
16
*/
17
17
18
- import Road , { Middleware } from '../core/road' ;
18
+ import Road , { Context } from '../core/road' ;
19
19
import Response from '../core/response' ;
20
20
import { StoreValsContext , middleware as storeValsMiddleware } from '../middleware/storeVals' ;
21
+ import { Route } from '../core/router' ;
21
22
22
23
/**
23
24
* This is a helper class to make PJAX easier. PJAX is a clean way of improving the performance of webpages
24
25
* by progressively turning standard HTML links into AJAX requests for portions of a web page.
25
26
*/
26
- export default class RoadsPjax {
27
- protected _road : Road ;
27
+ export default class RoadsPjax < PjaxContext extends Context & StoreValsContext > {
28
+ protected _road : Road < PjaxContext > ;
28
29
protected _page_title ?: string ;
29
30
protected _window : Window ;
30
31
protected _container_element : HTMLElement ;
@@ -38,7 +39,7 @@ export default class RoadsPjax {
38
39
* @param {HTMLElement } containerElement - The element that will be filled with your roads output
39
40
* @param {Window } window - The page's window object to help set page title url
40
41
*/
41
- constructor ( road : Road , containerElement : HTMLElement , window : Window ) {
42
+ constructor ( road : Road < PjaxContext > , containerElement : HTMLElement , window : Window ) {
42
43
this . _road = road ;
43
44
this . _page_title = undefined ;
44
45
this . _window = window ;
@@ -62,11 +63,11 @@ export default class RoadsPjax {
62
63
* @param {titleKey } string - The key of the title as stored in the "storeVals" middleware.
63
64
* @returns {RoadsPjax } Returns the PJAX object. This is provided to allow for easy function chaining.
64
65
*/
65
- addTitleMiddleware ( titleKey : string ) : RoadsPjax {
66
+ addTitleMiddleware ( titleKey : string ) : RoadsPjax < PjaxContext > {
66
67
// eslint-disable-next-line @typescript-eslint/no-this-alias
67
68
const pjaxObj = this ;
68
69
69
- const titleMiddleware : Middleware < StoreValsContext > = function ( method , url , body , headers , next ) {
70
+ const titleMiddleware : Route < StoreValsContext > = function ( method , url , body , headers , next ) {
70
71
71
72
return next ( ) . then ( ( response ) => {
72
73
if ( this . getVal ) {
@@ -77,8 +78,8 @@ export default class RoadsPjax {
77
78
} ) ;
78
79
} ;
79
80
80
- this . _road . use ( storeValsMiddleware ) ;
81
- this . _road . use ( titleMiddleware ) ;
81
+ this . _road . beforeRoute ( storeValsMiddleware ) ;
82
+ this . _road . beforeRoute ( titleMiddleware ) ;
82
83
83
84
return this ;
84
85
}
0 commit comments