-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjs-requests.htm
219 lines (189 loc) · 8.68 KB
/
js-requests.htm
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
title = "JavaScript Requests | Snowboard"
url = "/snowboard/js-requests"
layout = "snowboard"
==
<?php
function onSimple()
{
return [
'#simple-request-response' => '<span class="text-green-600">AJAX Request was successful!</span>',
];
}
function onFormSubmission()
{
if (empty(post('name'))) {
return [
'#form-submission-response' => '<div class="mt-4 p-4 bg-red-50 border border-red-200">
You did not provide your name.
</div>',
];
} elseif (empty(post('email'))) {
return [
'#form-submission-response' => '<div class="mt-4 p-4 bg-red-50 border border-red-200">
You did not provide your email address.
</div>',
];
} else {
return [
'#form-submission-response' => '<div class="mt-4 p-4 bg-green-50 border border-green-200">
Your name is <strong>' . post('name') . '</strong> and you can be contacted via email
at <strong>' . post('email') . '</strong>.
</div>',
];
}
}
function onError()
{
throw new ApplicationException('This AJAX response threw an exception.');
}
function onFlash()
{
$type = post('type', 'success');
Flash::$type('This is a flash message');
}
function onPrepend()
{
return [
'^#prepend-content' => '<div>' . date('Y-m-d H:i:s') . '</div>',
];
}
function onAppend()
{
return [
'@#append-content' => '<div>' . date('Y-m-d H:i:s') . '</div>',
];
}
==
<section id="body" class="py-20 flex-grow">
<div class="container max-w-screen-xl">
<div class="block lg:flex lg:flex-row divide-x divide-gray-100">
<div class="flex-grow-0 px-8 lg:w-2/12 xl:pr-0">
{% partial 'snowboard/sections-list' %}
</div>
<div class="flex-grow-0 px-8 mt-16 lg:w-10/12 lg:mt-0">
<h2>JavaScript Requests</h2>
<!-- BEGIN Simple request -->
<hr class="mb-8">
<h4 class="mb-0">Simple request</h4>
<p>This is a test to do a simple AJAX request on the click of a button and show a success message next to the button on completion.</p>
<div class="flex flex-row gap-4 items-center" id="simple-request">
<div>
{% partial 'ui/button' color="amber" text="Test now" %}
</div>
<div id="simple-request-response">
</div>
</div>
<script>
document.querySelector('#simple-request .button').addEventListener('click', function (event) {
event.preventDefault();
Snowboard.request('#simple-request .button', 'onSimple');
});
</script>
<!-- END Simple request -->
<!-- BEGIN Form submission -->
<hr class="my-8">
<h4 class="mb-0">Form submission</h4>
<p>This is a test to make a form submission via AJAX and display the form contents on completion. The AJAX request
is initiated from the Submit button.</p>
<form class="grid grid-cols-2 gap-4" id="form-submission">
<div>
<label class="font-bold block mb-2" for="name">Name</label>
<input class="block w-full border border-amber-600 p-4 rounded" type="text" name="name" placeholder="Enter your name">
</div>
<div>
<label class="font-bold block mb-2" for="email">Email</label>
<input class="block w-full border border-amber-600 p-4 rounded" type="email" name="email" placeholder="Enter your email address">
</div>
<div>
{% partial 'ui/button' color="amber" text="Submit" %}
</div>
</form>
<div id="form-submission-response">
</div>
<script>
document.querySelector('#form-submission .button').addEventListener('click', function (event) {
event.preventDefault();
Snowboard.request('#form-submission .button', 'onFormSubmission');
});
</script>
<!-- END Form submission -->
<!-- BEGIN Error response -->
<hr class="my-8">
<h4 class="mb-0">Error response</h4>
<p>This is a test to do an AJAX request that throws an exception. It should show a Flash error message.</p>
<div class="flex flex-row gap-4 items-center" id="error-request">
<div>
{% partial 'ui/button' color="amber" text="Test now" %}
</div>
</div>
<script>
document.querySelector('#error-request .button').addEventListener('click', function (event) {
event.preventDefault();
Snowboard.request('#error-request .button', 'onError');
});
</script>
<!-- END Error response -->
<!-- BEGIN Flash response -->
<hr class="my-8">
<h4 class="mb-0">Flash response</h4>
<p>This is a test to do an AJAX request that sends a Flash response back of differing types.</p>
<div class="flex flex-row gap-4 items-center" id="flash-response">
<div>
{% partial 'ui/button' color="green" text="Test success" %}
</div>
<div>
{% partial 'ui/button' color="amber" text="Test warning" %}
</div>
<div>
{% partial 'ui/button' color="red" text="Test error" %}
</div>
</div>
<script>
document.querySelector('#flash-response div:nth-child(1) .button').addEventListener('click', function (event) {
event.preventDefault();
Snowboard.request('#flash-response div:nth-child(1) .button', 'onFlash', { data: { type: 'success' }, flash: true });
});
document.querySelector('#flash-response div:nth-child(2) .button').addEventListener('click', function (event) {
event.preventDefault();
Snowboard.request('#flash-response div:nth-child(2) .button', 'onFlash', { data: { type: 'warning' }, flash: true });
});
document.querySelector('#flash-response div:nth-child(3) .button').addEventListener('click', function (event) {
event.preventDefault();
Snowboard.request('#flash-response div:nth-child(3) .button', 'onFlash', { data: { type: 'error' }, flash: true });
});
</script>
<!-- END Flash response -->
<!-- BEGIN Append / Prepend -->
<hr class="my-8">
<h4 class="mb-0">Appended and prepended content</h4>
<p>This is a test to do an AJAX request that appends or prepends content as opposed to replacing the
content of the targeted element.</p>
<div class="flex flex-row gap-4" id="append-prepend">
<div>
{% partial 'ui/button' color="amber" text="Test prepend" %}
<div id="prepend-content">
<div class="font-bold">Prepended content</div>
</div>
</div>
<div>
{% partial 'ui/button' color="amber" text="Test append" %}
<div id="append-content">
<div class="font-bold">Appended content</div>
</div>
</div>
</div>
<script>
document.querySelector('#append-prepend div:nth-child(1) .button').addEventListener('click', function (event) {
event.preventDefault();
Snowboard.request('#append-prepend div:nth-child(1) .button', 'onPrepend');
});
document.querySelector('#append-prepend div:nth-child(2) .button').addEventListener('click', function (event) {
event.preventDefault();
Snowboard.request('#append-prepend div:nth-child(2) .button', 'onAppend');
});
</script>
<!-- END Append / Prepend -->
</div>
</div>
</div>
</section>