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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
520e5b95
Commit
520e5b95
authored
May 20, 2013
by
Antonio Ramirez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored code after feedback
parent
105e277f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
54 deletions
+32
-54
Modal.php
yii/bootstrap/Modal.php
+12
-25
Widget.php
yii/bootstrap/Widget.php
+20
-29
No files found.
yii/bootstrap/Modal.php
View file @
520e5b95
...
@@ -21,10 +21,11 @@ use yii\helpers\ArrayHelper;
...
@@ -21,10 +21,11 @@ use yii\helpers\ArrayHelper;
* 'id' => 'myModal',
* 'id' => 'myModal',
* 'header' => 'Modal Heading',
* 'header' => 'Modal Heading',
* 'content' => '<p>One fine body...</p>',
* 'content' => '<p>One fine body...</p>',
* 'footer' => '//modal/_footer', // we can also use view paths
* 'footer' => 'Modal Footer',
* // if we wish to display a modal button
* 'buttonOptions' => array(
* 'buttonOptions' => array(
* 'label' => 'Show Modal',
* 'label' => 'Show Modal',
* 'class' =>
\yii\bootstrap\enum\Button::TYPE_DEFAULT
* 'class' =>
'btn btn-primary'
* )
* )
* ));
* ));
* ```
* ```
...
@@ -139,11 +140,11 @@ class Modal extends Widget
...
@@ -139,11 +140,11 @@ class Modal extends Widget
$this
->
defaultOption
(
'role'
,
'dialog'
);
$this
->
defaultOption
(
'role'
,
'dialog'
);
$this
->
defaultOption
(
'tabindex'
,
'-1'
);
$this
->
defaultOption
(
'tabindex'
,
'-1'
);
$this
->
add
Option
(
'class'
,
'modal'
);
$this
->
add
ClassName
(
'modal'
);
$this
->
add
Option
(
'class'
,
'hide'
);
$this
->
add
ClassName
(
'hide'
);
if
(
$this
->
fade
)
if
(
$this
->
fade
)
$this
->
add
Option
(
'class'
,
'fade'
);
$this
->
add
ClassName
(
'fade'
);
$this
->
initPluginOptions
();
$this
->
initPluginOptions
();
$this
->
initPluginEvents
();
$this
->
initPluginEvents
();
...
@@ -243,7 +244,7 @@ class Modal extends Widget
...
@@ -243,7 +244,7 @@ class Modal extends Widget
echo
Html
::
beginTag
(
'div'
,
array
(
'class'
=>
'modal-header'
));
echo
Html
::
beginTag
(
'div'
,
array
(
'class'
=>
'modal-header'
));
if
(
$this
->
closeText
)
if
(
$this
->
closeText
)
echo
Html
::
button
(
$this
->
closeText
,
null
,
null
,
array
(
'data-dismiss'
=>
'modal'
,
'class'
=>
'close'
));
echo
Html
::
button
(
$this
->
closeText
,
null
,
null
,
array
(
'data-dismiss'
=>
'modal'
,
'class'
=>
'close'
));
echo
$this
->
renderSection
(
$this
->
header
)
;
echo
$this
->
header
;
echo
Html
::
endTag
(
'div'
);
echo
Html
::
endTag
(
'div'
);
}
}
...
@@ -253,7 +254,7 @@ class Modal extends Widget
...
@@ -253,7 +254,7 @@ class Modal extends Widget
public
function
renderModalBody
()
public
function
renderModalBody
()
{
{
echo
Html
::
beginTag
(
'div'
,
array
(
'class'
=>
'modal-body'
));
echo
Html
::
beginTag
(
'div'
,
array
(
'class'
=>
'modal-body'
));
echo
$this
->
renderSection
(
$this
->
content
)
;
echo
$this
->
content
;
echo
Html
::
endTag
(
'div'
);
echo
Html
::
endTag
(
'div'
);
}
}
...
@@ -264,37 +265,22 @@ class Modal extends Widget
...
@@ -264,37 +265,22 @@ class Modal extends Widget
{
{
echo
Html
::
beginTag
(
'div'
,
array
(
'class'
=>
'modal-footer'
));
echo
Html
::
beginTag
(
'div'
,
array
(
'class'
=>
'modal-footer'
));
echo
$this
->
renderSection
(
$this
->
footer
)
;
echo
$this
->
footer
;
echo
Html
::
endTag
(
'div'
);
echo
Html
::
endTag
(
'div'
);
}
}
/**
/**
* Renders a section. If the section is a view file, the returned string will be the contents of the view file,
* otherwise, it will return the string in the $section variable.
* @param string $section
* @return string
*/
public
function
renderSection
(
$section
)
{
$viewFile
=
Yii
::
getAlias
(
$section
);
if
(
is_file
(
$viewFile
))
return
$this
->
view
->
renderFile
(
$viewFile
,
array
(),
$this
);
return
$section
;
}
/**
* Registers client scripts
* Registers client scripts
*/
*/
public
function
registerScript
()
public
function
registerScript
()
{
{
$id
=
'#'
.
ArrayHelper
::
getValue
(
$this
->
options
,
'id'
);
// do we render a button? If so, bootstrap will handle its behavior through its
// do we render a button? If so, bootstrap will handle its behavior through its
// mark-up, otherwise, register the plugin.
// mark-up, otherwise, register the plugin.
if
(
empty
(
$this
->
buttonOptions
))
if
(
empty
(
$this
->
buttonOptions
))
$this
->
registerPlugin
(
$id
,
$this
->
pluginOptions
);
$this
->
registerPlugin
(
'modal'
,
$this
->
pluginOptions
);
// register events
// register events
$this
->
registerEvents
(
$
id
,
$
this
->
events
);
$this
->
registerEvents
(
$this
->
events
);
}
}
}
}
\ No newline at end of file
yii/bootstrap/Widget.php
View file @
520e5b95
...
@@ -31,11 +31,6 @@ class Widget extends \yii\base\Widget
...
@@ -31,11 +31,6 @@ class Widget extends \yii\base\Widget
public
$options
=
array
();
public
$options
=
array
();
/**
/**
* @var string the widget name (ie. modal, typeahead, tab)
*/
protected
$name
;
/**
* Initializes the widget.
* Initializes the widget.
*/
*/
public
function
init
()
public
function
init
()
...
@@ -48,11 +43,10 @@ class Widget extends \yii\base\Widget
...
@@ -48,11 +43,10 @@ class Widget extends \yii\base\Widget
* Registers plugin events with the API.
* Registers plugin events with the API.
* @param string $selector the CSS selector.
* @param string $selector the CSS selector.
* @param string[] $events the JavaScript event configuration (name=>handler).
* @param string[] $events the JavaScript event configuration (name=>handler).
* @param int $position the position of the JavaScript code.
* @return boolean whether the events were registered.
* @return boolean whether the events were registered.
* @todo To be discussed
* @todo To be discussed
*/
*/
protected
function
registerEvents
(
$selector
,
$events
=
array
()
,
$position
=
View
::
POS_END
)
protected
function
registerEvents
(
$selector
,
$events
=
array
())
{
{
if
(
empty
(
$events
))
if
(
empty
(
$events
))
return
;
return
;
...
@@ -63,24 +57,24 @@ class Widget extends \yii\base\Widget
...
@@ -63,24 +57,24 @@ class Widget extends \yii\base\Widget
?
$handler
?
$handler
:
new
JsExpression
(
$handler
);
:
new
JsExpression
(
$handler
);
$script
.=
";jQuery(
document).ready(function ()
{
jQuery('{$selector}').on('{$name
}
',
{
$handler
}
);
});"
;
$script
.=
";jQuery(
'
{
$selector
}
').on('
{
$name
}
',
{
$handler
}
);"
;
}
}
if
(
!
empty
(
$script
))
if
(
!
empty
(
$script
))
$this
->
view
->
registerJs
(
$script
,
array
(
'position'
=>
$position
)
);
$this
->
view
->
registerJs
(
$script
);
}
}
/**
/**
* Registers a specific Bootstrap plugin using the given selector and options.
* Registers a specific Bootstrap plugin using the given selector and options.
*
*
* @param string $
selector the CSS selector
* @param string $
name the name of the javascript widget to initialize
* @param array $options the Javascript options for the plugin
* @param array $options the Javascript options for the plugin
* @param int $position the position of the JavaScript code
*/
*/
public
function
registerPlugin
(
$
selector
,
$options
=
array
(),
$position
=
View
::
POS_END
)
public
function
registerPlugin
(
$
name
,
$options
=
array
()
)
{
{
$selector
=
'#'
.
ArrayHelper
::
getValue
(
$this
->
options
,
'id'
);
$options
=
!
empty
(
$options
)
?
Json
::
encode
(
$options
)
:
''
;
$options
=
!
empty
(
$options
)
?
Json
::
encode
(
$options
)
:
''
;
$script
=
";jQuery(
document).ready(function ()
{
jQuery('{$selector}').{$this->name}({$options});
}
);"
;
$script
=
";jQuery(
'
{
$selector
}
').
{
$name
}
(
{
$options
}
);"
;
$this
->
view
->
registerJs
(
$script
,
array
(
'position'
=>
$position
)
);
$this
->
view
->
registerJs
(
$script
);
}
}
/**
/**
...
@@ -89,31 +83,28 @@ class Widget extends \yii\base\Widget
...
@@ -89,31 +83,28 @@ class Widget extends \yii\base\Widget
*/
*/
public
function
registerBundle
(
$responsive
=
false
)
public
function
registerBundle
(
$responsive
=
false
)
{
{
$bundle
=
$responsive
?
'yii/bootstrap'
:
'yii/bootstrap-responsive'
;
$bundle
=
$responsive
?
'yii/bootstrap-responsive'
:
'yii/bootstrap'
;
$this
->
view
->
registerAssetBundle
(
$bundle
);
$this
->
view
->
registerAssetBundle
(
$bundle
);
}
}
/**
/**
* Adds a new
option. If the key does not exists, it will create one, if it exists it will append the value
* Adds a new
class to options. If the class key does not exists, it will create one, if it exists it will append
* and also makes sure the uniqueness of them.
*
the value
and also makes sure the uniqueness of them.
*
*
* @param string $key
* @param string $class
* @param mixed $value
* @param string $glue
* @return array
* @return array
*/
*/
protected
function
add
Option
(
$key
,
$value
,
$glue
=
' '
)
protected
function
add
ClassName
(
$class
)
{
{
if
(
isset
(
$this
->
options
[
$key
]))
{
if
(
isset
(
$this
->
options
[
'class'
]))
{
if
(
!
is_array
(
$this
->
options
[
$key
]))
if
(
!
is_array
(
$this
->
options
[
'class'
]))
$this
->
options
[
$key
]
=
explode
(
$glue
,
$this
->
options
[
$key
]);
$this
->
options
[
'class'
]
=
explode
(
' '
,
$this
->
options
[
'class'
]);
$this
->
options
[
$key
][]
=
$value
;
$this
->
options
[
'class'
][]
=
$class
;
$this
->
options
[
$key
]
=
array_unique
(
$this
->
options
[
$key
]);
$this
->
options
[
'class'
]
=
array_unique
(
$this
->
options
[
'class'
]);
$this
->
options
[
$key
]
=
implode
(
$glue
,
$this
->
options
[
$key
]);
$this
->
options
[
'class'
]
=
implode
(
' '
,
$this
->
options
[
'class'
]);
}
else
}
else
$this
->
options
[
$key
]
=
$value
;
$this
->
options
[
'class'
]
=
$class
;
return
$this
->
options
;
return
$this
->
options
;
}
}
...
...
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