JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
3748e36c400489e655880c5dc7ac2415a75c1a0a
[ckeditor.git] / _source / plugins / link / dialogs / anchor.js
1 /*\r
2 Copyright (c) 2003-2011, 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.dialog.add( 'anchor', function( editor )\r
7 {\r
8         // Function called in onShow to load selected element.\r
9         var loadElements = function( editor, selection, element )\r
10         {\r
11                 this.editMode = true;\r
12                 this.editObj = element;\r
13 \r
14                 var attributeValue = this.editObj.getAttribute( 'name' );\r
15                 if ( attributeValue )\r
16                         this.setValueOf( 'info','txtName', attributeValue );\r
17                 else\r
18                         this.setValueOf( 'info','txtName', "" );\r
19         };\r
20 \r
21         return {\r
22                 title : editor.lang.anchor.title,\r
23                 minWidth : 300,\r
24                 minHeight : 60,\r
25                 onOk : function()\r
26                 {\r
27                         // Always create a new anchor, because of IE BUG.\r
28                         var name = this.getValueOf( 'info', 'txtName' ),\r
29                                 element = CKEDITOR.env.ie && !( CKEDITOR.document.$.documentMode >= 8 ) ?\r
30                                 editor.document.createElement( '<a name="' + CKEDITOR.tools.htmlEncode( name ) + '">' ) :\r
31                                 editor.document.createElement( 'a' );\r
32 \r
33                         // Move contents and attributes of old anchor to new anchor.\r
34                         if ( this.editMode )\r
35                         {\r
36                                 this.editObj.copyAttributes( element, { name : 1 } );\r
37                                 this.editObj.moveChildren( element );\r
38                         }\r
39 \r
40                         // Set name.\r
41                         element.data( 'cke-saved-name', false );\r
42                         element.setAttribute( 'name', name );\r
43 \r
44                         // Insert a new anchor.\r
45                         var fakeElement = editor.createFakeElement( element, 'cke_anchor', 'anchor' );\r
46                         if ( !this.editMode )\r
47                                 editor.insertElement( fakeElement );\r
48                         else\r
49                         {\r
50                                 fakeElement.replace( this.fakeObj );\r
51                                 editor.getSelection().selectElement( fakeElement );\r
52                         }\r
53 \r
54                         return true;\r
55                 },\r
56                 onShow : function()\r
57                 {\r
58                         this.editObj = false;\r
59                         this.fakeObj = false;\r
60                         this.editMode = false;\r
61 \r
62                         var selection = editor.getSelection();\r
63                         var element = selection.getSelectedElement();\r
64                         if ( element && element.data( 'cke-real-element-type' ) && element.data( 'cke-real-element-type' ) == 'anchor' )\r
65                         {\r
66                                 this.fakeObj = element;\r
67                                 element = editor.restoreRealElement( this.fakeObj );\r
68                                 loadElements.apply( this, [ editor, selection, element ] );\r
69                                 selection.selectElement( this.fakeObj );\r
70                         }\r
71                         this.getContentElement( 'info', 'txtName' ).focus();\r
72                 },\r
73                 contents : [\r
74                         {\r
75                                 id : 'info',\r
76                                 label : editor.lang.anchor.title,\r
77                                 accessKey : 'I',\r
78                                 elements :\r
79                                 [\r
80                                         {\r
81                                                 type : 'text',\r
82                                                 id : 'txtName',\r
83                                                 label : editor.lang.anchor.name,\r
84                                                 required: true,\r
85                                                 validate : function()\r
86                                                 {\r
87                                                         if ( !this.getValue() )\r
88                                                         {\r
89                                                                 alert( editor.lang.anchor.errorName );\r
90                                                                 return false;\r
91                                                         }\r
92                                                         return true;\r
93                                                 }\r
94                                         }\r
95                                 ]\r
96                         }\r
97                 ]\r
98         };\r
99 } );\r