基于Ckeditor的表单设计器的开发

前面基本环境都做得差不多了,这篇我们来介绍自定义插件的开发。


我们以"文本框“为例,

先来看效果:



点击确定,即可插入一个文本框


实现方式:


1、在ckeditor目录下 plugins文件夹下,新建如下结构:



plugin.js


[javascript]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. CKEDITOR.plugins.add( 'ths_textfield', {  
  2.     icons: 'ths_textfield',  
  3.     init: function( editor ) {   //初始化  
  4.         var pluginName = 'ths_textfield'//控件名称  
  5.         editor.addCommand( pluginName, new CKEDITOR.dialogCommand( pluginName ) ); //给编辑器注册一个打开弹出窗命令  
  6.                  
  7.         editor.ui.addButton(pluginName, {  //在工具栏上增加一个按钮,绑定按钮事件  
  8.             label: '单行文本框',  
  9.             command: pluginName  
  10.         });  
  11.       
  12.         if ( editor.contextMenu ) {  //为文本框加右键属性菜单  
  13.             editor.addMenuGroup( 'textFieldGroup' );  
  14.              editor.addMenuItem( 'textFieldItem', {  
  15.                     label: '文本框属性',  
  16.                     command:pluginName,  
  17.                     group: 'textFieldGroup'  
  18.                 });  
  19.               //右键菜单的监听器,判断是否显示菜单         
  20.               editor.contextMenu.addListener( function( element ) {  
  21.                     if ( element && !element.isReadOnly() ) {  
  22.                         var name = element.getName();  
  23.                         if ( name == 'input' ) {  
  24.                             var type = element.getAttribute( 'type' ) || 'text';  
  25.                             if ( type=='text' ){  
  26.                                    return { textFieldItem: CKEDITOR.TRISTATE_OFF };  
  27.                             }  
  28.                         }  
  29.                     }  
  30.                 });  
  31.         }  
  32.                 //增加弹出窗  
  33.         CKEDITOR.dialog.add( pluginName, this.path + 'dialogs/'+pluginName+'.js' );  
  34.         //为文本框双击事件绑定一个事件,即显示弹出窗  
  35.         editor.on( 'doubleclick'function( evt ) {  
  36.             var element = evt.data.element;  
  37.               
  38.              if ( element.is( 'input' ) ) {  
  39.                     var type = element.getAttribute( 'type' ) || 'text';  
  40.                     if ( type=='text' ){  
  41.                         evt.data.dialog =pluginName;  
  42.                     }  
  43.             }  
  44.         })  
  45.           
  46.     }  
  47. });  


代码不过多解释,请看注释
不喜欢讲代码,大家结合文档看注释


ths_textfield.js

