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 CKEDITOR.dialog.add( 'anchor', function( editor )
  7 {
  8 	// Function called in onShow to load selected element.
  9 	var loadElements = function( editor, selection, ranges, element )
 10 	{
 11 		this.saveSelection();
 12 		this.editMode = true;
 13 		this.editObj = element;
 14
 15 		var attributeValue = this.editObj.getAttribute( 'name' );
 16 		if ( attributeValue == null )
 17 			this.setValueOf( 'info','txtName', "" );
 18 		else
 19 			this.setValueOf( 'info','txtName', attributeValue );
 20 	};
 21
 22 	return {
 23 		title : editor.lang.anchor.title,
 24 		minWidth : 350,
 25 		minHeight : 150,
 26 		onOk : function()
 27 		{
 28 			// Always create a new anchor, because of IE BUG.
 29 			var name = this.getValueOf( 'info', 'txtName' ),
 30 				element = CKEDITOR.env.ie ?
 31 				editor.document.createElement( '<a name="' + CKEDITOR.tools.htmlEncode( name ) + '">' ) :
 32 				editor.document.createElement( 'a' );
 33
 34 			// Move contents and attributes of old anchor to new anchor.
 35 			if ( this.editMode )
 36 			{
 37 				this.editObj.copyAttributes( element, { name : 1 } );
 38 				this.editObj.moveChildren( element );
 39 			}
 40
 41 			// Set name.
 42 			element.setAttribute( 'name', name );
 43
 44 			// Insert a new anchor.
 45 			var fakeElement = editor.createFakeElement( element, 'cke_anchor', 'anchor' );
 46 			if ( !this.editMode )
 47 			{
 48 				// It doesn't work with IE.
 49 				this.restoreSelection();
 50 				this.clearSavedSelection();
 51
 52 				editor.insertElement( fakeElement );
 53 			}
 54 			else
 55 				fakeElement.replace( this.fakeObj );
 56 			return true;
 57 		},
 58 		onShow : function()
 59 		{
 60 			this.editObj = false;
 61 			this.fakeObj = false;
 62 			this.editMode = false;
 63
 64 			// IE BUG: Selection must be in the editor for getSelection() to work.
 65 			this.restoreSelection();
 66
 67 			var editor = this.getParentEditor(),
 68 				selection = editor.getSelection(),
 69 				ranges = selection.getRanges();
 70
 71 			if ( ranges.length == 1 )
 72 			{
 73 				ranges[0].enlarge( CKEDITOR.ENLARGE_ELEMENT );
 74 				rangeRoot = ranges[0].getCommonAncestor( true );
 75 				var element = rangeRoot.getAscendant( 'img', true );
 76 				if ( element && element.getAttribute( '_cke_real_element_type' ) && element.getAttribute( '_cke_real_element_type' ) == 'anchor' )
 77 				{
 78 					this.fakeObj = element;
 79 					element = editor.restoreRealElement( this.fakeObj );
 80 					loadElements.apply( this, [ editor, selection, ranges, element ] );
 81 					selection.selectElement( this.fakeObj );
 82 					this.saveSelection();
 83 				}
 84 			}
 85 			this.getContentElement( 'info', 'txtName' ).focus();
 86 		},
 87 		contents : [
 88 			{
 89 				id : 'info',
 90 				label : editor.lang.anchor.title,
 91 				accessKey : 'I',
 92 				elements :
 93 				[
 94 					{
 95 						type : 'text',
 96 						id : 'txtName',
 97 						label : editor.lang.anchor.name,
 98 						validate : function()
 99 						{
100 							if ( this.getValue() == '' )
101 							{
102 								alert( editor.lang.anchor.errorName );
103 								return false;
104 							}
105 							return true;
106 						}
107 					},
108 				]
109 			}
110 		]
111 	};
112 } );
113