1 /*
  2  * CKEditor - The text editor for Internet - http://ckeditor.com
  3  * Copyright (C) 2003-2008 Frederico Caldeira Knabben
  4  *
  5  * == BEGIN LICENSE ==
  6  *
  7  * Licensed under the terms of any of the following licenses at your
  8  * choice:
  9  *
 10  *  - GNU General Public License Version 2 or later (the "GPL")
 11  *    http://www.gnu.org/licenses/gpl.html
 12  *
 13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14  *    http://www.gnu.org/licenses/lgpl.html
 15  *
 16  *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17  *    http://www.mozilla.org/MPL/MPL-1.1.html
 18  *
 19  * == END LICENSE ==
 20  */
 21
 22 CKEDITOR.plugins.add( 'button',
 23 {
 24 	beforeInit : function( editor )
 25 	{
 26 		editor.ui.addHandler( CKEDITOR.UI_BUTTON, CKEDITOR.ui.button.handler );
 27 	}
 28 });
 29
 30 /**
 31  * Button UI element.
 32  * @constant
 33  * @example
 34  */
 35 CKEDITOR.UI_BUTTON = 1;
 36
 37 /**
 38  * Represents a button UI element. This class should not be called directly. To
 39  * create new buttons use {@link CKEDITOR.ui.prototype.addButton} instead.
 40  * @constructor
 41  * @param {Object} definition The button definition.
 42  * @example
 43  */
 44 CKEDITOR.ui.button = function( definition )
 45 {
 46 	/**
 47 	 * The button label.
 48 	 * @name CKEDITOR.ui.button.prototype.label
 49 	 * @type String
 50 	 * @example
 51 	 */
 52 	this.label = definition.label;
 53
 54 	/**
 55 	 * The command name associated to the button. If no command is defined, the
 56 	 * "click" event is used.
 57 	 * @name CKEDITOR.ui.button.prototype.command
 58 	 * @type String
 59 	 * @example
 60 	 */
 61 	this.command = definition.command;
 62
 63 	/**
 64 	 * The button advisory title. It is usually displayed as the button tooltip.
 65 	 * If not defined, the label is used.
 66 	 * @name CKEDITOR.ui.button.prototype.title
 67 	 * @type String
 68 	 * @example
 69 	 */
 70 	this.title = definition.title || this.label;
 71
 72 	/**
 73 	 * The function to be called when the user clicks the button. If not
 74 	 * defined, the "command" property is required, and the command gets
 75 	 * executed on click.
 76 	 * @function
 77 	 * @name CKEDITOR.ui.button.prototype.click
 78 	 * @type Function
 79 	 * @example
 80 	 */
 81 	this.click = definition.click || function( editor )
 82 		{
 83 			editor.execCommand( definition.command );
 84 		};
 85 };
 86
 87 /**
 88  * Transforms a button definition in a {@link CKEDITOR.ui.button} instance.
 89  * @example
 90  */
 91 CKEDITOR.ui.button.handler =
 92 {
 93 	create : function( definition )
 94 	{
 95 		return new CKEDITOR.ui.button( definition );
 96 	}
 97 };
 98
 99 CKEDITOR.ui.button.prototype =
100 {
101 	/**
102 	 * Renders the button.
103 	 * @param {CKEDITOR.editor} editor The editor instance which this button is
104 	 *		to be used by.
105 	 * @param {Array} output The output array to which append the HTML relative
106 	 *		to this button.
107 	 * @example
108 	 */
109 	render : function( editor, output )
110 	{
111 		output.push(
112 			'<a id="cke_', CKEDITOR.tools.getNextNumber(),
113 				'" class="cke_button" href="do:', this.label,
114 				'" title="', this.title,
115 				'" onclick="return CKEDITOR.ui.button._.click(\'', editor.name, '\',\'', this.name, '\', this.id);">',
116 			this.label, '</a>' );
117 	}
118 };
119
120 /**
121  * Handles a button click.
122  * @private
123  */
124 CKEDITOR.ui.button._ =
125 {
126 	click : function( instanceName, uiItemName, elementId )
127 	{
128 		var editor = CKEDITOR.instances[ instanceName ];
129 		editor.focus();
130
131 		var uiItem = editor.ui.get( uiItemName ).click( editor, elementId );
132
133 		return false;
134 	}
135 };
136
137 /**
138  * Adds a button definition to the UI elements list.
139  * @param {String} The button name.
140  * @param {Object} The button definition.
141  * @example
142  * editorInstance.ui.addButton( 'MyBold',
143  *     {
144  *         label : 'My Bold',
145  *         command : 'bold'
146  *     });
147  */
148 CKEDITOR.ui.prototype.addButton = function( name, definition )
149 {
150 	this.add( name, CKEDITOR.UI_BUTTON, definition );
151 };
152