JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / plugins / forms / dialogs / radio.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 CKEDITOR.dialog.add( 'radio', function( editor )\r
6 {\r
7         return {\r
8                 title : editor.lang.checkboxAndRadio.radioTitle,\r
9                 minWidth : 350,\r
10                 minHeight : 140,\r
11                 onShow : function()\r
12                 {\r
13                         delete this.radioButton;\r
14 \r
15                         var element = this.getParentEditor().getSelection().getSelectedElement();\r
16                         if ( element && element.getName() == "input" && element.getAttribute( 'type' ) == "radio" )\r
17                         {\r
18                                 this.radioButton = element;\r
19                                 this.setupContent( element );\r
20                         }\r
21                 },\r
22                 onOk : function()\r
23                 {\r
24                         var editor,\r
25                                 element = this.radioButton,\r
26                                 isInsertMode = !element;\r
27 \r
28                         if ( isInsertMode )\r
29                         {\r
30                                 editor = this.getParentEditor();\r
31                                 element = editor.document.createElement( 'input' );\r
32                                 element.setAttribute( 'type', 'radio' );\r
33                         }\r
34 \r
35                         if ( isInsertMode )\r
36                                 editor.insertElement( element );\r
37                         this.commitContent( { element : element } );\r
38                 },\r
39                 contents : [\r
40                         {\r
41                                 id : 'info',\r
42                                 label : editor.lang.checkboxAndRadio.radioTitle,\r
43                                 title : editor.lang.checkboxAndRadio.radioTitle,\r
44                                 elements : [\r
45                                         {\r
46                                                 id : 'name',\r
47                                                 type : 'text',\r
48                                                 label : editor.lang.common.name,\r
49                                                 'default' : '',\r
50                                                 accessKey : 'N',\r
51                                                 setup : function( element )\r
52                                                 {\r
53                                                         this.setValue(\r
54                                                                         element.getAttribute( '_cke_saved_name' ) ||\r
55                                                                         element.getAttribute( 'name' ) ||\r
56                                                                         '' );\r
57                                                 },\r
58                                                 commit : function( data )\r
59                                                 {\r
60                                                         var element = data.element;\r
61 \r
62                                                         if ( this.getValue() )\r
63                                                                 element.setAttribute( '_cke_saved_name', this.getValue() );\r
64                                                         else\r
65                                                         {\r
66                                                                 element.removeAttribute( '_cke_saved_name' );\r
67                                                                 element.removeAttribute( 'name' );\r
68                                                         }\r
69                                                 }\r
70                                         },\r
71                                         {\r
72                                                 id : 'value',\r
73                                                 type : 'text',\r
74                                                 label : editor.lang.checkboxAndRadio.value,\r
75                                                 'default' : '',\r
76                                                 accessKey : 'V',\r
77                                                 setup : function( element )\r
78                                                 {\r
79                                                         this.setValue( element.getAttribute( 'value' ) || '' );\r
80                                                 },\r
81                                                 commit : function( data )\r
82                                                 {\r
83                                                         var element = data.element;\r
84 \r
85                                                         if ( this.getValue() )\r
86                                                                 element.setAttribute( 'value', this.getValue() );\r
87                                                         else\r
88                                                                 element.removeAttribute( 'value' );\r
89                                                 }\r
90                                         },\r
91                                         {\r
92                                                 id : 'checked',\r
93                                                 type : 'checkbox',\r
94                                                 label : editor.lang.checkboxAndRadio.selected,\r
95                                                 'default' : '',\r
96                                                 accessKey : 'S',\r
97                                                 value : "checked",\r
98                                                 setup : function( element )\r
99                                                 {\r
100                                                         this.setValue( element.getAttribute( 'checked' ) );\r
101                                                 },\r
102                                                 commit : function( data )\r
103                                                 {\r
104                                                         var element = data.element;\r
105 \r
106                                                         if ( !CKEDITOR.env.ie )\r
107                                                         {\r
108                                                                 if ( this.getValue() )\r
109                                                                         element.setAttribute( 'checked', 'checked' );\r
110                                                                 else\r
111                                                                         element.removeAttribute( 'checked' );\r
112                                                         }\r
113                                                         else\r
114                                                         {\r
115                                                                 var isElementChecked = element.getAttribute( 'checked' );\r
116                                                                 var isChecked = !!this.getValue();\r
117 \r
118                                                                 if ( isElementChecked != isChecked )\r
119                                                                 {\r
120                                                                         var replace = CKEDITOR.dom.element.createFromHtml( '<input type="radio"'\r
121                                                                                         + ( isChecked ? ' checked="checked"' : '' )\r
122                                                                                         + '></input>', editor.document );\r
123                                                                         element.copyAttributes( replace, { type : 1, checked : 1 } );\r
124                                                                         replace.replace( element );\r
125                                                                         editor.getSelection().selectElement( replace );\r
126                                                                         data.element = replace;\r
127                                                                 }\r
128                                                         }\r
129                                                 }\r
130                                         }\r
131                                 ]\r
132                         }\r
133                 ]\r
134         };\r
135 });\r