1
1
<?php
2
+
2
3
namespace demogorgorn \ajax ;
3
4
4
5
use yii \base \Widget ;
7
8
use yii \web \JsExpression ;
8
9
9
10
/**
10
- * AjaxSubmitButton renders an ajax button which is very similar to ajaxSubmitButton from Yii1.
11
+ * AjaxSubmitButton renders an ajax button which is very similar to
12
+ * ajaxSubmitButton from Yii1.
11
13
*
12
14
* Example:
13
15
*
18
20
* 'data' => Country::getAllCountries(),
19
21
* 'options' => [
20
22
* 'id' => 'country_select',
21
- * 'multiple' => false,
23
+ * 'multiple' => false,
22
24
* 'placeholder' => 'Select country...',
23
25
* 'class' => 'uk-width-medium-7-10'
24
26
* ]
44
46
*
45
47
* @author Oleg Martemjanov <[email protected] >
46
48
*/
47
-
48
49
class AjaxSubmitButton extends Widget
49
50
{
51
+
52
+ /**
53
+ * Icon positions
54
+ */
55
+ public const ICON_POSITION_LEFT = 'left ' ;
56
+
57
+ public const ICON_POSITION_RIGHT = 'right ' ;
58
+
50
59
public $ ajaxOptions = [];
51
60
52
61
/**
53
- * @var array the HTML attributes for the widget container tag.
54
- */
55
- public $ options = [];
62
+ * @var array the HTML attributes for the widget container tag.
63
+ */
64
+ public $ options = [];
65
+
66
+ /**
67
+ * @var string the tag to use to render the button
68
+ */
69
+ public $ tagName = 'button ' ;
70
+
71
+ /**
72
+ * @var string the button label
73
+ */
74
+ public $ label = 'Button ' ;
75
+
76
+ /**
77
+ * @var string The button icon.
78
+ */
79
+ public $ icon ;
80
+
81
+ /**
82
+ * @var string Icon position.
83
+ * Valid values are 'left', 'right'.
84
+ */
85
+ public $ iconPosition = self ::ICON_POSITION_LEFT ;
56
86
57
87
/**
58
- * @var string the tag to use to render the button
59
- */
60
- public $ tagName = 'button ' ;
61
- /**
62
- * @var string the button label
63
- */
64
- public $ label = 'Button ' ;
65
- /**
66
- * @var boolean whether the label should be HTML-encoded.
67
- */
68
- public $ encodeLabel = true ;
88
+ * @var boolean whether the label should be HTML-encoded.
89
+ */
90
+ public $ encodeLabel = true ;
91
+
69
92
/**
70
93
* @var string js object name.
71
94
* it is unused when useWithActiveForm is enabled
72
95
*/
73
96
public $ clickedButtonVarName = '_clickedButton ' ;
97
+
74
98
/**
75
99
* @var boolean whether the button should not be used with ActiveForm.
76
- * string the id of ActiveForm to use the button with
100
+ * string the id of ActiveForm to use the button with
77
101
*/
78
102
public $ useWithActiveForm = false ;
79
103
80
104
81
-
82
- /**
83
- * Initializes the widget.
84
- */
85
- public function init ()
86
- {
87
- parent ::init ();
88
-
89
- if (!isset ($ this ->options ['id ' ])) {
90
- $ this ->options ['id ' ] = $ this ->getId ();
91
- }
92
- }
93
-
94
- public function run ()
105
+ /**
106
+ * Initializes the widget.
107
+ */
108
+ public function init ()
95
109
{
96
- parent ::run ();
110
+ parent ::init ();
97
111
98
- echo Html::tag ($ this ->tagName , $ this ->encodeLabel ? Html::encode ($ this ->label ) : $ this ->label , $ this ->options );
99
-
100
- if (!empty ($ this ->ajaxOptions )) {
101
-
102
- if ($ this ->useWithActiveForm !== false )
103
- $ this ->registerAjaxFormScript ();
104
- else
105
- $ this ->registerAjaxScript ();
112
+ if (!isset ($ this ->options ['id ' ])) {
113
+ $ this ->options ['id ' ] = $ this ->getId ();
106
114
}
107
115
}
108
116
109
- protected function registerAjaxScript ()
117
+ public function run ()
110
118
{
111
- $ view = $ this -> getView ();
119
+ parent :: run ();
112
120
113
- if (!isset ($ this ->ajaxOptions ['type ' ])) {
114
- $ this ->ajaxOptions ['type ' ] = new JsExpression ('$(this).parents("form").attr("method") ' );
115
- }
121
+ $ label = $ this ->encodeLabel ? Html::encode ($ this ->label ) : $ this ->label ;
116
122
117
- if (!isset ($ this ->ajaxOptions ['url ' ])) {
118
- $ this ->ajaxOptions ['url ' ] = new JsExpression ('$(this).parents("form").attr("action") ' );
123
+ if ($ this ->icon !== null ) {
124
+ $ icon = Html::tag ('i ' , '' , ['class ' => $ this ->icon ]);
125
+ $ label = strcasecmp ($ this ->iconPosition ,
126
+ self ::ICON_POSITION_LEFT ) === 0 ? sprintf ('%s %s ' , $ icon ,
127
+ $ label ) : sprintf ('%s %s ' , $ label , $ icon );
119
128
}
120
129
121
- if (!isset ($ this ->ajaxOptions ['data ' ]) && isset ($ this ->ajaxOptions ['type ' ]))
122
- $ this ->ajaxOptions ['data ' ] = new JsExpression ('$(this).parents("form").serialize() ' );
130
+ echo Html::tag ($ this ->tagName ,
131
+ $ label ,
132
+ $ this ->options );
123
133
124
- $ this ->ajaxOptions = Json::encode ($ this ->ajaxOptions );
125
- $ view ->registerJs ("$('# " .$ this ->options ['id ' ]."').unbind('click').click(function() {
126
- " . (null !== $ this ->clickedButtonVarName ? "var {$ this ->clickedButtonVarName } = this; " : "" ) . "
127
- $.ajax( " . $ this ->ajaxOptions . ");
128
- return false;
129
- }); " );
134
+ if (!empty ($ this ->ajaxOptions )) {
135
+
136
+ if ($ this ->useWithActiveForm !== false ) {
137
+ $ this ->registerAjaxFormScript ();
138
+ } else {
139
+ $ this ->registerAjaxScript ();
140
+ }
141
+ }
130
142
}
131
143
132
144
protected function registerAjaxFormScript ()
133
145
{
134
146
$ view = $ this ->getView ();
135
147
136
- if (!isset ($ this ->ajaxOptions ['type ' ])) {
148
+ if (!isset ($ this ->ajaxOptions ['type ' ])) {
137
149
$ this ->ajaxOptions ['type ' ] = new JsExpression ('$(this).attr("method") ' );
138
150
}
139
151
140
- if (!isset ($ this ->ajaxOptions ['url ' ])) {
152
+ if (!isset ($ this ->ajaxOptions ['url ' ])) {
141
153
$ this ->ajaxOptions ['url ' ] = new JsExpression ('$(this).attr("action") ' );
142
154
}
143
155
144
- if (!isset ($ this ->ajaxOptions ['data ' ]) && isset ($ this ->ajaxOptions ['type ' ]))
156
+ if (!isset ($ this ->ajaxOptions ['data ' ]) && isset ($ this ->ajaxOptions ['type ' ])) {
145
157
$ this ->ajaxOptions ['data ' ] = new JsExpression ('$(this).serialize() ' );
158
+ }
146
159
147
- $ this ->ajaxOptions = Json::encode ($ this ->ajaxOptions );
160
+ $ this ->ajaxOptions = Json::encode ($ this ->ajaxOptions );
148
161
149
- $ js = <<<SEL
162
+ $ js = <<<SEL
150
163
$(document).unbind('beforeSubmit. {$ this ->useWithActiveForm }').on('beforeSubmit. {$ this ->useWithActiveForm }', "# {$ this ->useWithActiveForm }", function () {
151
164
if ($(this).find('.has-error').length < 1) {
152
165
$.ajax( {$ this ->ajaxOptions });
@@ -158,7 +171,30 @@ protected function registerAjaxFormScript()
158
171
$ view ->registerJs ($ js );
159
172
160
173
174
+ }
175
+
176
+ protected function registerAjaxScript ()
177
+ {
178
+ $ view = $ this ->getView ();
161
179
180
+ if (!isset ($ this ->ajaxOptions ['type ' ])) {
181
+ $ this ->ajaxOptions ['type ' ] = new JsExpression ('$(this).parents("form").attr("method") ' );
182
+ }
183
+
184
+ if (!isset ($ this ->ajaxOptions ['url ' ])) {
185
+ $ this ->ajaxOptions ['url ' ] = new JsExpression ('$(this).parents("form").attr("action") ' );
186
+ }
187
+
188
+ if (!isset ($ this ->ajaxOptions ['data ' ]) && isset ($ this ->ajaxOptions ['type ' ])) {
189
+ $ this ->ajaxOptions ['data ' ] = new JsExpression ('$(this).parents("form").serialize() ' );
190
+ }
191
+
192
+ $ this ->ajaxOptions = Json::encode ($ this ->ajaxOptions );
193
+ $ view ->registerJs ("$('# " . $ this ->options ['id ' ] . "').unbind('click').click(function() {
194
+ " . (null !== $ this ->clickedButtonVarName ? "var {$ this ->clickedButtonVarName } = this; " : "" ) . "
195
+ $.ajax( " . $ this ->ajaxOptions . ");
196
+ return false;
197
+ }); " );
162
198
}
163
199
164
200
}
0 commit comments