Skip to content

Commit de2d733

Browse files
committed
1.0.0
0 parents  commit de2d733

9 files changed

+710
-0
lines changed

.editorconfig

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# editorconfig.org
2+
root = true
3+
4+
# Unix-style newlines with a newline ending every file
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = true
8+
charset = utf-8
9+
10+
[*.php]
11+
indent_style = space
12+
indent_size = 4
13+
trim_trailing_whitespace = true
14+
15+
[*.{js,twig,css,scss,html}]
16+
indent_style = space
17+
indent_size = 2
18+
19+
[{Gruntfile.js,bower.json,composer.json,package.json}]
20+
indent_style = space
21+
indent_size = 2
22+
23+
[*.md]
24+
trim_trailing_whitespace = false

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

LockBsFormAsset.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php namespace light\widgets;
2+
3+
use yii\web\AssetBundle;
4+
5+
/**
6+
* Asset bundle for the widget
7+
*
8+
* Use this bundle when you have install yii2-bootstrap
9+
*
10+
* @package light\widgets
11+
* @version 1.0.0
12+
* @author lichunqiang <[email protected]>
13+
* @license MIT
14+
*/
15+
class LockBsFormAsset extends AssetBundle
16+
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
public $js = ['assets/lock.js'];
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public $sourcePath = __DIR__;
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public $depends = [
31+
'yii\web\YiiAsset',
32+
'yii\bootstrap\BootstrapPluginAsset',
33+
];
34+
}

