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 
  6 (function()
  7 {
  8 
  9 function setupAdvParams( element )
 10 {
 11 	var attrName = this.att;
 12 
 13 	var value = element && element.hasAttribute( attrName ) && element.getAttribute( attrName ) || '';
 14 
 15 	if ( value !== undefined )
 16 		this.setValue( value );
 17 }
 18 
 19 function commitAdvParams()
 20 {
 21 	// Dialogs may use different parameters in the commit list, so, by
 22 	// definition, we take the first CKEDITOR.dom.element available.
 23 	var element;
 24 
 25 	for ( var i = 0 ; i < arguments.length ; i++ )
 26 	{
 27 		if ( arguments[ i ] instanceof CKEDITOR.dom.element )
 28 		{
 29 			element = arguments[ i ];
 30 			break;
 31 		}
 32 	}
 33 
 34 	if ( element )
 35 	{
 36 		var attrName = this.att,
 37 			value = this.getValue();
 38 
 39 		if ( value )
 40 			element.setAttribute( attrName, value );
 41 		else
 42 			element.removeAttribute( attrName, value );
 43 	}
 44 }
 45 
 46 CKEDITOR.plugins.add( 'dialogadvtab',
 47 {
 48 	/**
 49 	 *
 50 	 * @param tabConfig
 51 	 * id, dir, classes, styles
 52 	 */
 53 	createAdvancedTab : function( editor, tabConfig )
 54 	{
 55 		if ( !tabConfig )
 56 			tabConfig = { id:1, dir:1, classes:1, styles:1 };
 57 
 58 		var lang = editor.lang.common;
 59 
 60 		var result =
 61 		{
 62 			id : 'advanced',
 63 			label : lang.advancedTab,
 64 			title : lang.advancedTab,
 65 			elements :
 66 				[
 67 					{
 68 						type : 'vbox',
 69 						padding : 1,
 70 						children : []
 71 					}
 72 				]
 73 		};
 74 
 75 		var contents = [];
 76 
 77 		if ( tabConfig.id || tabConfig.dir )
 78 		{
 79 			if ( tabConfig.id )
 80 			{
 81 				contents.push(
 82 					{
 83 						id : 'advId',
 84 						att : 'id',
 85 						type : 'text',
 86 						label : lang.id,
 87 						setup : setupAdvParams,
 88 						commit : commitAdvParams
 89 					});
 90 			}
 91 
 92 			if ( tabConfig.dir )
 93 			{
 94 				contents.push(
 95 					{
 96 						id : 'advLangDir',
 97 						att : 'dir',
 98 						type : 'select',
 99 						label : lang.langDir,
100 						'default' : '',
101 						style : 'width:100%',
102 						items :
103 						[
104 							[ lang.notSet, '' ],
105 							[ lang.langDirLTR, 'ltr' ],
106 							[ lang.langDirRTL, 'rtl' ]
107 						],
108 						setup : setupAdvParams,
109 						commit : commitAdvParams
110 					});
111 			}
112 
113 			result.elements[ 0 ].children.push(
114 				{
115 					type : 'hbox',
116 					widths : [ '50%', '50%' ],
117 					children : [].concat( contents )
118 				});
119 		}
120 
121 		if ( tabConfig.styles || tabConfig.classes )
122 		{
123 			contents = [];
124 
125 			if ( tabConfig.styles )
126 			{
127 				contents.push(
128 					{
129 						id : 'advStyles',
130 						att : 'style',
131 						type : 'text',
132 						label : lang.styles,
133 						'default' : '',
134 
135 						validate : CKEDITOR.dialog.validate.inlineStyle( lang.invalidInlineStyle ),
136 						onChange : function(){},
137 
138 						getStyle : function( name, defaultValue )
139 						{
140 							var match = this.getValue().match( new RegExp( name + '\\s*:\\s*([^;]*)', 'i') );
141 							return match ? match[ 1 ] : defaultValue;
142 						},
143 
144 						updateStyle : function( name, value )
145 						{
146 							var styles = this.getValue();
147 
148 							var tmp = editor.document.createElement( 'span' );
149 							tmp.setAttribute( 'style', styles );
150 							tmp.setStyle( name, value );
151 							styles = CKEDITOR.tools.normalizeCssText( tmp.getAttribute( 'style' ) );
152 
153 							this.setValue( styles, 1 );
154 						},
155 
156 						setup : setupAdvParams,
157 
158 						commit : commitAdvParams
159 
160 					});
161 			}
162 
163 			if ( tabConfig.classes )
164 			{
165 				contents.push(
166 					{
167 						type : 'hbox',
168 						widths : [ '45%', '55%' ],
169 						children :
170 						[
171 							{
172 								id : 'advCSSClasses',
173 								att : 'class',
174 								type : 'text',
175 								label : lang.cssClasses,
176 								'default' : '',
177 								setup : setupAdvParams,
178 								commit : commitAdvParams
179 
180 							}
181 						]
182 					});
183 			}
184 
185 			result.elements[ 0 ].children.push(
186 				{
187 					type : 'hbox',
188 					widths : [ '50%', '50%' ],
189 					children : [].concat( contents )
190 				});
191 		}
192 
193 		return result;
194 	}
195 });
196 
197 })();
198