现在的位置: 首页 > 综合 > 正文

YII-视图- kindeditor扩展

2013年12月04日 ⁄ 综合 ⁄ 共 4009字 ⁄ 字号 评论关闭

比较喜欢用kindeditor,YII上的版本比较旧,所以自己重新整了个扩展

先在protected\extensions下创建KEditor文件夹用来放文件,keSource里放kindeditor的源文件,然后建三个类KEditor、KEditorManage和KEditorUpload,KEditor是扩展的主文件,KEditorManage是用来浏览服务器文件的,KEditorUpload是用来示例接收上传文件的,

KEditor代码

[html] view
plain
copy

  1. <?php  
  2. class KEditor extends CWidget{  
  3.     /*  
  4.      * TEXTAREA输入框的属性,保证js调用KE失败时,文本框的样式。  
  5.      */  
  6.     public $textareaOptions=array();  
  7.     /*  
  8.      * 编辑器属性集。  
  9.      */  
  10.     public $properties=array();  
  11.     /*  
  12.      * TEXTAREA输入框的name,必须设置。  
  13.      * 数据类型:String  
  14.      */  
  15.     public $name;  
  16.     /*  
  17.      * TEXTAREA的id,可为空  
  18.      */  
  19.     public $id;  
  20.       
  21.     public $model;  
  22.       
  23.     public $baseUrl;  
  24.       
  25.     public static function getUploadPath(){  
  26.         $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'keSource';  
  27.         if(isset(Yii::app()->params->uploadPath)){  
  28.             return Yii::getPathOfAlias('webroot').str_replace(  
  29.                                 '/',DIRECTORY_SEPARATOR,  
  30.                                 Yii::app()->params->  
  31.                                 uploadPath);  
  32.         }  
  33.         return Yii::app()->getAssetmanager()  
  34.                 ->getPublishedPath($dir).DIRECTORY_SEPARATOR.'upload';  
  35.     }  
  36.       
  37.     public static function getUploadUrl(){  
  38.         $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'keSource';  
  39.         if(isset(Yii::app()->params->uploadPath)){  
  40.             return Yii::app()->baseUrl.Yii::app()->params->uploadPath;  
  41.         }  
  42.         return Yii::app()->getAssetManager()->publish($dir).'/upload';  
  43.     }  
  44.       
  45.     public function init(){  
  46.         if($this->name===null)  
  47.             throw new CException(Yii::t('zii','The id property cannot be empty.'));  
  48.               
  49.         $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'keSource';  
  50.           
  51.         $this->baseUrl=Yii::app()->getAssetManager()->publish($dir);  
  52.         $cs=Yii::app()->getClientScript();  
  53.         $cs->registerCssFile($this->baseUrl.'/themes/default/default.css');  
  54.         if(YII_DEBUG) $cs->registerScriptFile($this->baseUrl.'/kindeditor.js');  
  55.         else $cs->registerScriptFile($this->baseUrl.'/kindeditor-min.js');  
  56.     }  
  57.       
  58.     public function run(){  
  59.         $cs=Yii::app()->getClientScript();  
  60.         $textAreaOptions=$this->gettextareaOptions();  
  61.         $textAreaOptions['name']=CHtml::resolveName($this->model,$this->name);  
  62.         $this->id=$textAreaOptions['id']=CHtml::getIdByName($textAreaOptions['name']);  
  63.         echo CHtml::activeTextArea($this->model,$this->name,$textAreaOptions);  
  64.           
  65.         $properties_string = CJavaScript::encode($this->getKeProperties());  
  66.   
  67.         $js=<<<EOF  
  68. KindEditor.ready(function(K) {  
  69.     var editor_$this->id = K.create('#$this->id',   
  70. $properties_string  
  71.     );  
  72. });  
  73. EOF;  
  74.         $cs->registerScript('KE'.$this->name,$js,CClientScript::POS_HEAD);  
  75.     }  
  76.       
  77.     public function gettextareaOptions(){  
  78.         //允许获取的属性  
  79.         $allowParams=array('rows','cols','style');  
  80.         //准备返回的属性数组  
  81.         $params=array();  
  82.         foreach($allowParams as $key){  
  83.             if(isset($this->textareaOptions[$key]))  
  84.                 $params[$key]=$this->textareaOptions[$key];  
  85.         }  
  86.         $params['name']=$params['id']=$this->name;  
  87.         return $params;  
  88.     }  
  89.       
  90.     public function getKeProperties(){  
  91.         $properties_key=array(  
  92.             'width',  
  93.             'height',  
  94.             'minWidth',  
  95.             'minHeight',  
  96.             'items',  
  97.             'noDisableItems',  
  98.             'filterMode',  
  99.             'htmlTags',  
  100.             'wellFormatMode',  
  101.             'resizeType',  
  102.             'themeType',  
  103.             'langType',  
  104.             'designMode',  
  105.             'fullscreenMode',  
  106.             'basePath',  
  107.             'themesPath',  
  108.             'pluginsPath',  
  109.             'langPath',  
  110.             'minChangeSize',  
  111.             'urlType',  
  112.             'newlineTag',  
  113.             'pasteType',  
  114.             'dialogAlignType',  
  115.             'shadowMode',  
  116.             'useContextmenu',  
  117.             'syncType',  
  118.             'indentChar',  

抱歉!评论已关闭.