unchanged
Title
How to create a wrapper for a js library
In thisThis wiki willbe explainedexplain 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. Our example: uploadify ------------------ Let's imagine we have a page with a file field, and we want to exchange it withauploadify. The first step is to create a widget that will replace this file field. Creating the widget ------------------ Let's create a file in components named ZUploadify: ~~~ [php] <?php class ZUploadify extends CInputWidget {public function run() {echo CHtml::activeFileField($this->model, $this->attribute); }} ~~~ We extend CInputWidget, the base widget for input field. Thismasterclass give useparent class gives us the attributes 'model' and 'attribute'.AndNow, instead of the file field in our form we can place: ~~~ [php] <?php $this->widget('ZUploadify', array('model'=>$model, 'attribute'=>'file')); ?> ~~~ Excellent! Now we have a widgetwichwhich works exactly howworked CHtml::activeFileField(), weCHtml::activeFileField() worked. We can move forwardforto useuploadifyuploadify. Import uploadify ---------------- Following[uploadify's[uploadify documentation](http://www.uploadify.com/documentation/ ""), we have to download the package and importinit on the web page. Let's save the package inprotected/compoments/assetsprotected/components/assets for now. Now we have topublispublish this directory and to include the files. All simple as: ~~~ [php]publicpublic 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); }{ $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)publishpublishes the directory andreturnreturns the path created. The otheroptions publishinstructions publishes the script and css file. Please note that weincludeare including the yii version ofjquery (),jquery, not the uploadify oneforto avoid conflicts. ### Register the script Our objective is to registerthis script:the following js script, and to reserve us the possibility to customize it. ~~~ [php] <script type="text/javascript"> $(document).ready(function() { $('#file_upload').uploadify({ 'uploader' : '/uploadify/uploadify.swf', 'script' : '/uploadify/uploadify.php', 'cancelImg' : '/uploadify/cancel.png', 'folder' : '/uploads', 'auto' : true }); }); </script> ~~~And to reserve us the possiblity of customize it.Let's add a property options: ~~~ [php] public $options; ~~~ And then we prepare the options likethat:this: ~~~ [php] $options= CJavaScript::encode(array_merge( array( 'uploader' => $assetsFolder.'/uploadify.swf', 'script' => $assetsFolder.'/uploadify.php', 'cancelImg' => $assetsFolder.'/cancel.png', 'folder' => '/uploads', 'auto' => true ), $this->options )); ~~~ The options are made up by some default standard options (uploader, script, cancelImg,,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 thepossiblitypossibility 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 thebegining.beginning.ForTo register the script we need only the id, we can generate the id with CHtml, respecting anychoucechoice of the user: ~~~ [php]ifif (isset ($htmlOptions['id']))$id=$id= $htmlOptions['id'];else $id=else $id= CHtml::activeId($this->model, $this->attribute); ~~~ And finally register the script: ~~~ [php]Yii::app()->clientScript->registerScript($id, "$(document).ready(function()Yii::app()->clientScript->registerScript($id, "$(document).ready(function() { $('#$id').uploadify($options);});"); ~~~ That's it, our uploadify widget is ready to be used. Conclusion ------------------ In summary:Create- Create a widgetDownload- Download the library somewherePublish- Publish the libraryInclude- Include needed script/cssPrepare- Prepare options and register the script Here is the complete result: ~~~ [php] <?php class ZUploadify extends CInputWidget { public $options = array(); public function run() { $assetsFolder= Yii::app()->assetManager->publish(Yii::getPathOfAlias('application.modules.administrator.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'); $options= CJavaScript::encode(array_merge( array( 'uploader' => $assetsFolder.'/uploadify.swf', 'script' => $assetsFolder.'/uploadify.php', 'cancelImg' => $assetsFolder.'/cancel.png', 'folder' => '/uploads', 'auto' => true ), $this->options )); if (isset ($htmlOptions['id'])) $id= $htmlOptions['id']; else $id= CHtml::activeId($this->model, $this->attribute); Yii::app()->clientScript->registerScript($id, "$(document).ready(function() { $('#$id').uploadify($options);});"); echo CHtml::activeFileField($this->model, $this->attribute, $this->htmlOptions); } } ~~~ For any customization, follow the documentation of uploadify. The work of Yii frameworkwasis only to give a structure inwich easy inlcudewhich to include all needed file.