-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsweetalert2.php
469 lines (433 loc) · 12 KB
/
sweetalert2.php
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
/**
* Tested With SweetAlert2 : 10.12.2
*
* @author Varun Sridharan <varunsridharan23@gmail.com>
* @link https://github.com/varunsridharan/sweetalert2-php
* @license MIT
*/
if ( ! class_exists( 'SweetAlert2' ) ) {
/**
* Class SweetAlert2
*
* @author Varun Sridharan <varunsridharan23@gmail.com>
* @since 1.0
*
* @method \SweetAlert2 title( $title = null )
* @method \SweetAlert2 titleText( $titleText = null )
* @method \SweetAlert2 html( $html = null )
* @method \SweetAlert2 text( $text = null )
* @method \SweetAlert2 type( $type = null )
* @method \SweetAlert2 icon( $icon = null )
* @method \SweetAlert2 iconHtml( $iconHtml = null )
* @method \SweetAlert2 iconColor( $iconColor = null )
* @method \SweetAlert2 footer( $footer = null )
* @method \SweetAlert2 backdrop( $backdrop = true )
* @method \SweetAlert2 toast( $toast = false )
* @method \SweetAlert2 target( $target = 'body' )
* @method \SweetAlert2 input( $input = null )
* @method \SweetAlert2 width( $width = null )
* @method \SweetAlert2 padding( $padding = null )
* @method \SweetAlert2 background( $background = null )
* @method \SweetAlert2 position( $position = 'center' )
* @method \SweetAlert2 grow( $grow = false )
* @method \SweetAlert2 customClass( $customClass = array() )
* @method \SweetAlert2 timer( $timer = null )
* @method \SweetAlert2 timerProgressBar( $timerProgressBar = null )
* @method \SweetAlert2 heightAuto( $heightAuto = true )
* @method \SweetAlert2 allowOutsideClick( $allowOutsideClick = true )
* @method \SweetAlert2 allowEscapeKey( $allowEscapeKey = true )
* @method \SweetAlert2 allowEnterKey( $allowEnterKey = true )
* @method \SweetAlert2 stopKeydownPropagation( $stopKeydownPropagation = true )
* @method \SweetAlert2 keydownListenerCapture( $keydownListenerCapture = true )
* @method \SweetAlert2 showConfirmButton( $showConfirmButton = true )
* @method \SweetAlert2 showCancelButton( $showCancelButton = false )
* @method \SweetAlert2 confirmButtonText( $confirmButtonText = 'OK' )
* @method \SweetAlert2 cancelButtonText( $cancelButtonText = 'Cancel' )
* @method \SweetAlert2 confirmButtonColor( $confirmButtonColor = null )
* @method \SweetAlert2 cancelButtonColor( $cancelButtonColor = null )
* @method \SweetAlert2 confirmButtonAriaLabel( $confirmButtonAriaLabel = '' )
* @method \SweetAlert2 cancelButtonAriaLabel( $cancelButtonAriaLabel = '' )
* @method \SweetAlert2 buttonsStyling( $buttonsStyling = true )
* @method \SweetAlert2 reverseButtons( $reverseButtons = false )
* @method \SweetAlert2 focusConfirm( $focusConfirm = true )
* @method \SweetAlert2 focusCancel( $focusCancel = false )
* @method \SweetAlert2 showCloseButton( $showCloseButton = false )
* @method \SweetAlert2 closeButtonHtml( $closeButtonHtml = false )
* @method \SweetAlert2 closeButtonAriaLabel( $closeButtonAriaLabel = 'Close this dialog' )
* @method \SweetAlert2 showLoaderOnConfirm( $showLoaderOnConfirm = false )
* @method \SweetAlert2 scrollbarPadding( $scrollbarPadding = true )
* @method \SweetAlert2 preConfirm( $preConfirm = null )
* @method \SweetAlert2 imageUrl( $imageUrl = null )
* @method \SweetAlert2 imageWidth( $imageWidth = null )
* @method \SweetAlert2 imageHeight( $imageHeight = null )
* @method \SweetAlert2 imageAlt( $imageAlt = '' )
* @method \SweetAlert2 inputPlaceholder( $inputPlaceholder = '' )
* @method \SweetAlert2 inputValue( $inputValue = '' )
* @method \SweetAlert2 inputOptions( $inputOptions = array() )
* @method \SweetAlert2 inputAutoTrim( $inputAutoTrim = true )
* @method \SweetAlert2 inputAttributes( $inputAttributes = array() )
* @method \SweetAlert2 inputValidator( $inputValidator = null )
* @method \SweetAlert2 inputLabel( $inputLabel = null )
* @method \SweetAlert2 returnInputValueOnDeny( $returnInputValueOnDeny = null )
* @method \SweetAlert2 preDeny( $preDeny = null )
* @method \SweetAlert2 validationMesage( $validationMesage = null )
* @method \SweetAlert2 progressSteps( $progressSteps = array() )
* @method \SweetAlert2 currentProgressStep( $currentProgressStep = null )
* @method \SweetAlert2 progressStepsDistance( $progressStepsDistance = '40px' )
* @method \SweetAlert2 willOpen( $willOpen = null )
* @method \SweetAlert2 didOpen( $didOpen = null )
* @method \SweetAlert2 didRender( $didRender = null )
* @method \SweetAlert2 willClose( $willClose = null )
* @method \SweetAlert2 didClose( $didClose = null )
* @method \SweetAlert2 didDestroy( $didDestroy = null )
* Custom Methods
* @method \SweetAlert2 warning( $warning = true )
* @method \SweetAlert2 success( $success = true )
* @method \SweetAlert2 error( $error = true )
* @method \SweetAlert2 question( $question = true )
* @method \SweetAlert2 info( $info = true )
*/
class SweetAlert2 implements \JsonSerializable {
/**
* Stores Thens.
*
* @var array
* @access
*/
private $then = array();
/**
* Stores Current Instance Config.
*
* @var array
* @access
*/
private $config = array();
/**
* before
*
* @var string
*/
protected $before = '';
/**
* after
*
* @var string
*/
protected $after = '';
/**
* Stores Encoded Values.
*
* @var bool
* @access
*/
private $encoded = false;
/**
* SweetAlert2 constructor.
*
* @param string $title
* @param string $text
* @param string $type
*/
public function __construct( $title = '', $text = '', $type = 'success' ) {
$this->data( 'title', $title );
$this->data( 'text', $text );
$this->data( 'icon', $type );
}
/**
* @param string $title
* @param string $text
* @param string $type
*
* @return \SweetAlert2
*/
public function swal( $title = '', $text = '', $type = 'success' ) {
return new self( $title, $text, $type );
}
/**
* @return mixed|string
*/
public function jsonSerialize() {
return $this->render();
}
/**
* @return mixed
*/
public function __toString() {
return $this->render();
}
/**
* Returns Encoded Values.
*
* @return false|string
*/
public function to_json() {
if ( false === $this->encoded ) {
$this->encoded = json_encode( $this->config );
}
return $this->encoded;
}
/**
* Stores Value.
*
* @param string $key
* @param mixed $value
*
* @return $this
*/
protected function data( $key = '', $value = '' ) {
$this->config[ $key ] = $value;
return $this;
}
/**
* Returns Javascript Variable Name.
*
* @return string
*/
protected function var_name() {
return 'swal2_' . md5( $this->to_json() . '-' . microtime() );
}
/**
* Renders Output.
*
* @return string
*/
public function render() {
$output = $this->before . ' var ' . $this->var_name() . ' = Swal.fire(' . $this->to_json() . ') ';
if ( ! empty( $this->then ) ) {
foreach ( $this->then as $data ) {
$data = ( $data instanceof SweetAlert2 ) ? $data->render() : $data;
$output .= '.then((result) => {' . $data . '})';
}
}
return $output . ';' . $this->after;
}
/**
* @param $name
* @param $arguments
*
* @return $this
*/
public function __call( $name, $arguments ) {
return $this->data( $name, $arguments[0] );
}
/**
* @param $name
*
* @return mixed
*/
public function __get( $name ) {
return ( isset( $this->config[ $name ] ) ) ? $this->config[ $name ] : null;
}
/**
* @param $then
*
* @return $this
*/
public function then( $then ) {
$this->then[] = $then;
return $this;
}
/**
* @param $before
*
* @return $this
*/
public function before( $before ) {
$this->before = $before;
return $this;
}
/**
* @param $after
*
* @return $this
*/
public function after( $after ) {
$this->after = $after;
return $this;
}
/**
* @param bool $url
* @param null $height
* @param null $width
* @param null $alt
*
* @return $this
*/
public function image( $url = false, $height = null, $width = null, $alt = null ) {
if ( false === $url && null === $height && null === $alt && null === $width ) {
$url = $this->title;
$height = $this->text;
$alt = $this->type;
$this->title( false );
$this->text( false );
$this->type( false );
}
$this->data( 'imageUrl', $url );
$this->data( 'imageHeight', $height );
$this->data( 'imageWidth', $width );
$this->data( 'imageAlt', $alt );
return $this;
}
/**
* @param bool $text
* @param null $color
* @param null $aria_label
*
* @return $this
*/
public function cancelButton( $text = false, $color = null, $aria_label = null ) {
$is_show = true;
if ( false === $text ) {
$text = null;
$color = null;
$aria_label = null;
$is_show = false;
}
$this->data( 'showCancelButton', $is_show );
$this->data( 'cancelButtonText', $text );
$this->data( 'cancelButtonColor', $color );
$this->data( 'cancelButtonAriaLabel', $aria_label );
return $this;
}
/**
* @param bool $text
* @param null $color
* @param null $aria_label
*
* @return $this
*/
public function confirmButton( $text = false, $color = null, $aria_label = null ) {
$is_show = true;
if ( false === $text ) {
$text = null;
$color = null;
$aria_label = null;
$is_show = false;
}
$this->data( 'confirmButtonText', $text );
$this->data( 'showConfirmButton', $is_show );
$this->data( 'confirmButtonColor', $color );
$this->data( 'confirmButtonAriaLabel', $aria_label );
return $this;
}
/**
* @param bool $animation
*
* @return $this
* @deprecated
*/
public function animation( $animation = true ) {
return $this->data( 'animation', $animation );
}
/**
* @param mixed $onBeforeOpen
*
* @return $this
* @deprecated
*/
public function onBeforeOpen( $onBeforeOpen = null ) {
return $this->data( 'onBeforeOpen', $onBeforeOpen );
}
/**
* @param mixed $onOpen
*
* @return $this
* @deprecated
*/
public function onOpen( $onOpen = null ) {
return $this->data( 'onOpen', $onOpen );
}
/**
* @param mixed $onOpen
*
* @return $this
* @deprecated
*/
public function onRender( $onRender = null ) {
return $this->data( 'onRender', $onRender );
}
/**
* @param mixed $onClose
*
* @return $this
* @deprecated
*/
public function onClose( $onClose = null ) {
return $this->data( 'onClose', $onClose );
}
/**
* @param mixed $onAfterClose
*
* @return $this
* @deprecated
*/
public function onAfterClose( $onAfterClose = null ) {
return $this->data( 'onAfterClose', $onAfterClose );
}
/**
* @param mixed $onDestroy
*
* @return $this
* @deprecated
*/
public function onDestroy( $onDestroy = null ) {
return $this->data( 'onDestroy', $onDestroy );
}
}
/**
* @param string $title
* @param string $content
* @param string $type
*
* @return \SweetAlert2
*/
function swal2( $title = '', $content = '', $type = 'success' ) {
return new \SweetAlert2( $title, $content, $type );
}
/**
* @param string $title
* @param string $content
*
* @return \SweetAlert2
*/
function swal2_success( $title = '', $content = '' ) {
return swal2( $title, $content, 'success' );
}
/**
* @param string $title
* @param string $content
*
* @return \SweetAlert2
*/
function swal2_info( $title = '', $content = '' ) {
return swal2( $title, $content, 'info' );
}
/**
* @param string $title
* @param string $content
*
* @return \SweetAlert2
*/
function swal2_question( $title = '', $content = '' ) {
return swal2( $title, $content, 'question' );
}
/**
* @param string $title
* @param string $content
*
* @return \SweetAlert2
*/
function swal2_warning( $title = '', $content = '' ) {
return swal2( $title, $content, 'warning' );
}
/**
* @param string $title
* @param string $content
*
* @return \SweetAlert2
*/
function swal2_error( $title = '', $content = '' ) {
return swal2( $title, $content, 'error' );
}
}