      1 /*
      2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
      3 For licensing, see LICENSE.html or http://ckeditor.com/license
      4 */
      5 CKEDITOR.dialog.add( 'checkbox', function( editor )
      6 {
      7 	return {
      8 		title : editor.lang.checkboxAndRadio.checkboxTitle,
      9 		minWidth : 350,
     10 		minHeight : 140,
     11 		onShow : function()
     12 		{
     13 			delete this.checkbox;
     14
     15 			var element = this.getParentEditor().getSelection().getSelectedElement();
     16
     17 			if ( element && element.getAttribute( 'type' ) == "checkbox" )
     18 			{
     19 				this.checkbox = element;
     20 				this.setupContent( element );
     21 			}
     22 		},
     23 		onOk : function()
     24 		{
     25 			var editor,
     26 				element = this.checkbox,
     27 				isInsertMode = !element;
     28
     29 			if ( isInsertMode )
     30 			{
     31 				editor = this.getParentEditor();
     32 				element = editor.document.createElement( 'input' );
     33 				element.setAttribute( 'type', 'checkbox' );
     34 			}
     35
     36 			if ( isInsertMode )
     37 				editor.insertElement( element );
     38 			this.commitContent( { element : element } );
     39 		},
     40 		contents : [
     41 			{
     42 				id : 'info',
     43 				label : editor.lang.checkboxAndRadio.checkboxTitle,
     44 				title : editor.lang.checkboxAndRadio.checkboxTitle,
     45 				startupFocus : 'txtName',
     46 				elements : [
     47 					{
     48 						id : 'txtName',
     49 						type : 'text',
     50 						label : editor.lang.common.name,
     51 						'default' : '',
     52 						accessKey : 'N',
     53 						setup : function( element )
     54 						{
     55 							this.setValue(
     56 									element.getAttribute( '_cke_saved_name' ) ||
     57 									element.getAttribute( 'name' ) ||
     58 									'' );
     59 						},
     60 						commit : function( data )
     61 						{
     62 							var element = data.element;
     63
     64 							// IE failed to update 'name' property on input elements, protect it now.
     65 							if ( this.getValue() )
     66 								element.setAttribute( '_cke_saved_name', this.getValue() );
     67 							else
     68 							{
     69 								element.removeAttribute( '_cke_saved_name' );
     70 								element.removeAttribute( 'name' );
     71 							}
     72 						}
     73 					},
     74 					{
     75 						id : 'txtValue',
     76 						type : 'text',
     77 						label : editor.lang.checkboxAndRadio.value,
     78 						'default' : '',
     79 						accessKey : 'V',
     80 						setup : function( element )
     81 						{
     82 							this.setValue( element.getAttribute( 'value' ) || '' );
     83 						},
     84 						commit : function( data )
     85 						{
     86 							var element = data.element;
     87
     88 							if ( this.getValue() )
     89 								element.setAttribute( 'value', this.getValue() );
     90 							else
     91 								element.removeAttribute( 'value' );
     92 						}
     93 					},
     94 					{
     95 						id : 'cmbSelected',
     96 						type : 'checkbox',
     97 						label : editor.lang.checkboxAndRadio.selected,
     98 						'default' : '',
     99 						accessKey : 'S',
    100 						value : "checked",
    101 						setup : function( element )
    102 						{
    103 							this.setValue( element.getAttribute( 'checked' ) );
    104 						},
    105 						commit : function( data )
    106 						{
    107 							var element = data.element;
    108
    109 							if ( CKEDITOR.env.ie )
    110 							{
    111 								var isElementChecked = !!element.getAttribute( 'checked' );
    112 								var isChecked = !!this.getValue();
    113
    114 								if ( isElementChecked != isChecked )
    115 								{
    116 									var replace = CKEDITOR.dom.element.createFromHtml( '<input type="checkbox"'
    117 										   + ( isChecked ? ' checked="checked"' : '' )
    118 										   + '></input>', editor.document );
    119 									element.copyAttributes( replace, { type : 1, checked : 1 } );
    120 									replace.replace( element );
    121 									editor.getSelection().selectElement( replace );
    122 									data.element = replace;
    123 								}
    124 							}
    125 							else
    126 							{
    127 								if ( this.getValue() )
    128 									element.setAttribute( 'checked', this.getValue() );
    129 								else
    130 									element.removeAttribute( 'checked' );
    131 							}
    132 						}
    133 					}
    134 				]
    135 			}
    136 		]
    137 	};
    138 });
    139