dialog.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!DOCTYPE html>
  2. <!--
  3. Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  4. For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  5. -->
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <title>Using API to Customize Dialog Windows &mdash; CKEditor Sample</title>
  10. <script src="../../../ckeditor.js"></script>
  11. <link rel="stylesheet" href="../../../samples/old/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.">
  15. <style>
  16. .cke_button__mybutton_icon
  17. {
  18. display: none !important;
  19. }
  20. .cke_button__mybutton_label
  21. {
  22. display: inline !important;
  23. }
  24. </style>
  25. <script>
  26. CKEDITOR.on( 'instanceCreated', function( ev ){
  27. var editor = ev.editor;
  28. // Listen for the "pluginsLoaded" event, so we are sure that the
  29. // "dialog" plugin has been loaded and we are able to do our
  30. // customizations.
  31. editor.on( 'pluginsLoaded', function() {
  32. // If our custom dialog has not been registered, do that now.
  33. if ( !CKEDITOR.dialog.exists( 'myDialog' ) ) {
  34. // We need to do the following trick to find out the dialog
  35. // definition file URL path. In the real world, you would simply
  36. // point to an absolute path directly, like "/mydir/mydialog.js".
  37. var href = document.location.href.split( '/' );
  38. href.pop();
  39. href.push( 'assets/my_dialog.js' );
  40. href = href.join( '/' );
  41. // Finally, register the dialog.
  42. CKEDITOR.dialog.add( 'myDialog', href );
  43. }
  44. // Register the command used to open the dialog.
  45. editor.addCommand( 'myDialogCmd', new CKEDITOR.dialogCommand( 'myDialog' ) );
  46. // Add the a custom toolbar buttons, which fires the above
  47. // command..
  48. editor.ui.add( 'MyButton', CKEDITOR.UI_BUTTON, {
  49. label: 'My Dialog',
  50. command: 'myDialogCmd'
  51. });
  52. });
  53. });
  54. // When opening a dialog, its "definition" is created for it, for
  55. // each editor instance. The "dialogDefinition" event is then
  56. // fired. We should use this event to make customizations to the
  57. // definition of existing dialogs.
  58. CKEDITOR.on( 'dialogDefinition', function( ev ) {
  59. // Take the dialog name and its definition from the event data.
  60. var dialogName = ev.data.name;
  61. var dialogDefinition = ev.data.definition;
  62. // Check if the definition is from the dialog we're
  63. // interested on (the "Link" dialog).
  64. if ( dialogName == 'myDialog' && ev.editor.name == 'editor2' ) {
  65. // Get a reference to the "Link Info" tab.
  66. var infoTab = dialogDefinition.getContents( 'tab1' );
  67. // Add a new text field to the "tab1" tab page.
  68. infoTab.add( {
  69. type: 'text',
  70. label: 'My Custom Field',
  71. id: 'customField',
  72. 'default': 'Sample!',
  73. validate: function() {
  74. if ( ( /\d/ ).test( this.getValue() ) )
  75. return 'My Custom Field must not contain digits';
  76. }
  77. });
  78. // Remove the "select1" field from the "tab1" tab.
  79. infoTab.remove( 'select1' );
  80. // Set the default value for "input1" field.
  81. var input1 = infoTab.get( 'input1' );
  82. input1[ 'default' ] = 'www.example.com';
  83. // Remove the "tab2" tab page.
  84. dialogDefinition.removeContents( 'tab2' );
  85. // Add a new tab to the "Link" dialog.
  86. dialogDefinition.addContents( {
  87. id: 'customTab',
  88. label: 'My Tab',
  89. accessKey: 'M',
  90. elements: [
  91. {
  92. id: 'myField1',
  93. type: 'text',
  94. label: 'My Text Field'
  95. },
  96. {
  97. id: 'myField2',
  98. type: 'text',
  99. label: 'Another Text Field'
  100. }
  101. ]
  102. });
  103. // Provide the focus handler to start initial focus in "customField" field.
  104. dialogDefinition.onFocus = function() {
  105. var urlField = this.getContentElement( 'tab1', 'customField' );
  106. urlField.select();
  107. };
  108. }
  109. });
  110. var config = {
  111. extraPlugins: 'dialog',
  112. toolbar: [ [ 'MyButton' ] ]
  113. };
  114. </script>
  115. </head>
  116. <body>
  117. <h1 class="samples">
  118. <a href="../../../samples/old/index.html">CKEditor Samples</a> &raquo; Using CKEditor Dialog API
  119. </h1>
  120. <div class="warning deprecated">
  121. This sample is not maintained anymore. Check out the <a href="https://ckeditor.com/docs/ckeditor4/latest/examples/index.html">brand new samples in CKEditor Examples</a>.
  122. </div>
  123. <div class="description">
  124. <p>
  125. This sample shows how to use the
  126. <a class="samples" href="https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_dialog.html">CKEditor Dialog API</a>
  127. to customize CKEditor dialog windows without changing the original editor code.
  128. The following customizations are being done in the example below:
  129. </p>
  130. <p>
  131. For details on how to create this setup check the source code of this sample page.
  132. </p>
  133. </div>
  134. <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>
  135. <ol>
  136. <li><strong>Creating a custom dialog window</strong> &ndash; "My Dialog" dialog window opened with the "My Dialog" toolbar button.</li>
  137. <li><strong>Creating a custom button</strong> &ndash; Add button to open the dialog with "My Dialog" toolbar button.</li>
  138. </ol>
  139. <textarea cols="80" id="editor1" name="editor1" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="https://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
  140. <script>
  141. // Replace the <textarea id="editor1"> with an CKEditor instance.
  142. CKEDITOR.replace( 'editor1', config );
  143. </script>
  144. <p>The below editor modify the dialog definition of the above added dialog using the <code>dialogDefinition</code> event:</p>
  145. <ol>
  146. <li><strong>Adding dialog tab</strong> &ndash; Add new tab "My Tab" to dialog window.</li>
  147. <li><strong>Removing a dialog window tab</strong> &ndash; Remove "Second Tab" page from the dialog window.</li>
  148. <li><strong>Adding dialog window fields</strong> &ndash; Add "My Custom Field" to the dialog window.</li>
  149. <li><strong>Removing dialog window field</strong> &ndash; Remove "Select Field" selection field from the dialog window.</li>
  150. <li><strong>Setting default values for dialog window fields</strong> &ndash; Set default value of "Text Field" text field. </li>
  151. <li><strong>Setup initial focus for dialog window</strong> &ndash; Put initial focus on "My Custom Field" text field. </li>
  152. </ol>
  153. <textarea cols="80" id="editor2" name="editor2" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="https://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
  154. <script>
  155. // Replace the <textarea id="editor1"> with an CKEditor instance.
  156. CKEDITOR.replace( 'editor2', config );
  157. </script>
  158. <div id="footer">
  159. <hr>
  160. <p>
  161. CKEditor - The text editor for the Internet - <a class="samples" href="https://ckeditor.com/">https://ckeditor.com</a>
  162. </p>
  163. <p id="copy">
  164. Copyright &copy; 2003-2019, <a class="samples" href="https://cksource.com/">CKSource</a> - Frederico
  165. Knabben. All rights reserved.
  166. </p>
  167. </div>
  168. </body>
  169. </html>