1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* DialogLibraryInterface.php - Adapter for the Toastr library. |
5
|
|
|
* |
6
|
|
|
* @package jaxon-dialogs |
7
|
|
|
* @author Thierry Feuzeu <[email protected]> |
8
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
10
|
|
|
* @link https://github.com/jaxon-php/jaxon-dialogs |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Jaxon\Dialogs\Toastr; |
14
|
|
|
|
15
|
|
|
use Jaxon\App\Dialog\Library\DialogLibraryTrait; |
16
|
|
|
use Jaxon\App\Dialog\LibraryInterface; |
17
|
|
|
use Jaxon\App\Dialog\MessageInterface; |
18
|
|
|
|
19
|
|
|
class ToastrLibrary implements LibraryInterface, MessageInterface |
20
|
|
|
{ |
21
|
|
|
use DialogLibraryTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @const The library name |
25
|
|
|
*/ |
26
|
|
|
const NAME = 'toastr'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritDoc |
30
|
|
|
*/ |
31
|
|
|
public function getName(): string |
32
|
|
|
{ |
33
|
|
|
return self::NAME; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
|
public function getSubdir(): string |
40
|
|
|
{ |
41
|
|
|
return 'toastr.js'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function getVersion(): string |
48
|
|
|
{ |
49
|
|
|
return '2.1.3'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritDoc |
54
|
|
|
*/ |
55
|
|
|
public function getJs(): string |
56
|
|
|
{ |
57
|
|
|
return $this->helper()->getJsCode('toastr.min.js'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
public function getCss(): string |
64
|
|
|
{ |
65
|
|
|
return $this->helper()->getCssCode('toastr.min.css'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
|
|
public function getScript(): string |
72
|
|
|
{ |
73
|
|
|
return $this->helper()->render('toastr/alert.js'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
|
|
public function getReadyScript(): string |
80
|
|
|
{ |
81
|
|
|
return $this->helper()->render('toastr/ready.js.php', [ |
82
|
|
|
'options' => $this->helper()->getOptionScript('toastr.options.', 'options.') |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Print an alert message. |
88
|
|
|
* |
89
|
|
|
* @param string $sMessage The text of the message |
90
|
|
|
* @param string $sTitle The title of the message |
91
|
|
|
* @param string $sType The type of the message |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function alert(string $sMessage, string $sTitle, string $sType): string |
96
|
|
|
{ |
97
|
|
|
if($this->returnCode()) |
98
|
|
|
{ |
99
|
|
|
return empty($sTitle) ? "toastr.$sType($sMessage)" : "toastr.$sType($sMessage, '$sTitle')"; |
100
|
|
|
} |
101
|
|
|
$aOptions = ['message' => $sMessage, 'title' => $sTitle]; |
102
|
|
|
// Show the alert |
103
|
|
|
$this->addCommand(['cmd' => 'toastr.' . $sType], $aOptions); |
104
|
|
|
return ''; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritDoc |
109
|
|
|
*/ |
110
|
|
|
public function success(string $sMessage, string $sTitle = ''): string |
111
|
|
|
{ |
112
|
|
|
return $this->alert($sMessage, $sTitle, 'success'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritDoc |
117
|
|
|
*/ |
118
|
|
|
public function info(string $sMessage, string $sTitle = ''): string |
119
|
|
|
{ |
120
|
|
|
return $this->alert($sMessage, $sTitle, 'info'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritDoc |
125
|
|
|
*/ |
126
|
|
|
public function warning(string $sMessage, string $sTitle = ''): string |
127
|
|
|
{ |
128
|
|
|
return $this->alert($sMessage, $sTitle, 'warning'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritDoc |
133
|
|
|
*/ |
134
|
|
|
public function error(string $sMessage, string $sTitle = ''): string |
135
|
|
|
{ |
136
|
|
|
return $this->alert($sMessage, $sTitle, 'error'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function remove() |
140
|
|
|
{ |
141
|
|
|
$this->response()->script('toastr.remove()'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function clear() |
145
|
|
|
{ |
146
|
|
|
$this->response()->script('toastr.clear()'); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|