1 | /* |
---|
2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. |
---|
3 | For licensing, see LICENSE.html or http://ckeditor.com/license |
---|
4 | */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @file Forms Plugin |
---|
8 | */ |
---|
9 | |
---|
10 | CKEDITOR.plugins.add( 'forms', |
---|
11 | { |
---|
12 | init : function( editor ) |
---|
13 | { |
---|
14 | var lang = editor.lang; |
---|
15 | |
---|
16 | editor.addCss( |
---|
17 | 'form' + |
---|
18 | '{' + |
---|
19 | 'border: 1px dotted #FF0000;' + |
---|
20 | 'padding: 2px;' + |
---|
21 | '}\n' ); |
---|
22 | |
---|
23 | editor.addCss( |
---|
24 | 'img.cke_hidden' + |
---|
25 | '{' + |
---|
26 | 'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/hiddenfield.gif' ) + ');' + |
---|
27 | 'background-position: center center;' + |
---|
28 | 'background-repeat: no-repeat;' + |
---|
29 | 'border: 1px solid #a9a9a9;' + |
---|
30 | 'width: 16px !important;' + |
---|
31 | 'height: 16px !important;' + |
---|
32 | '}' ); |
---|
33 | |
---|
34 | // All buttons use the same code to register. So, to avoid |
---|
35 | // duplications, let's use this tool function. |
---|
36 | var addButtonCommand = function( buttonName, commandName, dialogFile ) |
---|
37 | { |
---|
38 | editor.addCommand( commandName, new CKEDITOR.dialogCommand( commandName ) ); |
---|
39 | |
---|
40 | editor.ui.addButton( buttonName, |
---|
41 | { |
---|
42 | label : lang.common[ buttonName.charAt(0).toLowerCase() + buttonName.slice(1) ], |
---|
43 | command : commandName |
---|
44 | }); |
---|
45 | CKEDITOR.dialog.add( commandName, dialogFile ); |
---|
46 | }; |
---|
47 | |
---|
48 | var dialogPath = this.path + 'dialogs/'; |
---|
49 | addButtonCommand( 'Form', 'form', dialogPath + 'form.js' ); |
---|
50 | addButtonCommand( 'Checkbox', 'checkbox', dialogPath + 'checkbox.js' ); |
---|
51 | addButtonCommand( 'Radio', 'radio', dialogPath + 'radio.js' ); |
---|
52 | addButtonCommand( 'TextField', 'textfield', dialogPath + 'textfield.js' ); |
---|
53 | addButtonCommand( 'Textarea', 'textarea', dialogPath + 'textarea.js' ); |
---|
54 | addButtonCommand( 'Select', 'select', dialogPath + 'select.js' ); |
---|
55 | addButtonCommand( 'Button', 'button', dialogPath + 'button.js' ); |
---|
56 | addButtonCommand( 'ImageButton', 'imagebutton', CKEDITOR.plugins.getPath('image') + 'dialogs/image.js' ); |
---|
57 | addButtonCommand( 'HiddenField', 'hiddenfield', dialogPath + 'hiddenfield.js' ); |
---|
58 | |
---|
59 | // If the "menu" plugin is loaded, register the menu items. |
---|
60 | if ( editor.addMenuItems ) |
---|
61 | { |
---|
62 | editor.addMenuItems( |
---|
63 | { |
---|
64 | form : |
---|
65 | { |
---|
66 | label : lang.form.menu, |
---|
67 | command : 'form', |
---|
68 | group : 'form' |
---|
69 | }, |
---|
70 | |
---|
71 | checkbox : |
---|
72 | { |
---|
73 | label : lang.checkboxAndRadio.checkboxTitle, |
---|
74 | command : 'checkbox', |
---|
75 | group : 'checkbox' |
---|
76 | }, |
---|
77 | |
---|
78 | radio : |
---|
79 | { |
---|
80 | label : lang.checkboxAndRadio.radioTitle, |
---|
81 | command : 'radio', |
---|
82 | group : 'radio' |
---|
83 | }, |
---|
84 | |
---|
85 | textfield : |
---|
86 | { |
---|
87 | label : lang.textfield.title, |
---|
88 | command : 'textfield', |
---|
89 | group : 'textfield' |
---|
90 | }, |
---|
91 | |
---|
92 | hiddenfield : |
---|
93 | { |
---|
94 | label : lang.hidden.title, |
---|
95 | command : 'hiddenfield', |
---|
96 | group : 'hiddenfield' |
---|
97 | }, |
---|
98 | |
---|
99 | imagebutton : |
---|
100 | { |
---|
101 | label : lang.image.titleButton, |
---|
102 | command : 'imagebutton', |
---|
103 | group : 'imagebutton' |
---|
104 | }, |
---|
105 | |
---|
106 | button : |
---|
107 | { |
---|
108 | label : lang.button.title, |
---|
109 | command : 'button', |
---|
110 | group : 'button' |
---|
111 | }, |
---|
112 | |
---|
113 | select : |
---|
114 | { |
---|
115 | label : lang.select.title, |
---|
116 | command : 'select', |
---|
117 | group : 'select' |
---|
118 | }, |
---|
119 | |
---|
120 | textarea : |
---|
121 | { |
---|
122 | label : lang.textarea.title, |
---|
123 | command : 'textarea', |
---|
124 | group : 'textarea' |
---|
125 | } |
---|
126 | }); |
---|
127 | } |
---|
128 | |
---|
129 | // If the "contextmenu" plugin is loaded, register the listeners. |
---|
130 | if ( editor.contextMenu ) |
---|
131 | { |
---|
132 | editor.contextMenu.addListener( function( element ) |
---|
133 | { |
---|
134 | if ( element && element.hasAscendant( 'form', true ) && !element.isReadOnly() ) |
---|
135 | return { form : CKEDITOR.TRISTATE_OFF }; |
---|
136 | }); |
---|
137 | |
---|
138 | editor.contextMenu.addListener( function( element ) |
---|
139 | { |
---|
140 | if ( element && !element.isReadOnly() ) |
---|
141 | { |
---|
142 | // replace IF statements with switch |
---|
143 | switch(element.getName()) |
---|
144 | { |
---|
145 | case 'select' : |
---|
146 | return { select : CKEDITOR.TRISTATE_OFF }; |
---|
147 | |
---|
148 | case 'textarea' : |
---|
149 | return { textarea : CKEDITOR.TRISTATE_OFF }; |
---|
150 | |
---|
151 | case 'input' : |
---|
152 | |
---|
153 | // IE sometimes does not set the type attribute, |
---|
154 | // use element.$.type instead of element.getAttribute('type') |
---|
155 | switch(element.$.type) |
---|
156 | { |
---|
157 | case 'text' : |
---|
158 | case 'password' : |
---|
159 | return { textfield : CKEDITOR.TRISTATE_OFF }; |
---|
160 | |
---|
161 | case 'button' : |
---|
162 | case 'submit' : |
---|
163 | case 'reset' : |
---|
164 | return { button : CKEDITOR.TRISTATE_OFF }; |
---|
165 | |
---|
166 | case 'checkbox' : |
---|
167 | return { checkbox : CKEDITOR.TRISTATE_OFF }; |
---|
168 | |
---|
169 | case 'radio' : |
---|
170 | return { radio : CKEDITOR.TRISTATE_OFF }; |
---|
171 | |
---|
172 | case 'image' : |
---|
173 | return { imagebutton : CKEDITOR.TRISTATE_OFF }; |
---|
174 | } |
---|
175 | |
---|
176 | // break for inputs of unrecognized types |
---|
177 | break; |
---|
178 | |
---|
179 | case 'img' : |
---|
180 | if(element.getAttribute( '_cke_real_element_type' ) === 'hiddenfield' ) |
---|
181 | { |
---|
182 | return { hiddenfield : CKEDITOR.TRISTATE_OFF }; |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | }); |
---|
187 | } |
---|
188 | |
---|
189 | editor.on( 'doubleclick', function( evt ) |
---|
190 | { |
---|
191 | var element = evt.data.element; |
---|
192 | |
---|
193 | // original used if/else statements each with a call to element.is() |
---|
194 | // lets use the more direct element.getName() in a switch since element.is() uses element.getName() internally |
---|
195 | switch(element.getName()) |
---|
196 | { |
---|
197 | case 'form' : |
---|
198 | evt.data.dialog = 'form'; |
---|
199 | break; |
---|
200 | |
---|
201 | case 'select' : |
---|
202 | evt.data.dialog = 'select'; |
---|
203 | break; |
---|
204 | |
---|
205 | case 'textarea' : |
---|
206 | evt.data.dialog = 'textarea'; |
---|
207 | break; |
---|
208 | |
---|
209 | case 'img' : |
---|
210 | if(element.getAttribute( '_cke_real_element_type' ) === 'hiddenfield' ) |
---|
211 | { |
---|
212 | evt.data.dialog = 'hiddenfield'; |
---|
213 | } |
---|
214 | break; |
---|
215 | |
---|
216 | case 'input' : |
---|
217 | // see note above for using element.$.type |
---|
218 | switch(element.$.type) |
---|
219 | { |
---|
220 | case 'text' : |
---|
221 | case 'password' : |
---|
222 | evt.data.dialog = 'textfield'; |
---|
223 | break; |
---|
224 | |
---|
225 | case 'button' : |
---|
226 | case 'submit' : |
---|
227 | case 'reset' : |
---|
228 | evt.data.dialog = 'button'; |
---|
229 | break; |
---|
230 | |
---|
231 | case 'checkbox' : |
---|
232 | evt.data.dialog = 'checkbox'; |
---|
233 | break; |
---|
234 | |
---|
235 | case 'radio' : |
---|
236 | evt.data.dialog = 'radio'; |
---|
237 | break; |
---|
238 | |
---|
239 | case 'image' : |
---|
240 | evt.data.dialog = 'imagebutton'; |
---|
241 | break; |
---|
242 | } |
---|
243 | } |
---|
244 | }); |
---|
245 | }, |
---|
246 | |
---|
247 | afterInit : function( editor ) |
---|
248 | { |
---|
249 | var dataProcessor = editor.dataProcessor, |
---|
250 | htmlFilter = dataProcessor && dataProcessor.htmlFilter, |
---|
251 | dataFilter = dataProcessor && dataProcessor.dataFilter; |
---|
252 | |
---|
253 | // Cleanup certain IE form elements default values. |
---|
254 | if ( CKEDITOR.env.ie ) |
---|
255 | { |
---|
256 | htmlFilter && htmlFilter.addRules( |
---|
257 | { |
---|
258 | elements : |
---|
259 | { |
---|
260 | input : function( input ) |
---|
261 | { |
---|
262 | var attrs = input.attributes, |
---|
263 | type = attrs.type; |
---|
264 | if ( type == 'checkbox' || type == 'radio' ) |
---|
265 | attrs.value == 'on' && delete attrs.value; |
---|
266 | } |
---|
267 | } |
---|
268 | } ); |
---|
269 | } |
---|
270 | |
---|
271 | if ( dataFilter ) |
---|
272 | { |
---|
273 | dataFilter.addRules( |
---|
274 | { |
---|
275 | elements : |
---|
276 | { |
---|
277 | input : function( element ) |
---|
278 | { |
---|
279 | if ( element.attributes.type == 'hidden' ) |
---|
280 | return editor.createFakeParserElement( element, 'cke_hidden', 'hiddenfield' ); |
---|
281 | } |
---|
282 | } |
---|
283 | } ); |
---|
284 | } |
---|
285 | }, |
---|
286 | requires : [ 'image', 'fakeobjects' ] |
---|
287 | } ); |
---|
288 | |
---|
289 | if ( CKEDITOR.env.ie ) |
---|
290 | { |
---|
291 | CKEDITOR.dom.element.prototype.hasAttribute = function( name ) |
---|
292 | { |
---|
293 | var $attr = this.$.attributes.getNamedItem( name ); |
---|
294 | |
---|
295 | if ( this.getName() == 'input' ) |
---|
296 | { |
---|
297 | switch ( name ) |
---|
298 | { |
---|
299 | case 'class' : |
---|
300 | return this.$.className.length > 0; |
---|
301 | case 'checked' : |
---|
302 | return !!this.$.checked; |
---|
303 | case 'value' : |
---|
304 | var type = this.getAttribute( 'type' ); |
---|
305 | if ( type == 'checkbox' || type == 'radio' ) |
---|
306 | return this.$.value != 'on'; |
---|
307 | break; |
---|
308 | default: |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | return !!( $attr && $attr.specified ); |
---|
313 | }; |
---|
314 | } |
---|