bigpipe-util 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +9 -0
- package/CHANGELOG.md +10 -0
- package/LICENSE +19 -0
- package/README.md +133 -0
- package/bigpipe.svg +13 -0
- package/package.json +24 -0
- package/src/Primer.js +103 -0
- package/src/ServerJS.js +41 -0
- package/src/async/AsyncDOM.js +51 -0
- package/src/async/AsyncRequest.js +171 -0
- package/src/async/AsyncResponse.js +21 -0
- package/src/core/$.js +3 -0
- package/src/core/DOM.js +41 -0
- package/src/core/Parent.js +9 -0
- package/src/core/ReloadPage.js +9 -0
- package/src/core/ServerRedirect.js +7 -0
- package/src/core/copyProperties.js +46 -0
- package/src/core/replaceTransportMarkers.js +27 -0
package/.editorconfig
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `bigpipe-util` will be documented in this file.
|
|
4
|
+
|
|
5
|
+
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
|
|
6
|
+
|
|
7
|
+
## v0.1.1 - 2022-03-27
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
- Part of BigPipe implementation for Webpack.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (C) 2017 Richard Dobroň
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
<img src="bigpipe.svg">
|
|
2
|
+
|
|
3
|
+
This library currently implements small part of [Facebook BigPipe][blog] so far, but the advantage is to efficiently insert/replace content and work with the DOM. It is also possible to easily call JavaScript modules from PHP.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Demo App
|
|
7
|
+
Try the app with [live demo](http://bigpipe.xf.cz).
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
* PHP 7.1 or higher
|
|
11
|
+
* Node 8, 10+.
|
|
12
|
+
* Webpack
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
These steps are required:
|
|
17
|
+
1. Install composer package:
|
|
18
|
+
```shell
|
|
19
|
+
$ composer require richarddobron/bigpipe
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
2. Install npm package:
|
|
23
|
+
```shell
|
|
24
|
+
$ npm install bigpipe-util
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
3. Add this lines to /path/to/resources/js/app.js:
|
|
28
|
+
```javascript
|
|
29
|
+
import Primer from 'bigpipe-util/src/Primer';
|
|
30
|
+
|
|
31
|
+
Primer();
|
|
32
|
+
|
|
33
|
+
window.require = (modulePath) => {
|
|
34
|
+
return modulePath.startsWith('bigpipe-util/')
|
|
35
|
+
? require('bigpipe-util/' + modulePath.substring(13) + '.js').default
|
|
36
|
+
: require('./' + modulePath).default;
|
|
37
|
+
};
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
4. Create file /path/to/resources/js/ServerJS.js
|
|
41
|
+
- this step is optional, but if you skip it, use this in next step:
|
|
42
|
+
```require("bigpipe-util/ServerJS")```
|
|
43
|
+
```javascript
|
|
44
|
+
import ServerJSImpl from 'bigpipe-util/src/ServerJS';
|
|
45
|
+
export default class ServerJS extends ServerJSImpl {
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
5. Add this lines to page footer:
|
|
50
|
+
```html
|
|
51
|
+
<script>
|
|
52
|
+
(new (require("ServerJS"))).handle(<?=json_encode(\dobron\BigPipe\BigPipe::jsmods())?>);
|
|
53
|
+
</script>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Request API
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
import AsyncRequest from 'bigpipe-util/src/AsyncRequest';
|
|
60
|
+
|
|
61
|
+
const request = AsyncRequest('/ajax/remove.php')
|
|
62
|
+
// or .setURI('/ajax/remove.php')
|
|
63
|
+
.setMethod('POST')
|
|
64
|
+
.setData({
|
|
65
|
+
param: 'value',
|
|
66
|
+
})
|
|
67
|
+
.setInitialHandler(() => {
|
|
68
|
+
// pre-request callback function
|
|
69
|
+
})
|
|
70
|
+
.setHandler((jsonResponse) => {
|
|
71
|
+
// A function to be called if the request succeeds
|
|
72
|
+
})
|
|
73
|
+
.setErrorHandler((xhr) => {
|
|
74
|
+
// A function to be called if the request fails
|
|
75
|
+
})
|
|
76
|
+
.send();
|
|
77
|
+
|
|
78
|
+
if (OH_NOES_WE_NEED_TO_CANCEL_RIGHT_NOW_OR_ELSE) {
|
|
79
|
+
request.abort();
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
# What all can be Ajaxifed?
|
|
84
|
+
|
|
85
|
+
## Links
|
|
86
|
+
```html
|
|
87
|
+
<a href="#"
|
|
88
|
+
ajaxify="/ajax/remove.php"
|
|
89
|
+
rel="async">Remove Item</a>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Forms
|
|
93
|
+
```html
|
|
94
|
+
<form action="/submit.php"
|
|
95
|
+
method="POST"
|
|
96
|
+
rel="async">
|
|
97
|
+
<input name="user">
|
|
98
|
+
<input type="submit" name="Done">
|
|
99
|
+
</form>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Dialogs
|
|
103
|
+
```html
|
|
104
|
+
<a href="#"
|
|
105
|
+
ajaxify="/ajax/modal.php"
|
|
106
|
+
rel="dialog">Open Modal</a>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Credits
|
|
110
|
+
|
|
111
|
+
- [Richard Dobroň][link-author]
|
|
112
|
+
|
|
113
|
+
## Inspiration
|
|
114
|
+
|
|
115
|
+
BigPipe is inspired by the concept behind Facebook's BigPipe. For more details
|
|
116
|
+
read their blog post: [Pipelining web pages for high performance][blog].
|
|
117
|
+
|
|
118
|
+
## Motivation
|
|
119
|
+
|
|
120
|
+
There is a large number of PHP projects for which moving to modern frameworks like Laravel Livewire, React, Vue.js (and many more!) could be very challenging.
|
|
121
|
+
|
|
122
|
+
The purpose of this library is to rapidly reduce the continuously repetitive code to work with the DOM and improve the communication barrier between PHP and JavaScript.
|
|
123
|
+
|
|
124
|
+
## Credits
|
|
125
|
+
|
|
126
|
+
- [Richard Dobroň][link-author]
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
|
|
131
|
+
|
|
132
|
+
[link-author]: https://github.com/richardDobron
|
|
133
|
+
[blog]: https://www.facebook.com/notes/facebook-engineering/bigpipe-pipelining-web-pages-for-high-performance/389414033919
|
package/bigpipe.svg
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<svg width="254.23999568394254" height="64.8" viewBox="0 0 254.23999568394254 64.8" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<g transform="matrix(3.0856473041983374,0,0,3.0856473041983374,-6.171373325690784,-6.1698442238608315)" fill="#0C056D">
|
|
3
|
+
<g xmlns="http://www.w3.org/2000/svg">
|
|
4
|
+
<path d="M2.2427,8.9287a.5.5,0,0,1,0-.8574l10-6a.4971.4971,0,0,1,.5146,0l10,6a.5.5,0,0,1,0,.8574l-10,6a.5.5,0,0,1-.5146,0Zm20,3.1426L12.5,17.917,2.7573,12.0713a.5.5,0,0,0-.5146.8574l10,6a.5.5,0,0,0,.5146,0l10-6a.5.5,0,0,0-.5146-.8574Zm0,4L12.5,21.917,2.7573,16.0713a.5.5,0,0,0-.5146.8574l10,6a.5.5,0,0,0,.5146,0l10-6a.5.5,0,0,0-.5146-.8574Z"></path>
|
|
5
|
+
</g>
|
|
6
|
+
</g>
|
|
7
|
+
<g transform="matrix(2.500000066227385,0,0,2.500000066227385,81.99999920527138,0.49999675485814166)" fill="#0C056D">
|
|
8
|
+
<path d="M8.92 12.24 c1.48 0.4 2.88 1.72 2.88 3.66 c0 2.54 -1.6 4.1 -5.08 4.1 l-5.52 0 l0 -14 l5.22 0 c2.9 0 4.36 1.64 4.36 3.52 c0 1.48 -0.96 2.32 -1.86 2.72 z M6.14 8.56 l-2.02 0 l0 2.82 l2.02 0 c1.24 0 1.74 -0.58 1.74 -1.42 c0 -0.9 -0.58 -1.4 -1.74 -1.4 z M6.56 17.44 c1.6 0 2.32 -0.66 2.32 -1.9 c0 -1.06 -0.72 -1.8 -2.44 -1.8 l-2.32 0 l0 3.7 l2.44 0 z M16.62 6 l0 14 l-2.92 0 l0 -14 l2.92 0 z M32.2 12.559999999999999 c0.62 4.7 -2.44 7.64 -6.22 7.64 c-3.98 0 -7.26 -2.94 -7.26 -7.2 s3.36 -7.2 7.26 -7.2 c1.86 0 3.5 0.6 4.7 1.66 l-1.76 1.98 c-0.74 -0.52 -1.74 -0.9 -2.76 -0.9 c-2.54 0 -4.4 1.86 -4.4 4.46 s1.8 4.46 4.22 4.46 c1.84 0 3.12 -0.66 3.52 -2.42 l-3.3 0 l0 -2.48 l6 0 z"></path>
|
|
9
|
+
</g>
|
|
10
|
+
<g transform="matrix(2.5714285714285716,0,0,2.5714285714285716,166.91428559167045,-0.4285714285714306)" fill="#0C056D">
|
|
11
|
+
<path d="M5.46 6 c3.14 0 4.8 1.94 4.8 4.3 c0 2.42 -1.66 4.34 -4.8 4.34 l-3.68 0 l0 5.36 l-0.58 0 l0 -14 l4.26 0 z M5.54 14.04 c2.52 0 4.14 -1.46 4.14 -3.74 c0 -2.32 -1.62 -3.72 -4.14 -3.72 l-3.76 0 l0 7.46 l3.76 0 z M12.74 6 l0 14 l-0.58 0 l0 -14 l0.58 0 z M19.400000000000002 6 c3.14 0 4.8 1.94 4.8 4.3 c0 2.42 -1.66 4.34 -4.8 4.34 l-3.68 0 l0 5.36 l-0.58 0 l0 -14 l4.26 0 z M19.48 14.04 c2.52 0 4.14 -1.46 4.14 -3.74 c0 -2.32 -1.62 -3.72 -4.14 -3.72 l-3.76 0 l0 7.46 l3.76 0 z M26.680000000000003 19.42 l7.28 0 l0 0.58 l-7.68 0 l-0.18 0 l0 -14 l0.58 0 l7.1 0 l0 0.58 l-7.1 0 l0 6.1 l5.66 0 l0 0.58 l-5.66 0 l0 6.16 z"></path>
|
|
12
|
+
</g>
|
|
13
|
+
</svg>
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bigpipe-util",
|
|
3
|
+
"description": "This library currently implements small part of Facebook BigPipe so far, but the advantage is to efficiently insert/replace content and work with the DOM. It is also possible to easily call JavaScript modules from PHP.",
|
|
4
|
+
"version": "0.1.1",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bigpipe",
|
|
7
|
+
"xhr",
|
|
8
|
+
"html",
|
|
9
|
+
"dom",
|
|
10
|
+
"async",
|
|
11
|
+
"javascript"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/richardDobron/bigpipe-util",
|
|
14
|
+
"author": "Richard Dobroň",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git://github.com/richardDobron/bigpipe-util.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs:": "https://github.com/richardDobron/bigpipe-util/issues",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"fbjs": "^3.0.4"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/Primer.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import AsyncRequest from "./async/AsyncRequest";
|
|
2
|
+
import {byTag} from "./core/Parent";
|
|
3
|
+
|
|
4
|
+
export default function Primer() {
|
|
5
|
+
const rootElement = document.documentElement;
|
|
6
|
+
|
|
7
|
+
const RELATIONSHIP_REGEX = /async(?:-post)?/;
|
|
8
|
+
|
|
9
|
+
rootElement.onclick = function (event) {
|
|
10
|
+
event = event || window.event;
|
|
11
|
+
const elementOnClicked = event.target || event.srcElement;
|
|
12
|
+
|
|
13
|
+
const linkNodeOnClicked = byTag(elementOnClicked, "A");
|
|
14
|
+
|
|
15
|
+
if (!linkNodeOnClicked) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let relationship = linkNodeOnClicked.rel && linkNodeOnClicked.rel.match(RELATIONSHIP_REGEX);
|
|
20
|
+
relationship = relationship && relationship[0];
|
|
21
|
+
|
|
22
|
+
switch (relationship) {
|
|
23
|
+
case "async":
|
|
24
|
+
case "async-post":
|
|
25
|
+
event.preventDefault();
|
|
26
|
+
|
|
27
|
+
(new AsyncRequest(linkNodeOnClicked.getAttribute("ajaxify")))
|
|
28
|
+
.setRelative(linkNodeOnClicked)
|
|
29
|
+
.setMethod(relationship === "async-post" ? "POST" : "GET")
|
|
30
|
+
.send();
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
rootElement.onsubmit = function (event) {
|
|
36
|
+
event = event || window.event;
|
|
37
|
+
const eventTarget = event.target || event.srcElement;
|
|
38
|
+
|
|
39
|
+
if (eventTarget &&
|
|
40
|
+
eventTarget.nodeName === "FORM" &&
|
|
41
|
+
eventTarget.getAttribute("rel") === "async") {
|
|
42
|
+
event.preventDefault();
|
|
43
|
+
|
|
44
|
+
const submitter = event.submitter || eventTarget.querySelector("button[type='submit']"),
|
|
45
|
+
activeControls = eventTarget.querySelectorAll("input:not([readonly]),select:not([readonly]),textarea:not([readonly])");
|
|
46
|
+
|
|
47
|
+
(new AsyncRequest(eventTarget.getAttribute("ajaxify") || eventTarget.getAttribute("action")))
|
|
48
|
+
.setMethod(eventTarget.method || "POST")
|
|
49
|
+
.setRelative(eventTarget)
|
|
50
|
+
.setData(new FormData(eventTarget))
|
|
51
|
+
.setInitialHandler(function () {
|
|
52
|
+
if (!eventTarget.classList.contains("disable-prevent-form")) {
|
|
53
|
+
activeControls.forEach(function (control) {
|
|
54
|
+
control.setAttribute("readonly", "readonly");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (submitter) {
|
|
58
|
+
submitter.disabled = true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (eventTarget) {
|
|
63
|
+
const loader = eventTarget.querySelector(".form-loader");
|
|
64
|
+
|
|
65
|
+
if (loader) {
|
|
66
|
+
loader.classList.add("loading");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
.setHandler(function (response) {
|
|
71
|
+
activeControls.forEach(function (control) {
|
|
72
|
+
control.removeAttribute("readonly");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (submitter) {
|
|
76
|
+
submitter.disabled = false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (eventTarget) {
|
|
80
|
+
const loader = eventTarget.querySelector(".small-loader");
|
|
81
|
+
|
|
82
|
+
if (loader) {
|
|
83
|
+
loader.classList.remove("loading");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
.setErrorHandler(function () {
|
|
88
|
+
activeControls.forEach(function (control) {
|
|
89
|
+
control.removeAttribute("readonly");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (eventTarget) {
|
|
93
|
+
const loader = eventTarget.querySelector(".small-loader");
|
|
94
|
+
|
|
95
|
+
if (loader) {
|
|
96
|
+
loader.classList.remove("loading");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
.send();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
package/src/ServerJS.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import replaceTransportMarkers from "./core/replaceTransportMarkers";
|
|
2
|
+
|
|
3
|
+
function handler(dependencies, guard, context) {
|
|
4
|
+
return dependencies.map(function (args) {
|
|
5
|
+
guard.apply(context, args);
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default class ServerJS {
|
|
10
|
+
constructor() {
|
|
11
|
+
this._relativeTo = document.body;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
handle(jsMods) {
|
|
15
|
+
handler(jsMods.require || [], this._handleRequire, this);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
_handleRequire(modulePath, method, marker) {
|
|
19
|
+
if (method && typeof method === 'string') {
|
|
20
|
+
if (marker) {
|
|
21
|
+
replaceTransportMarkers(this._relativeTo, marker);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const context = new (window.require(modulePath));
|
|
25
|
+
|
|
26
|
+
if (!context[method]) {
|
|
27
|
+
throw new TypeError(`Module ${modulePath} has no method "${method}"`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
context[method].apply(context, marker || []);
|
|
31
|
+
} else {
|
|
32
|
+
marker = method;
|
|
33
|
+
|
|
34
|
+
if (marker) {
|
|
35
|
+
replaceTransportMarkers(this._relativeTo, marker);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
new (window.require(modulePath))(...(marker || []));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import DOM from "../core/DOM";
|
|
2
|
+
|
|
3
|
+
export default class AsyncDOM {
|
|
4
|
+
invoke(domOps, element) {
|
|
5
|
+
for (let i = 0; i < domOps.length; ++i) {
|
|
6
|
+
let domOp = domOps[i],
|
|
7
|
+
type = domOp[0],
|
|
8
|
+
selector = domOp[1],
|
|
9
|
+
enableTarget = domOp[2],
|
|
10
|
+
content = domOp[3];
|
|
11
|
+
|
|
12
|
+
let node = enableTarget && element || null;
|
|
13
|
+
|
|
14
|
+
if (selector) {
|
|
15
|
+
node = (node || document.documentElement).querySelector(selector);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
switch (type) {
|
|
19
|
+
case "eval":
|
|
20
|
+
(new Function(content)).apply(node);
|
|
21
|
+
break;
|
|
22
|
+
case "hide":
|
|
23
|
+
node.style.display = "none";
|
|
24
|
+
break;
|
|
25
|
+
case "show":
|
|
26
|
+
node.style.display = "";
|
|
27
|
+
break;
|
|
28
|
+
case "setContent":
|
|
29
|
+
DOM.setContent(node, content.__html);
|
|
30
|
+
break;
|
|
31
|
+
case "appendContent":
|
|
32
|
+
DOM.appendContent(node, content.__html);
|
|
33
|
+
break;
|
|
34
|
+
case "prependContent":
|
|
35
|
+
DOM.prependContent(node, content.__html);
|
|
36
|
+
break;
|
|
37
|
+
case "insertAfter":
|
|
38
|
+
DOM.insertAfter(node, content.__html);
|
|
39
|
+
break;
|
|
40
|
+
case "insertBefore":
|
|
41
|
+
DOM.insertBefore(node, content.__html);
|
|
42
|
+
break;
|
|
43
|
+
case "remove":
|
|
44
|
+
DOM.remove(node);
|
|
45
|
+
break;
|
|
46
|
+
case "replace":
|
|
47
|
+
DOM.replace(node, content.__html);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import emptyFunction from "fbjs/lib/emptyFunction";
|
|
2
|
+
import AsyncResponse from "./AsyncResponse";
|
|
3
|
+
|
|
4
|
+
function serialize(obj, prefix) {
|
|
5
|
+
const str = [];
|
|
6
|
+
for(const p in obj) {
|
|
7
|
+
if (obj.hasOwnProperty(p)) {
|
|
8
|
+
const k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
|
|
9
|
+
str.push(typeof v == "object" ?
|
|
10
|
+
serialize(v, k) :
|
|
11
|
+
encodeURIComponent(k) + "=" + encodeURIComponent(v));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return str.join("&");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function validateResponseHandler(handler) {
|
|
18
|
+
const valid = !handler || typeof handler === "function";
|
|
19
|
+
|
|
20
|
+
if (!valid) {
|
|
21
|
+
console.error("AsyncRequest response handlers must be functions. Pass a function, or use bind() to build one.");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return valid;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default function AsyncRequest(uri) {
|
|
28
|
+
this.method = "POST";
|
|
29
|
+
this.uri = "";
|
|
30
|
+
this.relative = null;
|
|
31
|
+
this.data = {};
|
|
32
|
+
this.headers = {};
|
|
33
|
+
this.initialHandler = emptyFunction;
|
|
34
|
+
this.handler = null;
|
|
35
|
+
this.errorHandler = emptyFunction;
|
|
36
|
+
|
|
37
|
+
if (uri !== undefined) {
|
|
38
|
+
this.setURI(uri);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
AsyncRequest.prototype.setMethod = function (method) {
|
|
43
|
+
this.method = method.toString().toUpperCase();
|
|
44
|
+
return this;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
AsyncRequest.prototype.getMethod = function () {
|
|
48
|
+
return this.method;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
AsyncRequest.prototype.setRelative = function (relative) {
|
|
52
|
+
this.relative = relative;
|
|
53
|
+
return this;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
AsyncRequest.prototype.getRelative = function () {
|
|
57
|
+
return this.relative;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
AsyncRequest.prototype.setData = function (obj) {
|
|
61
|
+
this.data = obj;
|
|
62
|
+
return this;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
AsyncRequest.prototype.getData = function () {
|
|
66
|
+
return this.data;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
AsyncRequest.prototype.setRequestHeader = function (name, value) {
|
|
70
|
+
this.headers[name] = value;
|
|
71
|
+
return this;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
AsyncRequest.prototype.setURI = function (uri) {
|
|
75
|
+
this.uri = uri;
|
|
76
|
+
|
|
77
|
+
return this;
|
|
78
|
+
};
|
|
79
|
+
AsyncRequest.prototype.getURI = function () {
|
|
80
|
+
return this.uri;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
AsyncRequest.prototype.setInitialHandler = function (fn) {
|
|
84
|
+
this.initialHandler = fn;
|
|
85
|
+
return this;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
AsyncRequest.prototype.getInitialHandler = function () {
|
|
89
|
+
return this.initialHandler || emptyFunction;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
AsyncRequest.prototype.setHandler = function (fn) {
|
|
93
|
+
if (validateResponseHandler(fn)) {
|
|
94
|
+
this.handler = fn;
|
|
95
|
+
}
|
|
96
|
+
return this;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
AsyncRequest.prototype.getHandler = function () {
|
|
100
|
+
return this.handler || emptyFunction;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
AsyncRequest.prototype.setErrorHandler = function (fn) {
|
|
104
|
+
if (validateResponseHandler(fn)) {
|
|
105
|
+
this.errorHandler = fn;
|
|
106
|
+
}
|
|
107
|
+
return this;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
AsyncRequest.prototype.getErrorHandler = function () {
|
|
111
|
+
return this.errorHandler || emptyFunction;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
AsyncRequest.prototype.abort = function () {
|
|
115
|
+
const transport = this.transport;
|
|
116
|
+
|
|
117
|
+
if (transport) {
|
|
118
|
+
transport.abort();
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
AsyncRequest.prototype.send = function () {
|
|
123
|
+
const {uri, method} = this;
|
|
124
|
+
let { data } = this;
|
|
125
|
+
|
|
126
|
+
const handler = this.getHandler();
|
|
127
|
+
const errorHandler = this.getErrorHandler();
|
|
128
|
+
const initialHandler = this.getInitialHandler();
|
|
129
|
+
|
|
130
|
+
initialHandler();
|
|
131
|
+
|
|
132
|
+
const request = new XMLHttpRequest();
|
|
133
|
+
const self = this;
|
|
134
|
+
|
|
135
|
+
request.open(method, uri, true);
|
|
136
|
+
|
|
137
|
+
request.onload = function () {
|
|
138
|
+
if (this.status >= 200 && this.status < 400) {
|
|
139
|
+
let response;
|
|
140
|
+
try {
|
|
141
|
+
response = eval("(" + this.responseText + ")");
|
|
142
|
+
} catch (e) {
|
|
143
|
+
throw new Error("Failed to handle response: " + e.message + "\n" + this.responseText);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
(new AsyncResponse).handle(response, self.relative);
|
|
147
|
+
|
|
148
|
+
handler(response);
|
|
149
|
+
} else {
|
|
150
|
+
errorHandler(this);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
|
155
|
+
if (!(this.data instanceof FormData)) {
|
|
156
|
+
data = serialize(data);
|
|
157
|
+
|
|
158
|
+
if (method === "POST") {
|
|
159
|
+
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
Object.keys(this.headers).forEach((name) => {
|
|
164
|
+
request.setRequestHeader(name, this.headers[name]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
request.onerror = errorHandler;
|
|
168
|
+
request.send(data);
|
|
169
|
+
|
|
170
|
+
return request;
|
|
171
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import AsyncDOM from "./AsyncDOM";
|
|
2
|
+
import ServerJS from "../ServerJS";
|
|
3
|
+
|
|
4
|
+
const _AsyncDOM = new AsyncDOM;
|
|
5
|
+
const _ServerJS = new ServerJS;
|
|
6
|
+
|
|
7
|
+
export default class AsyncResponse {
|
|
8
|
+
handle(response, element) {
|
|
9
|
+
const {domops, jsmods} = response;
|
|
10
|
+
|
|
11
|
+
if (typeof response === 'object') {
|
|
12
|
+
if (domops) {
|
|
13
|
+
_AsyncDOM.invoke(domops, element);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (jsmods) {
|
|
17
|
+
_ServerJS.handle(jsmods);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
};
|
package/src/core/$.js
ADDED
package/src/core/DOM.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
function removeNode(node) {
|
|
2
|
+
if (node.parentNode) {
|
|
3
|
+
node.parentNode.removeChild(node);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
prependContent(node, content) {
|
|
9
|
+
node.insertAdjacentHTML("afterbegin", content);
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
insertAfter(node, content) {
|
|
13
|
+
node.insertAdjacentHTML("afterend", content);
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
insertBefore(node, content) {
|
|
17
|
+
node.insertAdjacentHTML("beforebegin", content);
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
setContent(node, content) {
|
|
21
|
+
node.innerHTML = content;
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
appendContent(node, content) {
|
|
25
|
+
node.insertAdjacentHTML("beforeend", content);
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
replace(node, content) {
|
|
29
|
+
node.outerHTML = content;
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
remove(node) {
|
|
33
|
+
removeNode(node);
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
empty(node) {
|
|
37
|
+
while (node.firstChild) {
|
|
38
|
+
removeNode(node.firstChild);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2015-present, Facebook, Inc.
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the BSD-style license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree. An additional grant
|
|
7
|
+
* of patent rights can be found in the PATENTS file in the same directory.
|
|
8
|
+
*
|
|
9
|
+
* @providesModule copyProperties
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Copy properties from one or more objects (up to 5) into the first object.
|
|
15
|
+
* This is a shallow copy. It mutates the first object and also returns it.
|
|
16
|
+
*
|
|
17
|
+
* NOTE: `arguments` has a very significant performance penalty, which is why
|
|
18
|
+
* we don't support unlimited arguments.
|
|
19
|
+
*/
|
|
20
|
+
export default function copyProperties(obj, a, b, c, d, e, f) {
|
|
21
|
+
obj = obj || {};
|
|
22
|
+
|
|
23
|
+
if (__DEV__) {
|
|
24
|
+
if (f) {
|
|
25
|
+
throw new Error('Too many arguments passed to copyProperties');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var args = [a, b, c, d, e];
|
|
30
|
+
var ii = 0, v;
|
|
31
|
+
while (args[ii]) {
|
|
32
|
+
v = args[ii++];
|
|
33
|
+
for (var k in v) {
|
|
34
|
+
obj[k] = v[k];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// IE ignores toString in object iteration.. See:
|
|
38
|
+
// webreflection.blogspot.com/2007/07/quick-fix-internet-explorer-and.html
|
|
39
|
+
if (v.hasOwnProperty && v.hasOwnProperty('toString') &&
|
|
40
|
+
(typeof v.toString !== 'undefined') && (obj.toString !== v.toString)) {
|
|
41
|
+
obj.toString = v.toString;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return obj;
|
|
46
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import $ from './$';
|
|
2
|
+
|
|
3
|
+
export default function replaceTransportMarkers(markerValue, markers, key) {
|
|
4
|
+
const marker = typeof key !== "undefined" ? markers[key] : markers;
|
|
5
|
+
|
|
6
|
+
if (Array.isArray(marker)) {
|
|
7
|
+
for (let i = 0; i < marker.length; i++) {
|
|
8
|
+
replaceTransportMarkers(markerValue, marker, i);
|
|
9
|
+
}
|
|
10
|
+
} else {
|
|
11
|
+
if (marker && typeof marker == "object") {
|
|
12
|
+
if (marker.__m) {
|
|
13
|
+
markers[key] = window.require(marker.__m);
|
|
14
|
+
} else if (marker.__e) {
|
|
15
|
+
markers[key] = $(marker.__e);
|
|
16
|
+
} else if (marker.__map) {
|
|
17
|
+
markers[key] = new Map(marker.__map);
|
|
18
|
+
} else if (marker.__set) {
|
|
19
|
+
markers[key] = new Set(marker.__set);
|
|
20
|
+
} else {
|
|
21
|
+
for (let value in marker) {
|
|
22
|
+
replaceTransportMarkers(markerValue, marker, value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|