1 /*
  2 Copyright (c) 2003-2012, 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( element )
 10 	{
 11 		this._.selectedElement = element;
 12 
 13 		var attributeValue = element.data( 'cke-saved-name' );
 14 		this.setValueOf( 'info','txtName', attributeValue || '' );
 15 	};
 16 
 17 	function createFakeAnchor( editor, anchor )
 18 	{
 19 		return editor.createFakeElement( anchor, 'cke_anchor', 'anchor' );
 20 	}
 21 
 22 	return {
 23 		title : editor.lang.anchor.title,
 24 		minWidth : 300,
 25 		minHeight : 60,
 26 		onOk : function()
 27 		{
 28 			var name = CKEDITOR.tools.trim( this.getValueOf( 'info', 'txtName' ) );
 29 			var attributes =
 30 			{
 31 				id : name,
 32 				name : name,
 33 				'data-cke-saved-name' : name
 34 			};
 35 
 36 			if ( this._.selectedElement )
 37 			{
 38 				if ( this._.selectedElement.data( 'cke-realelement' ) )
 39 				{
 40 					var newFake = createFakeAnchor( editor, editor.document.createElement( 'a', { attributes: attributes } ) );
 41 					newFake.replace( this._.selectedElement );
 42 				}
 43 				else
 44 					this._.selectedElement.setAttributes( attributes );
 45 			}
 46 			else
 47 			{
 48 				var sel = editor.getSelection(),
 49 						range = sel && sel.getRanges()[ 0 ];
 50 
 51 				// Empty anchor
 52 				if ( range.collapsed )
 53 				{
 54 					if ( CKEDITOR.plugins.link.synAnchorSelector )
 55 						attributes[ 'class' ] = 'cke_anchor_empty';
 56 
 57 					if ( CKEDITOR.plugins.link.emptyAnchorFix )
 58 					{
 59 						attributes[ 'contenteditable' ] = 'false';
 60 						attributes[ 'data-cke-editable' ] = 1;
 61 					}
 62 
 63 					var anchor = editor.document.createElement( 'a', { attributes: attributes } );
 64 
 65 					// Transform the anchor into a fake element for browsers that need it.
 66 					if ( CKEDITOR.plugins.link.fakeAnchor )
 67 						anchor = createFakeAnchor( editor, anchor );
 68 
 69 					range.insertNode( anchor );
 70 				}
 71 				else
 72 				{
 73 					if ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 )
 74 						attributes['class'] = 'cke_anchor';
 75 
 76 					// Apply style.
 77 					var style = new CKEDITOR.style( { element : 'a', attributes : attributes } );
 78 					style.type = CKEDITOR.STYLE_INLINE;
 79 					style.apply( editor.document );
 80 				}
 81 			}
 82 		},
 83 
 84 		onHide : function()
 85 		{
 86 			delete this._.selectedElement;
 87 		},
 88 
 89 		onShow : function()
 90 		{
 91 			var selection = editor.getSelection(),
 92 				fullySelected = selection.getSelectedElement(),
 93 				partialSelected;
 94 
 95 			// Detect the anchor under selection.
 96 			if ( fullySelected )
 97 			{
 98 				if ( CKEDITOR.plugins.link.fakeAnchor )
 99 				{
100 					var realElement = CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, fullySelected );
101 					realElement && loadElements.call( this, realElement );
102 					this._.selectedElement = fullySelected;
103 				}
104 				else if ( fullySelected.is( 'a' ) && fullySelected.hasAttribute( 'name' ) )
105 					loadElements.call( this, fullySelected );
106 			}
107 			else
108 			{
109 				partialSelected = CKEDITOR.plugins.link.getSelectedLink( editor );
110 				if ( partialSelected )
111 				{
112 					loadElements.call( this, partialSelected );
113 					selection.selectElement( partialSelected );
114 				}
115 			}
116 
117 			this.getContentElement( 'info', 'txtName' ).focus();
118 		},
119 		contents : [
120 			{
121 				id : 'info',
122 				label : editor.lang.anchor.title,
123 				accessKey : 'I',
124 				elements :
125 				[
126 					{
127 						type : 'text',
128 						id : 'txtName',
129 						label : editor.lang.anchor.name,
130 						required: true,
131 						validate : function()
132 						{
133 							if ( !this.getValue() )
134 							{
135 								alert( editor.lang.anchor.errorName );
136 								return false;
137 							}
138 							return true;
139 						}
140 					}
141 				]
142 			}
143 		]
144 	};
145 } );
146