1 /*
  2  * CKEditor - The text editor for Internet - http://ckeditor.com
  3  * Copyright (C) 2003-2008 Frederico Caldeira Knabben
  4  *
  5  * == BEGIN LICENSE ==
  6  *
  7  * Licensed under the terms of any of the following licenses at your
  8  * choice:
  9  *
 10  *  - GNU General Public License Version 2 or later (the "GPL")
 11  *    http://www.gnu.org/licenses/gpl.html
 12  *
 13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14  *    http://www.gnu.org/licenses/lgpl.html
 15  *
 16  *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17  *    http://www.mozilla.org/MPL/MPL-1.1.html
 18  *
 19  * == END LICENSE ==
 20  */
 21
 22 /**
 23  * @fileOverview The "elementspath" plugin. It shows all elements in the DOM
 24  *		parent tree relative to the current selection in the editing area.
 25  */
 26
 27 CKEDITOR.plugins.add( 'elementspath',
 28 {
 29 	init : function( editor, pluginPath )
 30 	{
 31 		var spaceId = 'cke_path_' + editor.name;
 32 		var spaceElement;
 33 		var getSpaceElement = function()
 34 		{
 35 			if ( !spaceElement )
 36 				spaceElement = CKEDITOR.document.getById( spaceId );
 37 			return spaceElement;
 38 		};
 39
 40 		editor.on( 'themeSpace', function( event )
 41 			{
 42 				if ( event.data.space == 'bottom' )
 43 					event.data.html += '<div id="' + spaceId + '" class="cke_path"><br></div>';
 44 			});
 45
 46 		editor.on( 'selectionChange', function()
 47 			{
 48 				var element = this.getSelection().getStartElement(),
 49 					html = [],
 50 					elementsList = this._.elementsPathList = [];
 51
 52 				while ( element )
 53 				{
 54 					var index = elementsList.push( element ) - 1,
 55 						name = element.getName();
 56
 57 					html.unshift( '<a href="element:', name, '" onclick="return CKEDITOR._.elementsPathClick( \'', this.name, '\',', index, ');">', name, '</a>' );
 58
 59 					if ( name == 'body' )
 60 						break;
 61
 62 					element = element.getParent();
 63 				}
 64
 65 				getSpaceElement().setHtml( html.join('') );
 66 			});
 67
 68 		editor.on( 'contentDomUnload', function()
 69 			{
 70 				getSpaceElement().setHtml( '<br>' );
 71 			});
 72 	}
 73 });
 74
 75 /**
 76  * Handles the click on an element in the element path.
 77  * @private
 78  */
 79 CKEDITOR._.elementsPathClick = function( instanceName, elementIndex )
 80 {
 81 	var editor = CKEDITOR.instances[ instanceName ];
 82 	editor.focus();
 83
 84 	var element = editor._.elementsPathList[ elementIndex ];
 85 	editor.getSelection().selectElement( element );
 86
 87 	return false;
 88 };
 89