view.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /**
  2. * @license Copyright (c) 2003-2018, 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#registerChild 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. // console.log( 'new', this.constructor.name, '()' );
  92. /**
  93. * An HTML element of the view. `null` until {@link #render rendered}
  94. * from the {@link #template}.
  95. *
  96. * class SampleView extends View {
  97. * constructor() {
  98. * super();
  99. *
  100. * // A template instance the #element will be created from.
  101. * this.setTemplate( {
  102. * tag: 'p'
  103. *
  104. * // ...
  105. * } );
  106. * }
  107. * }
  108. *
  109. * const view = new SampleView();
  110. *
  111. * // Renders the #template.
  112. * view.render();
  113. *
  114. * // Append the HTML element of the view to <body>.
  115. * document.body.appendChild( view.element );
  116. *
  117. * **Note**: The element of the view can also be assigned directly:
  118. *
  119. * view.element = document.querySelector( '#my-container' );
  120. *
  121. * @member {HTMLElement}
  122. */
  123. this.element = null;
  124. /**
  125. * Set `true` when the view has already been {@link module:ui/view~View#render rendered}.
  126. *
  127. * @readonly
  128. * @member {Boolean} #isRendered
  129. */
  130. this.isRendered = false;
  131. /**
  132. * A set of tools to localize the user interface.
  133. *
  134. * Also see {@link module:core/editor/editor~Editor#locale}.
  135. *
  136. * @readonly
  137. * @member {module:utils/locale~Locale}
  138. */
  139. this.locale = locale;
  140. /**
  141. * Shorthand for {@link module:utils/locale~Locale#t}.
  142. *
  143. * Note: If {@link #locale} instance hasn't been passed to the view this method may not
  144. * be available.
  145. *
  146. * @see module:utils/locale~Locale#t
  147. * @method
  148. */
  149. this.t = locale && locale.t;
  150. /**
  151. * Collections registered with {@link #createCollection}.
  152. *
  153. * @protected
  154. * @member {Set.<module:ui/viewcollection~ViewCollection>}
  155. */
  156. this._viewCollections = new Collection();
  157. /**
  158. * A collection of view instances, which have been added directly
  159. * into the {@link module:ui/template~Template#children}.
  160. *
  161. * @protected
  162. * @member {module:ui/viewcollection~ViewCollection}
  163. */
  164. this._unboundChildren = this.createCollection();
  165. // Pass parent locale to its children.
  166. this._viewCollections.on( 'add', ( evt, collection ) => {
  167. collection.locale = locale;
  168. } );
  169. /**
  170. * Template of this view. It provides the {@link #element} representing
  171. * the view in DOM, which is {@link #render rendered}.
  172. *
  173. * @member {module:ui/template~Template} #template
  174. */
  175. /**
  176. * Cached {@link module:ui/template~BindChain bind chain} object created by the
  177. * {@link #template}. See {@link #bindTemplate}.
  178. *
  179. * @private
  180. * @member {Object} #_bindTemplate
  181. */
  182. this.decorate( 'render' );
  183. }
  184. /**
  185. * Shorthand for {@link module:ui/template~Template.bind}, a binding
  186. * {@link module:ui/template~BindChain interface} pre–configured for the view instance.
  187. *
  188. * It provides {@link module:ui/template~BindChain#to `to()`} and
  189. * {@link module:ui/template~BindChain#if `if()`} methods that initialize bindings with
  190. * observable attributes and attach DOM listeners.
  191. *
  192. * class SampleView extends View {
  193. * constructor( locale ) {
  194. * super( locale );
  195. *
  196. * const bind = this.bindTemplate;
  197. *
  198. * // These {@link module:utils/observablemixin~Observable observable} attributes will control
  199. * // the state of the view in DOM.
  200. * this.set( {
  201. * elementClass: 'foo',
  202. * isEnabled: true
  203. * } );
  204. *
  205. * this.setTemplate( {
  206. * tag: 'p',
  207. *
  208. * attributes: {
  209. * // The class HTML attribute will follow elementClass
  210. * // and isEnabled view attributes.
  211. * class: [
  212. * bind.to( 'elementClass' )
  213. * bind.if( 'isEnabled', 'present-when-enabled' )
  214. * ]
  215. * },
  216. *
  217. * on: {
  218. * // The view will fire the "clicked" event upon clicking <p> in DOM.
  219. * click: bind.to( 'clicked' )
  220. * }
  221. * } );
  222. * }
  223. * }
  224. *
  225. * @method #bindTemplate
  226. */
  227. get bindTemplate() {
  228. if ( this._bindTemplate ) {
  229. return this._bindTemplate;
  230. }
  231. return ( this._bindTemplate = Template.bind( this, this ) );
  232. }
  233. /**
  234. * Creates a new collection of views, which can be used as
  235. * {@link module:ui/template~Template#children} of this view.
  236. *
  237. * class SampleView extends View {
  238. * constructor( locale ) {
  239. * super( locale );
  240. *
  241. * this.items = this.createCollection();
  242. *
  243. * this.setTemplate( {
  244. * tag: 'p',
  245. *
  246. * // `items` collection will render here.
  247. * children: this.items
  248. * } );
  249. * }
  250. * }
  251. *
  252. * const view = new SampleView( locale );
  253. * const child = new ChildView( locale );
  254. *
  255. * view.render();
  256. *
  257. * // It will append <p></p> to the <body>.
  258. * document.body.appendChild( view.element );
  259. *
  260. * // From now on the child is nested under its parent, which is also reflected in DOM.
  261. * // <p><child#element></p>
  262. * view.items.add( child );
  263. *
  264. * @returns {module:ui/viewcollection~ViewCollection} A new collection of view instances.
  265. */
  266. createCollection() {
  267. const collection = new ViewCollection();
  268. this._viewCollections.add( collection );
  269. return collection;
  270. }
  271. /**
  272. * Registers a new child view under the view instance. Once registered, a child
  273. * view is managed by its parent, including {@link #render rendering}
  274. * and {@link #destroy destruction}.
  275. *
  276. * To revert this, use {@link #deregisterChild}.
  277. *
  278. * class SampleView extends View {
  279. * constructor( locale ) {
  280. * super( locale );
  281. *
  282. * this.childA = new SomeChildView( locale );
  283. * this.childB = new SomeChildView( locale );
  284. *
  285. * this.setTemplate( { tag: 'p' } );
  286. *
  287. * // Register the children.
  288. * this.registerChild( [ this.childA, this.childB ] );
  289. * }
  290. *
  291. * render() {
  292. * super.render();
  293. *
  294. * this.element.appendChild( this.childA.element );
  295. * this.element.appendChild( this.childB.element );
  296. * }
  297. * }
  298. *
  299. * const view = new SampleView( locale );
  300. *
  301. * view.render();
  302. *
  303. * // Will append <p><childA#element><b></b><childB#element></p>.
  304. * document.body.appendChild( view.element );
  305. *
  306. * **Note**: There's no need to add child views if they're already referenced in the
  307. * {@link #template}:
  308. *
  309. * class SampleView extends View {
  310. * constructor( locale ) {
  311. * super( locale );
  312. *
  313. * this.childA = new SomeChildView( locale );
  314. * this.childB = new SomeChildView( locale );
  315. *
  316. * this.setTemplate( {
  317. * tag: 'p',
  318. *
  319. * // These children will be added automatically. There's no
  320. * // need to call {@link #registerChild} for any of them.
  321. * children: [ this.childA, this.childB ]
  322. * } );
  323. * }
  324. *
  325. * // ...
  326. * }
  327. *
  328. * @param {module:ui/view~View|Iterable.<module:ui/view~View>} children Children views to be registered.
  329. */
  330. registerChild( children ) {
  331. if ( !isIterable( children ) ) {
  332. children = [ children ];
  333. }
  334. for ( const child of children ) {
  335. this._unboundChildren.add( child );
  336. }
  337. }
  338. /**
  339. * The opposite of {@link #registerChild}. Removes a child view from this view instance.
  340. * Once removed, the child is no longer managed by its parent, e.g. it can safely
  341. * become a child of another parent view.
  342. *
  343. * @see #registerChild
  344. * @param {module:ui/view~View|Iterable.<module:ui/view~View>} children Child views to be removed.
  345. */
  346. deregisterChild( children ) {
  347. if ( !isIterable( children ) ) {
  348. children = [ children ];
  349. }
  350. for ( const child of children ) {
  351. this._unboundChildren.remove( child );
  352. }
  353. }
  354. /**
  355. * Sets the {@link #template} of the view with with given definition.
  356. *
  357. * A shorthand for:
  358. *
  359. * view.setTemplate( definition );
  360. *
  361. * @param {module:ui/template~TemplateDefinition} definition Definition of view's template.
  362. */
  363. setTemplate( definition ) {
  364. this.template = new Template( definition );
  365. }
  366. /**
  367. * {@link module:ui/template~Template.extend Extends} the {@link #template} of the view with
  368. * with given definition.
  369. *
  370. * A shorthand for:
  371. *
  372. * Template.extend( view.template, definition );
  373. *
  374. * **Note**: Is requires the {@link #template} to be already set. See {@link #setTemplate}.
  375. *
  376. * @param {module:ui/template~TemplateDefinition} definition Definition which
  377. * extends the {@link #template}.
  378. */
  379. extendTemplate( definition ) {
  380. Template.extend( this.template, definition );
  381. }
  382. /**
  383. * Recursively renders the view.
  384. *
  385. * Once the view is rendered:
  386. * * the {@link #element} becomes an HTML element out of {@link #template},
  387. * * the {@link #isRendered} flag is set `true`.
  388. *
  389. * **Note**: The children of the view:
  390. * * defined directly in the {@link #template}
  391. * * residing in collections created by the {@link #createCollection} method,
  392. * * and added by {@link #registerChild}
  393. * are also rendered in the process.
  394. *
  395. * In general, `render()` method is the right place to keep the code which refers to the
  396. * {@link #element} and should be executed at the very beginning of the view's life cycle.
  397. *
  398. * It is possible to {@link module:ui/template~Template.extend} the {@link #template} before
  399. * the view is rendered. To allow an early customization of the view (e.g. by its parent),
  400. * such references should be done in `render()`.
  401. *
  402. * class SampleView extends View {
  403. * constructor() {
  404. * this.setTemplate( {
  405. * // ...
  406. * } );
  407. * },
  408. *
  409. * render() {
  410. * // View#element becomes available.
  411. * super.render();
  412. *
  413. * // The "scroll" listener depends on #element.
  414. * this.listenTo( window, 'scroll', () => {
  415. * // A reference to #element would render the #template and make it non-extendable.
  416. * if ( window.scrollY > 0 ) {
  417. * this.element.scrollLeft = 100;
  418. * } else {
  419. * this.element.scrollLeft = 0;
  420. * }
  421. * } );
  422. * }
  423. * }
  424. *
  425. * const view = new SampleView();
  426. *
  427. * // Let's customize the view before it gets rendered.
  428. * view.extendTemplate( {
  429. * attributes: {
  430. * class: [
  431. * 'additional-class'
  432. * ]
  433. * }
  434. * } );
  435. *
  436. * // Late rendering allows customization of the view.
  437. * view.render();
  438. */
  439. render() {
  440. if ( this.isRendered ) {
  441. /**
  442. * This View has already been rendered.
  443. *
  444. * @error ui-view-render-rendered
  445. */
  446. throw new CKEditorError( 'ui-view-render-already-rendered: This View has already been rendered.' );
  447. }
  448. // Render #element of the view.
  449. if ( this.template ) {
  450. this.element = this.template.render();
  451. // Auto–register view children from #template.
  452. this.registerChild( this.template.getViews() );
  453. }
  454. this.isRendered = true;
  455. }
  456. /**
  457. * Recursively destroys the view instance and child views added by {@link #registerChild} and
  458. * residing in collections created by the {@link #createCollection}.
  459. *
  460. * Destruction disables all event listeners:
  461. * * created on the view, e.g. `view.on( 'event', () => {} )`,
  462. * * defined in the {@link #template} for DOM events.
  463. */
  464. destroy() {
  465. this.stopListening();
  466. this._viewCollections.map( c => c.destroy() );
  467. this._viewCollections.clear();
  468. this._unboundChildren.clear();
  469. // Template isn't obligatory for views.
  470. if ( this.template && this.template._revertData ) {
  471. this.template.revert( this.element );
  472. }
  473. }
  474. /**
  475. * Event fired by the {@link #render} method. Actual rendering is executed as a listener to
  476. * this event with the default priority.
  477. *
  478. * See {@link module:utils/observablemixin~ObservableMixin.decorate} for more information and samples.
  479. *
  480. * @event render
  481. */
  482. }
  483. mix( View, DomEmitterMixin );
  484. mix( View, ObservableMixin );