3 Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved.
4 For licensing, see LICENSE.md or http://ckeditor.com/license
9 <title>Using API to Customize Dialog Windows
— CKEditor Sample
</title>
10 <script src=
"../../../ckeditor.js"></script>
11 <link rel=
"stylesheet" href=
"../../../samples/sample.css">
12 <meta name=
"ckeditor-sample-name" content=
"Using the JavaScript API to customize dialog windows">
13 <meta name=
"ckeditor-sample-group" content=
"Advanced Samples">
14 <meta name=
"ckeditor-sample-description" content=
"Using the dialog windows API to customize dialog windows without changing the original editor code.">
17 .cke_button__mybutton_icon
19 display: none !important;
22 .cke_button__mybutton_label
24 display: inline !important;
30 CKEDITOR.on( 'instanceCreated', function( ev ){
31 var editor = ev.editor;
33 // Listen for the
"pluginsLoaded" event, so we are sure that the
34 //
"dialog" plugin has been loaded and we are able to do our
36 editor.on( 'pluginsLoaded', function() {
38 // If our custom dialog has not been registered, do that now.
39 if ( !CKEDITOR.dialog.exists( 'myDialog' ) ) {
40 // We need to do the following trick to find out the dialog
41 // definition file URL path. In the real world, you would simply
42 // point to an absolute path directly, like
"/mydir/mydialog.js".
43 var href = document.location.href.split( '/' );
45 href.push( 'assets/my_dialog.js' );
46 href = href.join( '/' );
48 // Finally, register the dialog.
49 CKEDITOR.dialog.add( 'myDialog', href );
52 // Register the command used to open the dialog.
53 editor.addCommand( 'myDialogCmd', new CKEDITOR.dialogCommand( 'myDialog' ) );
55 // Add the a custom toolbar buttons, which fires the above
57 editor.ui.add( 'MyButton', CKEDITOR.UI_BUTTON, {
59 command: 'myDialogCmd'
64 // When opening a dialog, its
"definition" is created for it, for
65 // each editor instance. The
"dialogDefinition" event is then
66 // fired. We should use this event to make customizations to the
67 // definition of existing dialogs.
68 CKEDITOR.on( 'dialogDefinition', function( ev ) {
69 // Take the dialog name and its definition from the event data.
70 var dialogName = ev.data.name;
71 var dialogDefinition = ev.data.definition;
73 // Check if the definition is from the dialog we're
74 // interested on (the
"Link" dialog).
75 if ( dialogName == 'myDialog' && ev.editor.name == 'editor2' ) {
76 // Get a reference to the
"Link Info" tab.
77 var infoTab = dialogDefinition.getContents( 'tab1' );
79 // Add a new text field to the
"tab1" tab page.
82 label: 'My Custom Field',
85 validate: function() {
86 if ( ( /\d/ ).test( this.getValue() ) )
87 return 'My Custom Field must not contain digits';
91 // Remove the
"select1" field from the
"tab1" tab.
92 infoTab.remove( 'select1' );
94 // Set the default value for
"input1" field.
95 var input1 = infoTab.get( 'input1' );
96 input1[ 'default' ] = 'www.example.com';
98 // Remove the
"tab2" tab page.
99 dialogDefinition.removeContents( 'tab2' );
101 // Add a new tab to the
"Link" dialog.
102 dialogDefinition.addContents( {
110 label: 'My Text Field'
115 label: 'Another Text Field'
120 // Provide the focus handler to start initial focus in
"customField" field.
121 dialogDefinition.onFocus = function() {
122 var urlField = this.getContentElement( 'tab1', 'customField' );
129 extraPlugins: 'dialog',
130 toolbar: [ [ 'MyButton' ] ]
137 <a href=
"../../../samples/index.html">CKEditor Samples
</a> » Using CKEditor Dialog API
139 <div class=
"description">
141 This sample shows how to use the
142 <a class=
"samples" href=
"http://docs.ckeditor.com/#!/api/CKEDITOR.dialog">CKEditor Dialog API
</a>
143 to customize CKEditor dialog windows without changing the original editor code.
144 The following customizations are being done in the example below:
147 For details on how to create this setup check the source code of this sample page.
150 <p>A custom dialog is added to the editors using the
<code>pluginsLoaded
</code> event, from an external
<a target=
"_blank" href=
"assets/my_dialog.js">dialog definition file
</a>:
</p>
152 <li><strong>Creating a custom dialog window
</strong> – "My Dialog" dialog window opened with the
"My Dialog" toolbar button.
</li>
153 <li><strong>Creating a custom button
</strong> – Add button to open the dialog with
"My Dialog" toolbar button.
</li>
155 <textarea cols=
"80" id=
"editor1" name=
"editor1" rows=
"10"><p
>This is some
<strong
>sample text
</strong
>. You are using
<a
href=
"http://ckeditor.com/">CKEditor
</a
>.
</p
></textarea>
157 // Replace the
<textarea id=
"editor1"> with an CKEditor instance.
158 CKEDITOR.replace( 'editor1', config );
160 <p>The below editor modify the dialog definition of the above added dialog using the
<code>dialogDefinition
</code> event:
</p>
162 <li><strong>Adding dialog tab
</strong> – Add new tab
"My Tab" to dialog window.
</li>
163 <li><strong>Removing a dialog window tab
</strong> – Remove
"Second Tab" page from the dialog window.
</li>
164 <li><strong>Adding dialog window fields
</strong> – Add
"My Custom Field" to the dialog window.
</li>
165 <li><strong>Removing dialog window field
</strong> – Remove
"Select Field" selection field from the dialog window.
</li>
166 <li><strong>Setting default values for dialog window fields
</strong> – Set default value of
"Text Field" text field.
</li>
167 <li><strong>Setup initial focus for dialog window
</strong> – Put initial focus on
"My Custom Field" text field.
</li>
169 <textarea cols=
"80" id=
"editor2" name=
"editor2" rows=
"10"><p
>This is some
<strong
>sample text
</strong
>. You are using
<a
href=
"http://ckeditor.com/">CKEditor
</a
>.
</p
></textarea>
172 // Replace the
<textarea id=
"editor1"> with an CKEditor instance.
173 CKEDITOR.replace( 'editor2', config );
179 CKEditor - The text editor for the Internet -
<a class=
"samples" href=
"http://ckeditor.com/">http://ckeditor.com
</a>
182 Copyright
© 2003-
2014,
<a class=
"samples" href=
"http://cksource.com/">CKSource
</a> - Frederico
183 Knabben. All rights reserved.