JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
65e76c5552a12ce042f32e4e025deb548da9caa6
[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.isReadOnly()\r
94                                                                 && element.getAttribute( '_cke_real_element_type' ) == 'flash' )\r
95                                                         return { flash : CKEDITOR.TRISTATE_OFF };\r
96                                         });\r
97                         }\r
98                 },\r
99 \r
100                 afterInit : function( editor )\r
101                 {\r
102                         var dataProcessor = editor.dataProcessor,\r
103                                 dataFilter = dataProcessor && dataProcessor.dataFilter;\r
104 \r
105                         if ( dataFilter )\r
106                         {\r
107                                 dataFilter.addRules(\r
108                                         {\r
109                                                 elements :\r
110                                                 {\r
111                                                         'cke:object' : function( element )\r
112                                                         {\r
113                                                                 var attributes = element.attributes,\r
114                                                                         classId = attributes.classid && String( attributes.classid ).toLowerCase();\r
115 \r
116                                                                 if ( !classId )\r
117                                                                 {\r
118                                                                         // Look for the inner <embed>\r
119                                                                         for ( var i = 0 ; i < element.children.length ; i++ )\r
120                                                                         {\r
121                                                                                 if ( element.children[ i ].name == 'cke:embed' )\r
122                                                                                 {\r
123                                                                                         if ( !isFlashEmbed( element.children[ i ] ) )\r
124                                                                                                 return null;\r
125 \r
126                                                                                         return createFakeElement( editor, element );\r
127                                                                                 }\r
128                                                                         }\r
129                                                                         return null;\r
130                                                                 }\r
131 \r
132                                                                 return createFakeElement( editor, element );\r
133                                                         },\r
134 \r
135                                                         'cke:embed' : function( element )\r
136                                                         {\r
137                                                                 if ( !isFlashEmbed( element ) )\r
138                                                                         return null;\r
139 \r
140                                                                 return createFakeElement( editor, element );\r
141                                                         }\r
142                                                 }\r
143                                         },\r
144                                         5);\r
145                         }\r
146                 },\r
147 \r
148                 requires : [ 'fakeobjects' ]\r
149         });\r
150 })();\r
151 \r
152 CKEDITOR.tools.extend( CKEDITOR.config,\r
153 {\r
154         /**\r
155          * Save as EMBED tag only. This tag is unrecommended.\r
156          * @type Boolean\r
157          * @default false\r
158          */\r
159         flashEmbedTagOnly : false,\r
160 \r
161         /**\r
162          * Add EMBED tag as alternative: &lt;object&gt&lt;embed&gt&lt;/embed&gt&lt;/object&gt\r
163          * @type Boolean\r
164          * @default false\r
165          */\r
166         flashAddEmbedTag : true,\r
167 \r
168         /**\r
169          * Use embedTagOnly and addEmbedTag values on edit.\r
170          * @type Boolean\r
171          * @default false\r
172          */\r
173         flashConvertOnEdit : false\r
174 } );\r