JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
7d8ea4591250846824f3c3ed1680020b5a056e1c
[ckeditor.git] / _source / plugins / forms / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, 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                         '}\n' );\r
22 \r
23                 editor.addCss(\r
24                         'img.cke_hidden' +\r
25                         '{' +\r
26                                 'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/hiddenfield.gif' ) + ');' +\r
27                                 'background-position: center center;' +\r
28                                 'background-repeat: no-repeat;' +\r
29                                 'border: 1px solid #a9a9a9;' +\r
30                                 'width: 16px !important;' +\r
31                                 'height: 16px !important;' +\r
32                         '}' );\r
33 \r
34                 // All buttons use the same code to register. So, to avoid\r
35                 // duplications, let's use this tool function.\r
36                 var addButtonCommand = function( buttonName, commandName, dialogFile )\r
37                 {\r
38                         editor.addCommand( commandName, new CKEDITOR.dialogCommand( commandName ) );\r
39 \r
40                         editor.ui.addButton( buttonName,\r
41                                 {\r
42                                         label : lang.common[ buttonName.charAt(0).toLowerCase() + buttonName.slice(1) ],\r
43                                         command : commandName\r
44                                 });\r
45                         CKEDITOR.dialog.add( commandName, dialogFile );\r
46                 };\r
47 \r
48                 var dialogPath = this.path + 'dialogs/';\r
49                 addButtonCommand( 'Form',                       'form',                 dialogPath + 'form.js' );\r
50                 addButtonCommand( 'Checkbox',           'checkbox',             dialogPath + 'checkbox.js' );\r
51                 addButtonCommand( 'Radio',                      'radio',                dialogPath + 'radio.js' );\r
52                 addButtonCommand( 'TextField',          'textfield',    dialogPath + 'textfield.js' );\r
53                 addButtonCommand( 'Textarea',           'textarea',             dialogPath + 'textarea.js' );\r
54                 addButtonCommand( 'Select',                     'select',               dialogPath + 'select.js' );\r
55                 addButtonCommand( 'Button',                     'button',               dialogPath + 'button.js' );\r
56                 addButtonCommand( 'ImageButton',        'imagebutton',  CKEDITOR.plugins.getPath('image') + 'dialogs/image.js' );\r
57                 addButtonCommand( 'HiddenField',        'hiddenfield',  dialogPath + 'hiddenfield.js' );\r
58 \r
59                 // If the "menu" plugin is loaded, register the menu items.\r
60                 if ( editor.addMenuItems )\r
61                 {\r
62                         editor.addMenuItems(\r
63                                 {\r
64                                         form :\r
65                                         {\r
66                                                 label : lang.form.menu,\r
67                                                 command : 'form',\r
68                                                 group : 'form'\r
69                                         },\r
70 \r
71                                         checkbox :\r
72                                         {\r
73                                                 label : lang.checkboxAndRadio.checkboxTitle,\r
74                                                 command : 'checkbox',\r
75                                                 group : 'checkbox'\r
76                                         },\r
77 \r
78                                         radio :\r
79                                         {\r
80                                                 label : lang.checkboxAndRadio.radioTitle,\r
81                                                 command : 'radio',\r
82                                                 group : 'radio'\r
83                                         },\r
84 \r
85                                         textfield :\r
86                                         {\r
87                                                 label : lang.textfield.title,\r
88                                                 command : 'textfield',\r
89                                                 group : 'textfield'\r
90                                         },\r
91 \r
92                                         hiddenfield :\r
93                                         {\r
94                                                 label : lang.hidden.title,\r
95                                                 command : 'hiddenfield',\r
96                                                 group : 'hiddenfield'\r
97                                         },\r
98 \r
99                                         imagebutton :\r
100                                         {\r
101                                                 label : lang.image.titleButton,\r
102                                                 command : 'imagebutton',\r
103                                                 group : 'imagebutton'\r
104                                         },\r
105 \r
106                                         button :\r
107                                         {\r
108                                                 label : lang.button.title,\r
109                                                 command : 'button',\r
110                                                 group : 'button'\r
111                                         },\r
112 \r
113                                         select :\r
114                                         {\r
115                                                 label : lang.select.title,\r
116                                                 command : 'select',\r
117                                                 group : 'select'\r
118                                         },\r
119 \r
120                                         textarea :\r
121                                         {\r
122                                                 label : lang.textarea.title,\r
123                                                 command : 'textarea',\r
124                                                 group : 'textarea'\r
125                                         }\r
126                                 });\r
127                 }\r
128 \r
129                 // If the "contextmenu" plugin is loaded, register the listeners.\r
130                 if ( editor.contextMenu )\r
131                 {\r
132                         editor.contextMenu.addListener( function( element )\r
133                                 {\r
134                                         if ( element && element.hasAscendant( 'form', true ) && !element.isReadOnly() )\r
135                                                 return { form : CKEDITOR.TRISTATE_OFF };\r
136                                 });\r
137 \r
138                         editor.contextMenu.addListener( function( element )\r
139                                 {\r
140                                         if ( element && !element.isReadOnly() )\r
141                                         {\r
142                                                 var name = element.getName();\r
143 \r
144                                                 if ( name == 'select' )\r
145                                                         return { select : CKEDITOR.TRISTATE_OFF };\r
146 \r
147                                                 if ( name == 'textarea' )\r
148                                                         return { textarea : CKEDITOR.TRISTATE_OFF };\r
149 \r
150                                                 if ( name == 'input' )\r
151                                                 {\r
152                                                         var type = element.getAttribute( 'type' );\r
153 \r
154                                                         if ( type == 'text' || type == 'password' )\r
155                                                                 return { textfield : CKEDITOR.TRISTATE_OFF };\r
156 \r
157                                                         if ( type == 'button' || type == 'submit' || type == 'reset' )\r
158                                                                 return { button : CKEDITOR.TRISTATE_OFF };\r
159 \r
160                                                         if ( type == 'checkbox' )\r
161                                                                 return { checkbox : CKEDITOR.TRISTATE_OFF };\r
162 \r
163                                                         if ( type == 'radio' )\r
164                                                                 return { radio : CKEDITOR.TRISTATE_OFF };\r
165 \r
166                                                         if ( type == 'image' )\r
167                                                                 return { imagebutton : CKEDITOR.TRISTATE_OFF };\r
168                                                 }\r
169 \r
170                                                 if ( name == 'img' && element.getAttribute( '_cke_real_element_type' ) == 'hiddenfield' )\r
171                                                         return { hiddenfield : CKEDITOR.TRISTATE_OFF };\r
172                                         }\r
173                                 });\r
174                 }\r
175 \r
176                 editor.on( 'doubleclick', function( evt )\r
177                         {\r
178                                 var element = evt.data.element;\r
179 \r
180                                 if ( element.is( 'form' ) )\r
181                                         evt.data.dialog = 'form';\r
182                                 else if ( element.is( 'select' ) )\r
183                                         evt.data.dialog = 'select';\r
184                                 else if ( element.is( 'textarea' ) )\r
185                                         evt.data.dialog = 'textarea';\r
186                                 else if ( element.is( 'img' ) && element.getAttribute( '_cke_real_element_type' ) == 'hiddenfield' )\r
187                                         evt.data.dialog = 'hiddenfield';\r
188                                 else if ( element.is( 'input' ) )\r
189                                 {\r
190                                         var type = element.getAttribute( 'type' );\r
191 \r
192                                         switch ( type )\r
193                                         {\r
194                                                 case 'text' : case 'password':\r
195                                                         evt.data.dialog = 'textfield';\r
196                                                         break;\r
197                                                 case 'button' : case 'submit' : case 'reset' :\r
198                                                         evt.data.dialog = 'button';\r
199                                                         break;\r
200                                                 case 'checkbox' :\r
201                                                         evt.data.dialog = 'checkbox';\r
202                                                         break;\r
203                                                 case 'radio' :\r
204                                                         evt.data.dialog = 'radio';\r
205                                                         break;\r
206                                                 case 'image' :\r
207                                                         evt.data.dialog = 'imagebutton';\r
208                                                         break;\r
209                                         }\r
210                                 }\r
211                         });\r
212         },\r
213 \r
214         afterInit : function( editor )\r
215         {\r
216                 var dataProcessor = editor.dataProcessor,\r
217                         htmlFilter = dataProcessor && dataProcessor.htmlFilter,\r
218                         dataFilter = dataProcessor && dataProcessor.dataFilter;\r
219 \r
220                 // Cleanup certain IE form elements default values.\r
221                 if ( CKEDITOR.env.ie )\r
222                 {\r
223                         htmlFilter && htmlFilter.addRules(\r
224                         {\r
225                                 elements :\r
226                                 {\r
227                                         input : function( input )\r
228                                         {\r
229                                                 var attrs = input.attributes,\r
230                                                         type = attrs.type;\r
231                                                 if ( type == 'checkbox' || type == 'radio' )\r
232                                                         attrs.value == 'on' && delete attrs.value;\r
233                                         }\r
234                                 }\r
235                         } );\r
236                 }\r
237 \r
238                 if ( dataFilter )\r
239                 {\r
240                         dataFilter.addRules(\r
241                         {\r
242                                 elements :\r
243                                 {\r
244                                         input : function( element )\r
245                                         {\r
246                                                 if ( element.attributes.type == 'hidden' )\r
247                                                         return editor.createFakeParserElement( element, 'cke_hidden', 'hiddenfield' );\r
248                                         }\r
249                                 }\r
250                         } );\r
251                 }\r
252         },\r
253         requires : [ 'image', 'fakeobjects' ]\r
254 } );\r
255 \r
256 if ( CKEDITOR.env.ie )\r
257 {\r
258         CKEDITOR.dom.element.prototype.hasAttribute = function( name )\r
259         {\r
260                 var $attr = this.$.attributes.getNamedItem( name );\r
261 \r
262                 if ( this.getName() == 'input' )\r
263                 {\r
264                         switch ( name )\r
265                         {\r
266                                 case 'class' :\r
267                                         return this.$.className.length > 0;\r
268                                 case 'checked' :\r
269                                         return !!this.$.checked;\r
270                                 case 'value' :\r
271                                         var type = this.getAttribute( 'type' );\r
272                                         if ( type == 'checkbox' || type == 'radio' )\r
273                                                 return this.$.value != 'on';\r
274                                         break;\r
275                                 default:\r
276                         }\r
277                 }\r
278 \r
279                 return !!( $attr && $attr.specified );\r
280         };\r
281 }\r