Completed
Push — master ( be24e6...cb2fdf )
by Basil
02:06
created

SubmitButtonWidget   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 89
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setActiveForm() 0 4 1
A getActiveForm() 0 4 1
A init() 0 8 2
A run() 0 29 3
1
<?php
2
3
namespace luya\widgets;
4
5
use luya\base\Widget;
6
use luya\helpers\Html;
7
use luya\helpers\ArrayHelper;
8
use yii\base\InvalidConfigException;
9
use yii\web\JsExpression;
10
use yii\widgets\ActiveForm;
11
12
/**
13
 * Generates a submit button for a form. This should be used when submiting payment forms
14
 * in order to ensure a request is not send twice.
15
 *
16
 * ```php
17
 * $form = ActiveForm::begin();
18
 * // form code
19
 *
20
 * SubmitButtonWidget::widget(['label' => 'Save', 'pushed' => 'Saving ...', 'options' => ['class' => 'btn btn-primary']]);
21
 * $form::end();
22
 * ```
23
 * 
24
 * @property ActiveForm $activeForm Set active form context of the button.
25
 *
26
 * @author Basil Suter <[email protected]>
27
 * @since 1.0.21
28
 */
29
class SubmitButtonWidget extends Widget
30
{
31
    /**
32
     * @var string The label which should be displayed on button.
33
     */
34
    public $label;
35
36
    /**
37
     * @var string The label which should be visible when the button is pushed. for example `... sending`.
38
     */
39
    public $pushed;
40
41
    /**
42
     * @var array An array with Options which can be passed to the button, see {{luya\helpers\Html::submitButton}}.
43
     */
44
    public $options = [];
45
46
    private $_activeForm;
47
48
    /**
49
     * Setter method for Active Form context.
50
     *
51
     * @param ActiveForm $activeForm Set $activeForm and check for type "ActiveForm"
52
     * @since 1.0.24
53
     */
54
    public function setActiveForm(ActiveForm $activeForm)
55
    {
56
        $this->_activeForm = $activeForm;
57
    }
58
59
    /**
60
     * Getter method for Active Form context.
61
     *
62
     * @return ActiveForm Return $activeForm
63
     * @since 1.0.24
64
     */
65
    public function getActiveForm()
66
    {
67
        return $this->_activeForm;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function init()
74
    {
75
        parent::init();
76
77
        if (!$this->label) {
78
            throw new InvalidConfigException("The label property can not be empty.");
79
        }
80
    }
81
82
    /**
83
     * Add beforeSubmit handler which disables button and replaces button text
84
     * @return string
85
     * @since 1.0.24 Added "ActiveForm-Mode" which only disables button when given ActiveForm is validated successful
86
     */
87
    public function run()
88
    {
89
        if ($this->_activeForm) {
90
            $this->view->registerJs("            
91
            $(document).on('beforeSubmit', function (e) {                
92
                var buttonSelector = $(e.target).find(':submit');
93
                var formSelector = $(e.target);
94
                if (formSelector.find('div." . $this->_activeForm->errorCssClass . "').length === 0) {
95
                    buttonSelector.attr('disabled', true);
96
                    var newButtonLabel = '" . $this->pushed . "';
97
                    if (newButtonLabel) { buttonSelector.html('" . $this->pushed . "'); }                            
98
                }
99
                return true;});");
100
        } else {
101
            $js = [
102
                'this.disabled=true;',
103
            ];
104
105
            if ($this->pushed) {
106
                $js[] = "this.innerHTML='{$this->pushed}';";
107
            }
108
            $this->options = ArrayHelper::merge([
109
                'onclick' => new JsExpression(implode(" ", $js)),
110
                'encoding' => false,
111
            ], $this->options);
112
        }
113
114
        return Html::decode(Html::submitButton($this->label, $this->options));
115
    }
116
117
}
118