view.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module ui/view
  7. */
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import ViewCollection from './viewcollection';
  10. import Template from './template';
  11. import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
  12. import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
  13. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  14. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  15. import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
  16. import '../theme/globals/globals.css';
  17. /**
  18. * The basic view class, which represents an HTML element created out of a
  19. * {@link module:ui/view~View#template}. Views are building blocks of the user interface and handle
  20. * interaction
  21. *
  22. * Views {@link module:ui/view~View#registerChildren aggregate} children in
  23. * {@link module:ui/view~View#createCollection collections} and manage the life cycle of DOM
  24. * listeners e.g. by handling rendering and destruction.
  25. *
  26. * See the {@link module:ui/template~TemplateDefinition} syntax to learn more about shaping view
  27. * elements, attributes and listeners.
  28. *
  29. * class SampleView extends View {
  30. * constructor( locale ) {
  31. * super( locale );
  32. *
  33. * const bind = this.bindTemplate;
  34. *
  35. * // Views define their interface (state) using observable attributes.
  36. * this.set( 'elementClass', 'bar' );
  37. *
  38. * this.setTemplate( {
  39. * tag: 'p',
  40. *
  41. * // The element of the view can be defined with its children.
  42. * children: [
  43. * 'Hello',
  44. * {
  45. * tag: 'b',
  46. * children: [ 'world!' ]
  47. * }
  48. * ],
  49. * attributes: {
  50. * class: [
  51. * 'foo',
  52. *
  53. * // Observable attributes control the state of the view in DOM.
  54. * bind.to( 'elementClass' )
  55. * ]
  56. * },
  57. * on: {
  58. * // Views listen to DOM events and propagate them.
  59. * click: bind.to( 'clicked' )
  60. * }
  61. * } );
  62. * }
  63. * }
  64. *
  65. * const view = new SampleView( locale );
  66. *
  67. * view.render();
  68. *
  69. * // Append <p class="foo bar">Hello<b>world</b></p> to the <body>
  70. * document.body.appendChild( view.element );
  71. *
  72. * // Change the class attribute to <p class="foo baz">Hello<b>world</b></p>
  73. * view.elementClass = 'baz';
  74. *
  75. * // Respond to the "click" event in DOM by executing a custom action.
  76. * view.on( 'clicked', () => {
  77. * console.log( 'The view has been clicked!' );
  78. * } );
  79. *
  80. * @mixes module:utils/observablemixin~ObservableMixin
  81. */
  82. export default class View {
  83. /**
  84. * Creates an instance of the {@link module:ui/view~View} class.
  85. *
  86. * Also see {@link #render}.
  87. *
  88. * @param {module:utils/locale~Locale} [locale] The localization services instance.
  89. */
  90. constructor( locale ) {
  91. /**
  92. * An HTML element of the view. `null` until {@link #render rendered}
  93. * from the {@link #template}.
  94. *
  95. * class SampleView extends View {
  96. * constructor() {
  97. * super();
  98. *
  99. * // A template instance the #element will be created from.
  100. * this.setTemplate( {
  101. * tag: 'p'
  102. *
  103. * // ...
  104. * } );
  105. * }
  106. * }
  107. *
  108. * const view = new SampleView();
  109. *
  110. * // Renders the #template
  111. * view.render();
  112. *
  113. * // Append the HTML element of the view to <body>.
  114. * document.body.appendChild( view.element );
  115. *
  116. * **Note**: The element of the view can also be assigned directly:
  117. *
  118. * view.element = document.querySelector( '#my-container' );
  119. *
  120. * @member {HTMLElement}
  121. */
  122. this.element = null;
  123. /**
  124. * Set `true` when the view has already been {@link module:ui/view~View#render rendered}.
  125. *
  126. * @readonly
  127. * @member {Boolean} #isRendered
  128. */
  129. this.isRendered = false;
  130. /**
  131. * A set of tools to localize the user interface.
  132. *
  133. * Also see {@link module:core/editor/editor~Editor#locale}.
  134. *
  135. * @readonly
  136. * @member {module:utils/locale~Locale}
  137. */
  138. this.locale = locale;
  139. /**
  140. * Shorthand for {@link module:utils/locale~Locale#t}.
  141. *
  142. * Note: If {@link #locale} instance hasn't been passed to the view this method may not
  143. * be available.
  144. *
  145. * @see module:utils/locale~Locale#t
  146. * @method
  147. */
  148. this.t = locale && locale.t;
  149. /**
  150. * Collections registered with {@link #createCollection}.
  151. *
  152. * @protected
  153. * @member {Set.<module:ui/viewcollection~ViewCollection>}
  154. */
  155. this._viewCollections = new Collection();
  156. /**
  157. * A collection of view instances, which have been added directly
  158. * into the {@link module:ui/template~Template#children}.
  159. *
  160. * @protected
  161. * @member {module:ui/viewcollection~ViewCollection}
  162. */
  163. this._unboundChildren = this.createCollection();
  164. // Pass parent locale to its children.
  165. this._viewCollections.on( 'add', ( evt, collection ) => {
  166. collection.locale = locale;
  167. } );
  168. /**
  169. * Template of this view. It provides the {@link #element} representing
  170. * the view in DOM, which is {@link #render rendered}.
  171. *
  172. * @member {module:ui/template~Template} #template
  173. */
  174. /**
  175. * Cached {@link @link module:ui/template~BindChain bind chain} object created by the
  176. * {@link #template}. See {@link #bindTemplate}.
  177. *
  178. * @private
  179. * @member {Object} #_bindTemplate
  180. */
  181. }
  182. /**
  183. * Shorthand for {@link module:ui/template~Template.bind}, a binding
  184. * {@link module:ui/template~BindChain interface} pre–configured for the view instance.
  185. *
  186. * It provides {@link module:ui/template~BindChain#to `to()`} and
  187. * {@link module:ui/template~BindChain#if `if()`} methods that initialize bindings with
  188. * observable attributes and attach DOM listeners.
  189. *
  190. * class SampleView extends View {
  191. * constructor( locale ) {
  192. * super( locale );
  193. *
  194. * const bind = this.bindTemplate;
  195. *
  196. * // These {@link module:utils/observablemixin~Observable observable} attributes will control
  197. * // the state of the view in DOM.
  198. * this.set( {
  199. * elementClass: 'foo',
  200. * isEnabled: true
  201. * } );
  202. *
  203. * this.setTemplate( {
  204. * tag: 'p',
  205. *
  206. * attributes: {
  207. * // The class HTML attribute will follow elementClass
  208. * // and isEnabled view attributes.
  209. * class: [
  210. * bind.to( 'elementClass' )
  211. * bind.if( 'isEnabled', 'present-when-enabled' )
  212. * ]
  213. * },
  214. *
  215. * on: {
  216. * // The view will fire the "clicked" event upon clicking <p> in DOM.
  217. * click: bind.to( 'clicked' )
  218. * }
  219. * } );
  220. * }
  221. * }
  222. *
  223. * @method #bindTemplate
  224. */
  225. get bindTemplate() {
  226. if ( this._bindTemplate ) {
  227. return this._bindTemplate;
  228. }
  229. return ( this._bindTemplate = Template.bind( this, this ) );
  230. }
  231. /**
  232. * Creates a new collection of views, which can be used as
  233. * {@link module:ui/template~Template#children} of this view.
  234. *
  235. * class SampleView extends View {
  236. * constructor( locale ) {
  237. * super( locale );
  238. *
  239. * this.items = this.createCollection();
  240. *
  241. * this.setTemplate( {
  242. * tag: 'p',
  243. *
  244. * // `items` collection will render here.
  245. * children: this.items
  246. * } );
  247. * }
  248. * }
  249. *
  250. * const view = new SampleView( locale );
  251. * const child = new ChildView( locale );
  252. *
  253. * view.render();
  254. *
  255. * // It will append <p></p> to the <body>.
  256. * document.body.appendChild( view.element );
  257. *
  258. * // From now on the child is nested under its parent, which is also reflected in DOM.
  259. * // <p><child#element></p>
  260. * view.items.add( child );
  261. *
  262. * @returns {module:ui/viewcollection~ViewCollection} A new collection of view instances.
  263. */
  264. createCollection() {
  265. const collection = new ViewCollection();
  266. this._viewCollections.add( collection );
  267. return collection;
  268. }
  269. /**
  270. * Registers a new child view under the view instance. Once registered, a child
  271. * view is managed by its parent, including {@link #render rendering}
  272. * and {@link #destroy destruction}.
  273. *
  274. * To revert this, use {@link #deregisterChildren}.
  275. *
  276. * class SampleView extends View {
  277. * constructor( locale ) {
  278. * super( locale );
  279. *
  280. * this.childA = new SomeChildView( locale );
  281. * this.childB = new SomeChildView( locale );
  282. *
  283. * this.setTemplate( { tag: 'p' } );
  284. *
  285. * // Register the children.
  286. * this.registerChildren( [ this.childA, this.childB ] );
  287. * }
  288. *
  289. * render() {
  290. * super.render();
  291. *
  292. * this.element.appendChild( this.childA.element );
  293. * this.element.appendChild( this.childB.element );
  294. * }
  295. * }
  296. *
  297. * const view = new SampleView( locale );
  298. *
  299. * view.render();
  300. *
  301. * // Will append <p><childA#element><b></b><childB#element></p>.
  302. * document.body.appendChild( view.element );
  303. *
  304. * **Note**: There's no need to add child views if they're already referenced in the
  305. * {@link #template}:
  306. *
  307. * class SampleView extends View {
  308. * constructor( locale ) {
  309. * super( locale );
  310. *
  311. * this.childA = new SomeChildView( locale );
  312. * this.childB = new SomeChildView( locale );
  313. *
  314. * this.setTemplate( {
  315. * tag: 'p',
  316. *
  317. * // These children will be added automatically. There's no
  318. * // need to call {@link #registerChildren} for any of them.
  319. * children: [ this.childA, this.childB ]
  320. * } );
  321. * }
  322. *
  323. * // ...
  324. * }
  325. *
  326. * @param {module:ui/view~View|Iterable.<module:ui/view~View>} children Children views to be registered.
  327. */
  328. registerChildren( children ) {
  329. if ( !isIterable( children ) ) {
  330. children = [ children ];
  331. }
  332. for ( const child of children ) {
  333. this._unboundChildren.add( child );
  334. }
  335. }
  336. /**
  337. * The opposite of {@link #registerChildren}. Removes a child view from this view instance.
  338. * Once removed, the child is no longer managed by its parent, e.g. it can safely
  339. * become a child of another parent view.
  340. *
  341. * @see #registerChildren
  342. * @param {module:ui/view~View|Iterable.<module:ui/view~View>} children Child views to be removed.
  343. */
  344. deregisterChildren( children ) {
  345. if ( !isIterable( children ) ) {
  346. children = [ children ];
  347. }
  348. for ( const child of children ) {
  349. this._unboundChildren.remove( child );
  350. }
  351. }
  352. /**
  353. * Sets the {@link #template} of the view with with given definition.
  354. *
  355. * A shorthand for:
  356. *
  357. * view.setTemplate( definition );
  358. *
  359. * @param {module:ui/template~TemplateDefinition} definition Definition of view's template.
  360. */
  361. setTemplate( definition ) {
  362. this.template = new Template( definition );
  363. }
  364. /**
  365. * {@link module:ui/template~Template.extend Extends} the {@link #template} of the view with
  366. * with given definition.
  367. *
  368. * A shorthand for:
  369. *
  370. * Template.extend( view.template, definition );
  371. *
  372. * **Note**: Is requires the {@link #template} to be already set. See {@link #setTemplate}.
  373. *
  374. * @param {module:ui/template~TemplateDefinition} definition Definition which
  375. * extends the {@link #template}.
  376. */
  377. extendTemplate( definition ) {
  378. Template.extend( this.template, definition );
  379. }
  380. /**
  381. * Recursively renders the view.
  382. *
  383. * Once the view is rendered:
  384. * * the {@link #element} becomes an HTML element out of {@link #template},
  385. * * the {@link #isRendered} flag is set `true`.
  386. *
  387. * **Note**: The children of the view:
  388. * * defined directly in the {@link #template}
  389. * * residing in collections created by the {@link #createCollection} method,
  390. * * and added by {@link #registerChildren}
  391. * are also rendered in the process.
  392. *
  393. * In general, `render()` method is the right place to keep the code which refers to the
  394. * {@link #element} and should be executed at the very beginning of the view's life cycle.
  395. *
  396. * It is possible to {@link module:ui/template~Template.extend} the {@link #template} before
  397. * the view is rendered. To allow an early customization of the view (e.g. by its parent),
  398. * such references should be done in `render()`.
  399. *
  400. * class SampleView extends View {
  401. * constructor() {
  402. * this.setTemplate( {
  403. * // ...
  404. * } );
  405. * },
  406. *
  407. * render() {
  408. * // View#element becomes available.
  409. * super.render();
  410. *
  411. * // The "scroll" listener depends on #element.
  412. * this.listenTo( window, 'scroll', () => {
  413. * // A reference to #element would render the #template and make it non-extendable.
  414. * if ( window.scrollY > 0 ) {
  415. * this.element.scrollLeft = 100;
  416. * } else {
  417. * this.element.scrollLeft = 0;
  418. * }
  419. * } );
  420. * }
  421. * }
  422. *
  423. * const view = new SampleView();
  424. *
  425. * // Let's customize the view before it gets rendered.
  426. * view.extendTemplate( {
  427. * attributes: {
  428. * class: [
  429. * 'additional-class'
  430. * ]
  431. * }
  432. * } );
  433. *
  434. * // Late rendering allows customization of the view.
  435. * view.render();
  436. */
  437. render() {
  438. if ( this.isRendered ) {
  439. /**
  440. * This View has already been rendered.
  441. *
  442. * @error ui-view-render-rendered
  443. */
  444. throw new CKEditorError( 'ui-view-render-already-rendered: This View has already been rendered.' );
  445. }
  446. // Render #element of the view.
  447. if ( this.template ) {
  448. this.element = this.template.render();
  449. // Auto–register view children from #template.
  450. this.registerChildren( this.template.getViews() );
  451. }
  452. this.isRendered = true;
  453. }
  454. /**
  455. * Recursively destroys the view instance and child views added by {@link #registerChildren} and
  456. * residing in collections created by the {@link #createCollection}.
  457. *
  458. * Destruction disables all event listeners:
  459. * * created on the view, e.g. `view.on( 'event', () => {} )`,
  460. * * defined in the {@link #template} for DOM events.
  461. */
  462. destroy() {
  463. this.stopListening();
  464. this._viewCollections.map( c => c.destroy() );
  465. }
  466. }
  467. mix( View, DomEmitterMixin );
  468. mix( View, ObservableMixin );