JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
c76a7fe32c28283118b70994f02a2a7ac0b21b6e
[ckeditor.git] / _source / plugins / flash / 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 (function()\r
7 {\r
8         var flashFilenameRegex = /\.swf(?:$|\?)/i,\r
9                 numberRegex = /^\d+(?:\.\d+)?$/;\r
10 \r
11         function cssifyLength( length )\r
12         {\r
13                 if ( numberRegex.test( length ) )\r
14                         return length + 'px';\r
15                 return length;\r
16         }\r
17 \r
18         function isFlashEmbed( element )\r
19         {\r
20                 var attributes = element.attributes;\r
21 \r
22                 return ( attributes.type == 'application/x-shockwave-flash' || flashFilenameRegex.test( attributes.src || '' ) );\r
23         }\r
24 \r
25         function createFakeElement( editor, realElement )\r
26         {\r
27                 var fakeElement = editor.createFakeParserElement( realElement, 'cke_flash', 'flash', true ),\r
28                         fakeStyle = fakeElement.attributes.style || '';\r
29 \r
30                 var width = realElement.attributes.width,\r
31                         height = realElement.attributes.height;\r
32 \r
33                 if ( typeof width != 'undefined' )\r
34                         fakeStyle = fakeElement.attributes.style = fakeStyle + 'width:' + cssifyLength( width ) + ';';\r
35 \r
36                 if ( typeof height != 'undefined' )\r
37                         fakeStyle = fakeElement.attributes.style = fakeStyle + 'height:' + cssifyLength( height ) + ';';\r
38 \r
39                 return fakeElement;\r
40         }\r
41 \r
42         CKEDITOR.plugins.add( 'flash',\r
43         {\r
44                 init : function( editor )\r
45                 {\r
46                         editor.addCommand( 'flash', new CKEDITOR.dialogCommand( 'flash' ) );\r
47                         editor.ui.addButton( 'Flash',\r
48                                 {\r
49                                         label : editor.lang.common.flash,\r
50                                         command : 'flash'\r
51                                 });\r
52                         CKEDITOR.dialog.add( 'flash', this.path + 'dialogs/flash.js' );\r
53 \r
54                         editor.addCss(\r
55                                 'img.cke_flash' +\r
56                                 '{' +\r
57                                         'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/placeholder.png' ) + ');' +\r
58                                         'background-position: center center;' +\r
59                                         'background-repeat: no-repeat;' +\r
60                                         'border: 1px solid #a9a9a9;' +\r
61                                         'width: 80px;' +\r
62                                         'height: 80px;' +\r
63                                 '}'\r
64                                 );\r
65 \r
66                         // If the "menu" plugin is loaded, register the menu items.\r
67                         if ( editor.addMenuItems )\r
68                         {\r
69                                 editor.addMenuItems(\r
70                                         {\r
71                                                 flash :\r
72                                                 {\r
73                                                         label : editor.lang.flash.properties,\r
74                                                         command : 'flash',\r
75                                                         group : 'flash'\r
76                                                 }\r
77                                         });\r
78                         }\r
79 \r
80                         editor.on( 'doubleclick', function( evt )\r
81                                 {\r
82                                         var element = evt.data.element;\r
83 \r
84                                         if ( element.is( 'img' ) && element.getAttribute( '_cke_real_element_type' ) == 'flash' )\r
85                                                 evt.data.dialog = 'flash';\r
86                                 });\r
87 \r
88                         // If the "contextmenu" plugin is loaded, register the listeners.\r
89                         if ( editor.contextMenu )\r
90                         {\r
91                                 editor.contextMenu.addListener( function( element, selection )\r
92                                         {\r
93                                                 if ( element && element.is( 'img' ) && element.getAttribute( '_cke_real_element_type' ) == 'flash' )\r
94                                                         return { flash : CKEDITOR.TRISTATE_OFF };\r
95                                         });\r
96                         }\r
97                 },\r
98 \r
99                 afterInit : function( editor )\r
100                 {\r
101                         var dataProcessor = editor.dataProcessor,\r
102                                 dataFilter = dataProcessor && dataProcessor.dataFilter;\r
103 \r
104                         if ( dataFilter )\r
105                         {\r
106                                 dataFilter.addRules(\r
107                                         {\r
108                                                 elements :\r
109                                                 {\r
110                                                         'cke:object' : function( element )\r
111                                                         {\r
112                                                                 var attributes = element.attributes,\r
113                                                                         classId = attributes.classid && String( attributes.classid ).toLowerCase();\r
114 \r
115                                                                 if ( !classId )\r
116                                                                 {\r
117                                                                         // Look for the inner <embed>\r
118                                                                         for ( var i = 0 ; i < element.children.length ; i++ )\r
119                                                                         {\r
120                                                                                 if ( element.children[ i ].name == 'cke:embed' )\r
121                                                                                 {\r
122                                                                                         if ( !isFlashEmbed( element.children[ i ] ) )\r
123                                                                                                 return null;\r
124 \r
125                                                                                         return createFakeElement( editor, element );\r
126                                                                                 }\r
127                                                                         }\r
128                                                                         return null;\r
129                                                                 }\r
130 \r
131                                                                 return createFakeElement( editor, element );\r
132                                                         },\r
133 \r
134                                                         'cke:embed' : function( element )\r
135                                                         {\r
136                                                                 if ( !isFlashEmbed( element ) )\r
137                                                                         return null;\r
138 \r
139                                                                 return createFakeElement( editor, element );\r
140                                                         }\r
141                                                 }\r
142                                         },\r
143                                         5);\r
144                         }\r
145                 },\r
146 \r
147                 requires : [ 'fakeobjects' ]\r
148         });\r
149 })();\r
150 \r
151 CKEDITOR.tools.extend( CKEDITOR.config,\r
152 {\r
153         /**\r
154          * Save as EMBED tag only. This tag is unrecommended.\r
155          * @type Boolean\r
156          * @default false\r
157          */\r
158         flashEmbedTagOnly : false,\r
159 \r
160         /**\r
161          * Add EMBED tag as alternative: &lt;object&gt&lt;embed&gt&lt;/embed&gt&lt;/object&gt\r
162          * @type Boolean\r
163          * @default false\r
164          */\r
165         flashAddEmbedTag : true,\r
166 \r
167         /**\r
168          * Use embedTagOnly and addEmbedTag values on edit.\r
169          * @type Boolean\r
170          * @default false\r
171          */\r
172         flashConvertOnEdit : false\r
173 } );\r