create-prisma-php-app 2.0.0-beta.9 → 2.0.0-rc.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/dist/bootstrap.php
CHANGED
|
@@ -64,6 +64,7 @@ final class Bootstrap
|
|
|
64
64
|
|
|
65
65
|
Request::$pathname = $contentInfo['pathname'] ? '/' . $contentInfo['pathname'] : '/';
|
|
66
66
|
Request::$uri = $contentInfo['uri'] ? $contentInfo['uri'] : '/';
|
|
67
|
+
Request::$decodedUri = Request::getDecodedUrl(Request::$uri);
|
|
67
68
|
|
|
68
69
|
if (is_file(self::$contentToInclude)) {
|
|
69
70
|
Request::$fileToInclude = basename(self::$contentToInclude);
|
|
@@ -674,7 +675,7 @@ final class Bootstrap
|
|
|
674
675
|
}
|
|
675
676
|
}
|
|
676
677
|
|
|
677
|
-
$currentUrl =
|
|
678
|
+
$currentUrl = Request::getDecodedUrl(Request::$uri);
|
|
678
679
|
|
|
679
680
|
if (isset($currentData[$currentUrl])) {
|
|
680
681
|
$currentData[$currentUrl]['includedFiles'] = array_values(array_unique(
|
|
@@ -686,7 +687,7 @@ final class Bootstrap
|
|
|
686
687
|
}
|
|
687
688
|
} else {
|
|
688
689
|
$currentData[$currentUrl] = [
|
|
689
|
-
'url' =>
|
|
690
|
+
'url' => Request::$uri,
|
|
690
691
|
'fileName' => self::convertUrlToFileName($currentUrl),
|
|
691
692
|
'isCacheable' => CacheHandler::$isCacheable,
|
|
692
693
|
'cacheTtl' => CacheHandler::$ttl,
|
|
@@ -706,7 +707,7 @@ final class Bootstrap
|
|
|
706
707
|
{
|
|
707
708
|
$url = trim($url, '/');
|
|
708
709
|
$fileName = preg_replace('/[^a-zA-Z0-9-_]/', '_', $url);
|
|
709
|
-
return $fileName
|
|
710
|
+
return $fileName ? mb_strtolower($fileName, 'UTF-8') : 'index';
|
|
710
711
|
}
|
|
711
712
|
|
|
712
713
|
private static function authenticateUserToken(): void
|
|
@@ -818,16 +819,16 @@ try {
|
|
|
818
819
|
}
|
|
819
820
|
|
|
820
821
|
// If there’s caching
|
|
821
|
-
if (isset(Bootstrap::$requestFilesData[Request::$
|
|
822
|
+
if (isset(Bootstrap::$requestFilesData[Request::$decodedUri])) {
|
|
822
823
|
if ($_ENV['CACHE_ENABLED'] === 'true') {
|
|
823
|
-
CacheHandler::serveCache(Request::$
|
|
824
|
+
CacheHandler::serveCache(Request::$decodedUri, intval($_ENV['CACHE_TTL']));
|
|
824
825
|
}
|
|
825
826
|
}
|
|
826
827
|
|
|
827
828
|
// For wire calls, re-include the files if needed
|
|
828
829
|
if (Request::$isWire && !Bootstrap::$secondRequestC69CD) {
|
|
829
|
-
if (isset(Bootstrap::$requestFilesData[Request::$
|
|
830
|
-
foreach (Bootstrap::$requestFilesData[Request::$
|
|
830
|
+
if (isset(Bootstrap::$requestFilesData[Request::$decodedUri])) {
|
|
831
|
+
foreach (Bootstrap::$requestFilesData[Request::$decodedUri]['includedFiles'] as $file) {
|
|
831
832
|
if (file_exists($file)) {
|
|
832
833
|
ob_start();
|
|
833
834
|
require_once $file;
|
|
@@ -852,8 +853,8 @@ try {
|
|
|
852
853
|
MainLayout::$html = TemplateCompiler::injectDynamicContent(MainLayout::$html);
|
|
853
854
|
MainLayout::$html = "<!DOCTYPE html>\n" . MainLayout::$html;
|
|
854
855
|
|
|
855
|
-
if (isset(Bootstrap::$requestFilesData[Request::$
|
|
856
|
-
CacheHandler::saveCache(Request::$
|
|
856
|
+
if (isset(Bootstrap::$requestFilesData[Request::$decodedUri]['fileName']) && $_ENV['CACHE_ENABLED'] === 'true') {
|
|
857
|
+
CacheHandler::saveCache(Request::$decodedUri, MainLayout::$html);
|
|
857
858
|
}
|
|
858
859
|
|
|
859
860
|
echo MainLayout::$html;
|
|
@@ -9,6 +9,7 @@ class UploadFile
|
|
|
9
9
|
protected string $destination = '';
|
|
10
10
|
protected array $messages = [];
|
|
11
11
|
protected array $errorCode = [];
|
|
12
|
+
protected array $successfulUploads = [];
|
|
12
13
|
protected int $maxSize = 51200; // 50KB default
|
|
13
14
|
protected array $permittedTypes = [
|
|
14
15
|
'image/jpeg',
|
|
@@ -340,6 +341,11 @@ class UploadFile
|
|
|
340
341
|
if ($this->newName && $this->newName !== $file['name']) {
|
|
341
342
|
$message .= " Renamed to {$this->newName}";
|
|
342
343
|
}
|
|
344
|
+
|
|
345
|
+
$this->successfulUploads[] = [
|
|
346
|
+
'original' => $file['name'],
|
|
347
|
+
'final' => $filename
|
|
348
|
+
];
|
|
343
349
|
} else {
|
|
344
350
|
$message = "Failed to upload {$file['name']}.";
|
|
345
351
|
}
|
|
@@ -364,4 +370,14 @@ class UploadFile
|
|
|
364
370
|
}
|
|
365
371
|
return $rearranged;
|
|
366
372
|
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Retrieves the successfully uploaded file names.
|
|
376
|
+
*
|
|
377
|
+
* @return array An array of arrays containing 'original' and 'final' file names.
|
|
378
|
+
*/
|
|
379
|
+
public function getSuccessfulUploads(): array
|
|
380
|
+
{
|
|
381
|
+
return $this->successfulUploads;
|
|
382
|
+
}
|
|
367
383
|
}
|
|
@@ -9,7 +9,6 @@ class TwMerge
|
|
|
9
9
|
private static $classGroupPatterns = [
|
|
10
10
|
// **General Padding classes**
|
|
11
11
|
"p" => "/^p-/",
|
|
12
|
-
|
|
13
12
|
// **Specific Padding classes**
|
|
14
13
|
"pt" => "/^pt-/",
|
|
15
14
|
"pr" => "/^pr-/",
|
|
@@ -17,7 +16,6 @@ class TwMerge
|
|
|
17
16
|
"pl" => "/^pl-/",
|
|
18
17
|
"px" => "/^px-/",
|
|
19
18
|
"py" => "/^py-/",
|
|
20
|
-
|
|
21
19
|
// **Margin classes**
|
|
22
20
|
"m" => "/^m-/",
|
|
23
21
|
"mt" => "/^mt-/",
|
|
@@ -26,60 +24,42 @@ class TwMerge
|
|
|
26
24
|
"ml" => "/^ml-/",
|
|
27
25
|
"mx" => "/^mx-/",
|
|
28
26
|
"my" => "/^my-/",
|
|
29
|
-
|
|
30
27
|
// **Background color classes**
|
|
31
28
|
"bg" => "/^bg-/",
|
|
32
|
-
|
|
33
|
-
// **Text size classes
|
|
29
|
+
// **Text size classes**
|
|
34
30
|
"text-size" => '/^text-(xs|sm|base|lg|xl|[2-9]xl)$/',
|
|
35
|
-
|
|
36
31
|
// **Text alignment classes**
|
|
37
32
|
"text-alignment" => '/^text-(left|center|right|justify)$/',
|
|
38
|
-
|
|
39
|
-
// **Text color classes
|
|
33
|
+
// **Text color classes**
|
|
40
34
|
"text-color" => '/^text-(?!xs$|sm$|base$|lg$|xl$|[2-9]xl$).+$/',
|
|
41
|
-
|
|
42
35
|
// **Text transform classes**
|
|
43
|
-
"text-transform" =>
|
|
44
|
-
'/^text-(uppercase|lowercase|capitalize|normal-case)$/',
|
|
45
|
-
|
|
36
|
+
"text-transform" => '/^text-(uppercase|lowercase|capitalize|normal-case)$/',
|
|
46
37
|
// **Text decoration classes**
|
|
47
38
|
"text-decoration" => '/^text-(underline|line-through|no-underline)$/',
|
|
48
|
-
|
|
49
39
|
// **Border width classes**
|
|
50
40
|
"border-width" => '/^border(-[0-9]+)?$/',
|
|
51
|
-
|
|
52
41
|
// **Border color classes**
|
|
53
42
|
"border-color" => "/^border-(?![0-9])/",
|
|
54
|
-
|
|
55
43
|
// **Border radius classes**
|
|
56
44
|
"rounded" => '/^rounded(-.*)?$/',
|
|
57
|
-
|
|
58
45
|
// **Font weight classes**
|
|
59
|
-
"font" =>
|
|
60
|
-
'/^font-(thin|extralight|light|normal|medium|semibold|bold|extrabold|black)$/',
|
|
61
|
-
|
|
46
|
+
"font" => '/^font-(thin|extralight|light|normal|medium|semibold|bold|extrabold|black)$/',
|
|
62
47
|
// **Hover background color classes**
|
|
63
48
|
"hover:bg" => "/^hover:bg-/",
|
|
64
|
-
|
|
65
49
|
// **Hover text color classes**
|
|
66
50
|
"hover:text" => "/^hover:text-/",
|
|
67
|
-
|
|
68
51
|
// **Transition classes**
|
|
69
52
|
"transition" => '/^transition(-[a-z]+)?$/',
|
|
70
|
-
|
|
71
|
-
// **Opacity classes
|
|
53
|
+
// **Opacity classes**
|
|
72
54
|
"opacity" => '/^opacity(-[0-9]+)?$/',
|
|
73
|
-
|
|
74
55
|
// **Flexbox alignment classes**
|
|
75
56
|
"justify" => "/^justify-(start|end|center|between|around|evenly)$/",
|
|
76
|
-
|
|
77
57
|
// **Flexbox alignment classes**
|
|
78
58
|
"items" => "/^items-(start|end|center|baseline|stretch)$/",
|
|
79
|
-
|
|
80
59
|
// **Width classes**
|
|
81
60
|
"w" => "/^w-(full|[0-9]+|\\[.+\\])$/",
|
|
82
|
-
|
|
61
|
+
// **Max-width classes**
|
|
62
|
+
"max-w" => '/^max-w-(full|[0-9]+|\\[.+\\]|[a-zA-Z]+)$/',
|
|
83
63
|
// **Other utility classes can be added here**
|
|
84
64
|
];
|
|
85
65
|
|
|
@@ -92,7 +72,6 @@ class TwMerge
|
|
|
92
72
|
"pr" => ["pr"],
|
|
93
73
|
"pb" => ["pb"],
|
|
94
74
|
"pl" => ["pl"],
|
|
95
|
-
|
|
96
75
|
// **Margin conflict groups**
|
|
97
76
|
"m" => ["m", "mx", "my", "mt", "mr", "mb", "ml"],
|
|
98
77
|
"mx" => ["mx", "ml", "mr"],
|
|
@@ -101,57 +80,37 @@ class TwMerge
|
|
|
101
80
|
"mr" => ["mr"],
|
|
102
81
|
"mb" => ["mb"],
|
|
103
82
|
"ml" => ["ml"],
|
|
104
|
-
|
|
105
83
|
// **Border width conflict group**
|
|
106
84
|
"border-width" => ["border-width"],
|
|
107
|
-
|
|
108
85
|
// **Border color conflict group**
|
|
109
86
|
"border-color" => ["border-color"],
|
|
110
|
-
|
|
111
87
|
// **Text size conflict group**
|
|
112
88
|
"text-size" => ["text-size"],
|
|
113
|
-
|
|
114
89
|
// **Text color conflict group**
|
|
115
90
|
"text-color" => ["text-color"],
|
|
116
|
-
|
|
117
91
|
// **Text alignment conflict group**
|
|
118
92
|
"text-alignment" => ["text-alignment"],
|
|
119
|
-
|
|
120
93
|
// **Text transform conflict group**
|
|
121
94
|
"text-transform" => ["text-transform"],
|
|
122
|
-
|
|
123
95
|
// **Text decoration conflict group**
|
|
124
96
|
"text-decoration" => ["text-decoration"],
|
|
125
|
-
|
|
126
|
-
// **Opacity conflict group
|
|
97
|
+
// **Opacity conflict group**
|
|
127
98
|
"opacity" => ["opacity"],
|
|
128
|
-
|
|
129
|
-
// **Flexbox alignment conflict group**
|
|
99
|
+
// **Flexbox alignment conflict groups**
|
|
130
100
|
"justify" => ["justify"],
|
|
131
|
-
|
|
132
101
|
// **Flexbox alignment conflict group**
|
|
133
102
|
"items" => ["items"],
|
|
134
|
-
|
|
135
103
|
// **Width conflict group**
|
|
136
104
|
"w" => ["w"],
|
|
137
|
-
|
|
105
|
+
// **Max-width conflict group**
|
|
106
|
+
"max-w" => ["max-w"],
|
|
138
107
|
// **Add other conflict groups as needed**
|
|
139
108
|
];
|
|
140
109
|
|
|
141
110
|
/**
|
|
142
111
|
* Merges multiple CSS class strings or arrays of CSS class strings into a single, optimized CSS class string.
|
|
143
112
|
*
|
|
144
|
-
*
|
|
145
|
-
* duplicate or conflicting classes, and prioritizes the last occurrence of a class. It splits class strings
|
|
146
|
-
* by whitespace, handles conflicting class groups, and ensures a clean and well-formatted output.
|
|
147
|
-
*
|
|
148
|
-
* ### Features:
|
|
149
|
-
* - Accepts individual class strings or arrays of class strings.
|
|
150
|
-
* - Automatically handles arrays by flattening them into individual strings.
|
|
151
|
-
* - Removes duplicate or conflicting classes based on class groups.
|
|
152
|
-
* - Combines all classes into a single string, properly formatted and optimized.
|
|
153
|
-
*
|
|
154
|
-
* @param string|array ...$classes The CSS classes to be merged. Each argument can be a string or an array of strings.
|
|
113
|
+
* @param string|array ...$classes The CSS classes to be merged.
|
|
155
114
|
* @return string A single CSS class string with duplicates and conflicts resolved.
|
|
156
115
|
*/
|
|
157
116
|
public static function mergeClasses(string|array ...$classes): string
|
|
@@ -159,74 +118,73 @@ class TwMerge
|
|
|
159
118
|
$classArray = [];
|
|
160
119
|
|
|
161
120
|
foreach ($classes as $class) {
|
|
162
|
-
// Handle arrays by flattening them into strings
|
|
121
|
+
// Handle arrays by flattening them into strings.
|
|
163
122
|
$classList = is_array($class) ? $class : [$class];
|
|
164
123
|
foreach ($classList as $item) {
|
|
165
124
|
if (!empty(trim($item))) {
|
|
166
|
-
// Split the classes by any whitespace characters
|
|
125
|
+
// Split the classes by any whitespace characters.
|
|
167
126
|
$splitClasses = preg_split("/\s+/", $item);
|
|
168
127
|
foreach ($splitClasses as $individualClass) {
|
|
169
128
|
$classKey = self::getClassGroup($individualClass);
|
|
170
|
-
$conflictingKeys = self::getConflictingKeys($classKey);
|
|
171
129
|
|
|
172
|
-
//
|
|
130
|
+
// If the class is non-responsive (no colon), remove any responsive variants for the same base.
|
|
131
|
+
if (strpos($classKey, ':') === false) {
|
|
132
|
+
$baseGroup = $classKey;
|
|
133
|
+
foreach ($classArray as $existingKey => $existingClass) {
|
|
134
|
+
if ($existingKey !== $baseGroup && substr($existingKey, -strlen($baseGroup)) === $baseGroup) {
|
|
135
|
+
unset($classArray[$existingKey]);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Remove conflicting classes based on the conflict groups.
|
|
141
|
+
$conflictingKeys = self::getConflictingKeys($classKey);
|
|
173
142
|
foreach ($conflictingKeys as $key) {
|
|
174
143
|
unset($classArray[$key]);
|
|
175
144
|
}
|
|
176
145
|
|
|
177
|
-
// Update the array, prioritizing the last occurrence
|
|
146
|
+
// Update the array, prioritizing the last occurrence.
|
|
178
147
|
$classArray[$classKey] = $individualClass;
|
|
179
148
|
}
|
|
180
149
|
}
|
|
181
150
|
}
|
|
182
151
|
}
|
|
183
152
|
|
|
184
|
-
// Combine the final classes into a single string
|
|
153
|
+
// Combine the final classes into a single string.
|
|
185
154
|
return implode(" ", array_values($classArray));
|
|
186
155
|
}
|
|
187
156
|
|
|
188
157
|
private static function getClassGroup($class)
|
|
189
158
|
{
|
|
190
|
-
// Match optional prefixes (responsive and variants)
|
|
159
|
+
// Match optional prefixes (responsive and variants).
|
|
191
160
|
$pattern = '/^((?:[a-z-]+:)*)(.+)$/';
|
|
192
|
-
|
|
193
161
|
if (preg_match($pattern, $class, $matches)) {
|
|
194
|
-
$prefixes = $matches[1];
|
|
195
|
-
$utilityClass = $matches[2];
|
|
162
|
+
$prefixes = $matches[1];
|
|
163
|
+
$utilityClass = $matches[2];
|
|
196
164
|
|
|
197
|
-
//
|
|
165
|
+
// Match the utilityClass against patterns.
|
|
198
166
|
foreach (self::$classGroupPatterns as $groupKey => $regex) {
|
|
199
167
|
if (preg_match($regex, $utilityClass)) {
|
|
200
168
|
return $prefixes . $groupKey;
|
|
201
169
|
}
|
|
202
170
|
}
|
|
203
|
-
|
|
204
|
-
// If no match, use the full class
|
|
171
|
+
// If no match, use the full class.
|
|
205
172
|
return $prefixes . $utilityClass;
|
|
206
173
|
}
|
|
207
|
-
|
|
208
|
-
// For classes without a recognizable prefix, return the class itself
|
|
174
|
+
// For classes without a recognizable prefix, return the class itself.
|
|
209
175
|
return $class;
|
|
210
176
|
}
|
|
211
177
|
|
|
212
178
|
private static function getConflictingKeys($classKey)
|
|
213
179
|
{
|
|
214
|
-
// Remove any responsive or variant prefixes
|
|
180
|
+
// Remove any responsive or variant prefixes.
|
|
215
181
|
$baseClassKey = preg_replace("/^(?:[a-z-]+:)+/", "", $classKey);
|
|
216
|
-
|
|
217
|
-
// Check for conflicts
|
|
218
182
|
if (isset(self::$conflictGroups[$baseClassKey])) {
|
|
219
|
-
|
|
220
|
-
$prefix = preg_replace(
|
|
221
|
-
"/" . preg_quote($baseClassKey, "/") . '$/',
|
|
222
|
-
"",
|
|
223
|
-
$classKey
|
|
224
|
-
);
|
|
183
|
+
$prefix = preg_replace("/" . preg_quote($baseClassKey, "/") . '$/', "", $classKey);
|
|
225
184
|
return array_map(function ($conflict) use ($prefix) {
|
|
226
185
|
return $prefix . $conflict;
|
|
227
186
|
}, self::$conflictGroups[$baseClassKey]);
|
|
228
187
|
}
|
|
229
|
-
|
|
230
188
|
return [$classKey];
|
|
231
189
|
}
|
|
232
190
|
}
|
package/dist/src/Lib/Request.php
CHANGED
|
@@ -88,6 +88,11 @@ class Request
|
|
|
88
88
|
*/
|
|
89
89
|
public static string $uri = '';
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* @var string $decodedUri Holds the decoded request URI.
|
|
93
|
+
*/
|
|
94
|
+
public static string $decodedUri = '';
|
|
95
|
+
|
|
91
96
|
/**
|
|
92
97
|
* @var string $referer Holds the referer of the request.
|
|
93
98
|
*/
|
|
@@ -452,4 +457,16 @@ class Request
|
|
|
452
457
|
|
|
453
458
|
exit;
|
|
454
459
|
}
|
|
460
|
+
|
|
461
|
+
public static function getDecodedUrl(string $uri): string
|
|
462
|
+
{
|
|
463
|
+
$parsedUrl = parse_url($uri);
|
|
464
|
+
|
|
465
|
+
$queryString = isset($parsedUrl['query']) ? '?' . urldecode($parsedUrl['query']) : '';
|
|
466
|
+
$path = $parsedUrl['path'] ?? '';
|
|
467
|
+
|
|
468
|
+
$decodedUrl = urldecode($path . $queryString);
|
|
469
|
+
|
|
470
|
+
return $decodedUrl;
|
|
471
|
+
}
|
|
455
472
|
}
|
|
@@ -22,19 +22,25 @@ final class Validator
|
|
|
22
22
|
/**
|
|
23
23
|
* Validate and sanitize a string.
|
|
24
24
|
*
|
|
25
|
-
* This function converts the input to a string, trims any leading or trailing
|
|
26
|
-
* whitespace, and converts special characters to HTML entities to
|
|
27
|
-
* XSS attacks. If the input is null, an empty string is returned.
|
|
25
|
+
* This function converts the input to a string, trims any leading or trailing
|
|
26
|
+
* whitespace, and optionally converts special characters to HTML entities to
|
|
27
|
+
* prevent XSS attacks. If the input is null, an empty string is returned.
|
|
28
28
|
*
|
|
29
29
|
* @param mixed $value The value to validate and sanitize. This can be of any type.
|
|
30
|
-
* @
|
|
30
|
+
* @param bool $escapeHtml Whether to escape special characters as HTML entities.
|
|
31
|
+
* Defaults to true. Set to false when handling database
|
|
32
|
+
* queries or other non-HTML contexts.
|
|
33
|
+
* @return string The sanitized string. If the input is not a string or null,
|
|
34
|
+
* it is converted to its string representation before sanitization.
|
|
35
|
+
* If the input is null, an empty string is returned.
|
|
31
36
|
*/
|
|
32
|
-
public static function string($value): string
|
|
37
|
+
public static function string($value, bool $escapeHtml = true): string
|
|
33
38
|
{
|
|
34
39
|
// Convert the value to a string if it's not null
|
|
35
40
|
$stringValue = $value !== null ? (string)$value : '';
|
|
36
|
-
|
|
37
|
-
|
|
41
|
+
|
|
42
|
+
// If escaping is enabled, apply htmlspecialchars; otherwise, just trim
|
|
43
|
+
return $escapeHtml ? htmlspecialchars(trim($stringValue), ENT_QUOTES, 'UTF-8') : trim($stringValue);
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
/**
|
package/dist/src/app/js/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(()=>{const e=EventTarget.prototype.addEventListener,t=EventTarget.prototype.removeEventListener,n=new Map;EventTarget.prototype.addEventListener=function(t,s,i){n.has(this)||n.set(this,new Map);const a=n.get(this).get(t)||new Set;a.add(s),n.get(this).set(t,a),e.call(this,t,s,i)},EventTarget.prototype.removeEventListener=function(e,s,i){if(n.has(this)&&n.get(this).has(e)){const t=n.get(this).get(e);t&&(t.delete(s),0===t.size&&n.get(this).delete(e))}t.call(this,e,s,i)},EventTarget.prototype.removeAllEventListeners=function(e){if(n.has(this)&&n.get(this).has(e)){const s=n.get(this).get(e);s&&(s.forEach((n=>{t.call(this,e,n)})),n.get(this).delete(e))}}})();class PPHP{static _instance;eventHandlers;redirectRegex=/redirect_7F834\s*=\s*(\/[^\s]*)/;isNavigating=!1;responseData=null;state={checkedElements:new Set};constructor(){this.eventHandlers=new Set(["onclick","ondblclick","onmousedown","onmouseup","onmouseover","onmousemove","onmouseout","onwheel","onkeypress","onkeydown","onkeyup","onfocus","onblur","onchange","oninput","onselect","onsubmit","onreset","onresize","onscroll","onload","onunload","onabort","onerror","onbeforeunload","oncopy","oncut","onpaste","ondrag","ondragstart","ondragend","ondragover","ondragenter","ondragleave","ondrop","oncontextmenu","ontouchstart","ontouchmove","ontouchend","ontouchcancel","onpointerdown","onpointerup","onpointermove","onpointerover","onpointerout","onpointerenter","onpointerleave","onpointercancel"]),this.handlePopState()}static get instance(){return PPHP._instance||(PPHP._instance=new PPHP),PPHP._instance}handlePopState(){window.addEventListener("popstate",(async()=>{await this.handleNavigation()}))}attachWireFunctionEvents(){this.handleHiddenAttribute();document.querySelectorAll("button, input, select, textarea, a, form, label, div, span").forEach((e=>{if(this.handleAnchorTag(e),Array.from(e.attributes).filter((e=>this.eventHandlers.has(e.name))).forEach((t=>{const n=t.name.slice(2),s=t.value;e instanceof HTMLInputElement&&this.handleInputAppendParams(e,n),s&&(e.removeAttribute(t.name),this.handleDebounce(e,n,s))})),e instanceof HTMLFormElement){const t=e.getAttribute("onsubmit");t&&(e.removeAttribute("onsubmit"),this.handleDebounce(e,"submit",t))}}))}async handleDebounce(e,t,n){e.removeEventListener(t,(()=>{}));const s=e.getAttribute("pp-debounce")||"",i=e.getAttribute("pp-before-request")||"",a=e.getAttribute("pp-after-request")||"",o=async t=>{t.preventDefault();try{i&&await this.invokeHandler(e,i,t),await this.invokeHandler(e,n,t),a&&"@close"!==a&&await this.invokeHandler(e,a,t),this.handlerAutofocusAttribute()}catch(e){}};if(s){const n=this.parseTime(s),i=this.debounce(o,n);e instanceof HTMLFormElement&&"submit"===t?e.addEventListener(t,(e=>{e.preventDefault(),i(e)})):e.addEventListener(t,i)}else e.addEventListener(t,o)}debounce(e,t=300,n=!1){let s;return function(...i){const a=this;s&&clearTimeout(s),s=setTimeout((()=>{s=null,n||e.apply(a,i)}),t),n&&!s&&e.apply(a,i)}}handlerAutofocusAttribute(){const e=document.querySelectorAll("[pp-autofocus]");let t=!1;e.forEach((e=>{if(t)return;const n=e.getAttribute("pp-autofocus");if(!n||!this.isJsonLike(n))return;const s=this.parseJson(n);if(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement){const t=["text","search","tel","url","password"];if(e instanceof HTMLInputElement)if(t.includes(e.type))if("number"===e.type){e.type="text";const t=e.value.length||0;e.setSelectionRange(t,t),e.type="number"}else this.setCursorPosition(e,s);else;else e instanceof HTMLTextAreaElement&&this.setCursorPosition(e,s)}e.focus(),t=!0}))}async invokeHandler(e,t,n){try{const s=t.match(/^(\w+(\.\w+)*)\((.*)\)$/);if(s){const i=s[1],a=s[3],o=i.split("."),{context:r,methodName:c}=this.resolveContext(o);if("function"==typeof r[c])if(this.isJsonLike(a)){const t=this.parseJson(a);t.element=e,t.event=n;const s=[t];await r[c](...s)}else new Function("event",t).call(e,n);else await this.handleParsedCallback(e,t)}else await this.handleParsedCallback(e,t)}catch(e){}}async handleParsedCallback(e,t){const{funcName:n,data:s}=this.parseCallback(e,t);if(!n)return;const i=this[n],a=window[n];let o;if("function"==typeof i?o=i:"function"==typeof a&&(o=a),"function"==typeof o){const t=e.hasAttribute("pp-after-request"),n=Array.isArray(s.args)?s.args:[],i=this.responseData?this.parseJson(this.responseData):{response:this.responseData};let a={args:n,element:e,data:s};t&&(a={...a,...i}),await o.call(this,a)}else this.responseData=null,this.responseData=await this.handleUndefinedFunction(e,n,s)}async handleUndefinedFunction(e,t,n){const s={callback:t,...n},i=this.createFetchOptions(s),a=this.createFetchOptions({secondRequestC69CD:!0,...this.getUrlParams()});try{this.saveElementOriginalState(e),this.handleSuspenseElement(e);const n=new URL(window.location.href);let s="",o="",r={success:!1};const c=e.querySelector("input[type='file']");if(c){if(s=await this.fetchFile(n.href,t,c),o=this.extractJson(s)||"",o)try{r=this.parseJson(o)}catch(e){}}else{const e=await this.fetch(n.href,i);if(s=await e.text(),this.getRedirectUrl(s))return void await this.handleRedirect(this.getRedirectUrl(s)||"");if(o=this.extractJson(s)||"",o)try{r=this.parseJson(o)}catch(e){}}const l=e.getAttribute("pp-before-request")||"",h=e.getAttribute("pp-after-request")||"";if((l||h&&r.success)&&this.restoreSuspenseElement(e),l||h){let e="";if(r.success){e=s.replace(o,"")}else e=s;if(this.appendAfterbegin(e),!h&&!r.success)return}if(h&&r.success){this.handleAfterRequest(h,o);const e=s.replace(o,"");return this.appendAfterbegin(e),o}if("@close"===h)return r.success?o:void 0;const d=await this.fetch(n.href,a),u=await d.text();if(this.getRedirectUrl(u))return void await this.handleRedirect(this.getRedirectUrl(u)||"");await this.handleResponseRedirectOrUpdate(s,u,o,r)}catch(e){}}handleAfterRequest(e,t){if(!this.isJsonLike(e))return;const n=this.parseJson(e),s=t?this.parseJson(t):null,i=n.targets;Array.isArray(i)&&i.forEach((e=>{const{id:t,...n}=e,i=document.querySelector(t);let a={};if(s){for(const t in n)if(n.hasOwnProperty(t))switch(t){case"innerHTML":case"outerHTML":case"textContent":case"innerText":"response"===n[t]&&(a[t]=e.responseKey?s[e.responseKey]:s.response);break;default:a[t]=n[t];break}}else a=n;i&&this.updateElementAttributes(i,a)}))}async handleResponseRedirectOrUpdate(e,t,n,s){const i=this.getUpdatedHTMLContent(e,n,s),a=(new DOMParser).parseFromString(t,"text/html");i&&a.body.insertAdjacentElement("afterbegin",i),this.updateBodyContent(a.body.outerHTML)}getUpdatedHTMLContent(e,t,n){const s=document.createElement("div");if(s.id="afterbegin-8D95D",n&&t?.success){const t=e.replace(n,"");s.innerHTML=t}else s.innerHTML=e;return s.innerHTML?s:null}async updateBodyContent(e){const t=this.saveScrollPositions();this.saveState();const n=(new DOMParser).parseFromString(e,"text/html");document.removeAllEventListeners("PPBodyLoaded"),await this.appendCallbackResponse(n),this.restoreState(),this.restoreScrollPositions(t),this.attachWireFunctionEvents(),document.dispatchEvent(new Event("PPBodyLoaded"))}restoreState(){if(this.state.focusId){const e=document.getElementById(this.state.focusId)||document.querySelector(`[name="${this.state.focusId}"]`);if(e instanceof HTMLInputElement){const t=e.value.length||0;void 0!==this.state.focusSelectionStart&&null!==this.state.focusSelectionEnd&&e.setSelectionRange(t,t),this.state.focusValue&&("checkbox"===e.type||"radio"===e.type?e.checked=!!this.state.focusChecked:"number"===e.type||"email"===e.type?(e.type="text",e.setSelectionRange(t,t),e.type="number"===e.type?"number":"email"):"date"===e.type||"month"===e.type||"week"===e.type||"time"===e.type||"datetime-local"===e.type||"color"===e.type||"file"===e.type||""!==e.value&&(e.value=this.state.focusValue)),e.focus()}else if(e instanceof HTMLTextAreaElement){const t=e.value.length||0;void 0!==this.state.focusSelectionStart&&null!==this.state.focusSelectionEnd&&e.setSelectionRange(t,t),this.state.focusValue&&""!==e.value&&(e.value=this.state.focusValue),e.focus()}else e instanceof HTMLSelectElement&&(this.state.focusValue&&""!==e.value&&(e.value=this.state.focusValue),e.focus())}this.state.checkedElements.forEach((e=>{const t=document.getElementById(e);t&&(t.checked=!0)}))}async appendCallbackResponse(e){const t=e.getElementById("afterbegin-8D95D");if(t){const e=document.getElementById("afterbegin-8D95D");e?e.innerHTML=t.innerHTML:document.body.insertAdjacentHTML("afterbegin",t.outerHTML)}await this.populateDocumentBody(e)}saveState(){const e=document.activeElement;this.state.focusId=e?.id||e?.name,this.state.focusValue=e?.value,this.state.focusChecked=e?.checked,this.state.focusType=e?.type,this.state.focusSelectionStart=e?.selectionStart,this.state.focusSelectionEnd=e?.selectionEnd,this.state.isSuspense=e.hasAttribute("pp-suspense"),this.state.checkedElements.clear(),document.querySelectorAll('input[type="checkbox"]:checked').forEach((e=>{this.state.checkedElements.add(e.id||e.name)})),document.querySelectorAll('input[type="radio"]:checked').forEach((e=>{this.state.checkedElements.add(e.id||e.name)}))}updateElementAttributes(e,t){for(const n in t)if(t.hasOwnProperty(n))switch(n){case"innerHTML":case"outerHTML":case"textContent":case"innerText":e[n]=this.decodeHTML(t[n]);break;case"insertAdjacentHTML":e.insertAdjacentHTML(t.position||"beforeend",this.decodeHTML(t[n].html));break;case"insertAdjacentText":e.insertAdjacentText(t.position||"beforeend",this.decodeHTML(t[n].text));break;case"setAttribute":e.setAttribute(t.attrName,this.decodeHTML(t[n]));break;case"removeAttribute":e.removeAttribute(t[n]);break;case"className":e.className=this.decodeHTML(t[n]);break;case"classList.add":e.classList.add(...this.decodeHTML(t[n]).split(","));break;case"classList.remove":e.classList.remove(...this.decodeHTML(t[n]).split(","));break;case"classList.toggle":e.classList.toggle(this.decodeHTML(t[n]));break;case"classList.replace":const[s,i]=this.decodeHTML(t[n]).split(",");e.classList.replace(s,i);break;case"dataset":e.dataset[t.attrName]=this.decodeHTML(t[n]);break;case"style":Object.assign(e.style,t[n]);break;case"value":e.value=this.decodeHTML(t[n]);break;case"checked":e.checked=t[n];break;default:e.setAttribute(n,this.decodeHTML(t[n]))}}decodeHTML(e){const t=document.createElement("textarea");return t.innerHTML=e,t.value}appendAfterbegin(e){if(!e)return;const t="afterbegin-8D95D";let n=document.getElementById(t);n?(n.innerHTML=e,document.body.insertAdjacentElement("afterbegin",n)):(n=document.createElement("div"),n.id=t,n.innerHTML=e,document.body.insertAdjacentElement("afterbegin",n))}restoreSuspenseElement(e){const t=e.getAttribute("pp-original-state");if(e.hasAttribute("pp-suspense")&&t){const n=(e,t)=>{for(const n in t)t.hasOwnProperty(n)&&("textContent"===n?e.textContent=t[n]:"innerHTML"===n?e.innerHTML=t[n]:"disabled"===n?!0===t[n]?e.setAttribute("disabled","true"):e.removeAttribute("disabled"):e.setAttribute(n,t[n]));for(const n of Array.from(e.attributes))t.hasOwnProperty(n.name)||e.removeAttribute(n.name)},s=(e,t)=>{for(const s in t)if(t.hasOwnProperty(s))for(const t of Array.from(e.elements))if(t instanceof HTMLInputElement||t instanceof HTMLButtonElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement){const e=t.getAttribute("pp-original-state")||"";if(e){if(this.isJsonLike(e)){const s=this.parseJson(e);n(t,s)}else i(t,e);t.removeAttribute("pp-original-state")}}},i=(e,t)=>{e instanceof HTMLInputElement?e.value=t:e.textContent=t},a=(e,t)=>{e instanceof HTMLFormElement?s(e,t):n(e,t)};try{const i=this.parseJson(t);if(i)if(e instanceof HTMLFormElement){const t=e.id;if(t){const e=document.querySelector(`[form="${t}"]`);if(e){const t=e.getAttribute("pp-original-state");if(t&&this.isJsonLike(t)){const s=this.parseJson(t);n(e,s)}}}const i=new FormData(e),a={};if(i.forEach(((e,t)=>{a[t]=e})),s(e,a),e.hasAttribute("pp-suspense")){const t=e.getAttribute("pp-suspense")||"";if(this.parseJson(t).disabled)for(const t of Array.from(e.elements))(t instanceof HTMLInputElement||t instanceof HTMLButtonElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement)&&t.removeAttribute("disabled")}}else if(i.targets){i.targets.forEach((e=>{const{id:t,...n}=e,s=document.querySelector(t);s&&a(s,n)}));const{targets:t,...s}=i;n(e,s)}else{const{empty:t,...s}=i;n(e,s)}}catch(e){}}e.querySelectorAll("[pp-suspense]").forEach((e=>this.restoreSuspenseElement(e))),e.removeAttribute("pp-original-state")}extractJson(e){const t=e?.match(/\{[\s\S]*\}/);return t?t[0]:null}getRedirectUrl(e){const t=e.match(this.redirectRegex);return t?t[1]:null}async fetchFile(e,t,n){const s=new FormData,i=n.files;if(i)for(let e=0;e<i.length;e++)s.append("file[]",i[e]);s.append("callback",t);const a=await fetch(e,{method:"POST",headers:{HTTP_PPHP_WIRE_REQUEST:"true"},body:s});return await a.text()}async handleSuspenseElement(e){let t=e.getAttribute("pp-suspense")||"";const n=(e,t)=>{for(const n in t)if(t.hasOwnProperty(n))for(const t of e.elements)if(t instanceof HTMLInputElement||t instanceof HTMLButtonElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement){const e=t.getAttribute("pp-suspense")||"";if(e)if(this.isJsonLike(e)){const n=this.parseJson(e);"disabled"!==n.onsubmit&&this.updateElementAttributes(t,n),n.targets&&n.targets.forEach((e=>{const{id:t,...n}=e,s=document.querySelector(t);s&&i(s,n)}))}else s(t,e)}},s=(e,t)=>{e instanceof HTMLInputElement?e.value=t:e.textContent=t},i=(e,t)=>{e instanceof HTMLFormElement?n(e,t):this.updateElementAttributes(e,t)};try{if(t&&this.isJsonLike(t)){const s=this.parseJson(t);if(s)if(e instanceof HTMLFormElement){const t=new FormData(e),i={};t.forEach(((e,t)=>{i[t]=e})),s.disabled&&this.toggleFormElements(e,!0);const{disabled:a,...o}=s;this.updateElementAttributes(e,o),n(e,i)}else if(s.targets){s.targets.forEach((e=>{const{id:t,...n}=e,s=document.querySelector(t);s&&i(s,n)}));const{targets:t,...n}=s;this.updateElementAttributes(e,n)}else{if("disabled"===s.empty&&""===e.value)return;const{empty:t,...n}=s;this.updateElementAttributes(e,n)}}else if(t)s(e,t);else if(e instanceof HTMLFormElement){const t=new FormData(e),s={};t.forEach(((e,t)=>{s[t]=e})),n(e,s)}}catch(e){}}toggleFormElements(e,t){Array.from(e.elements).forEach((e=>{(e instanceof HTMLInputElement||e instanceof HTMLButtonElement||e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement)&&(e.disabled=t)}))}saveElementOriginalState(e){if(e.hasAttribute("pp-suspense")&&!e.hasAttribute("pp-original-state")){const t={};e.textContent&&(t.textContent=e.textContent.trim()),e.innerHTML&&(t.innerHTML=e.innerHTML.trim()),(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement)&&(t.value=e.value);for(let n=0;n<e.attributes.length;n++){const s=e.attributes[n];t[s.name]=s.value}e.setAttribute("pp-original-state",JSON.stringify(t))}if(e instanceof HTMLFormElement){let t=null;const n=e.id;n&&(t=document.querySelector(`[form="${n}"]`)),n&&t||(t=Array.from(e.elements).find((e=>e instanceof HTMLButtonElement||e instanceof HTMLInputElement))),t&&(t.hasAttribute("pp-original-state")||this.saveElementOriginalState(t))}e.querySelectorAll("[pp-suspense]").forEach((e=>this.saveElementOriginalState(e)))}getUrlParams(){const e={};return new URLSearchParams(window.location.search).forEach(((t,n)=>{e[n]=t})),e}createFetchOptions(e){return{method:"POST",headers:{"Content-Type":"application/json",HTTP_PPHP_WIRE_REQUEST:"true"},body:JSON.stringify(e)}}parseCallback(e,t){let n={};const s=e.closest("form");if(s){new FormData(s).forEach(((e,t)=>{n[t]?Array.isArray(n[t])?n[t].push(e):n[t]=[n[t],e]:n[t]=e}))}else e instanceof HTMLInputElement?n=this.handleInputElement(e):(e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement)&&(n[e.name]=e.value);const i=t.match(/(\w+)\((.*)\)/);if(i){const e=i[1];let t=i[2].trim();if(t.startsWith("{")&&t.endsWith("}"))try{const e=this.parseJson(t);"object"==typeof e&&null!==e&&(n={...n,...e})}catch(e){}else{const e=t.split(/,(?=(?:[^'"]*['"][^'"]*['"])*[^'"]*$)/).map((e=>e.trim().replace(/^['"]|['"]$/g,"")));n.args=e}return{funcName:e,data:n}}return{funcName:t,data:n}}handleInputElement(e){let t={};if(e.name)if("checkbox"===e.type)t[e.name]={value:e.value,checked:e.checked};else if("radio"===e.type){const n=document.querySelector(`input[name="${e.name}"]:checked`);t[e.name]=n?n.value:null}else t[e.name]=e.value;else"checkbox"===e.type||"radio"===e.type?t.value=e.checked:t.value=e.value;return t}resolveContext(e){let t=window;for(let n=0;n<e.length-1;n++)if(t=t[e[n]],!t)throw new Error(`Cannot find object ${e[n]} in the context.`);return{context:t,methodName:e[e.length-1]}}setCursorPosition(e,t){if(t.start)e.setSelectionRange(0,0);else if(t.end){const t=e.value.length||0;e.setSelectionRange(t,t)}else if(t.length){const n=parseInt(t.length,10)||0;e.setSelectionRange(n,n)}}handleInputAppendParams(e,t){const n=e.getAttribute("pp-append-params"),s=e.getAttribute("pp-append-params-sync");if("true"===n){if("true"===s){const t=e.name||e.id;if(t){const n=new URL(window.location.href),s=new URLSearchParams(n.search);s.has(t)&&(e.value=s.get(t)||"")}}e.addEventListener(t,(e=>{const t=e.currentTarget,n=t.value.trim(),s=new URL(window.location.href),i=new URLSearchParams(s.search),a=t.name||t.id;if(a){n?i.set(a,n):i.delete(a);const e=i.toString()?`${s.pathname}?${i.toString()}`:s.pathname;history.replaceState(null,"",e)}}))}}handleHiddenAttribute(){const e=document.querySelectorAll("[pp-visibility]"),t=document.querySelectorAll("[pp-display]");e.forEach((e=>this.handleVisibilityElementAttribute(e,"pp-visibility",this.handleElementVisibility))),t.forEach((e=>this.handleVisibilityElementAttribute(e,"pp-display",this.handleElementDisplay)))}handleVisibilityElementAttribute(e,t,n){const s=e.getAttribute(t);if(s)if(this.isJsonLike(s)){n(e,this.parseJson(s))}else{const n=this.parseTime(s);if(n>0){const s="pp-visibility"===t?"visibility":"display",i="visibility"===s?"hidden":"none";this.scheduleChange(e,n,s,i)}}}handleElementVisibility(e,t){this.handleElementChange(e,t,"visibility","hidden","visible")}handleElementDisplay(e,t){this.handleElementChange(e,t,"display","none","block")}handleElementChange(e,t,n,s,i){const a=t.start?this.parseTime(t.start):0,o=t.end?this.parseTime(t.end):0;a>0?(e.style[n]=s,this.scheduleChange(e,a,n,i),o>0&&this.scheduleChange(e,a+o,n,s)):o>0&&this.scheduleChange(e,o,n,s)}handleAnchorTag(e){e instanceof HTMLAnchorElement&&e.addEventListener("click",(async e=>{const t=e.currentTarget,n=t.getAttribute("href"),s=t.getAttribute("target");if(n&&"_blank"!==s&&!e.metaKey&&!e.ctrlKey&&(e.preventDefault(),!this.isNavigating)){this.isNavigating=!0;try{if(/^(https?:)?\/\//i.test(n)&&!n.startsWith(window.location.origin))window.location.href=n;else{const e=t.getAttribute("pp-append-params");if(n.startsWith("?")&&"true"===e){const e=new URL(window.location.href),t=new URLSearchParams(e.search);let s="";const[i,a]=n.split("#");a&&(s=`#${a}`);new URLSearchParams(i.split("?")[1]).forEach(((e,n)=>{t.set(n,e)}));const o=`${e.pathname}?${t.toString()}${s}`;history.pushState(null,"",o)}else{const[e,t]=n.split("#"),s=`${e}${t?`#${t}`:""}`;history.pushState(null,"",s)}const s=n.indexOf("#");if(-1!==s){const e=n.slice(s+1),t=document.getElementById(e);if(t)t.scrollIntoView({behavior:"smooth"});else{await this.handleNavigation();const t=document.getElementById(e);t&&t.scrollIntoView({behavior:"smooth"})}}else await this.handleNavigation()}}catch(e){}finally{this.isNavigating=!1}}}))}async handleNavigation(){try{const e=document.getElementById("loading-file-1B87E");if(e){const t=this.findLoadingElement(e,window.location.pathname);t&&await this.updateContentWithTransition(t)}const t=await this.fetch(window.location.href);if(!t.ok)return;const n=await t.text(),s=n.match(this.redirectRegex);if(s&&s[1])return void await this.handleRedirect(s[1]);await this.updateDocumentContent(n)}catch(e){}}findLoadingElement(e,t){let n=t;for(;;){const t=e.querySelector(`div[pp-loading-url='${n}']`);if(t)return t;if("/"===n)break;const s=n.lastIndexOf("/");n=s<=0?"/":n.substring(0,s)}return e.querySelector("div[pp-loading-url='/' ]")}async updateContentWithTransition(e){const t=document.querySelector("[pp-loading-content='true']")||document.body;if(!t)return;const{fadeIn:n,fadeOut:s}=this.parseTransition(e);await this.fadeOut(t,s),t.innerHTML=e.innerHTML,this.fadeIn(t,n)}parseTransition(e){let t=250,n=250;const s=e.querySelector("[pp-loading-transition]")?.getAttribute("pp-loading-transition");if(s)try{const e=this.parseJson(s);t=this.parseTime(e.fadeIn??t),n=this.parseTime(e.fadeOut??n)}catch(e){}return{fadeIn:t,fadeOut:n}}fadeOut(e,t){return new Promise((n=>{e.style.transition=`opacity ${t}ms ease-out`,e.style.opacity="0",setTimeout((()=>{e.style.transition="",n()}),t)}))}fadeIn(e,t){e.style.transition=`opacity ${t}ms ease-in`,e.style.opacity="1",setTimeout((()=>{e.style.transition=""}),t)}async updateDocumentContent(e){const t=this.saveScrollPositions(),n=(new DOMParser).parseFromString(e,"text/html"),s="pp-dynamic-script",i="pp-dynamic-link";document.head.querySelectorAll("[pp-dynamic-meta]").forEach((e=>e.remove()));document.head.querySelectorAll("[pp-dynamic-link]").forEach((e=>e.remove()));document.head.querySelectorAll("[pp-dynamic-script]").forEach((e=>e.remove()));document.removeAllEventListeners("PPBodyLoaded"),await(async e=>{Array.from(e.head.children).forEach((e=>{const t=e.tagName;if("SCRIPT"===t&&e.hasAttribute(s)){const t=document.createElement("script");Array.from(e.attributes).forEach((e=>t.setAttribute(e.name,e.value))),e.textContent&&(t.textContent=e.textContent),document.head.appendChild(t)}else if("META"===t){if(e.getAttribute("charset")||"viewport"===e.getAttribute("name"))return;const t=e.name,n=e.getAttribute("property"),s=document.head.querySelector(t?`meta[name="${t}"]`:`meta[property="${n}"]`),i=document.head.querySelector("title");s?document.head.replaceChild(e.cloneNode(!0),s):i?.nextSibling?document.head.insertBefore(e.cloneNode(!0),i.nextSibling):document.head.appendChild(e.cloneNode(!0))}else if("TITLE"===t){const t=document.head.querySelector("title");t?document.head.replaceChild(e.cloneNode(!0),t):document.head.appendChild(e.cloneNode(!0))}else if("LINK"===t){const t=t=>{const n=document.head.querySelector('link[rel="icon"]');if(n)document.head.replaceChild(e.cloneNode(!0),n);else{const e=document.createElement("link");e.rel="icon",e.href=t,document.head.appendChild(e)}};if("icon"===e.getAttribute("rel")){t(e.href)}else if(e.hasAttribute(i)){const t=e.cloneNode(!0);document.head.appendChild(t)}}})),await this.populateDocumentBody(e)})(n),this.restoreScrollPositions(t),this.attachWireFunctionEvents(),document.dispatchEvent(new Event("PPBodyLoaded"))}restoreScrollPositions(e){requestAnimationFrame((()=>{const t=e.window;t&&window.scrollTo(t.scrollLeft,t.scrollTop),document.querySelectorAll("*").forEach((t=>{const n=this.getElementKey(t);e[n]&&(t.scrollTop=e[n].scrollTop,t.scrollLeft=e[n].scrollLeft)}))}))}async populateDocumentBody(e){try{const t=e.body.cloneNode(!0);this.manageScriptTags(t),document.body.replaceWith(t)}catch(e){}}manageScriptTags(e,t){const n=e.querySelectorAll("script"),s=t?.querySelectorAll("script")||n;n.forEach(((e,t)=>{const n=document.createElement("script"),i=s[t]||e;Array.from(i.attributes).forEach((e=>{n.setAttribute(e.name,e.value)})),i.hasAttribute("src")||(n.textContent=i.textContent),e.parentNode?.replaceChild(n,e)}))}saveScrollPositions(){const e={window:{scrollTop:window.scrollY||document.documentElement.scrollTop,scrollLeft:window.scrollX||document.documentElement.scrollLeft}};return document.querySelectorAll("*").forEach((t=>{(t.scrollTop||t.scrollLeft)&&(e[this.getElementKey(t)]={scrollTop:t.scrollTop,scrollLeft:t.scrollLeft})})),e}getElementKey(e){return e.id||e.className||e.tagName}async handleRedirect(e){if(e)try{const t=new URL(e,window.location.origin);t.origin!==window.location.origin?window.location.href=e:(history.pushState(null,"",e),await this.handleNavigation())}catch(e){}}async fetch(e,t){return fetch(e,{...t,headers:{...t?.headers,"X-Requested-With":"XMLHttpRequest"}})}isJsonLike(e){return"string"==typeof e&&((e=e.trim()).startsWith("{")&&e.endsWith("}")||e.startsWith("[")&&e.endsWith("]"))}parseJson(e){try{return JSON5.parse(e)}catch(e){return null}}parseTime(e){if("number"==typeof e)return e;const t=e.match(/^(\d+)(ms|s|m)?$/);if(t){const e=parseInt(t[1],10);switch(t[2]||"ms"){case"ms":return e;case"s":return 1e3*e;case"m":return 60*e*1e3;default:return e}}return 0}scheduleChange(e,t,n,s){setTimeout((()=>{requestAnimationFrame((()=>{e.style[n]=s}))}),t)}observeDOMChanges(){new MutationObserver((e=>{for(const t of e)"childList"===t.type&&t.addedNodes.length>0&&this.attachWireFunctionEvents()})).observe(document.body,{childList:!0,subtree:!0})}async fetchFunction(e,t={}){try{const n={callback:e,...t},s=this.createFetchOptions(n),i=await this.fetch(window.location.href,s);if(!i.ok)throw new Error(`Fetch failed with status: ${i.status} ${i.statusText}`);const a=await i.text();try{return JSON.parse(a)}catch{return a}}catch(e){throw new Error("Failed to fetch data.")}}async sync(...e){try{const t=e.length>0?e.map((e=>`pp-sync="${e}"`)):['pp-sync="true"'],n=this.createFetchOptions({secondRequestC69CD:!0,...this.getUrlParams()}),s=await this.fetch(window.location.href,n),i=await s.text(),a=(new DOMParser).parseFromString(i,"text/html");t.forEach((e=>{const t=document.querySelectorAll(`[${e}]`),n=a.body.querySelectorAll(`[${e}]`);t.forEach(((e,t)=>{const s=n[t];s&&(e.innerHTML=s.innerHTML,this.reRunScripts(e))}))}))}catch(e){}}async fetchAndUpdateBodyContent(){const e=this.createFetchOptions({secondRequestC69CD:!0,...this.getUrlParams()}),t=await this.fetch(window.location.href,e),n=await t.text();await this.updateBodyContent(n)}reRunScripts(e){e.querySelectorAll("script").forEach((e=>{const t=document.createElement("script");Array.from(e.attributes).forEach((e=>{t.setAttribute(e.name,e.value)})),e.hasAttribute("src")||(t.textContent=e.textContent),e.parentNode?.replaceChild(t,e)}))}copyCode(e,t,n,s,i="img",a=2e3){if(!(e instanceof HTMLElement))return;const o=e.closest(`.${t}`)?.querySelector("pre code"),r=o?.textContent?.trim()||"";r?navigator.clipboard.writeText(r).then((()=>{const t=e.querySelector(i);if(t)for(const[e,n]of Object.entries(s))e in t?t[e]=n:t.setAttribute(e,n);setTimeout((()=>{if(t)for(const[e,s]of Object.entries(n))e in t?t[e]=s:t.setAttribute(e,s)}),a)}),(()=>{alert("Failed to copy command to clipboard")})):alert("Failed to find the code block to copy")}getCookie(e){return document.cookie.split("; ").find((t=>t.startsWith(e+"=")))?.split("=")[1]||null}}class PPHPLocalStore{static instance=null;state;listeners;pphp;STORAGE_KEY;constructor(e={}){this.state=e,this.listeners=[],this.pphp=PPHP.instance,this.STORAGE_KEY=this.pphp.getCookie("pphp_local_store_key")||"pphp_local_store_59e13",this.loadState()}static getInstance(e={}){return PPHPLocalStore.instance||(PPHPLocalStore.instance=new PPHPLocalStore(e)),PPHPLocalStore.instance}setState(e,t=!1){if(this.state={...this.state,...e},this.listeners.forEach((e=>e(this.state))),this.saveState(),t){const e=localStorage.getItem(this.STORAGE_KEY);e&&this.pphp.fetchFunction(this.STORAGE_KEY,{[this.STORAGE_KEY]:e})}}saveState(){localStorage.setItem(this.STORAGE_KEY,JSON.stringify(this.state))}loadState(){const e=localStorage.getItem(this.STORAGE_KEY);e&&(this.state=this.pphp.parseJson(e),this.listeners.forEach((e=>e(this.state))))}resetState(e,t=!1){if(e?(delete this.state[e],this.saveState()):(this.state={},localStorage.removeItem(this.STORAGE_KEY)),this.listeners.forEach((e=>e(this.state))),t){const t=e?localStorage.getItem(this.STORAGE_KEY):null;this.pphp.fetchFunction(this.STORAGE_KEY,{[this.STORAGE_KEY]:t})}}}
|
|
1
|
+
(()=>{const e=EventTarget.prototype.addEventListener,t=EventTarget.prototype.removeEventListener,n=new Map;EventTarget.prototype.addEventListener=function(t,s,i){n.has(this)||n.set(this,new Map);const a=n.get(this).get(t)||new Set;a.add(s),n.get(this).set(t,a),e.call(this,t,s,i)},EventTarget.prototype.removeEventListener=function(e,s,i){if(n.has(this)&&n.get(this).has(e)){const t=n.get(this).get(e);t&&(t.delete(s),0===t.size&&n.get(this).delete(e))}t.call(this,e,s,i)},EventTarget.prototype.removeAllEventListeners=function(e){if(n.has(this)&&n.get(this).has(e)){const s=n.get(this).get(e);s&&(s.forEach((n=>{t.call(this,e,n)})),n.get(this).delete(e))}}})();class PPHP{static _instance;eventHandlers;redirectRegex=/redirect_7F834\s*=\s*(\/[^\s]*)/;isNavigating=!1;responseData=null;state={checkedElements:new Set};constructor(){this.eventHandlers=new Set(["onclick","ondblclick","onmousedown","onmouseup","onmouseover","onmousemove","onmouseout","onwheel","onkeypress","onkeydown","onkeyup","onfocus","onblur","onchange","oninput","onselect","onsubmit","onreset","onresize","onscroll","onload","onunload","onabort","onerror","onbeforeunload","oncopy","oncut","onpaste","ondrag","ondragstart","ondragend","ondragover","ondragenter","ondragleave","ondrop","oncontextmenu","ontouchstart","ontouchmove","ontouchend","ontouchcancel","onpointerdown","onpointerup","onpointermove","onpointerover","onpointerout","onpointerenter","onpointerleave","onpointercancel"]),this.handlePopState()}static get instance(){return PPHP._instance||(PPHP._instance=new PPHP),PPHP._instance}handlePopState(){window.addEventListener("popstate",(async()=>{await this.handleNavigation()}))}attachWireFunctionEvents(){this.handleHiddenAttribute();document.querySelectorAll("button, input, select, textarea, a, form, label, div, span").forEach((e=>{if(this.handleAnchorTag(e),Array.from(e.attributes).filter((e=>this.eventHandlers.has(e.name))).forEach((t=>{const n=t.name.slice(2),s=t.value;e instanceof HTMLInputElement&&this.handleInputAppendParams(e,n),s&&(e.removeAttribute(t.name),this.handleDebounce(e,n,s))})),e instanceof HTMLFormElement){const t=e.getAttribute("onsubmit");t&&(e.removeAttribute("onsubmit"),this.handleDebounce(e,"submit",t))}}))}async handleDebounce(e,t,n){e.removeEventListener(t,(()=>{}));const s=e.getAttribute("pp-debounce")||"",i=e.getAttribute("pp-before-request")||"",a=e.getAttribute("pp-after-request")||"",o=async t=>{t.preventDefault();try{i&&await this.invokeHandler(e,i,t),await this.invokeHandler(e,n,t),a&&"@close"!==a&&await this.invokeHandler(e,a,t),this.handlerAutofocusAttribute()}catch(e){}};if(s){const n=this.parseTime(s),i=this.debounce(o,n);e instanceof HTMLFormElement&&"submit"===t?e.addEventListener(t,(e=>{e.preventDefault(),i(e)})):e.addEventListener(t,i)}else e.addEventListener(t,o)}debounce(e,t=300,n=!1){let s;return function(...i){const a=this;s&&clearTimeout(s),s=setTimeout((()=>{s=null,n||e.apply(a,i)}),t),n&&!s&&e.apply(a,i)}}handlerAutofocusAttribute(){const e=document.querySelectorAll("[pp-autofocus]");let t=!1;e.forEach((e=>{if(t)return;const n=e.getAttribute("pp-autofocus");if(!n||!this.isJsonLike(n))return;const s=this.parseJson(n);if(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement){const t=["text","search","tel","url","password"];if(e instanceof HTMLInputElement)if(t.includes(e.type))if("number"===e.type){e.type="text";const t=e.value.length||0;e.setSelectionRange(t,t),e.type="number"}else this.setCursorPosition(e,s);else;else e instanceof HTMLTextAreaElement&&this.setCursorPosition(e,s)}e.focus(),t=!0}))}async invokeHandler(e,t,n){try{const s=t.match(/^(\w+(\.\w+)*)\((.*)\)$/);if(s){const i=s[1],a=s[3],o=i.split("."),{context:r,methodName:c}=this.resolveContext(o);if("function"==typeof r[c])if(this.isJsonLike(a)){const t=this.parseJson(a);t.element=e,t.event=n;const s=[t];await r[c](...s)}else new Function("event",t).call(e,n);else await this.handleParsedCallback(e,t)}else await this.handleParsedCallback(e,t)}catch(e){}}async handleParsedCallback(e,t){const{funcName:n,data:s}=this.parseCallback(e,t);if(!n)return;const i=this[n],a=window[n];let o;if("function"==typeof i?o=i:"function"==typeof a&&(o=a),"function"==typeof o){const t=e.hasAttribute("pp-after-request"),n=Array.isArray(s.args)?s.args:[],i=this.responseData?this.parseJson(this.responseData):{response:this.responseData};let a={args:n,element:e,data:s};t&&(a={...a,...i}),await o.call(this,a)}else this.responseData=null,this.responseData=await this.handleUndefinedFunction(e,n,s)}async handleUndefinedFunction(e,t,n){const s={callback:t,...n},i=this.createFetchOptions(s),a=this.createFetchOptions({secondRequestC69CD:!0,...this.getUrlParams()});try{this.saveElementOriginalState(e),this.handleSuspenseElement(e);const s=new URL(window.location.href);let o="",r="",c={success:!1};const l=e.querySelector("input[type='file']");if(l){if(o=await this.fetchFileWithData(s.href,t,l,n),r=this.extractJson(o)||"",r)try{c=this.parseJson(r)}catch(e){}}else{const e=await this.fetch(s.href,i);if(o=await e.text(),this.getRedirectUrl(o))return void await this.handleRedirect(this.getRedirectUrl(o)||"");if(r=this.extractJson(o)||"",r)try{c=this.parseJson(r)}catch(e){}}const h=e.getAttribute("pp-before-request")||"",d=e.getAttribute("pp-after-request")||"";if((h||d&&c.success)&&this.restoreSuspenseElement(e),h||d){let e="";if(c.success){e=o.replace(r,"")}else e=o;if(this.appendAfterbegin(e),!d&&!c.success)return}if(d&&c.success){this.handleAfterRequest(d,r);const e=o.replace(r,"");return this.appendAfterbegin(e),r}if("@close"===d)return c.success?r:void 0;const u=await this.fetch(s.href,a),p=await u.text();if(this.getRedirectUrl(p))return void await this.handleRedirect(this.getRedirectUrl(p)||"");await this.handleResponseRedirectOrUpdate(o,p,r,c)}catch(e){}}handleAfterRequest(e,t){if(!this.isJsonLike(e))return;const n=this.parseJson(e),s=t?this.parseJson(t):null,i=n.targets;Array.isArray(i)&&i.forEach((e=>{const{id:t,...n}=e,i=document.querySelector(t);let a={};if(s){for(const t in n)if(n.hasOwnProperty(t))switch(t){case"innerHTML":case"outerHTML":case"textContent":case"innerText":"response"===n[t]&&(a[t]=e.responseKey?s[e.responseKey]:s.response);break;default:a[t]=n[t];break}}else a=n;i&&this.updateElementAttributes(i,a)}))}async handleResponseRedirectOrUpdate(e,t,n,s){const i=this.getUpdatedHTMLContent(e,n,s),a=(new DOMParser).parseFromString(t,"text/html");i&&a.body.insertAdjacentElement("afterbegin",i),this.updateBodyContent(a.body.outerHTML)}getUpdatedHTMLContent(e,t,n){const s=document.createElement("div");if(s.id="afterbegin-8D95D",n&&t?.success){const t=e.replace(n,"");s.innerHTML=t}else s.innerHTML=e;return s.innerHTML?s:null}async updateBodyContent(e){const t=this.saveScrollPositions();this.saveState();const n=(new DOMParser).parseFromString(e,"text/html");document.removeAllEventListeners("PPBodyLoaded"),await this.appendCallbackResponse(n),this.restoreState(),this.restoreScrollPositions(t),this.attachWireFunctionEvents(),document.dispatchEvent(new Event("PPBodyLoaded"))}restoreState(){if(this.state.focusId){const e=document.getElementById(this.state.focusId)||document.querySelector(`[name="${this.state.focusId}"]`);if(e instanceof HTMLInputElement){const t=e.value.length||0;void 0!==this.state.focusSelectionStart&&null!==this.state.focusSelectionEnd&&e.setSelectionRange(t,t),this.state.focusValue&&("checkbox"===e.type||"radio"===e.type?e.checked=!!this.state.focusChecked:"number"===e.type||"email"===e.type?(e.type="text",e.setSelectionRange(t,t),e.type="number"===e.type?"number":"email"):"date"===e.type||"month"===e.type||"week"===e.type||"time"===e.type||"datetime-local"===e.type||"color"===e.type||"file"===e.type||""!==e.value&&(e.value=this.state.focusValue)),e.focus()}else if(e instanceof HTMLTextAreaElement){const t=e.value.length||0;void 0!==this.state.focusSelectionStart&&null!==this.state.focusSelectionEnd&&e.setSelectionRange(t,t),this.state.focusValue&&""!==e.value&&(e.value=this.state.focusValue),e.focus()}else e instanceof HTMLSelectElement&&(this.state.focusValue&&""!==e.value&&(e.value=this.state.focusValue),e.focus())}this.state.checkedElements.forEach((e=>{const t=document.getElementById(e);t&&(t.checked=!0)}))}async appendCallbackResponse(e){const t=e.getElementById("afterbegin-8D95D");if(t){const e=document.getElementById("afterbegin-8D95D");e?e.innerHTML=t.innerHTML:document.body.insertAdjacentHTML("afterbegin",t.outerHTML)}await this.populateDocumentBody(e)}saveState(){const e=document.activeElement;this.state.focusId=e?.id||e?.name,this.state.focusValue=e?.value,this.state.focusChecked=e?.checked,this.state.focusType=e?.type,this.state.focusSelectionStart=e?.selectionStart,this.state.focusSelectionEnd=e?.selectionEnd,this.state.isSuspense=e.hasAttribute("pp-suspense"),this.state.checkedElements.clear(),document.querySelectorAll('input[type="checkbox"]:checked').forEach((e=>{this.state.checkedElements.add(e.id||e.name)})),document.querySelectorAll('input[type="radio"]:checked').forEach((e=>{this.state.checkedElements.add(e.id||e.name)}))}updateElementAttributes(e,t){for(const n in t)if(t.hasOwnProperty(n))switch(n){case"innerHTML":case"outerHTML":case"textContent":case"innerText":e[n]=this.decodeHTML(t[n]);break;case"insertAdjacentHTML":e.insertAdjacentHTML(t.position||"beforeend",this.decodeHTML(t[n].html));break;case"insertAdjacentText":e.insertAdjacentText(t.position||"beforeend",this.decodeHTML(t[n].text));break;case"setAttribute":e.setAttribute(t.attrName,this.decodeHTML(t[n]));break;case"removeAttribute":e.removeAttribute(t[n]);break;case"className":e.className=this.decodeHTML(t[n]);break;case"classList.add":e.classList.add(...this.decodeHTML(t[n]).split(","));break;case"classList.remove":e.classList.remove(...this.decodeHTML(t[n]).split(","));break;case"classList.toggle":e.classList.toggle(this.decodeHTML(t[n]));break;case"classList.replace":const[s,i]=this.decodeHTML(t[n]).split(",");e.classList.replace(s,i);break;case"dataset":e.dataset[t.attrName]=this.decodeHTML(t[n]);break;case"style":Object.assign(e.style,t[n]);break;case"value":e.value=this.decodeHTML(t[n]);break;case"checked":e.checked=t[n];break;default:e.setAttribute(n,this.decodeHTML(t[n]))}}decodeHTML(e){const t=document.createElement("textarea");return t.innerHTML=e,t.value}appendAfterbegin(e){if(!e)return;const t="afterbegin-8D95D";let n=document.getElementById(t);n?(n.innerHTML=e,document.body.insertAdjacentElement("afterbegin",n)):(n=document.createElement("div"),n.id=t,n.innerHTML=e,document.body.insertAdjacentElement("afterbegin",n))}restoreSuspenseElement(e){const t=e.getAttribute("pp-original-state");if(e.hasAttribute("pp-suspense")&&t){const n=(e,t)=>{for(const n in t)t.hasOwnProperty(n)&&("textContent"===n?e.textContent=t[n]:"innerHTML"===n?e.innerHTML=t[n]:"disabled"===n?!0===t[n]?e.setAttribute("disabled","true"):e.removeAttribute("disabled"):e.setAttribute(n,t[n]));for(const n of Array.from(e.attributes))t.hasOwnProperty(n.name)||e.removeAttribute(n.name)},s=(e,t)=>{for(const s in t)if(t.hasOwnProperty(s))for(const t of Array.from(e.elements))if(t instanceof HTMLInputElement||t instanceof HTMLButtonElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement){const e=t.getAttribute("pp-original-state")||"";if(e){if(this.isJsonLike(e)){const s=this.parseJson(e);n(t,s)}else i(t,e);t.removeAttribute("pp-original-state")}}},i=(e,t)=>{e instanceof HTMLInputElement?e.value=t:e.textContent=t},a=(e,t)=>{e instanceof HTMLFormElement?s(e,t):n(e,t)};try{const i=this.parseJson(t);if(i)if(e instanceof HTMLFormElement){const t=e.id;if(t){const e=document.querySelector(`[form="${t}"]`);if(e){const t=e.getAttribute("pp-original-state");if(t&&this.isJsonLike(t)){const s=this.parseJson(t);n(e,s)}}}const i=new FormData(e),a={};if(i.forEach(((e,t)=>{a[t]=e})),s(e,a),e.hasAttribute("pp-suspense")){const t=e.getAttribute("pp-suspense")||"";if(this.parseJson(t).disabled)for(const t of Array.from(e.elements))(t instanceof HTMLInputElement||t instanceof HTMLButtonElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement)&&t.removeAttribute("disabled")}}else if(i.targets){i.targets.forEach((e=>{const{id:t,...n}=e,s=document.querySelector(t);s&&a(s,n)}));const{targets:t,...s}=i;n(e,s)}else{const{empty:t,...s}=i;n(e,s)}}catch(e){}}e.querySelectorAll("[pp-suspense]").forEach((e=>this.restoreSuspenseElement(e))),e.removeAttribute("pp-original-state")}extractJson(e){const t=e?.match(/\{[\s\S]*\}/);return t?t[0]:null}getRedirectUrl(e){const t=e.match(this.redirectRegex);return t?t[1]:null}async fetchFileWithData(e,t,n,s={}){const i=new FormData,a=n.files;if(a)for(let e=0;e<a.length;e++)i.append("file[]",a[e]);i.append("callback",t);for(const e in s)s.hasOwnProperty(e)&&i.append(e,s[e]);const o=await fetch(e,{method:"POST",headers:{HTTP_PPHP_WIRE_REQUEST:"true"},body:i});return await o.text()}async handleSuspenseElement(e){let t=e.getAttribute("pp-suspense")||"";const n=(e,t)=>{for(const n in t)if(t.hasOwnProperty(n))for(const t of e.elements)if(t instanceof HTMLInputElement||t instanceof HTMLButtonElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement){const e=t.getAttribute("pp-suspense")||"";if(e)if(this.isJsonLike(e)){const n=this.parseJson(e);"disabled"!==n.onsubmit&&this.updateElementAttributes(t,n),n.targets&&n.targets.forEach((e=>{const{id:t,...n}=e,s=document.querySelector(t);s&&i(s,n)}))}else s(t,e)}},s=(e,t)=>{e instanceof HTMLInputElement?e.value=t:e.textContent=t},i=(e,t)=>{e instanceof HTMLFormElement?n(e,t):this.updateElementAttributes(e,t)};try{if(t&&this.isJsonLike(t)){const s=this.parseJson(t);if(s)if(e instanceof HTMLFormElement){const t=new FormData(e),i={};t.forEach(((e,t)=>{i[t]=e})),s.disabled&&this.toggleFormElements(e,!0);const{disabled:a,...o}=s;this.updateElementAttributes(e,o),n(e,i)}else if(s.targets){s.targets.forEach((e=>{const{id:t,...n}=e,s=document.querySelector(t);s&&i(s,n)}));const{targets:t,...n}=s;this.updateElementAttributes(e,n)}else{if("disabled"===s.empty&&""===e.value)return;const{empty:t,...n}=s;this.updateElementAttributes(e,n)}}else if(t)s(e,t);else if(e instanceof HTMLFormElement){const t=new FormData(e),s={};t.forEach(((e,t)=>{s[t]=e})),n(e,s)}}catch(e){}}toggleFormElements(e,t){Array.from(e.elements).forEach((e=>{(e instanceof HTMLInputElement||e instanceof HTMLButtonElement||e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement)&&(e.disabled=t)}))}saveElementOriginalState(e){if(e.hasAttribute("pp-suspense")&&!e.hasAttribute("pp-original-state")){const t={};e.textContent&&(t.textContent=e.textContent.trim()),e.innerHTML&&(t.innerHTML=e.innerHTML.trim()),(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement)&&(t.value=e.value);for(let n=0;n<e.attributes.length;n++){const s=e.attributes[n];t[s.name]=s.value}e.setAttribute("pp-original-state",JSON.stringify(t))}if(e instanceof HTMLFormElement){let t=null;const n=e.id;n&&(t=document.querySelector(`[form="${n}"]`)),n&&t||(t=Array.from(e.elements).find((e=>e instanceof HTMLButtonElement||e instanceof HTMLInputElement))),t&&(t.hasAttribute("pp-original-state")||this.saveElementOriginalState(t))}e.querySelectorAll("[pp-suspense]").forEach((e=>this.saveElementOriginalState(e)))}getUrlParams(){const e={};return new URLSearchParams(window.location.search).forEach(((t,n)=>{e[n]=t})),e}createFetchOptions(e){return{method:"POST",headers:{"Content-Type":"application/json",HTTP_PPHP_WIRE_REQUEST:"true"},body:JSON.stringify(e)}}parseCallback(e,t){let n={};const s=e.closest("form");if(s){new FormData(s).forEach(((e,t)=>{n[t]?Array.isArray(n[t])?n[t].push(e):n[t]=[n[t],e]:n[t]=e}))}else e instanceof HTMLInputElement?n=this.handleInputElement(e):(e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement)&&(n[e.name]=e.value);const i=t.match(/(\w+)\((.*)\)/);if(i){const e=i[1];let t=i[2].trim();if(t.startsWith("{")&&t.endsWith("}"))try{const e=this.parseJson(t);"object"==typeof e&&null!==e&&(n={...n,...e})}catch(e){}else{const e=t.split(/,(?=(?:[^'"]*['"][^'"]*['"])*[^'"]*$)/).map((e=>e.trim().replace(/^['"]|['"]$/g,"")));n.args=e}return{funcName:e,data:n}}return{funcName:t,data:n}}handleInputElement(e){let t={};if(e.name)if("checkbox"===e.type)t[e.name]={value:e.value,checked:e.checked};else if("radio"===e.type){const n=document.querySelector(`input[name="${e.name}"]:checked`);t[e.name]=n?n.value:null}else t[e.name]=e.value;else"checkbox"===e.type||"radio"===e.type?t.value=e.checked:t.value=e.value;return t}resolveContext(e){let t=window;for(let n=0;n<e.length-1;n++)if(t=t[e[n]],!t)throw new Error(`Cannot find object ${e[n]} in the context.`);return{context:t,methodName:e[e.length-1]}}setCursorPosition(e,t){if(t.start)e.setSelectionRange(0,0);else if(t.end){const t=e.value.length||0;e.setSelectionRange(t,t)}else if(t.length){const n=parseInt(t.length,10)||0;e.setSelectionRange(n,n)}}handleInputAppendParams(e,t){const n=e.getAttribute("pp-append-params"),s=e.getAttribute("pp-append-params-sync");if("true"===n){if("true"===s){const t=e.name||e.id;if(t){const n=new URL(window.location.href),s=new URLSearchParams(n.search);s.has(t)&&(e.value=s.get(t)||"")}}e.addEventListener(t,(e=>{const t=e.currentTarget,n=t.value.trim(),s=new URL(window.location.href),i=new URLSearchParams(s.search),a=t.name||t.id;if(a){n?i.set(a,n):i.delete(a);const e=i.toString()?`${s.pathname}?${i.toString()}`:s.pathname;history.replaceState(null,"",e)}}))}}handleHiddenAttribute(){const e=document.querySelectorAll("[pp-visibility]"),t=document.querySelectorAll("[pp-display]");e.forEach((e=>this.handleVisibilityElementAttribute(e,"pp-visibility",this.handleElementVisibility))),t.forEach((e=>this.handleVisibilityElementAttribute(e,"pp-display",this.handleElementDisplay)))}handleVisibilityElementAttribute(e,t,n){const s=e.getAttribute(t);if(s)if(this.isJsonLike(s)){n(e,this.parseJson(s))}else{const n=this.parseTime(s);if(n>0){const s="pp-visibility"===t?"visibility":"display",i="visibility"===s?"hidden":"none";this.scheduleChange(e,n,s,i)}}}handleElementVisibility(e,t){this.handleElementChange(e,t,"visibility","hidden","visible")}handleElementDisplay(e,t){this.handleElementChange(e,t,"display","none","block")}handleElementChange(e,t,n,s,i){const a=t.start?this.parseTime(t.start):0,o=t.end?this.parseTime(t.end):0;a>0?(e.style[n]=s,this.scheduleChange(e,a,n,i),o>0&&this.scheduleChange(e,a+o,n,s)):o>0&&this.scheduleChange(e,o,n,s)}handleAnchorTag(e){e instanceof HTMLAnchorElement&&e.addEventListener("click",(async e=>{const t=e.currentTarget,n=t.getAttribute("href"),s=t.getAttribute("target");if(n&&"_blank"!==s&&!e.metaKey&&!e.ctrlKey&&(e.preventDefault(),!this.isNavigating)){this.isNavigating=!0;try{if(/^(https?:)?\/\//i.test(n)&&!n.startsWith(window.location.origin))window.location.href=n;else{const e=t.getAttribute("pp-append-params");if(n.startsWith("?")&&"true"===e){const e=new URL(window.location.href),t=new URLSearchParams(e.search);let s="";const[i,a]=n.split("#");a&&(s=`#${a}`);new URLSearchParams(i.split("?")[1]).forEach(((e,n)=>{t.set(n,e)}));const o=`${e.pathname}?${t.toString()}${s}`;history.pushState(null,"",o)}else{const[e,t]=n.split("#"),s=`${e}${t?`#${t}`:""}`;history.pushState(null,"",s)}const s=n.indexOf("#");if(-1!==s){const e=n.slice(s+1),t=document.getElementById(e);if(t)t.scrollIntoView({behavior:"smooth"});else{await this.handleNavigation();const t=document.getElementById(e);t&&t.scrollIntoView({behavior:"smooth"})}}else await this.handleNavigation()}}catch(e){}finally{this.isNavigating=!1}}}))}async handleNavigation(){try{const e=document.getElementById("loading-file-1B87E");if(e){const t=this.findLoadingElement(e,window.location.pathname);t&&await this.updateContentWithTransition(t)}const t=await this.fetch(window.location.href);if(!t.ok)return;const n=await t.text(),s=n.match(this.redirectRegex);if(s&&s[1])return void await this.handleRedirect(s[1]);await this.updateDocumentContent(n)}catch(e){}}findLoadingElement(e,t){let n=t;for(;;){const t=e.querySelector(`div[pp-loading-url='${n}']`);if(t)return t;if("/"===n)break;const s=n.lastIndexOf("/");n=s<=0?"/":n.substring(0,s)}return e.querySelector("div[pp-loading-url='/' ]")}async updateContentWithTransition(e){const t=document.querySelector("[pp-loading-content='true']")||document.body;if(!t)return;const{fadeIn:n,fadeOut:s}=this.parseTransition(e);await this.fadeOut(t,s),t.innerHTML=e.innerHTML,this.fadeIn(t,n)}parseTransition(e){let t=250,n=250;const s=e.querySelector("[pp-loading-transition]")?.getAttribute("pp-loading-transition");if(s)try{const e=this.parseJson(s);t=this.parseTime(e.fadeIn??t),n=this.parseTime(e.fadeOut??n)}catch(e){}return{fadeIn:t,fadeOut:n}}fadeOut(e,t){return new Promise((n=>{e.style.transition=`opacity ${t}ms ease-out`,e.style.opacity="0",setTimeout((()=>{e.style.transition="",n()}),t)}))}fadeIn(e,t){e.style.transition=`opacity ${t}ms ease-in`,e.style.opacity="1",setTimeout((()=>{e.style.transition=""}),t)}async updateDocumentContent(e){const t=this.saveScrollPositions(),n=(new DOMParser).parseFromString(e,"text/html"),s="pp-dynamic-script",i="pp-dynamic-link";document.head.querySelectorAll("[pp-dynamic-meta]").forEach((e=>e.remove()));document.head.querySelectorAll("[pp-dynamic-link]").forEach((e=>e.remove()));document.head.querySelectorAll("[pp-dynamic-script]").forEach((e=>e.remove()));document.removeAllEventListeners("PPBodyLoaded"),await(async e=>{Array.from(e.head.children).forEach((e=>{const t=e.tagName;if("SCRIPT"===t&&e.hasAttribute(s)){const t=document.createElement("script");Array.from(e.attributes).forEach((e=>t.setAttribute(e.name,e.value))),e.textContent&&(t.textContent=e.textContent),document.head.appendChild(t)}else if("META"===t){if(e.getAttribute("charset")||"viewport"===e.getAttribute("name"))return;const t=e.name,n=e.getAttribute("property"),s=document.head.querySelector(t?`meta[name="${t}"]`:`meta[property="${n}"]`),i=document.head.querySelector("title");s?document.head.replaceChild(e.cloneNode(!0),s):i?.nextSibling?document.head.insertBefore(e.cloneNode(!0),i.nextSibling):document.head.appendChild(e.cloneNode(!0))}else if("TITLE"===t){const t=document.head.querySelector("title");t?document.head.replaceChild(e.cloneNode(!0),t):document.head.appendChild(e.cloneNode(!0))}else if("LINK"===t){const t=t=>{const n=document.head.querySelector('link[rel="icon"]');if(n)document.head.replaceChild(e.cloneNode(!0),n);else{const e=document.createElement("link");e.rel="icon",e.href=t,document.head.appendChild(e)}};if("icon"===e.getAttribute("rel")){t(e.href)}else if(e.hasAttribute(i)){const t=e.cloneNode(!0);document.head.appendChild(t)}}})),await this.populateDocumentBody(e)})(n),this.restoreScrollPositions(t),this.attachWireFunctionEvents(),document.dispatchEvent(new Event("PPBodyLoaded"))}restoreScrollPositions(e){requestAnimationFrame((()=>{const t=e.window;t&&window.scrollTo(t.scrollLeft,t.scrollTop),document.querySelectorAll("*").forEach((t=>{const n=this.getElementKey(t);e[n]&&(t.scrollTop=e[n].scrollTop,t.scrollLeft=e[n].scrollLeft)}))}))}async populateDocumentBody(e){try{const t=e.body.cloneNode(!0);this.manageScriptTags(t),document.body.replaceWith(t)}catch(e){}}manageScriptTags(e,t){const n=e.querySelectorAll("script"),s=t?.querySelectorAll("script")||n;n.forEach(((e,t)=>{const n=document.createElement("script"),i=s[t]||e;Array.from(i.attributes).forEach((e=>{n.setAttribute(e.name,e.value)})),i.hasAttribute("src")||(n.textContent=i.textContent),e.parentNode?.replaceChild(n,e)}))}saveScrollPositions(){const e={window:{scrollTop:window.scrollY||document.documentElement.scrollTop,scrollLeft:window.scrollX||document.documentElement.scrollLeft}};return document.querySelectorAll("*").forEach((t=>{(t.scrollTop||t.scrollLeft)&&(e[this.getElementKey(t)]={scrollTop:t.scrollTop,scrollLeft:t.scrollLeft})})),e}getElementKey(e){return e.id||e.className||e.tagName}async handleRedirect(e){if(e)try{const t=new URL(e,window.location.origin);t.origin!==window.location.origin?window.location.href=e:(history.pushState(null,"",e),await this.handleNavigation())}catch(e){}}async fetch(e,t){return fetch(e,{...t,headers:{...t?.headers,"X-Requested-With":"XMLHttpRequest"}})}isJsonLike(e){return"string"==typeof e&&((e=e.trim()).startsWith("{")&&e.endsWith("}")||e.startsWith("[")&&e.endsWith("]"))}parseJson(e){try{return JSON5.parse(e)}catch(e){return null}}parseTime(e){if("number"==typeof e)return e;const t=e.match(/^(\d+)(ms|s|m)?$/);if(t){const e=parseInt(t[1],10);switch(t[2]||"ms"){case"ms":return e;case"s":return 1e3*e;case"m":return 60*e*1e3;default:return e}}return 0}scheduleChange(e,t,n,s){setTimeout((()=>{requestAnimationFrame((()=>{e.style[n]=s}))}),t)}observeDOMChanges(){new MutationObserver((e=>{for(const t of e)"childList"===t.type&&t.addedNodes.length>0&&this.attachWireFunctionEvents()})).observe(document.body,{childList:!0,subtree:!0})}async fetchFunction(e,t={}){try{const n={callback:e,...t},s=this.createFetchOptions(n),i=await this.fetch(window.location.href,s);if(!i.ok)throw new Error(`Fetch failed with status: ${i.status} ${i.statusText}`);const a=await i.text();try{return JSON.parse(a)}catch{return a}}catch(e){throw new Error("Failed to fetch data.")}}async sync(...e){try{const t=e.length>0?e.map((e=>`pp-sync="${e}"`)):['pp-sync="true"'],n=this.createFetchOptions({secondRequestC69CD:!0,...this.getUrlParams()}),s=await this.fetch(window.location.href,n),i=await s.text(),a=(new DOMParser).parseFromString(i,"text/html");t.forEach((e=>{const t=document.querySelectorAll(`[${e}]`),n=a.body.querySelectorAll(`[${e}]`);t.forEach(((e,t)=>{const s=n[t];s&&(e.innerHTML=s.innerHTML,this.reRunScripts(e))}))})),this.attachWireFunctionEvents()}catch(e){}}async fetchAndUpdateBodyContent(){const e=this.createFetchOptions({secondRequestC69CD:!0,...this.getUrlParams()}),t=await this.fetch(window.location.href,e),n=await t.text();await this.updateBodyContent(n)}reRunScripts(e){e.querySelectorAll("script").forEach((e=>{const t=document.createElement("script");Array.from(e.attributes).forEach((e=>{t.setAttribute(e.name,e.value)})),e.hasAttribute("src")||(t.textContent=e.textContent),e.parentNode?.replaceChild(t,e)}))}copyCode(e,t,n,s,i="img",a=2e3){if(!(e instanceof HTMLElement))return;const o=e.closest(`.${t}`)?.querySelector("pre code"),r=o?.textContent?.trim()||"";r?navigator.clipboard.writeText(r).then((()=>{const t=e.querySelector(i);if(t)for(const[e,n]of Object.entries(s))e in t?t[e]=n:t.setAttribute(e,n);setTimeout((()=>{if(t)for(const[e,s]of Object.entries(n))e in t?t[e]=s:t.setAttribute(e,s)}),a)}),(()=>{alert("Failed to copy command to clipboard")})):alert("Failed to find the code block to copy")}getCookie(e){return document.cookie.split("; ").find((t=>t.startsWith(e+"=")))?.split("=")[1]||null}}class PPHPLocalStore{static instance=null;state;listeners;pphp;STORAGE_KEY;constructor(e={}){this.state=e,this.listeners=[],this.pphp=PPHP.instance,this.STORAGE_KEY=this.pphp.getCookie("pphp_local_store_key")||"pphp_local_store_59e13",this.loadState()}static getInstance(e={}){return PPHPLocalStore.instance||(PPHPLocalStore.instance=new PPHPLocalStore(e)),PPHPLocalStore.instance}setState(e,t=!1){if(this.state={...this.state,...e},this.listeners.forEach((e=>e(this.state))),this.saveState(),t){const e=localStorage.getItem(this.STORAGE_KEY);e&&this.pphp.fetchFunction(this.STORAGE_KEY,{[this.STORAGE_KEY]:e})}}saveState(){localStorage.setItem(this.STORAGE_KEY,JSON.stringify(this.state))}loadState(){const e=localStorage.getItem(this.STORAGE_KEY);e&&(this.state=this.pphp.parseJson(e),this.listeners.forEach((e=>e(this.state))))}resetState(e,t=!1){if(e?(delete this.state[e],this.saveState()):(this.state={},localStorage.removeItem(this.STORAGE_KEY)),this.listeners.forEach((e=>e(this.state))),t){const t=e?localStorage.getItem(this.STORAGE_KEY):null;this.pphp.fetchFunction(this.STORAGE_KEY,{[this.STORAGE_KEY]:t})}}}
|
|
2
2
|
// Initialize the PPHP instance
|
|
3
3
|
document.addEventListener("PPBodyLoaded", () => {
|
|
4
4
|
PPHP.instance.attachWireFunctionEvents();
|
|
@@ -9,4 +9,4 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
|
9
9
|
document.dispatchEvent(new Event("PPBodyLoaded"));
|
|
10
10
|
});
|
|
11
11
|
var pphp = PPHP.instance;
|
|
12
|
-
var store = PPHPLocalStore.getInstance();
|
|
12
|
+
var store = PPHPLocalStore.getInstance();
|