In tThis wiki will
be explain
ed how to include a javascript library in a widget.
We will use as example [uploadify](http://www.uploadify.com/ "uploadify"), a flash/javascript plugin for file uploading.[...]
------------------
Let's imagine we have a page with a file field, and we want to exchange it with
a uploadify.
The first step is to create a widget that will replace this file field.[...]
class ZUploadify extends CInputWidget
{
public function run()
{
echo CHtml::activeFileField($this->model, $this->attribute);
}
}
```
We extend CInputWidget, the base widget for input field. This
masterparent class give
s us
e the attributes 'model' and 'attribute'.
AndNow, instead of the file field in our form we can place:[...]
```
Excellent! Now we have a widget which works exactly how
worked CHtml::activeFileField()
, w
orked.
We can move forward
forto use uploadify
.[...]
Following [uploadify's documentation](http://www.uploadify.com/documentation/ ""), we have to download the package and import i
t on the web page.
Let's save the package in protected/compo
mnents/assets for now.
Now we have to publis
h this directory and to include the files. All simple as:[...]
```php
public function run()
{
$assetsFolder=
Yii::app()->assetManager->publish(
Yii::getPathOfAlias('application.components.assets.uploadify')
);
Yii::app()->clientScript->registerCssFile($assetsFolder.'/uploadify.css');
Yii::app()->clientScript->registerScriptFile($assetsFolder.'/swfobject.js');
Yii::app()->clientScript->registerScriptFile(
$assetsFolder.'/jquery.uploadify.v2.1.4.min.js'
);
Yii::app()->clientScript->registerCoreScript('jquery');
echo CHtml::activeFileField($this->model, $this->attribute);
}
```
The first instruction (assetsManger->publish) publish
es the directory and return
s the path created.
The other
opinstructions publish
es the script and css file. Please note that we
are includ
eing the yii version of jquery
(), not the uploadify one
forto avoid conflicts.
### Register the script
Our objective is to register th
is script:e following js script, and to reserve us the possibility to customize it.[...]
```
And to reserve us the possiblity of customize it.
Let's add a property options:[...]
```
And then we prepare the options like thatis:[...]
```
The options are made up by some default standard options (uploader, script ,
cancelImg, folder and auto) that will be merged with the user defined options.
User defined options will take precedence (they are the second array in array_merge), so we will have the possib
ility of override the default ones.
This options will be encoded with Cjavascript::encode(), a magic function that allow to give javascript functions as parameters, we have just to write 'js:' at the begin
ning.
ForTo register the script we need only the id, we can generate the id with CHtml, respecting any cho
uice of the user:
```php
if (isset ($htmlOptions['id']))
$id= $htmlOptions['id'];
else
$id= CHtml::activeId($this->model, $this->attribute);
```[...]
```php
Yii::app()->clientScript->registerScript($id, "$(document).ready(function() { $('#$id').uploadify($options);});");
```[...]
In summary:
- Create a widget
- Download the library somewhere
- Publish the library
- Include needed script/css
- Prepare options and register the script
Here is the complete result:[...]
For any customization, follow the documentation of uploadify. The work of Yii framework wais only to give a structure in w
hich
easyto in
lc
lude all needed file.[...]