LockFormAsset.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php namespace light\widgets;
2+
3+
use yii\web\AssetBundle;
4+
5+
/**
6+
* Asset bundle for the widget
7+
*
8+
* @package light\widgets
9+
* @version 1.0.0
10+
* @author lichunqiang <[email protected]>
11+
* @license MIT
12+
*/
13+
class LockFormAsset extends AssetBundle
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public $js = ['assets/lock.bootstrap.js'];
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public $sourcePath = __DIR__;
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
public $depends = [
29+
'yii\web\YiiAsset',
30+
];
31+
}

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
yii2-lock-form
2+
-----------
3+
[![version](https://img.shields.io/packagist/v/light/yii2-lock-form.svg?style=flat-square)](https://packagist.org/packages/light/yii2-lock-form)
4+
[![Download](https://img.shields.io/packagist/dd/light/yii2-lock-form.svg?style=flat-square)](https://packagist.org/packages/light/yii2-lock-form)
5+
[![Issues](https://img.shields.io/github/issues/lichunqiang/yii2-lock-form.svg?style=flat-square)](https://github.com/lichunqiang/yii2-lock-form/issues)
6+
7+
Make form submit button disabled when the form submit.
8+
9+
## Install
10+
11+
```
12+
$ composer require light\yii2-lock-form=*
13+
```
14+
15+
## Usage
16+
17+
Can can depends this in your assets:
18+
19+
>if you depends on `yii2-bootstrap`, you can use `light\widgets\LockBsFormAsset` instead. So can decrease the asset size.
20+
21+
```
22+
class YourAsset extends AssetBundle
23+
{
24+
//..
25+
26+
public $depends = [
27+
//your other depends
28+
'light\widgets\LockFormAsset'
29+
];
30+
}
31+
```
32+
33+
Or directly inject in the view:
34+
35+
```
36+
use light\widgets\LockFormAsset;
37+
38+
LockFormAsset::register($this);
39+
```
40+
41+
42+
## License
43+
44+
![MIT](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)

assets/lock.bootstrap.js

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* ========================================================================
2+
* Bootstrap: button.js v3.3.5
3+
* http://getbootstrap.com/javascript/#buttons
4+
* ========================================================================
5+
* Copyright 2011-2015 Twitter, Inc.
6+
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7+
* ======================================================================== */
8+
9+
+function ($) {
10+
'use strict';
11+
12+
// BUTTON PUBLIC CLASS DEFINITION
13+
// ==============================
14+
15+
var Button = function (element, options) {
16+
this.$element = $(element)
17+
this.options = $.extend({}, Button.DEFAULTS, options)
18+
this.isLoading = false
19+
}
20+
21+
Button.VERSION = '3.3.5'
22+
23+
Button.DEFAULTS = {
24+
loadingText: 'loading...'
25+
}
26+
27+
Button.prototype.setState = function (state) {
28+
var d = 'disabled'
29+
var $el = this.$element
30+
var val = $el.is('input') ? 'val' : 'html'
31+
var data = $el.data()
32+
33+
state += 'Text'
34+
35+
if (data.resetText == null) $el.data('resetText', $el[val]())
36+
37+
// push to event loop to allow forms to submit
38+
setTimeout($.proxy(function () {
39+
$el[val](data[state] == null ? this.options[state] : data[state])
40+
41+
if (state == 'loadingText') {
42+
this.isLoading = true
43+
$el.addClass(d).attr(d, d)
44+
} else if (this.isLoading) {
45+
this.isLoading = false
46+
$el.removeClass(d).removeAttr(d)
47+
}
48+
}, this), 0)
49+
}
50+
51+
Button.prototype.toggle = $.noop;
52+
53+
// BUTTON PLUGIN DEFINITION
54+
// ========================
55+
56+
function Plugin(option) {
57+
return this.each(function () {
58+
var $this = $(this)
59+
var data = $this.data('bs.button')
60+
var options = typeof option == 'object' && option
61+
62+
if (!data) $this.data('bs.button', (data = new Button(this, options)))
63+
64+
if (option == 'toggle') data.toggle()
65+
else if (option) data.setState(option)
66+
})
67+
}
68+
69+
var old = $.fn.button
70+
71+
$.fn.button = Plugin
72+
$.fn.button.Constructor = Button
73+
74+
75+
// BUTTON NO CONFLICT
76+
// ==================
77+
78+
$.fn.button.noConflict = function () {
79+
$.fn.button = old
80+
return this
81+
}
82+
83+
}(jQuery);
84+
+function (yii) {
85+
function showLoading() {
86+
var $self = $(this),
87+
data = $self.data('yiiActiveForm'),
88+
$submit = data.submitObject;
89+
if (!$submit) {
90+
$submit = $self.find('[type=submit]');
91+
}
92+
$submit.button('loading');
93+
}
94+
function resetLoading() {
95+
var $self = $(this),
96+
$submit = $self.data('yiiActiveForm').submitObject;
97+
if (!$submit) {
98+
$submit = $self.find('[type=submit]');
99+
}
100+
$submit.button('reset');
101+
}
102+
103+
yii.lock = {
104+
init: function () {
105+
$('body')
106+
.on('beforeSubmit', 'form', showLoading)
107+
.on('ajaxComplete', 'form', resetLoading);
108+
}
109+
};
110+
}(yii);

assets/lock.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(function(yii) {
2+
function showLoading() {
3+
var $self = $(this),
4+
data = $self.data('yiiActiveForm'),
5+
$submit = data.submitObject;
6+
if (!$submit) {
7+
$submit = $self.find('[type=submit]');
8+
}
9+
$submit.button('loading');
10+
}
11+
12+
function resetLoading() {
13+
var $self = $(this),
14+
$submit = $self.data('yiiActiveForm').submitObject;
15+
if (!$submit) {
16+
$submit = $self.find('[type=submit]');
17+
}
18+
$submit.button('reset');
19+
}
20+
21+
yii.lock = {
22+
init: function() {
23+
$('body')
24+
.on('beforeSubmit', 'form', showLoading)
25+
.on('ajaxComplete', 'form', resetLoading);
26+
}
27+
};
28+
})(yii);

composer.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "light/yii2-lock-form",
3+
"description": "yii2 activeform ActiveForm bootstrap loading",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "lichunqiang",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.4.0",
14+
"yiisoft/yii2": "^2.0.6"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"light\\widgets\\": ""
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)