Jx.Button

Extends: Object

Implements: Options, Events, Jx.Addable

Jx.Button creates a clickable element that can be added to a web page.  When the button is clicked, it fires a ‘click’ event.

The CSS styling for a button is controlled by several classes related to the various objects in the button’s HTML structure:

<div class="jxButtonContainer">
 <a class="jxButton">
  <span class="jxButtonContent">
   <img class="jxButtonIcon" src="image_url">
   <span class="jxButtonLabel">button label</span>
  </span>
 </a>
</div>

The CSS classes will change depending on the type option passed to the constructor of the button.  The default type is Button.  Passing another value such as Tab will cause all the CSS classes to change from jxButton to jxTab.  For example:

<div class="jxTabContainer">
 <a class="jxTab">
  <span class="jxTabContent">
   <img class="jxTabIcon" src="image_url">
   <span class="jxTabLabel">tab label</span>
  </span>
 </a>
</div>

When you construct a new instance of Jx.Button, the button does not automatically get inserted into the web page.  Typically a button is used as part of building another capability such as a Jx.Toolbar.  However, if you want to manually insert the button into your application, you may use the addTo method to append or insert the button into the page.

There are two modes for a button, normal and toggle.  A toggle button has an active state analogous to a checkbox.  A toggle button generates different events (down and up) from a normal button (click).  To create a toggle button, pass toggle: true to the Jx.Button constructor.

To use a Jx.Button in an application, you should to register for the ‘click’ event.  You can pass a function in the ‘onClick’ option when constructing a button or you can call the addEvent(‘click’, myFunction) method.  The addEvent method can be called several times, allowing more than one function to be called when a button is clicked.  You can use the removeEvent(‘click’, myFunction) method to stop receiving click events.

Example

var button = new Jx.Button(options);
button.addTo('myListItem'); // the id of an LI in the page.
Example:
var options = {
    imgPath: 'images/mybutton.png',
    tooltip: 'click me!',
    label: 'click me',
    onClick: function() {
        alert('you clicked me');
    }
};
var button = new Jx.Button(options);
button.addEvent('click', anotherFunction);

function anotherFunction() {
  alert('a second alert for a single click');
}

Events

clickthe button was pressed and released (only if type is not ‘toggle’).
downthe button is down (only if type is ‘toggle’)
upthe button is up (only if the type is ‘toggle’).

License

Copyright © 2008, DM Solutions Group Inc.

This file is licensed under an MIT style license

Summary
Jx.ButtonExtends: Object
Options
idoptional.
typeoptional.
imageoptional.
tooltipoptional.
labeloptional, default is no label.
toggledefault true, whether the button is a toggle button or not.
toggleClassdefaults to Toggle, this is class is added to buttons with the option toggle: true
halignhorizontal alignment of the button label, ‘center’ by default.
valign{String} vertical alignment of the button label, ‘middle’ by default.
activeoptional, default false.
enabledwhether the button is enabled or not.
containerthe tag name of the HTML element that should be created to contain the button, by default this is ‘div’.
Constructor
Jx.Buttoncreate a new button.
Functions
clickedtriggered when the user clicks the button, processes the actionPerformed event
isEnabledThis returns true if the button is enabled, false otherwise
setEnabledenable or disable the button.
isActiveFor toggle buttons, this returns true if the toggle button is currently active and false otherwise.
setActiveSet the active state of the button
setImageset the image of this button to a new image URL
setLabelsets the text of the button.
getLabelreturns the text of the button.
setTooltipsets the tooltip displayed by the button
focuscapture the keyboard focus on this button
blurremove the keyboard focus from this button

Options

id

optional.  A string value to use as the ID of the button container.

type

optional.  A string value that indicates what type of button this is.  The default value is Button.  The type is used to form the CSS class names used for various HTML elements within the button.

image

optional.  A string value that is the url to load the image to display in this button.  The default styles size this image to 16 x 16.  If not provided, then the button will have no icon.

tooltip

optional.  A string value to use as the alt/title attribute of the <A> tag that wraps the button, resulting in a tooltip that appears when the user hovers the mouse over a button in most browsers.  If not provided, the button will have no tooltip.

label

optional, default is no label.  A string value that is used as a label on the button.

toggle

default true, whether the button is a toggle button or not.

toggleClass

defaults to Toggle, this is class is added to buttons with the option toggle: true

halign

horizontal alignment of the button label, ‘center’ by default.  Other values are ‘left’ and ‘right’.

valign

{String} vertical alignment of the button label, ‘middle’ by default.  Other values are ‘top’ and ‘bottom’.

active

optional, default false.  Controls the initial state of toggle buttons.

enabled

whether the button is enabled or not.

container

the tag name of the HTML element that should be created to contain the button, by default this is ‘div’.

Constructor

Jx.Button

create a new button.

Parameters

options{Object} an object containing optional properties for this button as below.

Functions

clicked

clicked : function(evt)

triggered when the user clicks the button, processes the actionPerformed event

Parameters

evt{Event} the user click event

isEnabled

isEnabled: function()

This returns true if the button is enabled, false otherwise

Returns

{Boolean} whether the button is enabled or not

setEnabled

setEnabled: function(enabled)

enable or disable the button.

Parameters

enabled{Boolean} the new enabled state of the button

isActive

isActive: function()

For toggle buttons, this returns true if the toggle button is currently active and false otherwise.

Returns

{Boolean} the active state of a toggle button

setActive

setActive: function(active)

Set the active state of the button

Parameters

active{Boolean} the new active state of the button

setImage

setImage: function(path)

set the image of this button to a new image URL

Parameters

path{String} the new url to use as the image for this button

setLabel

setLabel: function(label)

sets the text of the button.  Only works if a label was supplied when the button was constructed

Parameters

label{String} the new label for the button

getLabel

getLabel: function()

returns the text of the button.

setTooltip

setTooltip: function(tooltip)

sets the tooltip displayed by the button

Parameters

tooltip{String} the new tooltip

focus

focus: function()

capture the keyboard focus on this button

blur

blur: function()

remove the keyboard focus from this button

clicked : function(evt)
triggered when the user clicks the button, processes the actionPerformed event
isEnabled: function()
This returns true if the button is enabled, false otherwise
setEnabled: function(enabled)
enable or disable the button.
isActive: function()
For toggle buttons, this returns true if the toggle button is currently active and false otherwise.
setActive: function(active)
Set the active state of the button
setImage: function(path)
set the image of this button to a new image URL
setLabel: function(label)
sets the text of the button.
getLabel: function()
returns the text of the button.
setTooltip: function(tooltip)
sets the tooltip displayed by the button
focus: function()
capture the keyboard focus on this button
blur: function()
remove the keyboard focus from this button
A mix-in class that provides a helper function that allows an object to be added to an existing element on the page.