-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
index.d.ts
205 lines (165 loc) · 6.04 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
declare class Pjax {
options: Pjax.IOptions;
constructor(options?: Partial<Pjax.IOptions>);
static switches: {
[key in DefaultSwitches]: Pjax.Switch
};
/**
* Checks if Pjax is supported by the environment.
*/
static isSupported: () => boolean;
log: VoidFunction;
getElements(el: Element | Document): NodeListOf<Element>;
parseDOM(el: Element | Document): void;
refresh: ElementFunction;
reload: VoidFunction;
attachLink(el: HTMLAnchorElement): void;
attachForm(el: HTMLFormElement): void;
forEachSelectors(cb: ElementFunction, context: Pjax, DOMcontext?: Element | Document): void;
switchesSelectors(selectors: string[], fromEl: Element | Document, toEl: Element | Document, options: Pjax.IOptions): void;
latestChance(href: string): void;
onSwitch: VoidFunction;
/**
* Loads the HTML from the response into the DOM.
*
* @param {string} html
* @param {Pjax.IOptions} options
*/
loadContent(html: string, options: Pjax.IOptions): void;
/**
* Aborts an ongoing XHR request.
*
* @param {XMLHttpRequest} request
*/
abortRequest(request: XMLHttpRequest): void;
/**
* Makes the XHR request.
*
* @param {string} location The URI for the request.
* @param {Pjax.IOptions | null} options
* @param {(requestText: string, request: XMLHttpRequest, href: string) => void} callback The callback to call when
* the response is received. The signature should match that of <code>handleResponse</code>.
* @returns {XMLHttpRequest}
*/
doRequest(location: string, options: Pjax.IOptions | null,
callback: (requestText: string, request: XMLHttpRequest, href: string) => void): XMLHttpRequest;
/**
* Saves the state, updates the URL if there were any redirects, then calls loadContent().
*
* @param {string} requestText The raw text of the response. Same as <code>request.responseText</code>.
* @param {XMLHttpRequest} request The XHR object.
* @param {string} href The original URI used to initiate the request.
* @param options The Pjax options object used for the request
*/
handleResponse(requestText: string, request: XMLHttpRequest, href: string, options?: Pjax.IOptions): void;
/**
* Initiates the request by calling <code>doRequest()</code>.
* @param {string} href
* @param {Pjax.IOptions} options
*/
loadUrl(href: string, options?: Pjax.IOptions): void;
/**
* Called after all switches complete (even async).
*/
afterAllSwitches: VoidFunction;
/**
* Allows reassignment of existing prototype functions to be able to do something before calling the original function.
* For example:
*
* <pre>
* pjax._handleResponse = pjax.handleResponse;
* pjax.handleResponse = (requestText: string, request: XMLHttpRequest, href: string) => {
* return pjax._handleResponse(requestText, request, href);
* }
* </pre>
*/
[key: string]: Function | Pjax.IOptions;
}
declare namespace Pjax {
export interface IOptions {
/**
* CSS selector to use to retrieve links to apply Pjax to.
*/
elements: string;
/**
* CSS selectors for the elements to replace.
*/
selectors: string[];
/**
* Objects containing callbacks that can be used to switch old elements with new elements.
* Keys should be one of the defined selectors.
*/
switches: StringKeyedObject<Switch>;
/**
* These are options that can be used during switches.
* Keys should be one of the defined selectors.
*/
switchesOptions: StringKeyedObject;
/**
* Enable the use of pushState(). Disabling this will prevent Pjax from updating browser history.
* Internally, this option is used when a popstate event triggers Pjax (to not pushState() again).
*/
history: boolean;
/**
* Function that allows you to add behavior for analytics.
* By default it tries to track a pageview with Google Analytics (if it exists on the page).
* It's called every time a page is switched, even for history navigation.
* Set to false to disable this behavior.
*/
analytics: Function | false;
/**
* When set to an integer, this is the value (in px from the top of the page) to scroll to when a page is switched.
* When set to an array of 2 integers ([x, y]), this is the value to scroll both horizontally and vertically.
* Set this to false to disable scrolling, which will mean the page will stay in that same position it was before
* loading the new elements.
*/
scrollTo: number | [number, number] | false;
/**
* When set to true, attempt to restore the scroll position when navigating backwards or forwards.
*/
scrollRestoration: boolean;
/**
* When set to true, append a timestamp query string segment to the requested URLs in order to skip browser cache.
*/
cacheBust: boolean;
/**
* Enables verbose mode.
*/
debug: boolean;
/**
* The timeout in milliseconds for the XHR requests. Set to 0 to disable the timeout.
*/
timeout: number;
/**
* When set to true, clicking on a link that points to the current URL will trigger a full page reload.
* (Note that if cacheBust is set to true, the code that checks if the href is the same as the current page's URL
* will not work, due to the query string appended to force a cache bust).
*/
currentUrlFullReload: boolean;
/**
* Hold the information to make an XHR request.
*/
requestOptions?: {
requestUrl?: string;
requestMethod?: string;
requestParams?: IRequestParams[];
formData?: FormData;
}
}
export type Switch = (oldEl: Element, newEl: Element, options?: IOptions, switchesOptions?: StringKeyedObject) => void;
export interface IRequestParams {
name: string,
value: string
}
}
interface StringKeyedObject<T = any> {
[key: string]: T
}
type ElementFunction = (el: Element) => void;
declare enum DefaultSwitches {
innerHTML = "innerHTML",
ouetrHTML = "outerHTML",
sideBySide = "sideBySide",
replaceNode = "replaceNode"
}
export = Pjax;