You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
7.0 KiB

  1. <!DOCTYPE html>
  2. <!--
  3. Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved.
  4. For licensing, see LICENSE.md or http://ckeditor.com/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/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/index.html">CKEditor Samples</a> &raquo; Using CKEditor Dialog API
  119. </h1>
  120. <div class="description">
  121. <p>
  122. This sample shows how to use the
  123. <a class="samples" href="http://docs.ckeditor.com/#!/api/CKEDITOR.dialog">CKEditor Dialog API</a>
  124. to customize CKEditor dialog windows without changing the original editor code.
  125. The following customizations are being done in the example below:
  126. </p>
  127. <p>
  128. For details on how to create this setup check the source code of this sample page.
  129. </p>
  130. </div>
  131. <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>
  132. <ol>
  133. <li><strong>Creating a custom dialog window</strong> &ndash; "My Dialog" dialog window opened with the "My Dialog" toolbar button.</li>
  134. <li><strong>Creating a custom button</strong> &ndash; Add button to open the dialog with "My Dialog" toolbar button.</li>
  135. </ol>
  136. <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="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
  137. <script>
  138. // Replace the <textarea id="editor1"> with an CKEditor instance.
  139. CKEDITOR.replace( 'editor1', config );
  140. </script>
  141. <p>The below editor modify the dialog definition of the above added dialog using the <code>dialogDefinition</code> event:</p>
  142. <ol>
  143. <li><strong>Adding dialog tab</strong> &ndash; Add new tab "My Tab" to dialog window.</li>
  144. <li><strong>Removing a dialog window tab</strong> &ndash; Remove "Second Tab" page from the dialog window.</li>
  145. <li><strong>Adding dialog window fields</strong> &ndash; Add "My Custom Field" to the dialog window.</li>
  146. <li><strong>Removing dialog window field</strong> &ndash; Remove "Select Field" selection field from the dialog window.</li>
  147. <li><strong>Setting default values for dialog window fields</strong> &ndash; Set default value of "Text Field" text field. </li>
  148. <li><strong>Setup initial focus for dialog window</strong> &ndash; Put initial focus on "My Custom Field" text field. </li>
  149. </ol>
  150. <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="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
  151. <script>
  152. // Replace the <textarea id="editor1"> with an CKEditor instance.
  153. CKEDITOR.replace( 'editor2', config );
  154. </script>
  155. <div id="footer">
  156. <hr>
  157. <p>
  158. CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>
  159. </p>
  160. <p id="copy">
  161. Copyright &copy; 2003-2014, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico
  162. Knabben. All rights reserved.
  163. </p>
  164. </div>
  165. </body>
  166. </html>