Update ckeditor to version 3.2.1
[gopost.git] / ckeditor / _source / plugins / link / plugin.js
blob26a8cb3be2ce39f9bd1c9b6e9242b785c87f3f41
1 /*
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
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,
17 command : 'link'
18 } );
19 editor.ui.addButton( 'Unlink',
21 label : editor.lang.unlink,
22 command : 'unlink'
23 } );
24 editor.ui.addButton( 'Anchor',
26 label : editor.lang.anchor.toolbar,
27 command : 'anchor'
28 } );
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.
33 editor.addCss(
34 'img.cke_anchor' +
35 '{' +
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;' +
40 'width: 18px;' +
41 'height: 18px;' +
42 '}\n' +
43 'a.cke_anchor' +
44 '{' +
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;' +
50 '}'
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 );
64 else
65 command.setState( CKEDITOR.TRISTATE_DISABLED );
66 } );
68 // If the "menu" plugin is loaded, register the menu items.
69 if ( editor.addMenuItems )
71 editor.addMenuItems(
73 anchor :
75 label : editor.lang.anchor.menu,
76 command : 'anchor',
77 group : 'anchor'
80 link :
82 label : editor.lang.link.menu,
83 command : 'link',
84 group : 'link',
85 order : 1
88 unlink :
90 label : editor.lang.unlink,
91 command : 'unlink',
92 group : 'link',
93 order : 5
95 });
98 // If the "contextmenu" plugin is loaded, register the listeners.
99 if ( editor.contextMenu )
101 editor.contextMenu.addListener( function( element, selection )
103 if ( !element )
104 return null;
106 var isAnchor = ( element.is( 'img' ) && element.getAttribute( '_cke_real_element_type' ) == 'anchor' );
108 if ( !isAnchor )
110 if ( !( element = CKEDITOR.plugins.link.getSelectedLink( editor ) ) )
111 return null;
113 isAnchor = ( element.getAttribute( 'name' ) && !element.getAttribute( 'href' ) );
116 return isAnchor ?
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;
130 if ( dataFilter )
132 dataFilter.addRules(
134 elements :
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' ]
148 } );
150 CKEDITOR.plugins.link =
153 * Get the surrounding link element of current selection.
154 * @param editor
155 * @example CKEDITOR.plugins.link.getSelectedLink( editor );
156 * @since 3.2.1
157 * The following selection will all return the link element.
158 * <pre>
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>
165 * </pre>
167 getSelectedLink : function( editor )
169 var range;
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 =
182 /** @ignore */
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(),
195 rangeRoot,
196 element;
198 for ( var i = 0 ; i < ranges.length ; i++ )
200 rangeRoot = ranges[i].getCommonAncestor( true );
201 element = rangeRoot.getAscendant( 'a', true );
202 if ( !element )
203 continue;
204 ranges[i].selectNodeContents( element );
207 selection.selectRanges( ranges );
208 editor.document.$.execCommand( 'unlink', false, null );
209 selection.selectBookmarks( bookmarks );
212 startDisabled : true
215 CKEDITOR.tools.extend( CKEDITOR.config,
217 linkShowAdvancedTab : true,
218 linkShowTargetTab : true
219 } );