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( 'button', function( editor )
  6 {
  7 	function commitAttributes( element )
  8 	{
  9 		var val = this.getValue();
 10 		if ( val )
 11 		{
 12 			element.attributes[ this.id ] = val;
 13 			if ( this.id == 'name' )
 14 				element.attributes[ 'data-cke-saved-name' ] = val;
 15 		}
 16 		else
 17 		{
 18 			delete element.attributes[ this.id ];
 19 			if ( this.id == 'name' )
 20 				delete element.attributes[ 'data-cke-saved-name' ];
 21 		}
 22 	}
 23 
 24 	return {
 25 		title : editor.lang.button.title,
 26 		minWidth : 350,
 27 		minHeight : 150,
 28 		onShow : function()
 29 		{
 30 			delete this.button;
 31 			var element = this.getParentEditor().getSelection().getSelectedElement();
 32 			if ( element && element.is( 'input' ) )
 33 			{
 34 				var type = element.getAttribute( 'type' );
 35 				if ( type in { button:1, reset:1, submit:1 } )
 36 				{
 37 					this.button = element;
 38 					this.setupContent( element );
 39 				}
 40 			}
 41 		},
 42 		onOk : function()
 43 		{
 44 			var editor = this.getParentEditor(),
 45 				element = this.button,
 46 				isInsertMode = !element;
 47 
 48 			var fake = element ? CKEDITOR.htmlParser.fragment.fromHtml( element.getOuterHtml() ).children[ 0 ]
 49 					: new CKEDITOR.htmlParser.element( 'input' );
 50 			this.commitContent( fake );
 51 
 52 			var writer = new CKEDITOR.htmlParser.basicWriter();
 53 			fake.writeHtml( writer );
 54 			var newElement = CKEDITOR.dom.element.createFromHtml( writer.getHtml(), editor.document );
 55 
 56 			if ( isInsertMode )
 57 				editor.insertElement( newElement );
 58 			else
 59 			{
 60 				newElement.replace( element );
 61 				editor.getSelection().selectElement( newElement );
 62 			}
 63 		},
 64 		contents : [
 65 			{
 66 				id : 'info',
 67 				label : editor.lang.button.title,
 68 				title : editor.lang.button.title,
 69 				elements : [
 70 					{
 71 						id : 'name',
 72 						type : 'text',
 73 						label : editor.lang.common.name,
 74 						'default' : '',
 75 						setup : function( element )
 76 						{
 77 							this.setValue(
 78 									element.data( 'cke-saved-name' ) ||
 79 									element.getAttribute( 'name' ) ||
 80 									'' );
 81 						},
 82 						commit : commitAttributes
 83 					},
 84 					{
 85 						id : 'value',
 86 						type : 'text',
 87 						label : editor.lang.button.text,
 88 						accessKey : 'V',
 89 						'default' : '',
 90 						setup : function( element )
 91 						{
 92 							this.setValue( element.getAttribute( 'value' ) || '' );
 93 						},
 94 						commit : commitAttributes
 95 					},
 96 					{
 97 						id : 'type',
 98 						type : 'select',
 99 						label : editor.lang.button.type,
100 						'default' : 'button',
101 						accessKey : 'T',
102 						items :
103 						[
104 							[ editor.lang.button.typeBtn, 'button' ],
105 							[ editor.lang.button.typeSbm, 'submit' ],
106 							[ editor.lang.button.typeRst, 'reset' ]
107 						],
108 						setup : function( element )
109 						{
110 							this.setValue( element.getAttribute( 'type' ) || '' );
111 						},
112 						commit : commitAttributes
113 					}
114 				]
115 			}
116 		]
117 	};
118 });
119