1 /*
  2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5
  6 CKEDITOR.plugins.add( 'panelbutton',
  7 {
  8 	requires : [ 'button' ],
  9 	beforeInit : function( editor )
 10 	{
 11 		editor.ui.addHandler( CKEDITOR.UI_PANELBUTTON, CKEDITOR.ui.panelButton.handler );
 12 	}
 13 });
 14
 15 /**
 16  * Button UI element.
 17  * @constant
 18  * @example
 19  */
 20 CKEDITOR.UI_PANELBUTTON = 4;
 21
 22 (function()
 23 {
 24 	var clickFn = function( editor )
 25 	{
 26 		var _ = this._;
 27
 28 		if ( _.state == CKEDITOR.TRISTATE_DISABLED )
 29 			return;
 30
 31 		this.createPanel( editor );
 32
 33 		if ( _.on )
 34 		{
 35 			_.panel.hide();
 36 			return;
 37 		}
 38
 39 		_.panel.showBlock( this._.id, this.document.getById( this._.id ), 4 );
 40 	};
 41
 42
 43 	CKEDITOR.ui.panelButton = CKEDITOR.tools.createClass(
 44 	{
 45 		base : CKEDITOR.ui.button,
 46
 47 		$ : function( definition )
 48 		{
 49 			// We don't want the panel definition in this object.
 50 			var panelDefinition = definition.panel;
 51 			delete definition.panel;
 52
 53 			this.base( definition );
 54
 55 			this.document = ( panelDefinition
 56 								&& panelDefinition.parent
 57 								&& panelDefinition.parent.getDocument() )
 58 							|| CKEDITOR.document;
 59
 60 			this.hasArrow = true;
 61
 62 			this.click = clickFn;
 63
 64 			this._ =
 65 			{
 66 				panelDefinition : panelDefinition
 67 			};
 68 		},
 69
 70 		statics :
 71 		{
 72 			handler :
 73 			{
 74 				create : function( definition )
 75 				{
 76 					return new CKEDITOR.ui.panelButton( definition );
 77 				}
 78 			}
 79 		},
 80
 81 		proto :
 82 		{
 83 			createPanel : function( editor )
 84 			{
 85 				var _ = this._;
 86
 87 				if ( _.panel )
 88 					return;
 89
 90 				var panelDefinition = this._.panelDefinition || {},
 91 					panelParentElement = panelDefinition.parent || CKEDITOR.document.getBody(),
 92 					panel = this._.panel = new CKEDITOR.ui.floatPanel( editor, panelParentElement, panelDefinition ),
 93 					me = this;
 94
 95 				panel.onShow = function()
 96 					{
 97 						if ( me.className )
 98 							this.element.getFirst().addClass( me.className + '_panel' );
 99
100 						_.oldState = me._.state;
101 						me.setState( CKEDITOR.TRISTATE_ON );
102
103 						_.on = 1;
104
105 						if ( me.onOpen )
106 							me.onOpen();
107 					};
108
109 				panel.onHide = function()
110 					{
111 						if ( me.className )
112 							this.element.getFirst().removeClass( me.className + '_panel' );
113
114 						me.setState( _.oldState );
115
116 						_.on = 0;
117
118 						if ( me.onClose )
119 							me.onClose();
120 					};
121
122 				panel.onEscape = function()
123 					{
124 						panel.hide();
125 						me.document.getById( _.id ).focus();
126 					};
127
128 				if ( this.onBlock )
129 					this.onBlock( panel, _.id );
130
131 				panel.getBlock( _.id ).onHide = function()
132 						{
133 								_.on = 0;
134 								me.setState( CKEDITOR.TRISTATE_OFF );
135 						};
136 			}
137 		}
138 	});
139
140 })();
141