JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2
[ckeditor.git] / _source / plugins / panel / 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 CKEDITOR.plugins.add( 'panel',\r
7 {\r
8         beforeInit : function( editor )\r
9         {\r
10                 editor.ui.addHandler( CKEDITOR.UI_PANEL, CKEDITOR.ui.panel.handler );\r
11         }\r
12 });\r
13 \r
14 /**\r
15  * Panel UI element.\r
16  * @constant\r
17  * @example\r
18  */\r
19 CKEDITOR.UI_PANEL = 2;\r
20 \r
21 CKEDITOR.ui.panel = function( document, definition )\r
22 {\r
23         // Copy all definition properties to this object.\r
24         if ( definition )\r
25                 CKEDITOR.tools.extend( this, definition );\r
26 \r
27         // Set defaults.\r
28         CKEDITOR.tools.extend( this,\r
29                 {\r
30                         className : '',\r
31                         css : []\r
32                 });\r
33 \r
34         this.id = CKEDITOR.tools.getNextNumber();\r
35         this.document = document;\r
36 \r
37         this._ =\r
38         {\r
39                 blocks : {}\r
40         };\r
41 };\r
42 \r
43 /**\r
44  * Transforms a rich combo definition in a {@link CKEDITOR.ui.richCombo}\r
45  * instance.\r
46  * @type Object\r
47  * @example\r
48  */\r
49 CKEDITOR.ui.panel.handler =\r
50 {\r
51         create : function( definition )\r
52         {\r
53                 return new CKEDITOR.ui.panel( definition );\r
54         }\r
55 };\r
56 \r
57 CKEDITOR.ui.panel.prototype =\r
58 {\r
59         renderHtml : function( editor )\r
60         {\r
61                 var output = [];\r
62                 this.render( editor, output );\r
63                 return output.join( '' );\r
64         },\r
65 \r
66         /**\r
67          * Renders the combo.\r
68          * @param {CKEDITOR.editor} editor The editor instance which this button is\r
69          *              to be used by.\r
70          * @param {Array} output The output array to which append the HTML relative\r
71          *              to this button.\r
72          * @example\r
73          */\r
74         render : function( editor, output )\r
75         {\r
76                 var id = 'cke_' + this.id;\r
77 \r
78                 output.push(\r
79                         '<div class="', editor.skinClass ,'"' +\r
80                                 ' lang="', editor.langCode, '"' +\r
81                                 ' role="presentation"' +\r
82                                 // iframe loading need sometime, keep the panel hidden(#4186).\r
83                                 ' style="display:none;z-index:' + ( editor.config.baseFloatZIndex + 1 ) + '">' +\r
84                                 '<div' +\r
85                                         ' id=', id,\r
86                                         ' dir=', editor.lang.dir,\r
87                                         ' role="presentation"' +\r
88                                         ' class="cke_panel cke_', editor.lang.dir );\r
89 \r
90                 if ( this.className )\r
91                         output.push( ' ', this.className );\r
92 \r
93                 output.push(\r
94                                 '">' );\r
95 \r
96                 if ( this.forceIFrame || this.css.length )\r
97                 {\r
98                         output.push(\r
99                                                 '<iframe id="', id, '_frame"' +\r
100                                                         ' frameborder="0"' +\r
101                                                         ' role="application" src="javascript:void(' );\r
102 \r
103                         output.push(\r
104                                                         // Support for custom document.domain in IE.\r
105                                                         CKEDITOR.env.isCustomDomain() ?\r
106                                                                 '(function(){' +\r
107                                                                         'document.open();' +\r
108                                                                         'document.domain=\'' + document.domain + '\';' +\r
109                                                                         'document.close();' +\r
110                                                                 '})()'\r
111                                                         :\r
112                                                                 '0' );\r
113 \r
114                         output.push(\r
115                                                 ')"></iframe>' );\r
116                 }\r
117 \r
118                 output.push(\r
119                                 '</div>' +\r
120                         '</div>' );\r
121 \r
122                 return id;\r
123         },\r
124 \r
125         getHolderElement : function()\r
126         {\r
127                 var holder = this._.holder;\r
128 \r
129                 if ( !holder )\r
130                 {\r
131                         if ( this.forceIFrame || this.css.length )\r
132                         {\r
133                                 var iframe = this.document.getById( 'cke_' + this.id + '_frame' ),\r
134                                         parentDiv = iframe.getParent(),\r
135                                         dir = parentDiv.getAttribute( 'dir' ),\r
136                                         className = parentDiv.getParent().getAttribute( 'class' ),\r
137                                         langCode = parentDiv.getParent().getAttribute( 'lang' ),\r
138                                         doc = iframe.getFrameDocument();\r
139                                 // Initialize the IFRAME document body.\r
140                                 doc.$.open();\r
141 \r
142                                 // Support for custom document.domain in IE.\r
143                                 if ( CKEDITOR.env.isCustomDomain() )\r
144                                         doc.$.domain = document.domain;\r
145 \r
146                                 var onLoad = CKEDITOR.tools.addFunction( CKEDITOR.tools.bind( function( ev )\r
147                                         {\r
148                                                 this.isLoaded = true;\r
149                                                 if ( this.onLoad )\r
150                                                         this.onLoad();\r
151                                         }, this ) );\r
152 \r
153                                 doc.$.write(\r
154                                         '<!DOCTYPE html>' +\r
155                                         '<html dir="' + dir + '" class="' + className + '_container" lang="' + langCode + '">' +\r
156                                                 '<head>' +\r
157                                                         '<style>.' + className + '_container{visibility:hidden}</style>' +\r
158                                                 '</head>' +\r
159                                                 '<body class="cke_' + dir + ' cke_panel_frame ' + CKEDITOR.env.cssClass + '" style="margin:0;padding:0"' +\r
160                                                 ' onload="( window.CKEDITOR || window.parent.CKEDITOR ).tools.callFunction(' + onLoad + ');"></body>' +\r
161                                                 // It looks strange, but for FF2, the styles must go\r
162                                                 // after <body>, so it (body) becames immediatelly\r
163                                                 // available. (#3031)\r
164                                                 CKEDITOR.tools.buildStyleHtml( this.css ) +\r
165                                         '<\/html>' );\r
166                                 doc.$.close();\r
167 \r
168                                 var win = doc.getWindow();\r
169 \r
170                                 // Register the CKEDITOR global.\r
171                                 win.$.CKEDITOR = CKEDITOR;\r
172 \r
173                                 doc.on( 'keydown', function( evt )\r
174                                         {\r
175                                                 var keystroke = evt.data.getKeystroke(),\r
176                                                         dir = this.document.getById( 'cke_' + this.id ).getAttribute( 'dir' );\r
177 \r
178                                                 // Delegate key processing to block.\r
179                                                 if ( this._.onKeyDown && this._.onKeyDown( keystroke ) === false )\r
180                                                 {\r
181                                                         evt.data.preventDefault();\r
182                                                         return;\r
183                                                 }\r
184 \r
185                                                 // ESC/ARROW-LEFT(ltr) OR ARROW-RIGHT(rtl)\r
186                                                 if ( keystroke == 27 || keystroke == ( dir == 'rtl' ? 39 : 37 ) )\r
187                                                 {\r
188                                                         if ( this.onEscape && this.onEscape( keystroke ) === false )\r
189                                                                 evt.data.preventDefault( );\r
190                                                 }\r
191                                         },\r
192                                         this );\r
193 \r
194                                 holder = doc.getBody();\r
195                         }\r
196                         else\r
197                                 holder = this.document.getById( 'cke_' + this.id );\r
198 \r
199                         this._.holder = holder;\r
200                 }\r
201 \r
202                 return holder;\r
203         },\r
204 \r
205         addBlock : function( name, block )\r
206         {\r
207                 block = this._.blocks[ name ] = block instanceof CKEDITOR.ui.panel.block ?  block\r
208                                 : new CKEDITOR.ui.panel.block( this.getHolderElement(), block );\r
209 \r
210                 if ( !this._.currentBlock )\r
211                         this.showBlock( name );\r
212 \r
213                 return block;\r
214         },\r
215 \r
216         getBlock : function( name )\r
217         {\r
218                 return this._.blocks[ name ];\r
219         },\r
220 \r
221         showBlock : function( name )\r
222         {\r
223                 var blocks = this._.blocks,\r
224                         block = blocks[ name ],\r
225                         current = this._.currentBlock,\r
226                         holder = this.forceIFrame ?\r
227                                 this.document.getById( 'cke_' + this.id + '_frame' )\r
228                                 : this._.holder;\r
229 \r
230                 if ( current )\r
231                 {\r
232                         // Clean up the current block's effects on holder.\r
233                         holder.removeAttributes( current.attributes );\r
234                         current.hide();\r
235                 }\r
236 \r
237                 this._.currentBlock = block;\r
238 \r
239                 holder.setAttributes( block.attributes );\r
240                 CKEDITOR.fire( 'ariaWidget', holder );\r
241 \r
242                 // Reset the focus index, so it will always go into the first one.\r
243                 block._.focusIndex = -1;\r
244 \r
245                 this._.onKeyDown = block.onKeyDown && CKEDITOR.tools.bind( block.onKeyDown, block );\r
246 \r
247                 block.show();\r
248 \r
249                 return block;\r
250         },\r
251 \r
252         destroy : function()\r
253         {\r
254                 this.element && this.element.remove();\r
255         }\r
256 };\r
257 \r
258 CKEDITOR.ui.panel.block = CKEDITOR.tools.createClass(\r
259 {\r
260         $ : function( blockHolder, blockDefinition )\r
261         {\r
262                 this.element = blockHolder.append(\r
263                         blockHolder.getDocument().createElement( 'div',\r
264                                 {\r
265                                         attributes :\r
266                                         {\r
267                                                 'tabIndex' : -1,\r
268                                                 'class' : 'cke_panel_block',\r
269                                                 'role' : 'presentation'\r
270                                         },\r
271                                         styles :\r
272                                         {\r
273                                                 display : 'none'\r
274                                         }\r
275                                 }) );\r
276 \r
277                 // Copy all definition properties to this object.\r
278                 if ( blockDefinition )\r
279                         CKEDITOR.tools.extend( this, blockDefinition );\r
280 \r
281                 this.keys = {};\r
282 \r
283                 this._.focusIndex = -1;\r
284 \r
285                 // Disable context menu for panels.\r
286                 this.element.disableContextMenu();\r
287         },\r
288 \r
289         _ : {\r
290 \r
291                 /**\r
292                  * Mark the item specified by the index as current activated.\r
293                  */\r
294                 markItem: function( index )\r
295                 {\r
296                         if ( index == -1 )\r
297                                 return;\r
298                         var links = this.element.getElementsByTag( 'a' );\r
299                         var item = links.getItem( this._.focusIndex = index );\r
300 \r
301                         // Safari need focus on the iframe window first(#3389), but we need\r
302                         // lock the blur to avoid hiding the panel.\r
303                         if ( CKEDITOR.env.webkit )\r
304                                 item.getDocument().getWindow().focus();\r
305                         item.focus();\r
306                 }\r
307         },\r
308 \r
309         proto :\r
310         {\r
311                 show : function()\r
312                 {\r
313                         this.element.setStyle( 'display', '' );\r
314                 },\r
315 \r
316                 hide : function()\r
317                 {\r
318                         if ( !this.onHide || this.onHide.call( this )  !== true )\r
319                                 this.element.setStyle( 'display', 'none' );\r
320                 },\r
321 \r
322                 onKeyDown : function( keystroke )\r
323                 {\r
324                         var keyAction = this.keys[ keystroke ];\r
325                         switch ( keyAction )\r
326                         {\r
327                                 // Move forward.\r
328                                 case 'next' :\r
329                                         var index = this._.focusIndex,\r
330                                                 links = this.element.getElementsByTag( 'a' ),\r
331                                                 link;\r
332 \r
333                                         while ( ( link = links.getItem( ++index ) ) )\r
334                                         {\r
335                                                 // Move the focus only if the element is marked with\r
336                                                 // the _cke_focus and it it's visible (check if it has\r
337                                                 // width).\r
338                                                 if ( link.getAttribute( '_cke_focus' ) && link.$.offsetWidth )\r
339                                                 {\r
340                                                         this._.focusIndex = index;\r
341                                                         link.focus();\r
342                                                         break;\r
343                                                 }\r
344                                         }\r
345                                         return false;\r
346 \r
347                                 // Move backward.\r
348                                 case 'prev' :\r
349                                         index = this._.focusIndex;\r
350                                         links = this.element.getElementsByTag( 'a' );\r
351 \r
352                                         while ( index > 0 && ( link = links.getItem( --index ) ) )\r
353                                         {\r
354                                                 // Move the focus only if the element is marked with\r
355                                                 // the _cke_focus and it it's visible (check if it has\r
356                                                 // width).\r
357                                                 if ( link.getAttribute( '_cke_focus' ) && link.$.offsetWidth )\r
358                                                 {\r
359                                                         this._.focusIndex = index;\r
360                                                         link.focus();\r
361                                                         break;\r
362                                                 }\r
363                                         }\r
364                                         return false;\r
365 \r
366                                 case 'click' :\r
367                                         index = this._.focusIndex;\r
368                                         link = index >= 0 && this.element.getElementsByTag( 'a' ).getItem( index );\r
369 \r
370                                         if ( link )\r
371                                                 link.$.click ? link.$.click() : link.$.onclick();\r
372 \r
373                                         return false;\r
374                         }\r
375 \r
376                         return true;\r
377                 }\r
378         }\r
379 });\r