JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
e84de254d5e5fa039792cb865f8d19ff98a2dfc3
[ckeditor.git] / _source / plugins / forms / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @file Forms Plugin\r
8  */\r
9 \r
10 CKEDITOR.plugins.add( 'forms',\r
11 {\r
12         init : function( editor )\r
13         {\r
14                 var lang = editor.lang;\r
15 \r
16                 editor.addCss(\r
17                         'form' +\r
18                         '{' +\r
19                                 'border: 1px dotted #FF0000;' +\r
20                                 'padding: 2px;' +\r
21                         '}' );\r
22 \r
23                 // All buttons use the same code to register. So, to avoid\r
24                 // duplications, let's use this tool function.\r
25                 var addButtonCommand = function( buttonName, commandName, dialogFile )\r
26                 {\r
27                         editor.addCommand( commandName, new CKEDITOR.dialogCommand( commandName ) );\r
28 \r
29                         editor.ui.addButton( buttonName,\r
30                                 {\r
31                                         label : lang.common[ buttonName.charAt(0).toLowerCase() + buttonName.slice(1) ],\r
32                                         command : commandName\r
33                                 });\r
34                         CKEDITOR.dialog.add( commandName, dialogFile );\r
35                 };\r
36 \r
37                 var dialogPath = this.path + 'dialogs/';\r
38                 addButtonCommand( 'Form',                       'form',                 dialogPath + 'form.js' );\r
39                 addButtonCommand( 'Checkbox',           'checkbox',             dialogPath + 'checkbox.js' );\r
40                 addButtonCommand( 'Radio',                      'radio',                dialogPath + 'radio.js' );\r
41                 addButtonCommand( 'TextField',          'textfield',    dialogPath + 'textfield.js' );\r
42                 addButtonCommand( 'Textarea',           'textarea',             dialogPath + 'textarea.js' );\r
43                 addButtonCommand( 'Select',                     'select',               dialogPath + 'select.js' );\r
44                 addButtonCommand( 'Button',                     'button',               dialogPath + 'button.js' );\r
45                 addButtonCommand( 'ImageButton',        'imagebutton',  CKEDITOR.plugins.getPath('image') + 'dialogs/image.js' );\r
46                 addButtonCommand( 'HiddenField',        'hiddenfield',  dialogPath + 'hiddenfield.js' );\r
47 \r
48                 // If the "menu" plugin is loaded, register the menu items.\r
49                 if ( editor.addMenuItems )\r
50                 {\r
51                         editor.addMenuItems(\r
52                                 {\r
53                                         form :\r
54                                         {\r
55                                                 label : lang.form.menu,\r
56                                                 command : 'form',\r
57                                                 group : 'form'\r
58                                         },\r
59 \r
60                                         checkbox :\r
61                                         {\r
62                                                 label : lang.checkboxAndRadio.checkboxTitle,\r
63                                                 command : 'checkbox',\r
64                                                 group : 'checkbox'\r
65                                         },\r
66 \r
67                                         radio :\r
68                                         {\r
69                                                 label : lang.checkboxAndRadio.radioTitle,\r
70                                                 command : 'radio',\r
71                                                 group : 'radio'\r
72                                         },\r
73 \r
74                                         textfield :\r
75                                         {\r
76                                                 label : lang.textfield.title,\r
77                                                 command : 'textfield',\r
78                                                 group : 'textfield'\r
79                                         },\r
80 \r
81                                         hiddenfield :\r
82                                         {\r
83                                                 label : lang.hidden.title,\r
84                                                 command : 'hiddenfield',\r
85                                                 group : 'hiddenfield'\r
86                                         },\r
87 \r
88                                         imagebutton :\r
89                                         {\r
90                                                 label : lang.image.titleButton,\r
91                                                 command : 'imagebutton',\r
92                                                 group : 'imagebutton'\r
93                                         },\r
94 \r
95                                         button :\r
96                                         {\r
97                                                 label : lang.button.title,\r
98                                                 command : 'button',\r
99                                                 group : 'button'\r
100                                         },\r
101 \r
102                                         select :\r
103                                         {\r
104                                                 label : lang.select.title,\r
105                                                 command : 'select',\r
106                                                 group : 'select'\r
107                                         },\r
108 \r
109                                         textarea :\r
110                                         {\r
111                                                 label : lang.textarea.title,\r
112                                                 command : 'textarea',\r
113                                                 group : 'textarea'\r
114                                         }\r
115                                 });\r
116                 }\r
117 \r
118                 // If the "contextmenu" plugin is loaded, register the listeners.\r
119                 if ( editor.contextMenu )\r
120                 {\r
121                         editor.contextMenu.addListener( function( element )\r
122                                 {\r
123                                         if ( element && element.hasAscendant( 'form', true ) )\r
124                                                 return { form : CKEDITOR.TRISTATE_OFF };\r
125                                 });\r
126 \r
127                         editor.contextMenu.addListener( function( element )\r
128                                 {\r
129                                         if ( element )\r
130                                         {\r
131                                                 var name = element.getName();\r
132 \r
133                                                 if ( name == 'select' )\r
134                                                         return { select : CKEDITOR.TRISTATE_OFF };\r
135 \r
136                                                 if ( name == 'textarea' )\r
137                                                         return { textarea : CKEDITOR.TRISTATE_OFF };\r
138 \r
139                                                 if ( name == 'input' )\r
140                                                 {\r
141                                                         var type = element.getAttribute( 'type' );\r
142 \r
143                                                         if ( type == 'text' || type == 'password' )\r
144                                                                 return { textfield : CKEDITOR.TRISTATE_OFF };\r
145 \r
146                                                         if ( type == 'button' || type == 'submit' || type == 'reset' )\r
147                                                                 return { button : CKEDITOR.TRISTATE_OFF };\r
148 \r
149                                                         if ( type == 'checkbox' )\r
150                                                                 return { checkbox : CKEDITOR.TRISTATE_OFF };\r
151 \r
152                                                         if ( type == 'radio' )\r
153                                                                 return { radio : CKEDITOR.TRISTATE_OFF };\r
154 \r
155                                                         if ( type == 'image' )\r
156                                                                 return { imagebutton : CKEDITOR.TRISTATE_OFF };\r
157                                                 }\r
158 \r
159                                                 if ( name == 'img' && element.getAttribute( '_cke_real_element_type' ) == 'hiddenfield' )\r
160                                                         return { hiddenfield : CKEDITOR.TRISTATE_OFF };\r
161                                         }\r
162                                 });\r
163                 }\r
164         },\r
165 \r
166         afterInit : function( editor )\r
167         {\r
168                 // Cleanup certain IE form elements default values.\r
169                 if( CKEDITOR.env.ie )\r
170                 {\r
171                         var dataProcessor = editor.dataProcessor,\r
172                                 htmlFilter = dataProcessor && dataProcessor.htmlFilter;\r
173 \r
174                         htmlFilter && htmlFilter.addRules(\r
175                         {\r
176                                 elements :\r
177                                 {\r
178                                         input : function( input )\r
179                                         {\r
180                                                 var attrs = input.attributes,\r
181                                                         type = attrs.type;\r
182                                                 if( type == 'checkbox' || type == 'radio' )\r
183                                                         attrs.value == 'on' && delete attrs.value;\r
184                                         }\r
185                                 }\r
186                         } );\r
187                 }\r
188         },\r
189         requires : [ 'image' ]\r
190 } );\r
191 \r
192 if ( CKEDITOR.env.ie )\r
193 {\r
194         CKEDITOR.dom.element.prototype.hasAttribute = function( name )\r
195         {\r
196                 var $attr = this.$.attributes.getNamedItem( name );\r
197 \r
198                 if ( this.getName() == 'input' )\r
199                 {\r
200                         switch ( name )\r
201                         {\r
202                                 case 'class' :\r
203                                         return this.$.className.length > 0;\r
204                                 case 'checked' :\r
205                                         return !!this.$.checked;\r
206                                 case 'value' :\r
207                                         var type = this.getAttribute( 'type' );\r
208                                         if ( type == 'checkbox' || type == 'radio' )\r
209                                                 return this.$.value != 'on';\r
210                                         break;\r
211                                 default:\r
212                         }\r
213                 }\r
214 \r
215                 return !!( $attr && $attr.specified );\r
216         };\r
217 }\r