forked from realrashid/sweet-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToaster.php
630 lines (563 loc) · 16.5 KB
/
Toaster.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
<?php
namespace RealRashid\SweetAlert;
use RealRashid\SweetAlert\Storage\SessionStore;
class Toaster
{
/**
* Session storage.
*
* @var RealRashid\SweetAlert\Storage\Session
* @author Rashid Ali <[email protected]>
*/
protected $session;
/**
* Configuration options.
*
* @var array
* @author Rashid Ali <[email protected]>
*/
protected $config;
/**
* Setting up the session
*
* @param SessionStore $session
* @author Rashid Ali <[email protected]>
*/
public function __construct(SessionStore $session)
{
$this->setDefaultConfig();
$this->session = $session;
}
/**
* The default configuration for alert
*
* @return void
* @author Rashid Ali <[email protected]>
*/
protected function setDefaultConfig()
{
$this->config = [
'title' => '',
'text' => '',
'timer' => config('sweetalert.timer'),
'width' => config('sweetalert.width'),
'heightAuto' => config('sweetalert.height_auto'),
'padding' => config('sweetalert.padding'),
'showConfirmButton' => config('sweetalert.show_confirm_button'),
'showCloseButton' => config('sweetalert.show_close_button'),
'timerProgressBar' => config('sweetalert.timer_progress_bar'),
'customClass' => [
'container' => config('sweetalert.customClass.container'),
'popup' => config('sweetalert.customClass.popup'),
'header' => config('sweetalert.customClass.header'),
'title' => config('sweetalert.customClass.title'),
'closeButton' => config('sweetalert.customClass.closeButton'),
'icon' => config('sweetalert.customClass.icon'),
'image' => config('sweetalert.customClass.image'),
'content' => config('sweetalert.customClass.content'),
'input' => config('sweetalert.customClass.input'),
'actions' => config('sweetalert.customClass.actions'),
'confirmButton' => config('sweetalert.customClass.confirmButton'),
'cancelButton' => config('sweetalert.customClass.cancelButton'),
'footer' => config('sweetalert.customClass.footer')
]
];
}
/**
* The default configuration for middleware alert.
*
* @return $config
* @author Rashid Ali <[email protected]>
*/
public function middleware()
{
unset($this->config['position'], $this->config['heightAuto'], $this->config['width'], $this->config['padding'], $this->config['showCloseButton']);
if(!config('sweetalert.middleware.autoClose')){
$this->removeTimer();
}else{
unset($this->config['timer']);
$this->config['timer'] = config('sweetalert.middleware.timer');
}
$this->config['position'] = config('sweetalert.middleware.toast_position');
$this->config['showCloseButton'] = config('sweetalert.middleware.toast_close_button');
$this->flash();
return $this;
}
/**
* Flash an alert message.
*
* @param string $title
* @param string $text
* @param array $icon
* @return void
* @author Rashid Ali <[email protected]>
*/
public function alert($title = '', $text = '', $icon = null)
{
$this->config['title'] = $title;
$this->config['text'] = $text;
if (!is_null($icon)) {
$this->config['icon'] = $icon;
}
$this->flash();
return $this;
}
/**
* Display a success typed alert message with a text and a title.
*
* @param string $title
* @param string $text
* @author Rashid Ali <[email protected]>
*/
public function success($title = '', $text = '')
{
$this->alert($title, $text, 'success');
return $this;
}
/**
* Display a info typed alert message with a text and a title.
*
* @param string $title
* @param string $text
* @author Rashid Ali <[email protected]>
*/
public function info($title = '', $text = '')
{
$this->alert($title, $text, 'info');
return $this;
}
/**
* Display a warning typed alert message with a text and a title.
*
* @param string $title
* @param string $text
* @author Rashid Ali <[email protected]>
*/
public function warning($title = '', $text = '')
{
$this->alert($title, $text, 'warning');
return $this;
}
/**
* Display a question typed alert message with a text and a title.
*
* @param string $title
* @param string $text
* @author Rashid Ali <[email protected]>
*/
public function question($title = '', $text = '')
{
$this->alert($title, $text, 'question');
$this->showCancelButton();
return $this;
}
/**
* Display a error typed alert message with a text and a title.
*
* @param string $title
* @param string $text
* @author Rashid Ali <[email protected]>
*/
public function error($title = '', $text = '')
{
$this->alert($title, $text, 'error');
return $this;
}
/**
* Display a message with a custom image and CSS animation disabled.
*
* @param string $title
* @param string $text
* @param string $imageUrl
* @param integer $imageWidth
* @param integer $imageHeight
* @param string $imageAlt
* @author Rashid Ali <[email protected]>
*/
public function image($title, $text,$imageUrl, $imageWidth, $imageHeight, $imageAlt)
{
$this->config['title'] = $title;
$this->config['text'] = $text;
$this->config['imageUrl'] = $imageUrl;
$this->config['imageWidth'] = $imageWidth;
$this->config['imageHeight'] = $imageHeight;
if (!is_null($imageAlt)) {
$this->config['imageAlt'] = $imageAlt;
} else {
$this->config['imageAlt'] = $title;
}
$this->config['animation'] = false;
$this->flash();
return $this;
}
/**
* Display a html typed alert message with html code.
*
* @param string $title
* @param string $code
* @param string $icon
* @author Rashid Ali <[email protected]>
*/
public function html($title = '', $code = '', $icon = '')
{
$this->config['title'] = $title;
$this->config['html'] = $code;
if (!is_null($icon)) {
$this->config['icon'] = $icon;
}
$this->flash();
return $this;
}
/**
* Display a toast message
*
* @param string $title
* @param string $icon
* @author Rashid Ali <[email protected]>
*/
public function toast($title = '', $icon = '')
{
$this->config['toast'] = true;
$this->config['title'] = $title;
$this->config['icon'] = $icon;
$this->config['position'] = config('sweetalert.toast_position');
$this->config['showCloseButton'] = true;
$this->config['showConfirmButton'] = false;
unset($this->config['heightAuto']);
$this->flash();
return $this;
}
/**
* Convert any alert modal to Toast
*
* @param string $position
* @author Rashid Ali <[email protected]>
*/
public function toToast($position = '')
{
$this->config['toast'] = true;
$this->config['showCloseButton'] = true;
if (!empty($position)) {
$this->config['position'] = $position;
} else {
$this->config['position'] = config('sweetalert.toast_position');
}
$this->config['showConfirmButton'] = false;
unset($this->config['width'], $this->config['padding']);
$this->flash();
return $this;
}
/**
* Convert any alert modal to html
*
* @author Rashid Ali <[email protected]>
*/
public function toHtml()
{
$this->config['html'] = $this->config['text'];
unset($this->config['text']);
$this->flash();
return $this;
}
/**
* Add a custom image to alert
*
* @param string $imageUrl
* @author Rashid Ali <[email protected]>
*/
public function addImage($imageUrl)
{
$this->config['imageUrl'] = $imageUrl;
$this->config['showCloseButton'] = true;
unset($this->config['icon']);
$this->flash();
return $this;
}
/**
* Add footer section to alert()
*
* @param string $code
* @author Rashid Ali <[email protected]>
*/
public function footer($code)
{
$this->config['footer'] = $code;
$this->flash();
return $this;
}
/**
* positioned alert dialog
*
* @param string $position
* @author Rashid Ali <[email protected]>
*/
public function position($position = 'top-end')
{
$this->config['position'] = $position;
$this->flash();
return $this;
}
/**
* Modal window width
* including paddings
* (box-sizing: border-box).
* Can be in px or %. The default width is 32rem
*
* @param string $width
* @author Rashid Ali <[email protected]>
*/
public function width($width = '32rem')
{
$this->config['width'] = $width;
$this->flash();
return $this;
}
/**
* Modal window padding.
* The default padding is 1.25rem.
*
* @param string $padding
* @author Rashid Ali <[email protected]>
*/
public function padding($padding = '1.25rem')
{
$this->config['padding'] = $padding;
$this->flash();
return $this;
}
/**
* Modal window background
* (CSS background property).
* The default background is '#fff'.
*
* @param string $background
* @author Rashid Ali <[email protected]>
*/
public function background($background = '#fff')
{
$this->config['background'] = $background;
$this->flash();
return $this;
}
/**
* Set to false if you want to
* focus the first element in tab
* order instead of "Confirm"-button by default.
*
* @param boolean $focus
* @author Rashid Ali <[email protected]>
*/
public function focusConfirm($focus = true)
{
$this->config['focusConfirm'] = $focus;
unset($this->config['focusCancel']);
$this->flash();
return $this;
}
/**
* Set to true if you want to focus the
* "Cancel"-button by default.
*
* @param boolean $focus
* @author Rashid Ali <[email protected]>
*/
public function focusCancel($focus = false)
{
$this->config['focusCancel'] = $focus;
unset($this->config['focusConfirm']);
$this->flash();
return $this;
}
/**
* Custom animation with [Animate.css](https://daneden.github.io/animate.css/)
* CSS classes for animations when showing a popup (fade in):
* CSS classes for animations when hiding a popup (fade out):
*
* @param string $showAnimation
* @param string $hideAnimation
* @author Rashid Ali <[email protected]>
*/
public function animation($showAnimation, $hideAnimation)
{
if (!config('sweetalert.animation.enable')) {
config(['sweetalert.animation.enable' => true]);
}
$this->config['showClass'] = ['popup' => "animate__animated {$showAnimation}"];
$this->config['hideClass'] = ['popup' => "animate__animated {$hideAnimation}"];
$this->flash();
return $this;
}
/**
* Persistent the alert modal
*
* @param boolean $showConfirmBtn
* @param boolean $showCloseBtn
* @author Rashid Ali <[email protected]>
*/
public function persistent($showConfirmBtn = true, $showCloseBtn = false)
{
$this->config['allowEscapeKey'] = false;
$this->config['allowOutsideClick'] = false;
$this->removeTimer();
if ($showConfirmBtn) {
$this->showConfirmButton();
}
if ($showCloseBtn) {
$this->showCloseButton();
}
$this->flash();
return $this;
}
/**
* auto close alert modal after
* specifid time
*
* @param integer $milliseconds
* @author Rashid Ali <[email protected]>
*/
public function autoClose($milliseconds = 5000)
{
$this->config['timer'] = $milliseconds;
$this->flash();
return $this;
}
/**
* Display confirm button
*
* @param string $btnText
* @param string $btnColor
* @author Rashid Ali <[email protected]>
*/
public function showConfirmButton($btnText = 'Ok', $btnColor = '#3085d6')
{
$this->config['showConfirmButton'] = true;
$this->config['confirmButtonText'] = $btnText;
$this->config['confirmButtonColor'] = $btnColor;
$this->config['allowOutsideClick'] = false;
$this->removeTimer();
$this->flash();
return $this;
}
/**
* Display cancel button
*
* @param string $btnText
* @param string $btnColor
* @author Rashid Ali <[email protected]>
*/
public function showCancelButton($btnText = 'Cancel', $btnColor = '#aaa')
{
$this->config['showCancelButton'] = true;
$this->config['cancelButtonText'] = $btnText;
$this->config['cancelButtonColor'] = $btnColor;
$this->removeTimer();
$this->flash();
return $this;
}
/**
* Display close button
*
* @param string $closeButtonAriaLabel
* @author Rashid Ali <[email protected]>
*/
public function showCloseButton($closeButtonAriaLabel = 'aria-label')
{
$this->config['showCloseButton'] = true;
$this->config['closeButtonAriaLabel'] = $closeButtonAriaLabel;
$this->flash();
return $this;
}
/**
* Hide close button from alert or toast
*
* @author Rashid Ali <[email protected]>
*/
public function hideCloseButton()
{
$this->config['showCloseButton'] = false;
$this->flash();
return $this;
}
/**
* Apply default styling to buttons.
* If you want to use your own classes (e.g. Bootstrap classes)
* set this parameter to false.
*
* @param boolean $buttonsStyling
* @author Rashid Ali <[email protected]>
*/
public function buttonsStyling($buttonsStyling)
{
$this->config['buttonsStyling'] = $buttonsStyling;
$this->flash();
return $this;
}
/**
* Use any HTML inside icons (e.g. Font Awesome)
*
* @param string $iconHtml
* @author Rashid Ali <[email protected]>
*/
public function iconHtml($iconHtml)
{
$this->config['iconHtml'] = $iconHtml;
$this->flash();
return $this;
}
/**
* If set to true, the timer will have a progress bar at the bottom of a popup.
* Mostly, this feature is useful with toasts.
*
* @author Rashid Ali <[email protected]>
*/
public function timerProgressBar()
{
$this->config['timerProgressBar'] = true;
$this->flash();
return $this;
}
/**
* Reverse buttons position
*
* @author Faber44 <https://github.com/Faber44>
*/
public function reverseButtons()
{
$this->config['reverseButtons'] = true;
$this->flash();
return $this;
}
/**
* Remove the timer from config option.
*
* @author Rashid Ali <[email protected]>
*/
protected function removeTimer()
{
if (array_key_exists('timer', $this->config)) {
unset($this->config['timer']);
}
}
/**
* Flash the config options for alert.
*
* @author Rashid Ali <[email protected]>
*/
public function flash()
{
foreach ($this->config as $key => $value) {
$this->session->flash("alert.config.{$key}", $value);
}
$this->session->flash('alert.config', $this->buildConfig());
}
/**
* Build Flash config options for flashing.
*
* @author Rashid Ali <[email protected]>
*/
public function buildConfig()
{
$config = $this->config;
return json_encode($config);
}
}