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