| 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 | |
| 6 | /** |
| 7 | * @fileOverview The "show border" plugin. The command display visible outline |
| 8 | * border line around all table elements if table doesn't have a none-zero 'border' attribute specified. |
| 9 | */ |
| 10 | |
| 11 | (function() |
| 12 | { |
| 13 | var showBorderClassName = 'cke_show_border', |
| 14 | cssStyleText, |
| 15 | cssTemplate = |
| 16 | // TODO: For IE6, we don't have child selector support, |
| 17 | // where nested table cells could be incorrect. |
| 18 | ( CKEDITOR.env.ie6Compat ? |
| 19 | [ |
| 20 | '.%1 table.%2,', |
| 21 | '.%1 table.%2 td, .%1 table.%2 th,', |
| 22 | '{', |
| 23 | 'border : #d3d3d3 1px dotted', |
| 24 | '}' |
| 25 | ] : |
| 26 | [ |
| 27 | '.%1 table.%2,', |
| 28 | '.%1 table.%2 > tr > td, .%1 table.%2 > tr > th,', |
| 29 | '.%1 table.%2 > tbody > tr > td, .%1 table.%2 > tbody > tr > th,', |
| 30 | '.%1 table.%2 > thead > tr > td, .%1 table.%2 > thead > tr > th,', |
| 31 | '.%1 table.%2 > tfoot > tr > td, .%1 table.%2 > tfoot > tr > th', |
| 32 | '{', |
| 33 | 'border : #d3d3d3 1px dotted', |
| 34 | '}' |
| 35 | ] ).join( '' ); |
| 36 | |
| 37 | cssStyleText = cssTemplate.replace( /%2/g, showBorderClassName ).replace( /%1/g, 'cke_show_borders ' ); |
| 38 | |
| 39 | var commandDefinition = |
| 40 | { |
| 41 | preserveState : true, |
| 42 | editorFocus : false, |
| 43 | |
| 44 | exec : function ( editor ) |
| 45 | { |
| 46 | this.toggleState(); |
| 47 | this.refresh( editor ); |
| 48 | }, |
| 49 | |
| 50 | refresh : function( editor ) |
| 51 | { |
| 52 | var funcName = ( this.state == CKEDITOR.TRISTATE_ON ) ? 'addClass' : 'removeClass'; |
| 53 | editor.document.getBody()[ funcName ]( 'cke_show_borders' ); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | CKEDITOR.plugins.add( 'showborders', |
| 58 | { |
| 59 | requires : [ 'wysiwygarea' ], |
| 60 | modes : { 'wysiwyg' : 1 }, |
| 61 | |
| 62 | init : function( editor ) |
| 63 | { |
| 64 | |
| 65 | var command = editor.addCommand( 'showborders', commandDefinition ); |
| 66 | command.canUndo = false; |
| 67 | |
| 68 | if ( editor.config.startupShowBorders != false ) |
| 69 | command.setState( CKEDITOR.TRISTATE_ON ); |
| 70 | |
| 71 | editor.addCss( cssStyleText ); |
| 72 | |
| 73 | // Refresh the command on setData. |
| 74 | editor.on( 'mode', function() |
| 75 | { |
| 76 | if ( command.state != CKEDITOR.TRISTATE_DISABLED ) |
| 77 | command.refresh( editor ); |
| 78 | }, null, null, 100 ); |
| 79 | |
| 80 | // Refresh the command on wysiwyg frame reloads. |
| 81 | editor.on( 'contentDom', function() |
| 82 | { |
| 83 | if ( command.state != CKEDITOR.TRISTATE_DISABLED ) |
| 84 | command.refresh( editor ); |
| 85 | }); |
| 86 | }, |
| 87 | |
| 88 | afterInit : function( editor ) |
| 89 | { |
| 90 | var dataProcessor = editor.dataProcessor, |
| 91 | dataFilter = dataProcessor && dataProcessor.dataFilter, |
| 92 | htmlFilter = dataProcessor && dataProcessor.htmlFilter; |
| 93 | |
| 94 | if ( dataFilter ) |
| 95 | { |
| 96 | dataFilter.addRules( |
| 97 | { |
| 98 | elements : |
| 99 | { |
| 100 | 'table' : function( element ) |
| 101 | { |
| 102 | var attributes = element.attributes, |
| 103 | cssClass = attributes[ 'class' ], |
| 104 | border = parseInt( attributes.border ); |
| 105 | |
| 106 | if ( !border || border <= 0 ) |
| 107 | attributes[ 'class' ] = ( cssClass || '' ) + ' ' + showBorderClassName; |
| 108 | } |
| 109 | } |
| 110 | } ); |
| 111 | } |
| 112 | |
| 113 | if( htmlFilter ) |
| 114 | { |
| 115 | htmlFilter.addRules( |
| 116 | { |
| 117 | elements : |
| 118 | { |
| 119 | 'table' : function( table ) |
| 120 | { |
| 121 | var attributes = table.attributes, |
| 122 | cssClass = attributes[ 'class' ]; |
| 123 | |
| 124 | cssClass && ( attributes[ 'class' ] = |
| 125 | cssClass.replace( showBorderClassName, '' ) |
| 126 | .replace( /\s{2}/, ' ' ) |
| 127 | .replace( /^\s+|\s+$/, '' ) ); |
| 128 | } |
| 129 | } |
| 130 | } ); |
| 131 | } |
| 132 | |
| 133 | // Table dialog must be aware of it. |
| 134 | CKEDITOR.on( 'dialogDefinition', function( ev ) |
| 135 | { |
| 136 | if( ev.editor != editor ) |
| 137 | return; |
| 138 | |
| 139 | var dialogName = ev.data.name; |
| 140 | |
| 141 | if ( dialogName == 'table' || dialogName == 'tableProperties' ) |
| 142 | { |
| 143 | var dialogDefinition = ev.data.definition, |
| 144 | infoTab = dialogDefinition.getContents( 'info' ), |
| 145 | borderField = infoTab.get( 'txtBorder' ), |
| 146 | originalCommit = borderField.commit; |
| 147 | |
| 148 | borderField.commit = CKEDITOR.tools.override( originalCommit, function( org ) |
| 149 | { |
| 150 | return function( data, selectedTable ) |
| 151 | { |
| 152 | org.apply( this, arguments ); |
| 153 | var value = parseInt( this.getValue() ); |
| 154 | selectedTable[ ( !value || value <= 0 ) ? 'addClass' : 'removeClass' ]( showBorderClassName ); |
| 155 | } |
| 156 | } ); |
| 157 | } |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | }); |
| 162 | } )(); |
| 163 | |
| 164 | /** |
| 165 | * Whether to automatically enable the "show borders" command when the editor loads. |
| 166 | * @type Boolean |
| 167 | * @default true |
| 168 | * @example |
| 169 | * config.startupShowBorders = false; |
| 170 | */ |
| 171 | |