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