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