8
0

viewcollection.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, setTimeout */
  6. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  7. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import View from '../src/view';
  10. import ViewCollection from '../src/viewcollection';
  11. import Template from '../src/template';
  12. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  13. let collection;
  14. testUtils.createSinonSandbox();
  15. describe( 'ViewCollection', () => {
  16. beforeEach( createTestCollection );
  17. describe( 'constructor()', () => {
  18. it( 'sets basic properties and attributes', () => {
  19. expect( collection.locale ).to.be.undefined;
  20. expect( collection.ready ).to.be.false;
  21. expect( collection._parentElement ).to.be.null;
  22. expect( collection._boundItemsToViewsMap ).to.be.instanceOf( Map );
  23. expect( collection._idProperty ).to.equal( 'viewUid' );
  24. } );
  25. it( 'accepts locale and defines the locale property', () => {
  26. const locale = { t() {} };
  27. expect( new ViewCollection( locale ).locale ).to.equal( locale );
  28. } );
  29. describe( 'child view management in DOM', () => {
  30. it( 'manages view#element of the children in DOM', () => {
  31. const parentElement = document.createElement( 'p' );
  32. collection.setParent( parentElement );
  33. const viewA = new View();
  34. expect( () => {
  35. collection.add( viewA );
  36. collection.remove( viewA );
  37. } ).to.not.throw();
  38. expect( () => {
  39. collection.ready = true;
  40. collection.add( viewA );
  41. collection.remove( viewA );
  42. } ).to.not.throw();
  43. viewA.element = document.createElement( 'b' );
  44. collection.add( viewA );
  45. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><b></b></p>' );
  46. const viewB = new View();
  47. viewB.element = document.createElement( 'i' );
  48. collection.add( viewB, 0 );
  49. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><i></i><b></b></p>' );
  50. collection.remove( viewA );
  51. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><i></i></p>' );
  52. collection.remove( viewB );
  53. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p></p>' );
  54. } );
  55. // #145
  56. it( 'always keeps the collection synchronized with DOM', () => {
  57. const view = new View();
  58. const viewA = getView( 'A' );
  59. const viewB = getView( 'B' );
  60. function getView( text ) {
  61. const view = new View();
  62. view.template = new Template( {
  63. tag: 'li',
  64. children: [
  65. {
  66. text: text
  67. }
  68. ]
  69. } );
  70. return view;
  71. }
  72. // Fill the collection with children.
  73. collection.add( viewA );
  74. collection.add( viewB );
  75. // Put the collection in the template.
  76. view.template = new Template( {
  77. tag: 'ul',
  78. children: collection
  79. } );
  80. // Render view#template along with collection of children.
  81. view.element;
  82. const viewC = getView( 'C' );
  83. // Modify the collection, while the view#element has already
  84. // been rendered but the view has **not** been inited yet.
  85. collection.add( viewC );
  86. collection.remove( 1 );
  87. // Finally init the view. Check what's in there.
  88. return view.init().then( () => {
  89. expect( view.element.childNodes ).to.have.length( 2 );
  90. expect( view.element.childNodes[ 0 ] ).to.equal( viewA.element );
  91. expect( view.element.childNodes[ 1 ] ).to.equal( viewC.element );
  92. } );
  93. } );
  94. } );
  95. } );
  96. describe( 'init()', () => {
  97. it( 'should return a promise', () => {
  98. expect( collection.init() ).to.be.instanceof( Promise );
  99. } );
  100. it( 'should return a composition of child init() promises', () => {
  101. const spyA = sinon.spy().named( 'A' );
  102. const spyB = sinon.spy().named( 'B' );
  103. const spyC = sinon.spy().named( 'C' );
  104. const childA = {
  105. init: () => {
  106. return new Promise( ( resolve ) => {
  107. setTimeout( () => {
  108. spyA();
  109. resolve();
  110. }, 20 );
  111. } );
  112. }
  113. };
  114. const childB = {
  115. init: () => {
  116. return new Promise( ( resolve ) => {
  117. setTimeout( () => {
  118. spyB();
  119. resolve();
  120. }, 10 );
  121. } );
  122. }
  123. };
  124. const childC = {
  125. init: () => {
  126. return new Promise( ( resolve ) => {
  127. setTimeout( () => {
  128. spyC();
  129. resolve();
  130. }, 0 );
  131. } );
  132. }
  133. };
  134. collection.add( childA );
  135. collection.add( childB );
  136. collection.add( childC );
  137. return collection.init()
  138. .then( () => {
  139. sinon.assert.callOrder( spyC, spyB, spyA );
  140. } );
  141. } );
  142. it( 'should throw if already initialized', () => {
  143. return collection.init()
  144. .then( () => {
  145. collection.init();
  146. throw new Error( 'This should not be executed.' );
  147. } )
  148. .catch( err => {
  149. expect( err ).to.be.instanceof( CKEditorError );
  150. expect( err.message ).to.match( /ui-viewcollection-init-reinit/ );
  151. } );
  152. } );
  153. it( 'calls #init on all views in the collection', () => {
  154. const viewA = new View();
  155. const viewB = new View();
  156. viewA.element = document.createElement( 'a' );
  157. viewB.element = document.createElement( 'b' );
  158. const spyA = testUtils.sinon.spy( viewA, 'init' );
  159. const spyB = testUtils.sinon.spy( viewB, 'init' );
  160. collection.setParent( document.body );
  161. collection.add( viewA );
  162. collection.add( viewB );
  163. return collection.init().then( () => {
  164. sinon.assert.calledOnce( spyA );
  165. sinon.assert.calledOnce( spyB );
  166. sinon.assert.callOrder( spyA, spyB );
  167. expect( viewA.element.parentNode ).to.equal( collection._parentElement );
  168. expect( viewA.element.nextSibling ).to.equal( viewB.element );
  169. expect( collection.ready ).to.be.true;
  170. } );
  171. } );
  172. } );
  173. describe( 'destroy()', () => {
  174. it( 'should return a promise', () => {
  175. expect( collection.destroy() ).to.be.instanceof( Promise );
  176. } );
  177. it( 'calls #destroy on all views in the collection', () => {
  178. const viewA = new View();
  179. const viewB = new View();
  180. const spyA = testUtils.sinon.spy( viewA, 'destroy' );
  181. const spyB = testUtils.sinon.spy( viewB, 'destroy' );
  182. collection.add( viewA );
  183. collection.add( viewB );
  184. return collection.destroy().then( () => {
  185. sinon.assert.calledOnce( spyA );
  186. sinon.assert.calledOnce( spyB );
  187. sinon.assert.callOrder( spyA, spyB );
  188. } );
  189. } );
  190. } );
  191. describe( 'add()', () => {
  192. it( 'returns a promise', () => {
  193. expect( collection.add( {} ) ).to.be.instanceof( Promise );
  194. } );
  195. it( 'initializes the new view in the collection', () => {
  196. let view = new View();
  197. let spy = testUtils.sinon.spy( view, 'init' );
  198. expect( collection.ready ).to.be.false;
  199. expect( view.ready ).to.be.false;
  200. return collection.add( view ).then( () => {
  201. expect( collection.ready ).to.be.false;
  202. expect( view.ready ).to.be.false;
  203. sinon.assert.notCalled( spy );
  204. view = new View();
  205. spy = testUtils.sinon.spy( view, 'init' );
  206. collection.ready = true;
  207. return collection.add( view ).then( () => {
  208. expect( view.ready ).to.be.true;
  209. sinon.assert.calledOnce( spy );
  210. } );
  211. } );
  212. } );
  213. it( 'works for a view with a Number view#id attribute', () => {
  214. const view = new View();
  215. view.set( 'id', 1 );
  216. return collection.add( view ).then( () => {
  217. expect( view.id ).to.equal( 1 );
  218. expect( view.viewUid ).to.be.a( 'string' );
  219. } );
  220. } );
  221. } );
  222. describe( 'setParent()', () => {
  223. it( 'sets #_parentElement', () => {
  224. const el = {};
  225. expect( collection._parentElement ).to.be.null;
  226. collection.setParent( el );
  227. expect( collection._parentElement ).to.equal( el );
  228. } );
  229. } );
  230. describe( 'bindTo()', () => {
  231. class ViewClass extends View {
  232. constructor( locale, data ) {
  233. super( locale );
  234. this.template = new Template( {
  235. tag: 'b'
  236. } );
  237. this.data = data;
  238. }
  239. }
  240. it( 'provides "as()" interface', () => {
  241. const returned = collection.bindTo( {} );
  242. expect( returned ).to.have.keys( 'as' );
  243. expect( returned.as ).to.be.a( 'function' );
  244. } );
  245. describe( 'as()', () => {
  246. it( 'does not chain', () => {
  247. const returned = collection.bindTo( new Collection() ).as( ViewClass );
  248. expect( returned ).to.be.undefined;
  249. } );
  250. it( 'binds collection as a view factory – initial content', () => {
  251. const locale = {};
  252. const items = new Collection();
  253. items.add( { id: '1' } );
  254. items.add( { id: '2' } );
  255. collection = new ViewCollection( locale );
  256. collection.bindTo( items ).as( ViewClass );
  257. expect( collection ).to.have.length( 2 );
  258. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  259. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  260. expect( collection.get( 0 ).locale ).to.equal( locale );
  261. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  262. } );
  263. it( 'binds collection as a view factory – new content', () => {
  264. const locale = {};
  265. const items = new Collection();
  266. collection = new ViewCollection( locale );
  267. collection.bindTo( items ).as( ViewClass );
  268. expect( collection ).to.have.length( 0 );
  269. items.add( { id: '1' } );
  270. items.add( { id: '2' } );
  271. expect( collection ).to.have.length( 2 );
  272. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  273. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  274. expect( collection.get( 0 ).locale ).to.equal( locale );
  275. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  276. } );
  277. it( 'binds collection as a view factory – item removal', () => {
  278. const locale = {};
  279. const items = new Collection();
  280. collection = new ViewCollection( locale );
  281. collection.bindTo( items ).as( ViewClass );
  282. expect( collection ).to.have.length( 0 );
  283. items.add( { id: '1' } );
  284. items.add( { id: '2' } );
  285. expect( collection ).to.have.length( 2 );
  286. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  287. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  288. expect( collection.get( 0 ).locale ).to.equal( locale );
  289. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  290. items.remove( 1 );
  291. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  292. items.remove( 0 );
  293. expect( collection ).to.have.length( 0 );
  294. } );
  295. it( 'binds collection as a view factory – custom factory (arrow function)', () => {
  296. const locale = {};
  297. const items = new Collection();
  298. collection = new ViewCollection( locale );
  299. collection.bindTo( items ).as( ( item ) => {
  300. return new ViewClass( locale, item );
  301. } );
  302. expect( collection ).to.have.length( 0 );
  303. items.add( { id: '1' } );
  304. items.add( { id: '2' } );
  305. expect( collection ).to.have.length( 2 );
  306. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  307. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  308. expect( collection.get( 0 ).locale ).to.equal( locale );
  309. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  310. } );
  311. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  312. it( 'binds collection as a view factory – custom factory (normal function)', () => {
  313. const locale = { locale: true };
  314. const items = new Collection();
  315. collection = new ViewCollection( locale );
  316. collection.bindTo( items ).as( function( item ) {
  317. return new ViewClass( locale, item );
  318. } );
  319. items.add( { id: '1' } );
  320. expect( collection ).to.have.length( 1 );
  321. const view = collection.get( 0 );
  322. // Wrong args will be passed to the callback if it's treated as the view constructor.
  323. expect( view ).to.be.instanceOf( ViewClass );
  324. expect( view.locale ).to.equal( locale );
  325. expect( view.data ).to.equal( items.get( 0 ) );
  326. } );
  327. } );
  328. } );
  329. describe( 'delegate()', () => {
  330. it( 'should throw when event names are not strings', () => {
  331. expect( () => {
  332. collection.delegate();
  333. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  334. expect( () => {
  335. collection.delegate( new Date() );
  336. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  337. expect( () => {
  338. collection.delegate( 'color', new Date() );
  339. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  340. } );
  341. it( 'returns object', () => {
  342. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  343. } );
  344. it( 'provides "to()" interface', () => {
  345. const delegate = collection.delegate( 'foo' );
  346. expect( delegate ).to.have.keys( 'to' );
  347. expect( delegate.to ).to.be.a( 'function' );
  348. } );
  349. describe( 'to()', () => {
  350. it( 'does not chain', () => {
  351. const returned = collection.delegate( 'foo' ).to( {} );
  352. expect( returned ).to.be.undefined;
  353. } );
  354. it( 'forwards an event to another observable – existing view', ( done ) => {
  355. const target = new View();
  356. const view = new View();
  357. collection.add( view );
  358. collection.delegate( 'foo' ).to( target );
  359. target.on( 'foo', ( ...args ) => {
  360. assertDelegated( args, {
  361. expectedName: 'foo',
  362. expectedSource: view,
  363. expectedPath: [ view, target ],
  364. expectedData: []
  365. } );
  366. done();
  367. } );
  368. view.fire( 'foo' );
  369. } );
  370. it( 'forwards an event to another observable – new view', ( done ) => {
  371. const target = new View();
  372. const view = new View();
  373. collection.delegate( 'foo' ).to( target );
  374. collection.add( view );
  375. target.on( 'foo', ( ...args ) => {
  376. assertDelegated( args, {
  377. expectedName: 'foo',
  378. expectedSource: view,
  379. expectedPath: [ view, target ],
  380. expectedData: []
  381. } );
  382. done();
  383. } );
  384. view.fire( 'foo' );
  385. } );
  386. it( 'forwards multiple events to another observable', () => {
  387. const target = new View();
  388. const viewA = new View();
  389. const viewB = new View();
  390. const viewC = new View();
  391. const spyFoo = sinon.spy();
  392. const spyBar = sinon.spy();
  393. const spyBaz = sinon.spy();
  394. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  395. collection.add( viewA );
  396. collection.add( viewB );
  397. collection.add( viewC );
  398. target.on( 'foo', spyFoo );
  399. target.on( 'bar', spyBar );
  400. target.on( 'baz', spyBaz );
  401. viewA.fire( 'foo' );
  402. sinon.assert.calledOnce( spyFoo );
  403. sinon.assert.notCalled( spyBar );
  404. sinon.assert.notCalled( spyBaz );
  405. assertDelegated( spyFoo.args[ 0 ], {
  406. expectedName: 'foo',
  407. expectedSource: viewA,
  408. expectedPath: [ viewA, target ],
  409. expectedData: []
  410. } );
  411. viewB.fire( 'bar' );
  412. sinon.assert.calledOnce( spyFoo );
  413. sinon.assert.calledOnce( spyBar );
  414. sinon.assert.notCalled( spyBaz );
  415. assertDelegated( spyBar.args[ 0 ], {
  416. expectedName: 'bar',
  417. expectedSource: viewB,
  418. expectedPath: [ viewB, target ],
  419. expectedData: []
  420. } );
  421. viewC.fire( 'baz' );
  422. sinon.assert.calledOnce( spyFoo );
  423. sinon.assert.calledOnce( spyBar );
  424. sinon.assert.calledOnce( spyBaz );
  425. assertDelegated( spyBaz.args[ 0 ], {
  426. expectedName: 'baz',
  427. expectedSource: viewC,
  428. expectedPath: [ viewC, target ],
  429. expectedData: []
  430. } );
  431. viewC.fire( 'not-delegated' );
  432. sinon.assert.calledOnce( spyFoo );
  433. sinon.assert.calledOnce( spyBar );
  434. sinon.assert.calledOnce( spyBaz );
  435. } );
  436. it( 'does not forward events which are not supposed to be delegated', () => {
  437. const target = new View();
  438. const view = new View();
  439. const spyFoo = sinon.spy();
  440. const spyBar = sinon.spy();
  441. const spyBaz = sinon.spy();
  442. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  443. collection.add( view );
  444. target.on( 'foo', spyFoo );
  445. target.on( 'bar', spyBar );
  446. target.on( 'baz', spyBaz );
  447. view.fire( 'foo' );
  448. view.fire( 'bar' );
  449. view.fire( 'baz' );
  450. view.fire( 'not-delegated' );
  451. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  452. sinon.assert.callCount( spyFoo, 1 );
  453. sinon.assert.callCount( spyBar, 1 );
  454. sinon.assert.callCount( spyBaz, 1 );
  455. } );
  456. it( 'stops forwarding when view removed from the collection', () => {
  457. const target = new View();
  458. const view = new View();
  459. const spy = sinon.spy();
  460. collection.delegate( 'foo' ).to( target );
  461. target.on( 'foo', spy );
  462. collection.add( view );
  463. view.fire( 'foo' );
  464. sinon.assert.callCount( spy, 1 );
  465. collection.remove( 0 );
  466. view.fire( 'foo' );
  467. sinon.assert.callCount( spy, 1 );
  468. } );
  469. it( 'supports deep event delegation', ( done ) => {
  470. const target = new View();
  471. const viewA = new View();
  472. const viewAA = new View();
  473. const data = {};
  474. const deepCollection = viewA.createCollection();
  475. collection.add( viewA );
  476. collection.delegate( 'foo' ).to( target );
  477. deepCollection.add( viewAA );
  478. deepCollection.delegate( 'foo' ).to( viewA );
  479. target.on( 'foo', ( ...args ) => {
  480. assertDelegated( args, {
  481. expectedName: 'foo',
  482. expectedSource: viewAA,
  483. expectedPath: [ viewAA, viewA, target ],
  484. expectedData: [ data ]
  485. } );
  486. done();
  487. } );
  488. viewAA.fire( 'foo', data );
  489. } );
  490. } );
  491. } );
  492. } );
  493. function createTestCollection() {
  494. collection = new ViewCollection();
  495. }
  496. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  497. const evtInfo = evtArgs[ 0 ];
  498. expect( evtInfo.name ).to.equal( expectedName );
  499. expect( evtInfo.source ).to.equal( expectedSource );
  500. expect( evtInfo.path ).to.deep.equal( expectedPath );
  501. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  502. }