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