ui.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. /**
  6. * Contains UI features related to an editor instance.
  7. *
  8. * @class
  9. * @mixins CKEDITOR.event
  10. * @constructor Creates a `ui` class instance.
  11. * @param {CKEDITOR.editor} editor The editor instance.
  12. */
  13. CKEDITOR.ui = function( editor ) {
  14. if ( editor.ui )
  15. return editor.ui;
  16. this.items = {};
  17. this.instances = {};
  18. this.editor = editor;
  19. /**
  20. * Object used to store private stuff.
  21. *
  22. * @private
  23. */
  24. this._ = {
  25. handlers: {}
  26. };
  27. return this;
  28. };
  29. // PACKAGER_RENAME( CKEDITOR.ui )
  30. CKEDITOR.ui.prototype = {
  31. /**
  32. * Adds a UI item to the items collection. These items can be later used in
  33. * the interface.
  34. *
  35. * // Add a new button named 'MyBold'.
  36. * editorInstance.ui.add( 'MyBold', CKEDITOR.UI_BUTTON, {
  37. * label: 'My Bold',
  38. * command: 'bold'
  39. * } );
  40. *
  41. * @param {String} name The UI item name.
  42. * @param {Object} type The item type.
  43. * @param {Object} definition The item definition. The properties of this
  44. * object depend on the item type.
  45. */
  46. add: function( name, type, definition ) {
  47. // Compensate the unique name of this ui item to definition.
  48. definition.name = name.toLowerCase();
  49. var item = this.items[ name ] = {
  50. type: type,
  51. // The name of {@link CKEDITOR.command} which associate with this UI.
  52. command: definition.command || null,
  53. args: Array.prototype.slice.call( arguments, 2 )
  54. };
  55. CKEDITOR.tools.extend( item, definition );
  56. },
  57. /**
  58. * Retrieves the created UI objects by name.
  59. *
  60. * @param {String} name The name of the UI definition.
  61. */
  62. get: function( name ) {
  63. return this.instances[ name ];
  64. },
  65. /**
  66. * Gets a UI object.
  67. *
  68. * @param {String} name The UI item name.
  69. * @returns {Object} The UI element.
  70. */
  71. create: function( name ) {
  72. var item = this.items[ name ],
  73. handler = item && this._.handlers[ item.type ],
  74. command = item && item.command && this.editor.getCommand( item.command );
  75. var result = handler && handler.create.apply( this, item.args );
  76. this.instances[ name ] = result;
  77. // Add reference inside command object.
  78. if ( command )
  79. command.uiItems.push( result );
  80. if ( result && !result.type )
  81. result.type = item.type;
  82. return result;
  83. },
  84. /**
  85. * Adds a handler for a UI item type. The handler is responsible for
  86. * transforming UI item definitions into UI objects.
  87. *
  88. * @param {Object} type The item type.
  89. * @param {Object} handler The handler definition.
  90. */
  91. addHandler: function( type, handler ) {
  92. this._.handlers[ type ] = handler;
  93. },
  94. /**
  95. * Returns the unique DOM element that represents one editor's UI part, also known as "space".
  96. * There are 3 main editor spaces available: `top`, `contents` and `bottom`
  97. * and their availability depends on editor type.
  98. *
  99. * // Hide the bottom space in the UI.
  100. * var bottom = editor.ui.space( 'bottom' );
  101. * bottom.setStyle( 'display', 'none' );
  102. *
  103. * @param {String} name The name of the space.
  104. * @returns {CKEDITOR.dom.element} The element that represents the space.
  105. */
  106. space: function( name ) {
  107. return CKEDITOR.document.getById( this.spaceId( name ) );
  108. },
  109. /**
  110. * Returns the HTML ID for a specific UI space name.
  111. *
  112. * @param {String} name The name of the space.
  113. * @returns {String} The ID of an element representing this space in the DOM.
  114. */
  115. spaceId: function( name ) {
  116. return this.editor.id + '_' + name;
  117. }
  118. };
  119. CKEDITOR.event.implementOn( CKEDITOR.ui );
  120. /**
  121. * Internal event fired when a new UI element is ready.
  122. *
  123. * @event ready
  124. * @param {Object} data The new UI element.
  125. */
  126. /**
  127. * Virtual class which just illustrates the features of handler objects to be
  128. * passed to the {@link CKEDITOR.ui#addHandler} function.
  129. * This class is not really a part of the API, so do not call its constructor.
  130. *
  131. * @class CKEDITOR.ui.handlerDefinition
  132. */
  133. /**
  134. * Transforms an item definition into a UI item object.
  135. *
  136. * editorInstance.ui.addHandler( CKEDITOR.UI_BUTTON, {
  137. * create: function( definition ) {
  138. * return new CKEDITOR.ui.button( definition );
  139. * }
  140. * } );
  141. *
  142. * @method create
  143. * @param {Object} definition The item definition.
  144. * @returns {Object} The UI element.
  145. * @todo We lack the "UI element" abstract super class.
  146. */
  147. /**
  148. * The element in the {@link CKEDITOR#document host page's document} that contains the editor content.
  149. * If the [fixed editor UI](#!/guide/dev_uitypes-section-fixed-user-interface) is used, then it will be set to
  150. * `editor.ui.space( 'contents' )` &mdash; i.e. the `<div>` which contains the editor `<iframe>` (in case of classic editor)
  151. * or {@link CKEDITOR.editable} (in case of inline editor). Otherwise it is set to the {@link CKEDITOR.editable} itself.
  152. *
  153. * Use the position of this element if you need to position elements placed in the host page's document relatively to the
  154. * editor content.
  155. *
  156. * var editor = CKEDITOR.instances.editor1;
  157. * console.log( editor.ui.contentsElement.getName() ); // 'div'
  158. *
  159. * @since 4.5
  160. * @readonly
  161. * @property {CKEDITOR.dom.element} contentsElement
  162. */