I’m writing a custom dialog/plugin for ckeditor. What I want to know is how I can add an eventlistener to a select box in the dialog, to alert when the selected value has been changed. How can I do this?
I’ve looked at the API and I’ve come across some useful information but it is not detailed enough. I can’t make a connection between the API information and what I am trying to implement.
The select elements in the dialogs automatically fire a change event when they are changed. You can add an onChange function in the definition for the select element. Here’s an example from the api:
These pages have examples for creating definitions used by dialogs and ui elements:
Class CKEDITOR.dialog
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.html
Class CKEDITOR.dialog.definition
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.definition.html
Class CKEDITOR.dialog.definition.select
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.definition.select.html
If you would like to listen for a change to a select element from another location, you can create a listener that keys on the “dialogShow” event. Here’s an example:
You can check if the dialog you want to work with is the one that fired the “dialogShow” event. If so, you can create an object for the select element you’re interested in and listen for a “change” event.
Note: the TAB-ID and ELEMENT-ID placeholders I used do not refer to the Id attribute of the element. The Id refers to the Id assigned in the dialog definition file. The Id attribute for the various elements are different each time the dialog is loaded.
This page has some info on events:
Class CKEDITOR.event
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.event.html
Be Well,
Joe
Answers to additional questions asked in comments:
Q1) Your event here is ‘dialogShow’, what other events are allowed? i.e are they pre-defined or user defined?
A1) The ‘dialogShow’ event is predefined. You can look at the file containing the dialogs code in your CKEditor install directory or on the website:
ckeditor\_source\plugins\dialog\plugin.js
http://docs.cksource.com/ckeditor_api/symbols/src/plugins_dialog_plugin.js.html
If you search the file for ‘fire’, you’ll see all the events that are fired for dialogs. The end of the file has definitions for the various events.
You can also define your own events to key on by using the “fire” method in your dialog code:
Then watch for it:
Q2) Can you explain the syntax
dialogShowEventData._.name? I’ve seen it before but i don’t know the significance, something to do with private variables?A2) For anyone wondering about the “._.” syntax used in the CKEditor code, it’s used to reduce the size of the code and replaces “.private.” See this post by @AlfonsoML in the CKEditor forum:
http://cksource.com/forums/viewtopic.php?t=22982
Q3) Where can i get more info on TAB-ID.ELEMENT-ID?
A3) The Tab-ID is the id that you assign to a particular tab (pane) of a dialog. ( see below: id : ‘tab1’, )
The Element-ID is the id that you assign to a particular element contained in a tab in your dialog. ( see below: id : ‘textareaId’, )
Look at this page: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.html#.add
It shows how you define the tabs and elements contained in a dialog window ( I added an example of a select element that fires a user defined event ):
Q4) Can you briefly explain what the above code is doing?
A4) See the original code, I’ve added comments.