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
75aa6659
Commit
75aa6659
authored
Nov 22, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #1222: refactored jui/Widget, intorduced jui/Slider and jui/SliderInput
parent
22439d33
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
171 additions
and
10 deletions
+171
-10
Slider.php
extensions/jui/Slider.php
+46
-0
SliderInput.php
extensions/jui/SliderInput.php
+80
-0
Widget.php
extensions/jui/Widget.php
+45
-10
No files found.
extensions/jui/Slider.php
0 → 100644
View file @
75aa6659
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\jui
;
use
yii\helpers\Html
;
/**
* Slider renders a slider jQuery UI widget.
*
* echo Slider::widget([
* 'clientOptions' => [
* 'min' => 1,
* 'max' => 10,
* ],
* ]);
* ```
*
* @see http://api.jqueryui.com/slider/
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class
Slider
extends
Widget
{
protected
$clientEventsMap
=
[
'change'
=>
'slidechange'
,
'create'
=>
'slidecreate'
,
'slide'
=>
'slide'
,
'start'
=>
'slidestart'
,
'stop'
=>
'slidestop'
,
];
/**
* Executes the widget.
*/
public
function
run
()
{
echo
Html
::
tag
(
'div'
,
''
,
$this
->
options
);
$this
->
registerWidget
(
'slider'
,
SliderAsset
::
className
());
}
}
\ No newline at end of file
extensions/jui/SliderInput.php
0 → 100644
View file @
75aa6659
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\jui
;
use
yii\helpers\Html
;
/**
* SliderInput renders a slider jQuery UI widget that writes its value into hidden input.
*
* For example,
*
* ```
* echo Slider::widget([
* 'model' => $model,
* 'attrbute' => 'amount',
* 'clientOptions' => [
* 'min' => 1,
* 'max' => 10,
* ],
* ]);
* ```
*
* The following example will use the name property instead:
*
* ```
* echo Slider::widget([
* 'name' => 'amount',
* 'clientOptions' => [
* 'min' => 1,
* 'max' => 10,
* ],
* ]);
* ```
*
* @see http://api.jqueryui.com/slider/
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class
SliderInput
extends
InputWidget
{
protected
$clientEventsMap
=
[
'change'
=>
'slidechange'
,
'create'
=>
'slidecreate'
,
'slide'
=>
'slide'
,
'start'
=>
'slidestart'
,
'stop'
=>
'slidestop'
,
];
/**
* Executes the widget.
*/
public
function
run
()
{
echo
Html
::
tag
(
'div'
,
''
,
$this
->
options
);
$inputId
=
$this
->
id
.
'-input'
;
$inputOptions
=
$this
->
options
;
$inputOptions
[
'id'
]
=
$inputId
;
if
(
$this
->
hasModel
())
{
echo
Html
::
activeHiddenInput
(
$this
->
model
,
$this
->
attribute
,
$inputOptions
);
}
else
{
echo
Html
::
hiddenInput
(
$this
->
name
,
$this
->
value
,
$inputOptions
);
}
if
(
!
isset
(
$this
->
clientEvents
[
'slide'
]))
{
$this
->
clientEvents
[
'slide'
]
=
'function(event, ui) {
$("#'
.
$inputId
.
'").val(ui.value);
}'
;
}
$this
->
registerWidget
(
'slider'
,
SliderAsset
::
className
());
$this
->
getView
()
->
registerJs
(
'$("#'
.
$inputId
.
'").val($("#'
.
$this
->
id
.
'").slider("value"));'
);
}
}
\ No newline at end of file
extensions/jui/Widget.php
View file @
75aa6659
...
...
@@ -42,6 +42,11 @@ class Widget extends \yii\base\Widget
*/
public
$clientEvents
=
[];
/**
* @var array event names mapped to what should be specified in .on(
* If empty, it is assumed that event passed to clientEvents is prefixed with widget name.
*/
protected
$clientEventsMap
=
[];
/**
* Initializes the widget.
...
...
@@ -56,32 +61,62 @@ class Widget extends \yii\base\Widget
}
/**
* Registers a specific jQuery UI widget and the related events
* @param string $name the name of the jQuery UI widget
* Registers a specific jQuery UI widget assets
* @param string $assetBundle the asset bundle for the widget
*/
protected
function
register
Widget
(
$name
,
$assetBundle
)
protected
function
register
Assets
(
$assetBundle
)
{
$view
=
$this
->
getView
();
/** @var \yii\web\AssetBundle $assetBundle */
$assetBundle
::
register
(
$
view
);
$assetBundle
::
register
(
$
this
->
getView
()
);
/** @var \yii\web\AssetBundle $themeAsset */
$themeAsset
=
static
::
$theme
;
$themeAsset
::
register
(
$view
);
$themeAsset
::
register
(
$this
->
getView
());
}
$id
=
$this
->
options
[
'id'
];
/**
* Registers a specific jQuery UI widget options
* @param string $name the name of the jQuery UI widget
*/
protected
function
registerClientOptions
(
$name
)
{
if
(
$this
->
clientOptions
!==
false
)
{
$id
=
$this
->
options
[
'id'
];
$options
=
empty
(
$this
->
clientOptions
)
?
''
:
Json
::
encode
(
$this
->
clientOptions
);
$js
=
"jQuery('#
$id
').
$name
(
$options
);"
;
$view
->
registerJs
(
$js
);
$this
->
getView
()
->
registerJs
(
$js
);
}
}
/**
* Registers a specific jQuery UI widget events
* @param string $name the name of the jQuery UI widget
*/
protected
function
registerClientEvents
(
$name
)
{
if
(
!
empty
(
$this
->
clientEvents
))
{
$id
=
$this
->
options
[
'id'
];
$js
=
[];
foreach
(
$this
->
clientEvents
as
$event
=>
$handler
)
{
$js
[]
=
"jQuery('#
$id
').on('
$name$event
',
$handler
);"
;
if
(
isset
(
$this
->
clientEventsMap
[
$event
]))
{
$eventName
=
$this
->
clientEventsMap
[
$event
];
}
else
{
$eventName
=
$name
.
$event
;
}
$view
->
registerJs
(
implode
(
"
\n
"
,
$js
))
;
$js
[]
=
"jQuery('#
$id
').on('
$eventName
',
$handler
);"
;
}
$this
->
getView
()
->
registerJs
(
implode
(
"
\n
"
,
$js
));
}
}
/**
* Registers a specific jQuery UI widget asset bundle, initializes it with client options and registers related events
* @param string $name the name of the jQuery UI widget
* @param string $assetBundle the asset bundle for the widget
*/
protected
function
registerWidget
(
$name
,
$assetBundle
)
{
$this
->
registerAssets
(
$assetBundle
);
$this
->
registerClientOptions
(
$name
);
$this
->
registerClientEvents
(
$name
);
}
}
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