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