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