JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / plugins / link / plugin.js
diff --git a/_source/plugins/link/plugin.js b/_source/plugins/link/plugin.js
new file mode 100644 (file)
index 0000000..3681b8e
--- /dev/null
@@ -0,0 +1,188 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+CKEDITOR.plugins.add( 'link',\r
+{\r
+       init : function( editor )\r
+       {\r
+               // Add the link and unlink buttons.\r
+               editor.addCommand( 'link', new CKEDITOR.dialogCommand( 'link' ) );\r
+               editor.addCommand( 'anchor', new CKEDITOR.dialogCommand( 'anchor' ) );\r
+               editor.addCommand( 'unlink', new CKEDITOR.unlinkCommand() );\r
+               editor.ui.addButton( 'Link',\r
+                       {\r
+                               label : editor.lang.link.toolbar,\r
+                               command : 'link'\r
+                       } );\r
+               editor.ui.addButton( 'Unlink',\r
+                       {\r
+                               label : editor.lang.unlink,\r
+                               command : 'unlink'\r
+                       } );\r
+               editor.ui.addButton( 'Anchor',\r
+                       {\r
+                               label : editor.lang.anchor.toolbar,\r
+                               command : 'anchor'\r
+                       } );\r
+               CKEDITOR.dialog.add( 'link', this.path + 'dialogs/link.js' );\r
+               CKEDITOR.dialog.add( 'anchor', this.path + 'dialogs/anchor.js' );\r
+\r
+               // Add the CSS styles for anchor placeholders.\r
+               editor.addCss(\r
+                       'img.cke_anchor' +\r
+                       '{' +\r
+                               'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/anchor.gif' ) + ');' +\r
+                               'background-position: center center;' +\r
+                               'background-repeat: no-repeat;' +\r
+                               'border: 1px solid #a9a9a9;' +\r
+                               'width: 18px;' +\r
+                               'height: 18px;' +\r
+                       '}\n' +\r
+                       'a.cke_anchor' +\r
+                       '{' +\r
+                               'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/anchor.gif' ) + ');' +\r
+                               'background-position: 0 center;' +\r
+                               'background-repeat: no-repeat;' +\r
+                               'border: 1px solid #a9a9a9;' +\r
+                               'padding-left: 18px;' +\r
+                       '}'\r
+                       );\r
+\r
+               // Register selection change handler for the unlink button.\r
+                editor.on( 'selectionChange', function( evt )\r
+                       {\r
+                               /*\r
+                                * Despite our initial hope, document.queryCommandEnabled() does not work\r
+                                * for this in Firefox. So we must detect the state by element paths.\r
+                                */\r
+                               var command = editor.getCommand( 'unlink' ),\r
+                                       element = evt.data.path.lastElement.getAscendant( 'a', true );\r
+                               if ( element && element.getName() == 'a' && element.getAttribute( 'href' ) )\r
+                                       command.setState( CKEDITOR.TRISTATE_OFF );\r
+                               else\r
+                                       command.setState( CKEDITOR.TRISTATE_DISABLED );\r
+                       } );\r
+\r
+               // If the "menu" plugin is loaded, register the menu items.\r
+               if ( editor.addMenuItems )\r
+               {\r
+                       editor.addMenuItems(\r
+                               {\r
+                                       anchor :\r
+                                       {\r
+                                               label : editor.lang.anchor.menu,\r
+                                               command : 'anchor',\r
+                                               group : 'anchor'\r
+                                       },\r
+\r
+                                       link :\r
+                                       {\r
+                                               label : editor.lang.link.menu,\r
+                                               command : 'link',\r
+                                               group : 'link',\r
+                                               order : 1\r
+                                       },\r
+\r
+                                       unlink :\r
+                                       {\r
+                                               label : editor.lang.unlink,\r
+                                               command : 'unlink',\r
+                                               group : 'link',\r
+                                               order : 5\r
+                                       }\r
+                               });\r
+               }\r
+\r
+               // If the "contextmenu" plugin is loaded, register the listeners.\r
+               if ( editor.contextMenu )\r
+               {\r
+                       editor.contextMenu.addListener( function( element, selection )\r
+                               {\r
+                                       if ( !element )\r
+                                               return null;\r
+\r
+                                       var isAnchor = ( element.is( 'img' ) && element.getAttribute( '_cke_real_element_type' ) == 'anchor' );\r
+\r
+                                       if ( !isAnchor )\r
+                                       {\r
+                                               if ( !( element = element.getAscendant( 'a', true ) ) )\r
+                                                       return null;\r
+\r
+                                               isAnchor = ( element.getAttribute( 'name' ) && !element.getAttribute( 'href' ) );\r
+                                       }\r
+\r
+                                       return isAnchor ?\r
+                                                       { anchor : CKEDITOR.TRISTATE_OFF } :\r
+                                                       { link : CKEDITOR.TRISTATE_OFF, unlink : CKEDITOR.TRISTATE_OFF };\r
+                               });\r
+               }\r
+       },\r
+\r
+       afterInit : function( editor )\r
+       {\r
+               // Register a filter to displaying placeholders after mode change.\r
+\r
+               var dataProcessor = editor.dataProcessor,\r
+                       dataFilter = dataProcessor && dataProcessor.dataFilter;\r
+\r
+               if ( dataFilter )\r
+               {\r
+                       dataFilter.addRules(\r
+                               {\r
+                                       elements :\r
+                                       {\r
+                                               a : function( element )\r
+                                               {\r
+                                                       var attributes = element.attributes;\r
+                                                       if ( attributes.name && !attributes.href )\r
+                                                               return editor.createFakeParserElement( element, 'cke_anchor', 'anchor' );\r
+                                               }\r
+                                       }\r
+                               });\r
+               }\r
+       },\r
+\r
+       requires : [ 'fakeobjects' ]\r
+} );\r
+\r
+CKEDITOR.unlinkCommand = function(){};\r
+CKEDITOR.unlinkCommand.prototype =\r
+{\r
+       /** @ignore */\r
+       exec : function( editor )\r
+       {\r
+               /*\r
+                * execCommand( 'unlink', ... ) in Firefox leaves behind <span> tags at where\r
+                * the <a> was, so again we have to remove the link ourselves. (See #430)\r
+                *\r
+                * TODO: Use the style system when it's complete. Let's use execCommand()\r
+                * as a stopgap solution for now.\r
+                */\r
+               var selection = editor.getSelection(),\r
+                       bookmarks = selection.createBookmarks(),\r
+                       ranges = selection.getRanges(),\r
+                       rangeRoot,\r
+                       element;\r
+\r
+               for ( var i = 0 ; i < ranges.length ; i++ )\r
+               {\r
+                       rangeRoot = ranges[i].getCommonAncestor( true );\r
+                       element = rangeRoot.getAscendant( 'a', true );\r
+                       if ( !element )\r
+                               continue;\r
+                       ranges[i].selectNodeContents( element );\r
+               }\r
+\r
+               selection.selectRanges( ranges );\r
+               editor.document.$.execCommand( 'unlink', false, null );\r
+               selection.selectBookmarks( bookmarks );\r
+       }\r
+};\r
+\r
+CKEDITOR.tools.extend( CKEDITOR.config,\r
+{\r
+       linkShowAdvancedTab : true,\r
+       linkShowTargetTab : true\r
+} );\r