JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / forms / dialogs / button.js
1 /*\r
2 Copyright (c) 2003-2012, 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( 'button', function( editor )\r
6 {\r
7         function commitAttributes( element )\r
8         {\r
9                 var val = this.getValue();\r
10                 if ( val )\r
11                 {\r
12                         element.attributes[ this.id ] = val;\r
13                         if ( this.id == 'name' )\r
14                                 element.attributes[ 'data-cke-saved-name' ] = val;\r
15                 }\r
16                 else\r
17                 {\r
18                         delete element.attributes[ this.id ];\r
19                         if ( this.id == 'name' )\r
20                                 delete element.attributes[ 'data-cke-saved-name' ];\r
21                 }\r
22         }\r
23 \r
24         return {\r
25                 title : editor.lang.button.title,\r
26                 minWidth : 350,\r
27                 minHeight : 150,\r
28                 onShow : function()\r
29                 {\r
30                         delete this.button;\r
31                         var element = this.getParentEditor().getSelection().getSelectedElement();\r
32                         if ( element && element.is( 'input' ) )\r
33                         {\r
34                                 var type = element.getAttribute( 'type' );\r
35                                 if ( type in { button:1, reset:1, submit:1 } )\r
36                                 {\r
37                                         this.button = element;\r
38                                         this.setupContent( element );\r
39                                 }\r
40                         }\r
41                 },\r
42                 onOk : function()\r
43                 {\r
44                         var editor = this.getParentEditor(),\r
45                                 element = this.button,\r
46                                 isInsertMode = !element;\r
47 \r
48                         var fake = element ? CKEDITOR.htmlParser.fragment.fromHtml( element.getOuterHtml() ).children[ 0 ]\r
49                                         : new CKEDITOR.htmlParser.element( 'input' );\r
50                         this.commitContent( fake );\r
51 \r
52                         var writer = new CKEDITOR.htmlParser.basicWriter();\r
53                         fake.writeHtml( writer );\r
54                         var newElement = CKEDITOR.dom.element.createFromHtml( writer.getHtml(), editor.document );\r
55 \r
56                         if ( isInsertMode )\r
57                                 editor.insertElement( newElement );\r
58                         else\r
59                         {\r
60                                 newElement.replace( element );\r
61                                 editor.getSelection().selectElement( newElement );\r
62                         }\r
63                 },\r
64                 contents : [\r
65                         {\r
66                                 id : 'info',\r
67                                 label : editor.lang.button.title,\r
68                                 title : editor.lang.button.title,\r
69                                 elements : [\r
70                                         {\r
71                                                 id : 'name',\r
72                                                 type : 'text',\r
73                                                 label : editor.lang.common.name,\r
74                                                 'default' : '',\r
75                                                 setup : function( element )\r
76                                                 {\r
77                                                         this.setValue(\r
78                                                                         element.data( 'cke-saved-name' ) ||\r
79                                                                         element.getAttribute( 'name' ) ||\r
80                                                                         '' );\r
81                                                 },\r
82                                                 commit : commitAttributes\r
83                                         },\r
84                                         {\r
85                                                 id : 'value',\r
86                                                 type : 'text',\r
87                                                 label : editor.lang.button.text,\r
88                                                 accessKey : 'V',\r
89                                                 'default' : '',\r
90                                                 setup : function( element )\r
91                                                 {\r
92                                                         this.setValue( element.getAttribute( 'value' ) || '' );\r
93                                                 },\r
94                                                 commit : commitAttributes\r
95                                         },\r
96                                         {\r
97                                                 id : 'type',\r
98                                                 type : 'select',\r
99                                                 label : editor.lang.button.type,\r
100                                                 'default' : 'button',\r
101                                                 accessKey : 'T',\r
102                                                 items :\r
103                                                 [\r
104                                                         [ editor.lang.button.typeBtn, 'button' ],\r
105                                                         [ editor.lang.button.typeSbm, 'submit' ],\r
106                                                         [ editor.lang.button.typeRst, 'reset' ]\r
107                                                 ],\r
108                                                 setup : function( element )\r
109                                                 {\r
110                                                         this.setValue( element.getAttribute( 'type' ) || '' );\r
111                                                 },\r
112                                                 commit : commitAttributes\r
113                                         }\r
114                                 ]\r
115                         }\r
116                 ]\r
117         };\r
118 });\r