JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / plugins / link / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, 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.plugins.add( 'link',\r
7 {\r
8         init : function( editor )\r
9         {\r
10                 // Add the link and unlink buttons.\r
11                 editor.addCommand( 'link', new CKEDITOR.dialogCommand( 'link' ) );\r
12                 editor.addCommand( 'anchor', new CKEDITOR.dialogCommand( 'anchor' ) );\r
13                 editor.addCommand( 'unlink', new CKEDITOR.unlinkCommand() );\r
14                 editor.ui.addButton( 'Link',\r
15                         {\r
16                                 label : editor.lang.link.toolbar,\r
17                                 command : 'link'\r
18                         } );\r
19                 editor.ui.addButton( 'Unlink',\r
20                         {\r
21                                 label : editor.lang.unlink,\r
22                                 command : 'unlink'\r
23                         } );\r
24                 editor.ui.addButton( 'Anchor',\r
25                         {\r
26                                 label : editor.lang.anchor.toolbar,\r
27                                 command : 'anchor'\r
28                         } );\r
29                 CKEDITOR.dialog.add( 'link', this.path + 'dialogs/link.js' );\r
30                 CKEDITOR.dialog.add( 'anchor', this.path + 'dialogs/anchor.js' );\r
31 \r
32                 // Add the CSS styles for anchor placeholders.\r
33                 editor.addCss(\r
34                         'img.cke_anchor' +\r
35                         '{' +\r
36                                 'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/anchor.gif' ) + ');' +\r
37                                 'background-position: center center;' +\r
38                                 'background-repeat: no-repeat;' +\r
39                                 'border: 1px solid #a9a9a9;' +\r
40                                 'width: 18px;' +\r
41                                 'height: 18px;' +\r
42                         '}\n' +\r
43                         'a.cke_anchor' +\r
44                         '{' +\r
45                                 'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/anchor.gif' ) + ');' +\r
46                                 'background-position: 0 center;' +\r
47                                 'background-repeat: no-repeat;' +\r
48                                 'border: 1px solid #a9a9a9;' +\r
49                                 'padding-left: 18px;' +\r
50                         '}'\r
51                         );\r
52 \r
53                 // Register selection change handler for the unlink button.\r
54                  editor.on( 'selectionChange', function( evt )\r
55                         {\r
56                                 /*\r
57                                  * Despite our initial hope, document.queryCommandEnabled() does not work\r
58                                  * for this in Firefox. So we must detect the state by element paths.\r
59                                  */\r
60                                 var command = editor.getCommand( 'unlink' ),\r
61                                         element = evt.data.path.lastElement.getAscendant( 'a', true );\r
62                                 if ( element && element.getName() == 'a' && element.getAttribute( 'href' ) )\r
63                                         command.setState( CKEDITOR.TRISTATE_OFF );\r
64                                 else\r
65                                         command.setState( CKEDITOR.TRISTATE_DISABLED );\r
66                         } );\r
67 \r
68                 // If the "menu" plugin is loaded, register the menu items.\r
69                 if ( editor.addMenuItems )\r
70                 {\r
71                         editor.addMenuItems(\r
72                                 {\r
73                                         anchor :\r
74                                         {\r
75                                                 label : editor.lang.anchor.menu,\r
76                                                 command : 'anchor',\r
77                                                 group : 'anchor'\r
78                                         },\r
79 \r
80                                         link :\r
81                                         {\r
82                                                 label : editor.lang.link.menu,\r
83                                                 command : 'link',\r
84                                                 group : 'link',\r
85                                                 order : 1\r
86                                         },\r
87 \r
88                                         unlink :\r
89                                         {\r
90                                                 label : editor.lang.unlink,\r
91                                                 command : 'unlink',\r
92                                                 group : 'link',\r
93                                                 order : 5\r
94                                         }\r
95                                 });\r
96                 }\r
97 \r
98                 // If the "contextmenu" plugin is loaded, register the listeners.\r
99                 if ( editor.contextMenu )\r
100                 {\r
101                         editor.contextMenu.addListener( function( element, selection )\r
102                                 {\r
103                                         if ( !element )\r
104                                                 return null;\r
105 \r
106                                         var isAnchor = ( element.is( 'img' ) && element.getAttribute( '_cke_real_element_type' ) == 'anchor' );\r
107 \r
108                                         if ( !isAnchor )\r
109                                         {\r
110                                                 if ( !( element = element.getAscendant( 'a', true ) ) )\r
111                                                         return null;\r
112 \r
113                                                 isAnchor = ( element.getAttribute( 'name' ) && !element.getAttribute( 'href' ) );\r
114                                         }\r
115 \r
116                                         return isAnchor ?\r
117                                                         { anchor : CKEDITOR.TRISTATE_OFF } :\r
118                                                         { link : CKEDITOR.TRISTATE_OFF, unlink : CKEDITOR.TRISTATE_OFF };\r
119                                 });\r
120                 }\r
121         },\r
122 \r
123         afterInit : function( editor )\r
124         {\r
125                 // Register a filter to displaying placeholders after mode change.\r
126 \r
127                 var dataProcessor = editor.dataProcessor,\r
128                         dataFilter = dataProcessor && dataProcessor.dataFilter;\r
129 \r
130                 if ( dataFilter )\r
131                 {\r
132                         dataFilter.addRules(\r
133                                 {\r
134                                         elements :\r
135                                         {\r
136                                                 a : function( element )\r
137                                                 {\r
138                                                         var attributes = element.attributes;\r
139                                                         if ( attributes.name && !attributes.href )\r
140                                                                 return editor.createFakeParserElement( element, 'cke_anchor', 'anchor' );\r
141                                                 }\r
142                                         }\r
143                                 });\r
144                 }\r
145         },\r
146 \r
147         requires : [ 'fakeobjects' ]\r
148 } );\r
149 \r
150 CKEDITOR.unlinkCommand = function(){};\r
151 CKEDITOR.unlinkCommand.prototype =\r
152 {\r
153         /** @ignore */\r
154         exec : function( editor )\r
155         {\r
156                 /*\r
157                  * execCommand( 'unlink', ... ) in Firefox leaves behind <span> tags at where\r
158                  * the <a> was, so again we have to remove the link ourselves. (See #430)\r
159                  *\r
160                  * TODO: Use the style system when it's complete. Let's use execCommand()\r
161                  * as a stopgap solution for now.\r
162                  */\r
163                 var selection = editor.getSelection(),\r
164                         bookmarks = selection.createBookmarks(),\r
165                         ranges = selection.getRanges(),\r
166                         rangeRoot,\r
167                         element;\r
168 \r
169                 for ( var i = 0 ; i < ranges.length ; i++ )\r
170                 {\r
171                         rangeRoot = ranges[i].getCommonAncestor( true );\r
172                         element = rangeRoot.getAscendant( 'a', true );\r
173                         if ( !element )\r
174                                 continue;\r
175                         ranges[i].selectNodeContents( element );\r
176                 }\r
177 \r
178                 selection.selectRanges( ranges );\r
179                 editor.document.$.execCommand( 'unlink', false, null );\r
180                 selection.selectBookmarks( bookmarks );\r
181         }\r
182 };\r
183 \r
184 CKEDITOR.tools.extend( CKEDITOR.config,\r
185 {\r
186         linkShowAdvancedTab : true,\r
187         linkShowTargetTab : true\r
188 } );\r