This example adds a button to the Rich Text Editor's Toolbar that displays a Calendar Control for choosing dates. It also demonstrates how to manage the state of a custom button.
Click the Date button () in the Toolbar to display the Calendar Control, then select a date and it will be placed inside the Editor.
You can also click on a date that has been inserted in the Editor and the Date button will enable. When the Date button is enabled and you click it the corresponding date will be selected in the Calendar Control when it is displayed.
Setting up the Editor's HTML is done by creating a
textarea control on the page.
textarea is on the page, then initialize the Editor like this:
Now we can create a button and add it to the Editor's Toolbar. First we subscribe to the Editor's
toolbarLoaded Custom Event.
From inside that function we will set up a new button config object literal with the following properties:
YAHOO.widget.Overlayinstance to be used as a menu.
Now add it to the Toolbar group called "insertitem" like this:
After we have created the new button and added it to the Toolbar, we need to listen for events to trigger our new button.
We can do that by listening to the
before/afterNodeChange events are fired when something interesting happens inside the Editor. Clicks, Key Presses, etc.
From inside the
afterNodeChange Event we can get access the the last element (or current element) that was affected. We get access to this element via:
Now that we have a reference we can use standard DOM manipulation to change the element and the Toolbar.
In this case, we are grabbing the current element and checking to see if it or its parent has a className of date.
If it does, then we are populating the var
selectedDate with the
innerHTML of the element.
We are also using this opportunity to select the "insertdate" button on the toolbar, so that it becomes active. We are doing this with the following command:
this.toolbar.getButtonByValue('insertdate') this method will return a Button reference from the toolbar, that we can pass to
this.toolbar.selectButton() and cause the button to be selected.
On the next
nodeChange Event, our button will be disabled automatically and this handler will re-select it if it is needed.
Now we add this code at the bottom to be activated when the Element
cal1Container becomes available in the DOM.
Once that Element is active on the page, we can now build and render our Calendar control.
Notice, that we are subscribing to the Calendar's
selectEvent to actually execute the insertdateClick event for the Editor.
From there, we get a button reference and call the menu's hide method to hide the Calendar.
There are 2 important states to style a button in the toolbar.
First is the default state, that can be accessed via this CSS rule:
.yui-skin-sam .yui-toolbar-container .yui-toolbar-insertdate span.yui-toolbar-icon
Second is the selected state, that can be accessed via this CSS rule:
.yui-skin-sam .yui-toolbar-container .yui-button-insertdate-selected span.yui-toolbar-icon
.yui-toolbar-container is the class applied to the top-most container of the toolbar.
.yui-toolbar-icon is an extra
SPAN injected into the button for spriting an image.
.yui-toolbar-VALUE is a dynamic class added to the button based on the
value passed into the buttons config. It is used for specific styling of a button that may appear in several places on the page.
Note: Logging and debugging is currently turned off for this example.