2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.plugins.add( 'link',
\r
8 init : function( editor )
\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
16 label : editor.lang.link.toolbar,
\r
19 editor.ui.addButton( 'Unlink',
\r
21 label : editor.lang.unlink,
\r
24 editor.ui.addButton( 'Anchor',
\r
26 label : editor.lang.anchor.toolbar,
\r
29 CKEDITOR.dialog.add( 'link', this.path + 'dialogs/link.js' );
\r
30 CKEDITOR.dialog.add( 'anchor', this.path + 'dialogs/anchor.js' );
\r
32 // Add the CSS styles for anchor placeholders.
\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 !important;' +
\r
41 'height: 18px !important;' +
\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
53 // Register selection change handler for the unlink button.
\r
54 editor.on( 'selectionChange', function( evt )
\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
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
65 command.setState( CKEDITOR.TRISTATE_DISABLED );
\r
68 editor.on( 'doubleclick', function( evt )
\r
70 var element = CKEDITOR.plugins.link.getSelectedLink( editor ) || evt.data.element;
\r
72 if ( element.is( 'a' ) )
\r
73 evt.data.dialog = ( element.getAttribute( 'name' ) && !element.getAttribute( 'href' ) ) ? 'anchor' : 'link';
\r
74 else if ( element.is( 'img' ) && element.getAttribute( '_cke_real_element_type' ) == 'anchor' )
\r
75 evt.data.dialog = 'anchor';
\r
78 // If the "menu" plugin is loaded, register the menu items.
\r
79 if ( editor.addMenuItems )
\r
81 editor.addMenuItems(
\r
85 label : editor.lang.anchor.menu,
\r
92 label : editor.lang.link.menu,
\r
100 label : editor.lang.unlink,
\r
101 command : 'unlink',
\r
108 // If the "contextmenu" plugin is loaded, register the listeners.
\r
109 if ( editor.contextMenu )
\r
111 editor.contextMenu.addListener( function( element, selection )
\r
116 var isAnchor = ( element.is( 'img' ) && element.getAttribute( '_cke_real_element_type' ) == 'anchor' );
\r
120 if ( !( element = CKEDITOR.plugins.link.getSelectedLink( editor ) ) )
\r
123 isAnchor = ( element.getAttribute( 'name' ) && !element.getAttribute( 'href' ) );
\r
127 { anchor : CKEDITOR.TRISTATE_OFF } :
\r
128 { link : CKEDITOR.TRISTATE_OFF, unlink : CKEDITOR.TRISTATE_OFF };
\r
133 afterInit : function( editor )
\r
135 // Register a filter to displaying placeholders after mode change.
\r
137 var dataProcessor = editor.dataProcessor,
\r
138 dataFilter = dataProcessor && dataProcessor.dataFilter;
\r
142 dataFilter.addRules(
\r
146 a : function( element )
\r
148 var attributes = element.attributes;
\r
149 if ( attributes.name && !attributes.href )
\r
150 return editor.createFakeParserElement( element, 'cke_anchor', 'anchor' );
\r
157 requires : [ 'fakeobjects' ]
\r
160 CKEDITOR.plugins.link =
\r
163 * Get the surrounding link element of current selection.
\r
165 * @example CKEDITOR.plugins.link.getSelectedLink( editor );
\r
167 * The following selection will all return the link element.
\r
169 * <a href="#">li^nk</a>
\r
170 * <a href="#">[link]</a>
\r
171 * text[<a href="#">link]</a>
\r
172 * <a href="#">li[nk</a>]
\r
173 * [<b><a href="#">li]nk</a></b>]
\r
174 * [<a href="#"><b>li]nk</b></a>
\r
177 getSelectedLink : function( editor )
\r
180 try { range = editor.getSelection().getRanges()[ 0 ]; }
\r
181 catch( e ) { return null; }
\r
183 range.shrink( CKEDITOR.SHRINK_TEXT );
\r
184 var root = range.getCommonAncestor();
\r
185 return root.getAscendant( 'a', true );
\r
189 CKEDITOR.unlinkCommand = function(){};
\r
190 CKEDITOR.unlinkCommand.prototype =
\r
193 exec : function( editor )
\r
196 * execCommand( 'unlink', ... ) in Firefox leaves behind <span> tags at where
\r
197 * the <a> was, so again we have to remove the link ourselves. (See #430)
\r
199 * TODO: Use the style system when it's complete. Let's use execCommand()
\r
200 * as a stopgap solution for now.
\r
202 var selection = editor.getSelection(),
\r
203 bookmarks = selection.createBookmarks(),
\r
204 ranges = selection.getRanges(),
\r
208 for ( var i = 0 ; i < ranges.length ; i++ )
\r
210 rangeRoot = ranges[i].getCommonAncestor( true );
\r
211 element = rangeRoot.getAscendant( 'a', true );
\r
214 ranges[i].selectNodeContents( element );
\r
217 selection.selectRanges( ranges );
\r
218 editor.document.$.execCommand( 'unlink', false, null );
\r
219 selection.selectBookmarks( bookmarks );
\r
222 startDisabled : true
\r
225 CKEDITOR.tools.extend( CKEDITOR.config,
\r
227 linkShowAdvancedTab : true,
\r
228 linkShowTargetTab : true
\r