api_dialog.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <!--
  3. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
  4. For licensing, see LICENSE.html or http://ckeditor.com/license
  5. -->
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <title>Using API to Customize Dialog Windows &mdash; CKEditor Sample</title>
  9. <meta content="text/html; charset=utf-8" http-equiv="content-type" />
  10. <script type="text/javascript" src="../ckeditor.js"></script>
  11. <script src="sample.js" type="text/javascript"></script>
  12. <link href="sample.css" rel="stylesheet" type="text/css" />
  13. <style id="styles" type="text/css">
  14. .cke_button_myDialogCmd .cke_icon
  15. {
  16. display: none !important;
  17. }
  18. .cke_button_myDialogCmd .cke_label
  19. {
  20. display: inline !important;
  21. }
  22. </style>
  23. <script type="text/javascript">
  24. //<![CDATA[
  25. // When opening a dialog, its "definition" is created for it, for
  26. // each editor instance. The "dialogDefinition" event is then
  27. // fired. We should use this event to make customizations to the
  28. // definition of existing dialogs.
  29. CKEDITOR.on( 'dialogDefinition', function( ev )
  30. {
  31. // Take the dialog name and its definition from the event
  32. // data.
  33. var dialogName = ev.data.name;
  34. var dialogDefinition = ev.data.definition;
  35. // Check if the definition is from the dialog we're
  36. // interested on (the "Link" dialog).
  37. if ( dialogName == 'link' )
  38. {
  39. // Get a reference to the "Link Info" tab.
  40. var infoTab = dialogDefinition.getContents( 'info' );
  41. // Add a text field to the "info" tab.
  42. infoTab.add( {
  43. type : 'text',
  44. label : 'My Custom Field',
  45. id : 'customField',
  46. 'default' : 'Sample!',
  47. validate : function()
  48. {
  49. if ( /\d/.test( this.getValue() ) )
  50. return 'My Custom Field must not contain digits';
  51. }
  52. });
  53. // Remove the "Link Type" combo and the "Browser
  54. // Server" button from the "info" tab.
  55. infoTab.remove( 'linkType' );
  56. infoTab.remove( 'browse' );
  57. // Set the default value for the URL field.
  58. var urlField = infoTab.get( 'url' );
  59. urlField['default'] = 'www.example.com';
  60. // Remove the "Target" tab from the "Link" dialog.
  61. dialogDefinition.removeContents( 'target' );
  62. // Add a new tab to the "Link" dialog.
  63. dialogDefinition.addContents({
  64. id : 'customTab',
  65. label : 'My Tab',
  66. accessKey : 'M',
  67. elements : [
  68. {
  69. id : 'myField1',
  70. type : 'text',
  71. label : 'My Text Field'
  72. },
  73. {
  74. id : 'myField2',
  75. type : 'text',
  76. label : 'Another Text Field'
  77. }
  78. ]
  79. });
  80. // Rewrite the 'onFocus' handler to always focus 'url' field.
  81. dialogDefinition.onFocus = function()
  82. {
  83. var urlField = this.getContentElement( 'info', 'url' );
  84. urlField.select();
  85. };
  86. }
  87. });
  88. //]]>
  89. </script>
  90. </head>
  91. <body>
  92. <h1 class="samples">
  93. CKEditor Sample &mdash; Using CKEditor Dialog API
  94. </h1>
  95. <div class="description">
  96. <p>
  97. This sample shows how to use the
  98. <a class="samples" href="http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.html">CKEditor Dialog API</a>
  99. to customize CKEditor dialog windows without changing the original editor code.
  100. The following customizations are being done in the example below:
  101. </p>
  102. <ol>
  103. <li><strong>Adding dialog window tabs</strong> &ndash; "My Tab" in the "Link" dialog window.</li>
  104. <li><strong>Removing a dialog window tab</strong> &ndash; "Target" tab from the "Link" dialog window.</li>
  105. <li><strong>Adding dialog window fields</strong> &ndash; "My Custom Field" in the "Link" dialog window.</li>
  106. <li><strong>Removing dialog window fields</strong> &ndash; "Link Type" and "Browse Server" in the "Link"
  107. dialog window.</li>
  108. <li><strong>Setting default values for dialog window fields</strong> &ndash; "URL" field in the
  109. "Link" dialog window. </li>
  110. <li><strong>Creating a custom dialog window</strong> &ndash; "My Dialog" dialog window opened with the "My Dialog" toolbar button.</li>
  111. </ol>
  112. <p>
  113. For details on how to create this setup check the source code of this sample page.
  114. </p>
  115. </div>
  116. <!-- This <div> holds alert messages to be display in the sample page. -->
  117. <div id="alerts">
  118. <noscript>
  119. <p>
  120. <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
  121. support, like yours, you should still see the contents (HTML data) and you should
  122. be able to edit it normally, without a rich editor interface.
  123. </p>
  124. </noscript>
  125. </div>
  126. <!-- This <fieldset> holds the HTML that you will usually find in your
  127. pages. -->
  128. <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>
  129. <script type="text/javascript">
  130. //<![CDATA[
  131. // Replace the <textarea id="editor1"> with an CKEditor instance.
  132. var editor = CKEDITOR.replace( 'editor1',
  133. {
  134. // Defines a simpler toolbar to be used in this sample.
  135. // Note that we have added out "MyButton" button here.
  136. toolbar : [ [ 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike','-','Link', '-', 'MyButton' ] ]
  137. });
  138. // Listen for the "pluginsLoaded" event, so we are sure that the
  139. // "dialog" plugin has been loaded and we are able to do our
  140. // customizations.
  141. editor.on( 'pluginsLoaded', function( ev )
  142. {
  143. // If our custom dialog has not been registered, do that now.
  144. if ( !CKEDITOR.dialog.exists( 'myDialog' ) )
  145. {
  146. // We need to do the following trick to find out the dialog
  147. // definition file URL path. In the real world, you would simply
  148. // point to an absolute path directly, like "/mydir/mydialog.js".
  149. var href = document.location.href.split( '/' );
  150. href.pop();
  151. href.push( 'api_dialog', 'my_dialog.js' );
  152. href = href.join( '/' );
  153. // Finally, register the dialog.
  154. CKEDITOR.dialog.add( 'myDialog', href );
  155. }
  156. // Register the command used to open the dialog.
  157. editor.addCommand( 'myDialogCmd', new CKEDITOR.dialogCommand( 'myDialog' ) );
  158. // Add the a custom toolbar buttons, which fires the above
  159. // command..
  160. editor.ui.addButton( 'MyButton',
  161. {
  162. label : 'My Dialog',
  163. command : 'myDialogCmd'
  164. } );
  165. });
  166. //]]>
  167. </script>
  168. <div id="footer">
  169. <hr />
  170. <p>
  171. CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>
  172. </p>
  173. <p id="copy">
  174. Copyright &copy; 2003-2011, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico
  175. Knabben. All rights reserved.
  176. </p>
  177. </div>
  178. </body>
  179. </html>