JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.2
[ckeditor.git] / _source / plugins / panel / plugin.js
1 /*\r
2 Copyright (c) 2003-2011, 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 = 'panel';\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.getNextId();\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 = 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( 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 \r
140                                 // Make it scrollable on iOS. (#8308)\r
141                                 CKEDITOR.env.iOS && parentDiv.setStyles(\r
142                                         {\r
143                                                 'overflow' : 'scroll',\r
144                                                 '-webkit-overflow-scrolling' : 'touch'\r
145                                         });\r
146 \r
147                                 var onLoad = CKEDITOR.tools.addFunction( CKEDITOR.tools.bind( function( ev )\r
148                                         {\r
149                                                 this.isLoaded = true;\r
150                                                 if ( this.onLoad )\r
151                                                         this.onLoad();\r
152                                         }, this ) );\r
153 \r
154                                 var data =\r
155                                         '<!DOCTYPE html>' +\r
156                                         '<html dir="' + dir + '" class="' + className + '_container" lang="' + langCode + '">' +\r
157                                                 '<head>' +\r
158                                                         '<style>.' + className + '_container{visibility:hidden}</style>' +\r
159                                                 '</head>' +\r
160                                                 '<body class="cke_' + dir + ' cke_panel_frame ' + CKEDITOR.env.cssClass + '" style="margin:0;padding:0"' +\r
161                                                 ' onload="( window.CKEDITOR || window.parent.CKEDITOR ).tools.callFunction(' + onLoad + ');"></body>' +\r
162                                                 // It looks strange, but for FF2, the styles must go\r
163                                                 // after <body>, so it (body) becames immediatelly\r
164                                                 // available. (#3031)\r
165                                                 CKEDITOR.tools.buildStyleHtml( this.css ) +\r
166                                         '<\/html>';\r
167 \r
168                                 doc.write( data );\r
169 \r
170                                 var win = doc.getWindow();\r
171 \r
172                                 // Register the CKEDITOR global.\r
173                                 win.$.CKEDITOR = CKEDITOR;\r
174 \r
175                                 // Arrow keys for scrolling is only preventable with 'keypress' event in Opera (#4534).\r
176                                 doc.on( 'key' + ( CKEDITOR.env.opera? 'press':'down' ), function( evt )\r
177                                         {\r
178                                                 var keystroke = evt.data.getKeystroke(),\r
179                                                         dir = this.document.getById( this.id ).getAttribute( 'dir' );\r
180 \r
181                                                 // Delegate key processing to block.\r
182                                                 if ( this._.onKeyDown && this._.onKeyDown( keystroke ) === false )\r
183                                                 {\r
184                                                         evt.data.preventDefault();\r
185                                                         return;\r
186                                                 }\r
187 \r
188                                                 // ESC/ARROW-LEFT(ltr) OR ARROW-RIGHT(rtl)\r
189                                                 if ( keystroke == 27 || keystroke == ( dir == 'rtl' ? 39 : 37 ) )\r
190                                                 {\r
191                                                         if ( this.onEscape && this.onEscape( keystroke ) === false )\r
192                                                                 evt.data.preventDefault();\r
193                                                 }\r
194                                         },\r
195                                         this );\r
196 \r
197                                 holder = doc.getBody();\r
198                                 holder.unselectable();\r
199                                 CKEDITOR.env.air && CKEDITOR.tools.callFunction( onLoad );\r
200                         }\r
201                         else\r
202                                 holder = this.document.getById( this.id );\r
203 \r
204                         this._.holder = holder;\r
205                 }\r
206 \r
207                 return holder;\r
208         },\r
209 \r
210         addBlock : function( name, block )\r
211         {\r
212                 block = this._.blocks[ name ] = block instanceof CKEDITOR.ui.panel.block ?  block\r
213                                 : new CKEDITOR.ui.panel.block( this.getHolderElement(), block );\r
214 \r
215                 if ( !this._.currentBlock )\r
216                         this.showBlock( name );\r
217 \r
218                 return block;\r
219         },\r
220 \r
221         getBlock : function( name )\r
222         {\r
223                 return this._.blocks[ name ];\r
224         },\r
225 \r
226         showBlock : function( name )\r
227         {\r
228                 var blocks = this._.blocks,\r
229                         block = blocks[ name ],\r
230                         current = this._.currentBlock,\r
231                         holder = this.forceIFrame ?\r
232                                 this.document.getById( this.id + '_frame' )\r
233                                 : this._.holder;\r
234 \r
235                 // Disable context menu for block panel.\r
236                 holder.getParent().getParent().disableContextMenu();\r
237 \r
238                 if ( current )\r
239                 {\r
240                         // Clean up the current block's effects on holder.\r
241                         holder.removeAttributes( current.attributes );\r
242                         current.hide();\r
243                 }\r
244 \r
245                 this._.currentBlock = block;\r
246 \r
247                 holder.setAttributes( block.attributes );\r
248                 CKEDITOR.fire( 'ariaWidget', holder );\r
249 \r
250                 // Reset the focus index, so it will always go into the first one.\r
251                 block._.focusIndex = -1;\r
252 \r
253                 this._.onKeyDown = block.onKeyDown && CKEDITOR.tools.bind( block.onKeyDown, block );\r
254 \r
255                 block.show();\r
256 \r
257                 return block;\r
258         },\r
259 \r
260         destroy : function()\r
261         {\r
262                 this.element && this.element.remove();\r
263         }\r
264 };\r
265 \r
266 CKEDITOR.ui.panel.block = CKEDITOR.tools.createClass(\r
267 {\r
268         $ : function( blockHolder, blockDefinition )\r
269         {\r
270                 this.element = blockHolder.append(\r
271                         blockHolder.getDocument().createElement( 'div',\r
272                                 {\r
273                                         attributes :\r
274                                         {\r
275                                                 'tabIndex' : -1,\r
276                                                 'class' : 'cke_panel_block',\r
277                                                 'role' : 'presentation'\r
278                                         },\r
279                                         styles :\r
280                                         {\r
281                                                 display : 'none'\r
282                                         }\r
283                                 }) );\r
284 \r
285                 // Copy all definition properties to this object.\r
286                 if ( blockDefinition )\r
287                         CKEDITOR.tools.extend( this, blockDefinition );\r
288 \r
289                 if ( !this.attributes.title )\r
290                         this.attributes.title = this.attributes[ 'aria-label' ];\r
291 \r
292                 this.keys = {};\r
293 \r
294                 this._.focusIndex = -1;\r
295 \r
296                 // Disable context menu for panels.\r
297                 this.element.disableContextMenu();\r
298         },\r
299 \r
300         _ : {\r
301 \r
302                 /**\r
303                  * Mark the item specified by the index as current activated.\r
304                  */\r
305                 markItem: function( index )\r
306                 {\r
307                         if ( index == -1 )\r
308                                 return;\r
309                         var links = this.element.getElementsByTag( 'a' );\r
310                         var item = links.getItem( this._.focusIndex = index );\r
311 \r
312                         // Safari need focus on the iframe window first(#3389), but we need\r
313                         // lock the blur to avoid hiding the panel.\r
314                         if ( CKEDITOR.env.webkit || CKEDITOR.env.opera )\r
315                                 item.getDocument().getWindow().focus();\r
316                         item.focus();\r
317 \r
318                         this.onMark && this.onMark( item );\r
319                 }\r
320         },\r
321 \r
322         proto :\r
323         {\r
324                 show : function()\r
325                 {\r
326                         this.element.setStyle( 'display', '' );\r
327                 },\r
328 \r
329                 hide : function()\r
330                 {\r
331                         if ( !this.onHide || this.onHide.call( this )  !== true )\r
332                                 this.element.setStyle( 'display', 'none' );\r
333                 },\r
334 \r
335                 onKeyDown : function( keystroke )\r
336                 {\r
337                         var keyAction = this.keys[ keystroke ];\r
338                         switch ( keyAction )\r
339                         {\r
340                                 // Move forward.\r
341                                 case 'next' :\r
342                                         var index = this._.focusIndex,\r
343                                                 links = this.element.getElementsByTag( 'a' ),\r
344                                                 link;\r
345 \r
346                                         while ( ( link = links.getItem( ++index ) ) )\r
347                                         {\r
348                                                 // Move the focus only if the element is marked with\r
349                                                 // the _cke_focus and it it's visible (check if it has\r
350                                                 // width).\r
351                                                 if ( link.getAttribute( '_cke_focus' ) && link.$.offsetWidth )\r
352                                                 {\r
353                                                         this._.focusIndex = index;\r
354                                                         link.focus();\r
355                                                         break;\r
356                                                 }\r
357                                         }\r
358                                         return false;\r
359 \r
360                                 // Move backward.\r
361                                 case 'prev' :\r
362                                         index = this._.focusIndex;\r
363                                         links = this.element.getElementsByTag( 'a' );\r
364 \r
365                                         while ( index > 0 && ( link = links.getItem( --index ) ) )\r
366                                         {\r
367                                                 // Move the focus only if the element is marked with\r
368                                                 // the _cke_focus and it it's visible (check if it has\r
369                                                 // width).\r
370                                                 if ( link.getAttribute( '_cke_focus' ) && link.$.offsetWidth )\r
371                                                 {\r
372                                                         this._.focusIndex = index;\r
373                                                         link.focus();\r
374                                                         break;\r
375                                                 }\r
376                                         }\r
377                                         return false;\r
378 \r
379                                 case 'click' :\r
380                                 case 'mouseup' :\r
381                                         index = this._.focusIndex;\r
382                                         link = index >= 0 && this.element.getElementsByTag( 'a' ).getItem( index );\r
383 \r
384                                         if ( link )\r
385                                                 link.$[ keyAction ] ? link.$[ keyAction ]() : link.$[ 'on' + keyAction ]();\r
386 \r
387                                         return false;\r
388                         }\r
389 \r
390                         return true;\r
391                 }\r
392         }\r
393 });\r
394 \r
395 /**\r
396  * Fired when a panel is added to the document\r
397  * @name CKEDITOR#ariaWidget\r
398  * @event\r
399  * @param {Object} holder The element wrapping the panel\r
400  */\r