Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
41f7a7d2
Commit
41f7a7d2
authored
Sep 20, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Supports more elements to use data-confirm and data-method attributes.
parent
1aeb86df
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
26 deletions
+45
-26
yii.js
framework/yii/assets/yii.js
+45
-26
No files found.
framework/yii/assets/yii.js
View file @
41f7a7d2
...
...
@@ -44,9 +44,13 @@
yii
=
(
function
(
$
)
{
var
pub
=
{
/**
* The selector for
links that
support confirmation and form submission.
* The selector for
clickable elements that need to
support confirmation and form submission.
*/
linkClickSelector
:
'a[data-confirm], a[data-method]'
,
clickableSelector
:
'a, button, input[type="submit"], input[type="button"], input[type="reset"], input[type="image"]'
,
/**
* The selector for changeable elements that need to support confirmation and form submission.
*/
changeableSelector
:
'select, input, textarea'
,
/**
* @return string|undefined the CSRF variable name. Undefined is returned is CSRF validation is not enabled.
...
...
@@ -75,41 +79,44 @@ yii = (function ($) {
/**
* Returns a value indicating whether to allow executing the action defined for the specified element.
* This method recognizes the `data-confirm` attribute of the element and uses it
* as the message in a confirmation dialog. The method will return true if this special attribute
* is not defined or if the user confirms the message.
* @param $e the jQuery representation of the element
* @return boolean whether to allow executing the action defined for the specified element.
*/
allowAction
:
function
(
$e
)
{
var
message
=
$e
.
data
(
'confirm'
);
if
(
!
message
)
{
return
true
;
}
return
pub
.
confirm
(
message
);
return
message
===
undefined
||
pub
.
confirm
(
message
);
},
/**
* Handles form submission triggered by elements with "method" data attribute.
* If the element is enclosed within an existing form, the form will be submitted.
* Otherwise, a new form will be created and submitted. The new form's method and action
* are determined using the element's "method" and "action" data attributes, respectively.
* If the "action" data attribute is not specified, it will try the "href" property and
* the current URL.
* Handles the action triggered by user.
* This method recognizes the `data-method` attribute of the element. If the attribute exists,
* the method will submit the form containing this element. If there is no containing form, a form
* will be created and submitted using the method given by this attribute value (e.g. "post", "put").
* For hyperlinks, the form action will take the value of the "href" attribute of the link.
* For other elements, either the containing form action or the current page URL will be used
* as the form action URL.
*
* If the `data-method` attribute is not defined, the default element action will be performed.
*
* @param $e the jQuery representation of the element
* @return boolean whether to execute the default action for the element.
*/
handle
Submit
:
function
(
$e
)
{
handle
Action
:
function
(
$e
)
{
var
method
=
$e
.
data
(
'method'
);
if
(
method
===
undefined
)
{
return
;
return
true
;
}
var
$form
=
$e
.
closest
(
'form'
);
if
(
!
$form
.
length
)
{
var
action
=
$e
.
data
(
'action'
);
if
(
action
===
undefined
)
{
action
=
$e
.
prop
(
'href'
);
if
(
action
===
undefined
)
{
var
newForm
=
!
$form
.
length
;
if
(
newForm
)
{
var
action
=
$e
.
prop
(
'href'
);
if
(
!
action
||
!
action
.
match
(
/
(
^
\/
|:
\/\/)
/
))
{
action
=
window
.
location
.
href
;
}
}
$form
=
$
(
'<form method="'
+
method
+
'" action="'
+
action
+
'"></form>'
);
var
target
=
$e
.
prop
(
'target'
);
if
(
target
)
{
...
...
@@ -132,6 +139,12 @@ yii = (function ($) {
}
$form
.
trigger
(
'submit'
);
if
(
newForm
)
{
$form
.
remove
();
}
return
false
;
},
initModule
:
function
(
module
)
{
...
...
@@ -150,16 +163,22 @@ yii = (function ($) {
init
:
function
()
{
var
$document
=
$
(
document
);
$document
.
on
(
'click.yii'
,
pub
.
linkClickSelector
,
function
(
)
{
$document
.
on
(
'click.yii'
,
pub
.
clickableSelector
,
function
(
event
)
{
var
$this
=
$
(
this
);
if
(
!
pub
.
allowAction
(
$this
))
{
$this
.
stopImmediatePropagation
();
return
false
;
if
(
pub
.
allowAction
(
$this
))
{
return
pub
.
handleAction
(
$this
);
}
else
{
if
(
$this
.
data
(
'method'
))
{
pub
.
handleSubmit
(
$this
);
event
.
stopImmediatePropagation
();
return
false
;
}
});
$document
.
on
(
'change.yii'
,
pub
.
changeableSelector
,
function
(
event
)
{
var
$this
=
$
(
this
);
if
(
pub
.
allowAction
(
$this
))
{
return
pub
.
handleAction
(
$this
);
}
else
{
event
.
stopImmediatePropagation
();
return
false
;
}
});
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment