1 /*
  2 Copyright (c) 2003-2012, 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 				editor.insertElement( element );
 35 			}
 36 			this.commitContent( { element : element } );
 37 		},
 38 		contents : [
 39 			{
 40 				id : 'info',
 41 				label : editor.lang.checkboxAndRadio.checkboxTitle,
 42 				title : editor.lang.checkboxAndRadio.checkboxTitle,
 43 				startupFocus : 'txtName',
 44 				elements : [
 45 					{
 46 						id : 'txtName',
 47 						type : 'text',
 48 						label : editor.lang.common.name,
 49 						'default' : '',
 50 						accessKey : 'N',
 51 						setup : function( element )
 52 						{
 53 							this.setValue(
 54 									element.data( 'cke-saved-name' ) ||
 55 									element.getAttribute( 'name' ) ||
 56 									'' );
 57 						},
 58 						commit : function( data )
 59 						{
 60 							var element = data.element;
 61 
 62 							// IE failed to update 'name' property on input elements, protect it now.
 63 							if ( this.getValue() )
 64 								element.data( 'cke-saved-name', this.getValue() );
 65 							else
 66 							{
 67 								element.data( 'cke-saved-name', false );
 68 								element.removeAttribute( 'name' );
 69 							}
 70 						}
 71 					},
 72 					{
 73 						id : 'txtValue',
 74 						type : 'text',
 75 						label : editor.lang.checkboxAndRadio.value,
 76 						'default' : '',
 77 						accessKey : 'V',
 78 						setup : function( element )
 79 						{
 80 							var value = element.getAttribute( 'value' );
 81 							// IE Return 'on' as default attr value.
 82 							this.setValue(  CKEDITOR.env.ie && value == 'on' ? '' : value  );
 83 						},
 84 						commit : function( data )
 85 						{
 86 							var element = data.element,
 87 								value = this.getValue();
 88 
 89 							if ( value && !( CKEDITOR.env.ie && value == 'on' ) )
 90 								element.setAttribute( 'value', value );
 91 							else
 92 							{
 93 								if ( CKEDITOR.env.ie )
 94 								{
 95 									// Remove attribute 'value' of checkbox (#4721).
 96 									var checkbox = new CKEDITOR.dom.element( 'input', element.getDocument() );
 97 									element.copyAttributes( checkbox, { value: 1 } );
 98 									checkbox.replace( element );
 99 									editor.getSelection().selectElement( checkbox );
100 									data.element = checkbox;
101 								}
102 								else
103 									element.removeAttribute( 'value' );
104 							}
105 						}
106 					},
107 					{
108 						id : 'cmbSelected',
109 						type : 'checkbox',
110 						label : editor.lang.checkboxAndRadio.selected,
111 						'default' : '',
112 						accessKey : 'S',
113 						value : "checked",
114 						setup : function( element )
115 						{
116 							this.setValue( element.getAttribute( 'checked' ) );
117 						},
118 						commit : function( data )
119 						{
120 							var element = data.element;
121 
122 							if ( CKEDITOR.env.ie )
123 							{
124 								var isElementChecked = !!element.getAttribute( 'checked' ),
125 									isChecked = !!this.getValue();
126 
127 								if ( isElementChecked != isChecked )
128 								{
129 									var replace = CKEDITOR.dom.element.createFromHtml( '<input type="checkbox"'
130 										   + ( isChecked ? ' checked="checked"' : '' )
131 										   + '/>', editor.document );
132 
133 									element.copyAttributes( replace, { type : 1, checked : 1 } );
134 									replace.replace( element );
135 									editor.getSelection().selectElement( replace );
136 									data.element = replace;
137 								}
138 							}
139 							else
140 							{
141 								var value = this.getValue();
142 								if ( value )
143 									element.setAttribute( 'checked', 'checked' );
144 								else
145 									element.removeAttribute( 'checked' );
146 							}
147 						}
148 					}
149 				]
150 			}
151 		]
152 	};
153 });
154