2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
6 CKEDITOR
.plugins
.add( 'link',
8 init : function( editor
)
10 // Add the link and unlink buttons.
11 editor
.addCommand( 'link', new CKEDITOR
.dialogCommand( 'link' ) );
12 editor
.addCommand( 'anchor', new CKEDITOR
.dialogCommand( 'anchor' ) );
13 editor
.addCommand( 'unlink', new CKEDITOR
.unlinkCommand() );
14 editor
.ui
.addButton( 'Link',
16 label
: editor
.lang
.link
.toolbar
,
19 editor
.ui
.addButton( 'Unlink',
21 label
: editor
.lang
.unlink
,
24 editor
.ui
.addButton( 'Anchor',
26 label
: editor
.lang
.anchor
.toolbar
,
29 CKEDITOR
.dialog
.add( 'link', this.path
+ 'dialogs/link.js' );
30 CKEDITOR
.dialog
.add( 'anchor', this.path
+ 'dialogs/anchor.js' );
32 // Add the CSS styles for anchor placeholders.
36 'background-image: url(' + CKEDITOR
.getUrl( this.path
+ 'images/anchor.gif' ) + ');' +
37 'background-position: center center;' +
38 'background-repeat: no-repeat;' +
39 'border: 1px solid #a9a9a9;' +
45 'background-image: url(' + CKEDITOR
.getUrl( this.path
+ 'images/anchor.gif' ) + ');' +
46 'background-position: 0 center;' +
47 'background-repeat: no-repeat;' +
48 'border: 1px solid #a9a9a9;' +
49 'padding-left: 18px;' +
53 // Register selection change handler for the unlink button.
54 editor
.on( 'selectionChange', function( evt
)
57 * Despite our initial hope, document.queryCommandEnabled() does not work
58 * for this in Firefox. So we must detect the state by element paths.
60 var command
= editor
.getCommand( 'unlink' ),
61 element
= evt
.data
.path
.lastElement
.getAscendant( 'a', true );
62 if ( element
&& element
.getName() == 'a' && element
.getAttribute( 'href' ) )
63 command
.setState( CKEDITOR
.TRISTATE_OFF
);
65 command
.setState( CKEDITOR
.TRISTATE_DISABLED
);
68 // If the "menu" plugin is loaded, register the menu items.
69 if ( editor
.addMenuItems
)
75 label
: editor
.lang
.anchor
.menu
,
82 label
: editor
.lang
.link
.menu
,
90 label
: editor
.lang
.unlink
,
98 // If the "contextmenu" plugin is loaded, register the listeners.
99 if ( editor
.contextMenu
)
101 editor
.contextMenu
.addListener( function( element
, selection
)
106 var isAnchor
= ( element
.is( 'img' ) && element
.getAttribute( '_cke_real_element_type' ) == 'anchor' );
110 if ( !( element
= CKEDITOR
.plugins
.link
.getSelectedLink( editor
) ) )
113 isAnchor
= ( element
.getAttribute( 'name' ) && !element
.getAttribute( 'href' ) );
117 { anchor
: CKEDITOR
.TRISTATE_OFF
} :
118 { link
: CKEDITOR
.TRISTATE_OFF
, unlink
: CKEDITOR
.TRISTATE_OFF
};
123 afterInit : function( editor
)
125 // Register a filter to displaying placeholders after mode change.
127 var dataProcessor
= editor
.dataProcessor
,
128 dataFilter
= dataProcessor
&& dataProcessor
.dataFilter
;
136 a : function( element
)
138 var attributes
= element
.attributes
;
139 if ( attributes
.name
&& !attributes
.href
)
140 return editor
.createFakeParserElement( element
, 'cke_anchor', 'anchor' );
147 requires
: [ 'fakeobjects' ]
150 CKEDITOR
.plugins
.link
=
153 * Get the surrounding link element of current selection.
155 * @example CKEDITOR.plugins.link.getSelectedLink( editor );
157 * The following selection will all return the link element.
159 * <a href="#">li^nk</a>
160 * <a href="#">[link]</a>
161 * text[<a href="#">link]</a>
162 * <a href="#">li[nk</a>]
163 * [<b><a href="#">li]nk</a></b>]
164 * [<a href="#"><b>li]nk</b></a>
167 getSelectedLink : function( editor
)
170 try { range
= editor
.getSelection().getRanges()[ 0 ]; }
171 catch( e
) { return null; }
173 range
.shrink( CKEDITOR
.SHRINK_TEXT
);
174 var root
= range
.getCommonAncestor();
175 return root
.getAscendant( 'a', true );
179 CKEDITOR
.unlinkCommand = function(){};
180 CKEDITOR
.unlinkCommand
.prototype =
183 exec : function( editor
)
186 * execCommand( 'unlink', ... ) in Firefox leaves behind <span> tags at where
187 * the <a> was, so again we have to remove the link ourselves. (See #430)
189 * TODO: Use the style system when it's complete. Let's use execCommand()
190 * as a stopgap solution for now.
192 var selection
= editor
.getSelection(),
193 bookmarks
= selection
.createBookmarks(),
194 ranges
= selection
.getRanges(),
198 for ( var i
= 0 ; i
< ranges
.length
; i
++ )
200 rangeRoot
= ranges
[i
].getCommonAncestor( true );
201 element
= rangeRoot
.getAscendant( 'a', true );
204 ranges
[i
].selectNodeContents( element
);
207 selection
.selectRanges( ranges
);
208 editor
.document
.$.execCommand( 'unlink', false, null );
209 selection
.selectBookmarks( bookmarks
);
215 CKEDITOR
.tools
.extend( CKEDITOR
.config
,
217 linkShowAdvancedTab
: true,
218 linkShowTargetTab
: true