[javascript]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. CKEDITOR.dialog.add( 'ths_textfield'function( editor ) {  
  2.       
  3.     return {  
  4.         title: '单行文本框属性',  
  5.         minWidth: 400,  
  6.         minHeight: 200,  
  7.         //弹出窗上显示的内容  
  8.         contents: [  
  9.             {  
  10.                 id: 'tab-basic',  
  11.                 label: '基本属性',  
  12.                 elements:[ {  
  13.                     type: 'hbox',  
  14.                     widths: [ '50%''50%' ],  
  15.                     children:  
  16.                     [  
  17.                          ths_editor_field(editor),              
  18.                              ths_editor_value(editor)  
  19.                      ]  
  20.                 },  
  21.                 {  
  22.                         type: 'hbox',  
  23.                         widths: [ '50%''50%' ],  
  24.                         children:  
  25.                         [ths_editor_relative_width(editor) ,  
  26.                          ths_editor_style(editor)  
  27.                         ]  
  28.                  }  
  29.                 ]  
  30.             },  
  31.   
  32.             {  
  33.                 id: 'tab-validate',  
  34.                 label: '数据校验',  
  35.                 elements: [  
  36.                     {  
  37.                         type: 'checkbox',  
  38.                         id: 'required',  
  39.                         label: '必填',  
  40.                         setup: function( element ) {  
  41.                             if(element.getAttribute( "required" )){  
  42.                                 this.setValue(true);  
  43.                             }  
  44.                         },  
  45.                         commit: function ( element ) {  
  46.                             var required = this.getValue();  
  47.                             if ( required )  
  48.                                 element.setAttribute( 'required''true' );  
  49.                             else if ( !this.insertMode )  
  50.                                 element.removeAttribute( 'required' );  
  51.                         }  
  52.                     }  
  53.                 ]  
  54.             },  
  55.             {  
  56.                 id: 'tab-event',  
  57.                 label: '事件',  
  58.                 elements: [  
  59.                                 ths_editor_onblur(editor),  
  60.                             ths_editor_onfocus(editor),  
  61.                             ths_editor_onclick(editor),  
  62.                             ths_editor_onchange(editor)  
  63.                 ]  
  64.             }  
  65.         ],  
  66.         //弹出窗显示事件  
  67.         onShow: function() {  
  68.   
  69.             var selection = editor.getSelection();  
  70.             var element = selection.getStartElement();  
  71.             if ( !element || element.getName() != 'input' || element.getAttribute( 'type' )!='text'  ) {  
  72.                 this.insertMode = true;  
  73.             }else{  
  74.                 this.insertMode = false;  
  75.             }  
  76.   
  77.             this.element = element;  
  78.             if ( !this.insertMode ){  
  79.                 this.setupContent( this.element );  
  80.             }  
  81.                   
  82.         },  
  83.         //弹出窗确定按钮事件  
  84.         onOk: function() {  
  85.             submitElement(this,editor,'text');  
  86.         }  
  87.     };  
  88. });  

补充js函数

这是针对所有表单组件的js

[javascript]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. function submitElement(dialog,editor,type){  
  2.       if ( dialog.insertMode ){  //如果是新建状态  
  3.           
  4.         var div=editor.document.createElement( 'div' );  
  5.         var label;  
  6.         var element;  
  7.                 //为不同的元素赋值  
  8.             switch ( type ) {  
  9.                 case 'select':  
  10.                     element = editor.document.createElement( 'select' );  
  11.                     if(dialog.getValueOf( 'tab-basic''dictionary')){element.setAttribute( 'dictionary', dialog.getValueOf( 'tab-basic''dictionary') )};           
  12.                     break;  
  13.                 case 'textarea':  
  14.                     element = editor.document.createElement( 'textarea' );  
  15.                     if(dialog.getValueOf( 'tab-basic''value')){element.setAttribute( 'value', dialog.getValueOf( 'tab-basic''value') )};              
  16.                     break;  
  17.                 case 'text':  
  18.                     element = editor.document.createElement( 'input' );  
  19.                     element.setAttribute( 'type', type );  
  20.                     //if(dialog.getValueOf( 'tab-basic', 'size')!='default'){element.addClass( dialog.getValueOf( 'tab-basic', 'size') )};  
  21.                     if(dialog.getValueOf( 'tab-basic''value')){element.setAttribute( 'value', dialog.getValueOf( 'tab-basic''value') )};              
  22.                     break;  
  23.                 case 'checkbox':  
  24.                     label=editor.document.createElement( 'label' );  
  25.                     label.addClass( 'checkbox-inline' );  
  26.                     element = editor.document.createElement( 'input' );  
  27.                     element.setAttribute( 'type', type );  
  28.                     if(dialog.getValueOf( 'tab-basic''dictionary')){element.setAttribute( 'dictionary', dialog.getValueOf( 'tab-basic''dictionary') )};           
  29.                     break;  
  30.                 case 'radio':  
  31.                     label=editor.document.createElement( 'label' );  
  32.                     label.addClass( 'radio-inline' );  
  33.                     element = editor.document.createElement( 'input' );  
  34.                     element.setAttribute( 'type', type );  
  35.                     if(dialog.getValueOf( 'tab-basic''dictionary')){element.setAttribute( 'dictionary', dialog.getValueOf( 'tab-basic''dictionary') )};           
  36.                     break;  
  37.                 case 'hidden':  
  38.                     element = editor.document.createElement( 'input' );  
  39.                     element.setAttribute( 'type', type );  
  40.                     element.setAttribute( 'name', dialog.getValueOf( 'tab-basic''name' ) );  
  41.                     if(dialog.getValueOf( 'tab-basic''value')){element.setAttribute( 'value', dialog.getValueOf( 'tab-basic''value') )};              
  42.                     editor.insertElement( element );  
  43.                     return;  
  44.             }  
  45.           
  46.             element.addClass( 'form-control' );  
  47.             element.setAttribute( 'name', dialog.getValueOf( 'tab-basic''name' ) );  
  48.             if(dialog.getValueOf( 'tab-basic''style' )){  
  49.                 element.setAttribute( 'style', element.getAttribute('style') ? element.getAttribute('style')+dialog.getValueOf( 'tab-basic''style' ):dialog.getValueOf( 'tab-basic''style' ) )  
  50.             };  
  51.               
  52.             if(dialog.getValueOf( 'tab-basic''width' )) {div.addClass(dialog.getValueOf( 'tab-basic''width'))};  
  53.               
  54.             if(dialog.getValueOf( 'tab-event''onclick')){element.setAttribute( 'onclick', dialog.getValueOf( 'tab-event''onclick') )};  
  55.             if(dialog.getValueOf( 'tab-event''onfocus')){element.setAttribute( 'onfocus', dialog.getValueOf( 'tab-event''onfocus') )};  
  56.             if(dialog.getValueOf( 'tab-event''onblur')){element.setAttribute( 'onblur', dialog.getValueOf( 'tab-event''onblur') )} ;  
  57.             if(dialog.getValueOf( 'tab-event''onchange')){element.setAttribute( 'onchange', dialog.getValueOf( 'tab-event''onchange') )} ;  
  58.             if(dialog.getValueOf( 'tab-validate''required' )){element.setAttribute( 'required', dialog.getValueOf( 'tab-validate''required' ) )};  
  59.               
  60.             if(label){  
  61.                 label.append(element);  
  62.                 div.append(label);  
  63.             }else{  
  64.                 div.append(element);  
  65.             }  
  66.               
  67.             editor.insertElement( div );  
  68.           
  69.       }else{  
  70.           dialog.commitContent( dialog.element );  
  71.       }  
  72.       
  73. }  

