6f0753179740fae46a600915e5666fa49c2bb778
[ckeditor.git] / _source / plugins / menubutton / plugin.js
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
5
6 CKEDITOR.plugins.add( 'menubutton',
7 {
8 requires : [ 'button', 'menu' ],
9 beforeInit : function( editor )
10 {
11 editor.ui.addHandler( CKEDITOR.UI_MENUBUTTON, CKEDITOR.ui.menuButton.handler );
12 }
13 });
14
15 /**
16 * Button UI element.
17 * @constant
18 * @example
19 */
20 CKEDITOR.UI_MENUBUTTON = 'menubutton';
21
22 (function()
23 {
24 var clickFn = function( editor )
25 {
26 var _ = this._;
27
28 // Do nothing if this button is disabled.
29 if ( _.state === CKEDITOR.TRISTATE_DISABLED )
30 return;
31
32 _.previousState = _.state;
33
34 // Check if we already have a menu for it, otherwise just create it.
35 var menu = _.menu;
36 if ( !menu )
37 {
38 menu = _.menu = new CKEDITOR.menu( editor,
39 {
40 panel:
41 {
42 className : editor.skinClass + ' cke_contextmenu',
43 attributes : { 'aria-label' : editor.lang.common.options }
44 }
45 });
46
47 menu.onHide = CKEDITOR.tools.bind( function()
48 {
49 this.setState( this.modes && this.modes[ editor.mode ] ? _.previousState : CKEDITOR.TRISTATE_DISABLED );
50 },
51 this );
52
53 // Initialize the menu items at this point.
54 if ( this.onMenu )
55 menu.addListener( this.onMenu );
56 }
57
58 if ( _.on )
59 {
60 menu.hide();
61 return;
62 }
63
64 this.setState( CKEDITOR.TRISTATE_ON );
65
66 menu.show( CKEDITOR.document.getById( this._.id ), 4 );
67 };
68
69
70 CKEDITOR.ui.menuButton = CKEDITOR.tools.createClass(
71 {
72 base : CKEDITOR.ui.button,
73
74 $ : function( definition )
75 {
76 // We don't want the panel definition in this object.
77 var panelDefinition = definition.panel;
78 delete definition.panel;
79
80 this.base( definition );
81
82 this.hasArrow = true;
83
84 this.click = clickFn;
85 },
86
87 statics :
88 {
89 handler :
90 {
91 create : function( definition )
92 {
93 return new CKEDITOR.ui.menuButton( definition );
94 }
95 }
96 }
97 });
98 })();