| | 1 | /* |
| | 2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. |
| | 3 | For licensing, see LICENSE.html or http://ckeditor.com/license |
| | 4 | */ |
| | 5 | (function() |
| | 6 | { |
| | 7 | |
| | 8 | var advParamsMap = { |
| | 9 | 'advLangDir' : 'dir', |
| | 10 | 'advCSSClasses' : 'class', |
| | 11 | 'advStyles' : 'style', |
| | 12 | 'advId' : 'id' |
| | 13 | }; |
| | 14 | |
| | 15 | var setupAdvParams = function( element ) |
| | 16 | { |
| | 17 | var attrName = advParamsMap[ this.id ]; |
| | 18 | |
| | 19 | // Try to get existing value first in case of inChange commit. |
| | 20 | var value = this.getValue() || element && element.hasAttribute( attrName ) && element.getAttribute( attrName ) || ''; |
| | 21 | |
| | 22 | // Compatibility with showborders plugin. |
| | 23 | if ( attrName == 'class' ) |
| | 24 | value = value.replace( /cke_show_border/g, '' ); |
| | 25 | else if ( attrName == 'style' ) |
| | 26 | { |
| | 27 | // Inherit width value from width field. |
| | 28 | var widthField = this.getDialog().getContentElement( 'info', 'txtWidth'); |
| | 29 | var widthFieldValue = widthField.getValue(); |
| | 30 | |
| | 31 | if ( widthFieldValue ) |
| | 32 | { |
| | 33 | var widthUnit = this.getDialog().getContentElement( 'info', 'cmbWidthType').getValue(); |
| | 34 | widthUnit = widthUnit == 'percents' ? '%' : 'px'; |
| | 35 | |
| | 36 | widthFieldValue = 'width: ' + widthFieldValue + widthUnit + ';'; |
| | 37 | if ( value ) |
| | 38 | value = value.replace( /\s*width: .+?;\s*/, '' ); |
| | 39 | value = value ? value + '; ' + widthFieldValue : widthFieldValue; |
| | 40 | } |
| | 41 | else |
| | 42 | value = value.replace( /\s*width: .+?;\s*/, '' ); |
| | 43 | |
| | 44 | // Inherit height value from height field. |
| | 45 | var heightField = this.getDialog().getContentElement( 'info', 'txtHeight'); |
| | 46 | var heightFieldValue = heightField.getValue(); |
| | 47 | if ( heightFieldValue ) |
| | 48 | { |
| | 49 | heightFieldValue = 'height: ' + heightFieldValue + 'px;'; |
| | 50 | if ( value ) |
| | 51 | value = value.replace( /\s*height: .+?;\s*/, '' ); |
| | 52 | value = value ? value + '; ' + heightFieldValue : heightFieldValue; |
| | 53 | } |
| | 54 | else |
| | 55 | value = value.replace( /\s*height: .+?;\s*/, '' ); |
| | 56 | |
| | 57 | if ( value ) |
| | 58 | value = value.replace( /\s*;\s*;\s*/g, ';' ); |
| | 59 | } |
| | 60 | |
| | 61 | if ( value !== undefined ) |
| | 62 | this.setValue( value ) |
| | 63 | }; |
| | 64 | |
| | 65 | var commitAdvParams = function( data, element ) |
| | 66 | { |
| | 67 | element = data instanceof CKEDITOR.dom.element ? data : element; |
| | 68 | |
| | 69 | var value = this.getValue(); |
| | 70 | if ( value ) |
| | 71 | { |
| | 72 | var attrName = advParamsMap[ this.id ]; |
| | 73 | |
| | 74 | // Compatibility with showborders plugin. |
| | 75 | if ( attrName == 'style' ) |
| | 76 | { |
| | 77 | this._elementStyleRead = 0; |
| | 78 | } |
| | 79 | else if ( attrName == 'class' && this._.dialog._.editor.plugins.showborders ) |
| | 80 | value += ' cke_show_border'; |
| | 81 | |
| | 82 | if ( element ) |
| | 83 | element.setAttribute( attrName, value ); |
| | 84 | } |
| | 85 | else |
| | 86 | element.removeAttribute( attrName, value ); |
| | 87 | }; |
| | 88 | |
| | 89 | CKEDITOR.plugins.add( 'dialogadvtab', |
| | 90 | { |
| | 91 | // Synchronous field values to other impacted fields is required, e.g. div styles |
| | 92 | // change should also alter inline-style text. |
| | 93 | commitInternally: function( targetFields ) |
| | 94 | { |
| | 95 | var dialog = this.getDialog(), |
| | 96 | element = dialog._.selectedElement; |
| | 97 | |
| | 98 | // Commit this field and broadcast to target fields. |
| | 99 | // this.commit( element, true ); |
| | 100 | |
| | 101 | targetFields = [].concat( targetFields ); |
| | 102 | var length = targetFields.length, field; |
| | 103 | for ( var i = 0; i < length; i++ ) |
| | 104 | { |
| | 105 | field = dialog.getContentElement.apply( dialog, targetFields[ i ].split( ':' ) ); |
| | 106 | field && field.setup && field.setup( element, true ); |
| | 107 | } |
| | 108 | }, |
| | 109 | |
| | 110 | /** |
| | 111 | * |
| | 112 | * @param tabConfig |
| | 113 | * id, dir, classes, styles |
| | 114 | */ |
| | 115 | createAdvancedTab : function( editor, tabConfig ) |
| | 116 | { |
| | 117 | if ( tabConfig === undefined ) |
| | 118 | { |
| | 119 | tabConfig = { id:1, dir:1, classes:1, styles:1 }; |
| | 120 | } |
| | 121 | |
| | 122 | var lang = editor.lang.common; |
| | 123 | |
| | 124 | var result = |
| | 125 | { |
| | 126 | id : 'advanced', |
| | 127 | label : lang.advanced, |
| | 128 | title : lang.advanced, |
| | 129 | elements : |
| | 130 | [ |
| | 131 | { |
| | 132 | type : 'vbox', |
| | 133 | padding : 1, |
| | 134 | children : [] |
| | 135 | } |
| | 136 | ] |
| | 137 | }; |
| | 138 | |
| | 139 | var content = []; |
| | 140 | |
| | 141 | if ( tabConfig.id || tabConfig.dir ) |
| | 142 | { |
| | 143 | if ( tabConfig.id ) |
| | 144 | { |
| | 145 | content.push( |
| | 146 | { |
| | 147 | type : 'text', |
| | 148 | id : 'advId', |
| | 149 | label : lang.id, |
| | 150 | setup : setupAdvParams, |
| | 151 | commit : commitAdvParams |
| | 152 | }); |
| | 153 | } |
| | 154 | |
| | 155 | if ( tabConfig.dir ) |
| | 156 | { |
| | 157 | content.push( |
| | 158 | { |
| | 159 | type : 'select', |
| | 160 | id : 'advLangDir', |
| | 161 | label : lang.langDir, |
| | 162 | 'default' : '', |
| | 163 | style : 'width:110px', |
| | 164 | items : |
| | 165 | [ |
| | 166 | [ lang.notSet, '' ], |
| | 167 | [ lang.langDirLTR, 'ltr' ], |
| | 168 | [ lang.langDirRTL, 'rtl' ] |
| | 169 | ], |
| | 170 | setup : setupAdvParams, |
| | 171 | commit : commitAdvParams |
| | 172 | }); |
| | 173 | } |
| | 174 | |
| | 175 | result.elements[ 0 ].children.push( |
| | 176 | { |
| | 177 | type : 'hbox', |
| | 178 | widths : [ '50%', '50%' ], |
| | 179 | children : [].concat( content ) |
| | 180 | }); |
| | 181 | } |
| | 182 | |
| | 183 | if ( tabConfig.styles || tabConfig.classes ) |
| | 184 | { |
| | 185 | content = []; |
| | 186 | |
| | 187 | if ( tabConfig.id ) |
| | 188 | { |
| | 189 | content.push( |
| | 190 | { |
| | 191 | type : 'text', |
| | 192 | label : lang.styles, |
| | 193 | 'default' : '', |
| | 194 | id : 'advStyles', |
| | 195 | onChange : function() |
| | 196 | { |
| | 197 | // Transaction prevents from recursive calls to onChange() of this field. |
| | 198 | if ( this._duringTransaction ) |
| | 199 | return; |
| | 200 | |
| | 201 | this._duringTransaction = 1; |
| | 202 | |
| | 203 | var value = this.getValue(), |
| | 204 | dialog = this.getDialog(), |
| | 205 | match; |
| | 206 | |
| | 207 | // Synchronize width value. |
| | 208 | match = value.match( /width\s*:\s*(\d+)(px|%)/ ); |
| | 209 | if ( match ) |
| | 210 | { |
| | 211 | dialog.getContentElement( 'info', 'txtWidth' ).setValue( match[ 1 ] ); |
| | 212 | dialog.getContentElement( 'info', 'cmbWidthType' ).setValue( |
| | 213 | match[ 2 ] == 'px' ? 'pixels' : 'percents' |
| | 214 | ); |
| | 215 | } |
| | 216 | |
| | 217 | // Synchronize height value. |
| | 218 | match = value.match( /height\s*:\s*(\d+)(px|%)/ ); |
| | 219 | if ( match ) |
| | 220 | { |
| | 221 | dialog.getContentElement( 'info', 'txtHeight' ).setValue( match[ 1 ] ); |
| | 222 | } |
| | 223 | |
| | 224 | this._duringTransaction = 0; |
| | 225 | }, |
| | 226 | setup : setupAdvParams, |
| | 227 | commit : commitAdvParams |
| | 228 | |
| | 229 | }); |
| | 230 | } |
| | 231 | |
| | 232 | if ( tabConfig.classes ) |
| | 233 | { |
| | 234 | content.push( |
| | 235 | { |
| | 236 | type : 'hbox', |
| | 237 | widths : [ '45%', '55%' ], |
| | 238 | children : |
| | 239 | [ |
| | 240 | { |
| | 241 | type : 'text', |
| | 242 | label : lang.cssClasses, |
| | 243 | 'default' : '', |
| | 244 | id : 'advCSSClasses', |
| | 245 | setup : setupAdvParams, |
| | 246 | commit : commitAdvParams |
| | 247 | |
| | 248 | } |
| | 249 | ] |
| | 250 | }); |
| | 251 | } |
| | 252 | |
| | 253 | result.elements[ 0 ].children.push( |
| | 254 | { |
| | 255 | type : 'hbox', |
| | 256 | widths : [ '50%', '50%' ], |
| | 257 | children : [].concat( content ) |
| | 258 | }); |
| | 259 | } |
| | 260 | |
| | 261 | return result; |
| | 262 | } |
| | 263 | }); |
| | 264 | |
| | 265 | })(); |