弹出窗中,显示文本域的示例:

[javascript]   view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. function ths_editor_size(editor){  
  2.     var editor_size={  
  3.            type : 'select',  
  4.         id: 'size',  
  5.         label: '大小:',  
  6.         style: 'width:190px',  
  7.         'default' : 'default',  
  8.         items :  
  9.                 [  
  10.                     [ '大''input-lg' ],  
  11.                     [ '默认''default' ],  
  12.                     [ '小''input-sm' ]  
  13.                 ],  
  14.         setup: function( element ) {  //弹出窗初始化时会调用  
  15.              
  16.            var classStr=element.getAttribute( "class" ) ? element.getAttribute( "class" ) : 'default';  
  17.              
  18.            if(classStr.indexOf('input')>=0){  
  19.                classStr=classStr.substring(classStr.indexOf('input'),classStr.indexOf('input')+8);  
  20.                this.setValue(classStr);  
  21.            }else{  
  22.                return;  
  23.            }  
  24.              
  25.         },  
  26.         commit: function( element ) {  //提交时会调用  
  27.              var classStr = this.getValue();  
  28.                
  29.              if(element.hasClass('input-lg')) element.removeClass('input-lg');  
  30.              if(element.hasClass('input-sm')) element.removeClass('input-sm');  
  31.                
  32.               if ( classStr && classStr!='default'){  
  33.                  element.addClass(classStr);  
  34.               }  
  35.         }  
  36.        }  
  37.     return editor_size;  
  38. }  


不想贴代码,感觉代码真的很无力,但想说明白一件事,似乎代码来得更直接些,上面代码均为核心代码。


随后会把代码开源,请留意


如果你英文比较好,推荐看如下两篇文章:

http://docs.ckeditor.com/#!/guide/plugin_sdk_sample_1

http://docs.ckeditor.com/#!/guide/plugin_sdk_sample_2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值