JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
c7c6adb5dea676c2eb7e95d580310655ba0efc27
[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                                 holder.unselectable();\r
196                         }\r
197                         else\r
198                                 holder = this.document.getById( 'cke_' + this.id );\r
199 \r
200                         this._.holder = holder;\r
201                 }\r
202 \r
203                 return holder;\r
204         },\r
205 \r
206         addBlock : function( name, block )\r
207         {\r
208                 block = this._.blocks[ name ] = block instanceof CKEDITOR.ui.panel.block ?  block\r
209                                 : new CKEDITOR.ui.panel.block( this.getHolderElement(), block );\r
210 \r
211                 if ( !this._.currentBlock )\r
212                         this.showBlock( name );\r
213 \r
214                 return block;\r
215         },\r
216 \r
217         getBlock : function( name )\r
218         {\r
219                 return this._.blocks[ name ];\r
220         },\r
221 \r
222         showBlock : function( name )\r
223         {\r
224                 var blocks = this._.blocks,\r
225                         block = blocks[ name ],\r
226                         current = this._.currentBlock,\r
227                         holder = this.forceIFrame ?\r
228                                 this.document.getById( 'cke_' + this.id + '_frame' )\r
229                                 : this._.holder;\r
230 \r
231                 // Disable context menu for block panel.\r
232                 holder.getParent().getParent().disableContextMenu();\r
233 \r
234                 if ( current )\r
235                 {\r
236                         // Clean up the current block's effects on holder.\r
237                         holder.removeAttributes( current.attributes );\r
238                         current.hide();\r
239                 }\r
240 \r
241                 this._.currentBlock = block;\r
242 \r
243                 holder.setAttributes( block.attributes );\r
244                 CKEDITOR.fire( 'ariaWidget', holder );\r
245 \r
246                 // Reset the focus index, so it will always go into the first one.\r
247                 block._.focusIndex = -1;\r
248 \r
249                 this._.onKeyDown = block.onKeyDown && CKEDITOR.tools.bind( block.onKeyDown, block );\r
250 \r
251                 block.onMark = function( item )\r
252                 {\r
253                         holder.setAttribute( 'aria-activedescendant', item.getId() + '_option' );\r
254                 };\r
255 \r
256                 block.onUnmark = function()\r
257                 {\r
258                         holder.removeAttribute( 'aria-activedescendant' );\r
259                 };\r
260 \r
261                 block.show();\r
262 \r
263                 return block;\r
264         },\r
265 \r
266         destroy : function()\r
267         {\r
268                 this.element && this.element.remove();\r
269         }\r
270 };\r
271 \r
272 CKEDITOR.ui.panel.block = CKEDITOR.tools.createClass(\r
273 {\r
274         $ : function( blockHolder, blockDefinition )\r
275         {\r
276                 this.element = blockHolder.append(\r
277                         blockHolder.getDocument().createElement( 'div',\r
278                                 {\r
279                                         attributes :\r
280                                         {\r
281                                                 'tabIndex' : -1,\r
282                                                 'class' : 'cke_panel_block',\r
283                                                 'role' : 'presentation'\r
284                                         },\r
285                                         styles :\r
286                                         {\r
287                                                 display : 'none'\r
288                                         }\r
289                                 }) );\r
290 \r
291                 // Copy all definition properties to this object.\r
292                 if ( blockDefinition )\r
293                         CKEDITOR.tools.extend( this, blockDefinition );\r
294 \r
295                 if ( !this.attributes.title )\r
296                         this.attributes.title = this.attributes[ 'aria-label' ];\r
297 \r
298                 this.keys = {};\r
299 \r
300                 this._.focusIndex = -1;\r
301 \r
302                 // Disable context menu for panels.\r
303                 this.element.disableContextMenu();\r
304         },\r
305 \r
306         _ : {\r
307 \r
308                 /**\r
309                  * Mark the item specified by the index as current activated.\r
310                  */\r
311                 markItem: function( index )\r
312                 {\r
313                         if ( index == -1 )\r
314                                 return;\r
315                         var links = this.element.getElementsByTag( 'a' );\r
316                         var item = links.getItem( this._.focusIndex = index );\r
317 \r
318                         // Safari need focus on the iframe window first(#3389), but we need\r
319                         // lock the blur to avoid hiding the panel.\r
320                         if ( CKEDITOR.env.webkit )\r
321                                 item.getDocument().getWindow().focus();\r
322                         item.focus();\r
323 \r
324                         this.onMark && this.onMark( item );\r
325                 }\r
326         },\r
327 \r
328         proto :\r
329         {\r
330                 show : function()\r
331                 {\r
332                         this.element.setStyle( 'display', '' );\r
333                 },\r
334 \r
335                 hide : function()\r
336                 {\r
337                         if ( !this.onHide || this.onHide.call( this )  !== true )\r
338                                 this.element.setStyle( 'display', 'none' );\r
339                 },\r
340 \r
341                 onKeyDown : function( keystroke )\r
342                 {\r
343                         var keyAction = this.keys[ keystroke ];\r
344                         switch ( keyAction )\r
345                         {\r
346                                 // Move forward.\r
347                                 case 'next' :\r
348                                         var index = this._.focusIndex,\r
349                                                 links = this.element.getElementsByTag( 'a' ),\r
350                                                 link;\r
351 \r
352                                         while ( ( 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                                 // Move backward.\r
367                                 case 'prev' :\r
368                                         index = this._.focusIndex;\r
369                                         links = this.element.getElementsByTag( 'a' );\r
370 \r
371                                         while ( index > 0 && ( link = links.getItem( --index ) ) )\r
372                                         {\r
373                                                 // Move the focus only if the element is marked with\r
374                                                 // the _cke_focus and it it's visible (check if it has\r
375                                                 // width).\r
376                                                 if ( link.getAttribute( '_cke_focus' ) && link.$.offsetWidth )\r
377                                                 {\r
378                                                         this._.focusIndex = index;\r
379                                                         link.focus();\r
380                                                         break;\r
381                                                 }\r
382                                         }\r
383                                         return false;\r
384 \r
385                                 case 'click' :\r
386                                         index = this._.focusIndex;\r
387                                         link = index >= 0 && this.element.getElementsByTag( 'a' ).getItem( index );\r
388 \r
389                                         if ( link )\r
390                                                 link.$.click ? link.$.click() : link.$.onclick();\r
391 \r
392                                         return false;\r
393                         }\r
394 \r
395                         return true;\r
396                 }\r
397         }\r
398 });\r