JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
29117ba231d21f2126d4f410fa6295c2edf2080f
[ckeditor.git] / _source / plugins / flash / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, 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                         // If the "contextmenu" plugin is loaded, register the listeners.\r
81                         if ( editor.contextMenu )\r
82                         {\r
83                                 editor.contextMenu.addListener( function( element, selection )\r
84                                         {\r
85                                                 if ( element && element.is( 'img' ) && element.getAttribute( '_cke_real_element_type' ) == 'flash' )\r
86                                                         return { flash : CKEDITOR.TRISTATE_OFF };\r
87                                         });\r
88                         }\r
89                 },\r
90 \r
91                 afterInit : function( editor )\r
92                 {\r
93                         var dataProcessor = editor.dataProcessor,\r
94                                 dataFilter = dataProcessor && dataProcessor.dataFilter;\r
95 \r
96                         if ( dataFilter )\r
97                         {\r
98                                 dataFilter.addRules(\r
99                                         {\r
100                                                 elements :\r
101                                                 {\r
102                                                         'cke:object' : function( element )\r
103                                                         {\r
104                                                                 var attributes = element.attributes,\r
105                                                                         classId = attributes.classid && String( attributes.classid ).toLowerCase();\r
106 \r
107                                                                 if ( !classId )\r
108                                                                 {\r
109                                                                         // Look for the inner <embed>\r
110                                                                         for ( var i = 0 ; i < element.children.length ; i++ )\r
111                                                                         {\r
112                                                                                 if ( element.children[ i ].name == 'embed' )\r
113                                                                                 {\r
114                                                                                         if ( !isFlashEmbed( element.children[ i ] ) )\r
115                                                                                                 return null;\r
116 \r
117                                                                                         return createFakeElement( editor, element );\r
118                                                                                 }\r
119                                                                         }\r
120                                                                         return null;\r
121                                                                 }\r
122 \r
123                                                                 return createFakeElement( editor, element );\r
124                                                         },\r
125 \r
126                                                         'cke:embed' : function( element )\r
127                                                         {\r
128                                                                 if ( !isFlashEmbed( element ) )\r
129                                                                         return null;\r
130 \r
131                                                                 return createFakeElement( editor, element );\r
132                                                         }\r
133                                                 }\r
134                                         },\r
135                                         5);\r
136                         }\r
137                 },\r
138 \r
139                 requires : [ 'fakeobjects' ]\r
140         });\r
141 })();\r
142 \r
143 CKEDITOR.tools.extend( CKEDITOR.config,\r
144 {\r
145         /**\r
146          * Save as EMBED tag only. This tag is unrecommended.\r
147          * @type Boolean\r
148          * @default false\r
149          */\r
150         flashEmbedTagOnly : false,\r
151 \r
152         /**\r
153          * Add EMBED tag as alternative: &lt;object&gt&lt;embed&gt&lt;/embed&gt&lt;/object&gt\r
154          * @type Boolean\r
155          * @default false\r
156          */\r
157         flashAddEmbedTag : true,\r
158 \r
159         /**\r
160          * Use embedTagOnly and addEmbedTag values on edit.\r
161          * @type Boolean\r
162          * @default false\r
163          */\r
164         flashConvertOnEdit : false\r
165 } );\r