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 "sourcearea" plugin. It registers the "source" editing
  8  *		mode, which displays the raw data being edited in the editor.
  9  */
 10
 11 CKEDITOR.plugins.add( 'sourcearea',
 12 {
 13 	requires : [ 'editingblock' ],
 14
 15 	init : function( editor )
 16 	{
 17 		var sourcearea = CKEDITOR.plugins.sourcearea;
 18
 19 		editor.on( 'editingBlockReady', function()
 20 			{
 21 				var textarea;
 22
 23 				editor.addMode( 'source',
 24 					{
 25 						load : function( holderElement, data )
 26 						{
 27 							// Create the source area <textarea>.
 28 							textarea = new CKEDITOR.dom.element( 'textarea' );
 29 							textarea.setAttributes(
 30 								{
 31 									dir : 'ltr',
 32 									tabIndex : -1
 33 								});
 34 							textarea.addClass( 'cke_source' );
 35 							textarea.setStyles({
 36 								width	: '100%',
 37 								height	: '100%',
 38 								resize	: 'none',
 39 								outline	: 'none',
 40 								'text-align' : 'left' });
 41
 42 							// The textarea height/width='100%' doesn't
 43 							// constraint to the 'td' in IE strick mode
 44 							if ( CKEDITOR.env.ie )
 45 							{
 46 								textarea.setStyles({
 47 									height : holderElement.$.clientHeight + 'px',
 48 									width : holderElement.$.clientWidth + 'px' });
 49 							}
 50
 51 							// By some yet unknown reason, we must stop the
 52 							// mousedown propagation for the textarea,
 53 							// otherwise it's not possible to place the caret
 54 							// inside of it (non IE).
 55 							if ( !CKEDITOR.env.ie )
 56 							{
 57 								textarea.on( 'mousedown', function( evt )
 58 									{
 59 										evt = evt.data.$;
 60 										if ( evt.stopPropagation )
 61 											evt.stopPropagation();
 62 									} );
 63 							}
 64
 65 							// Reset the holder element and append the
 66 							// <textarea> to it.
 67 							holderElement.setHtml( '' );
 68 							holderElement.append( textarea );
 69
 70 							// The editor data "may be dirty" after this point.
 71 							editor.mayBeDirty = true;
 72
 73 							// Set the <textarea> value.
 74 							this.loadData( data );
 75
 76 							var keystrokeHandler = editor.keystrokeHandler;
 77 							if ( keystrokeHandler )
 78 								keystrokeHandler.attach( textarea );
 79
 80 							editor.mode = 'source';
 81 							editor.fire( 'mode' );
 82 						},
 83
 84 						loadData : function( data )
 85 						{
 86 							textarea.setValue( data );
 87 						},
 88
 89 						getData : function()
 90 						{
 91 							return textarea.getValue();
 92 						},
 93
 94 						getSnapshotData : function()
 95 						{
 96 							return textarea.getValue();
 97 						},
 98
 99 						unload : function( holderElement )
100 						{
101 							textarea = null;
102 						},
103
104 						focus : function()
105 						{
106 							textarea.focus();
107 						}
108 					});
109 			});
110
111 		editor.addCommand( 'source', sourcearea.commands.source );
112
113 		if ( editor.ui.addButton )
114 		{
115 			editor.ui.addButton( 'Source',
116 				{
117 					label : editor.lang.source,
118 					command : 'source'
119 				});
120 		}
121
122 		editor.on( 'mode', function()
123 			{
124 				editor.getCommand( 'source' ).setState(
125 					editor.mode == 'source' ?
126 						CKEDITOR.TRISTATE_ON :
127 						CKEDITOR.TRISTATE_OFF );
128 			});
129 	}
130 });
131
132 /**
133  * Holds the definition of commands an UI elements included with the sourcearea
134  * plugin.
135  * @example
136  */
137 CKEDITOR.plugins.sourcearea =
138 {
139 	commands :
140 	{
141 		source :
142 		{
143 			modes : { wysiwyg:1, source:1 },
144
145 			exec : function( editor )
146 			{
147 				editor.getCommand( 'source' ).setState( CKEDITOR.TRISTATE_DISABLED );
148 				editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );
149 			}
150 		}
151 	}
152 };
153