8
0

controllercollection.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: ui */
  7. import testUtils from '/tests/core/_utils/utils.js';
  8. import ControllerCollection from '/ckeditor5/ui/controllercollection.js';
  9. import Controller from '/ckeditor5/ui/controller.js';
  10. import Collection from '/ckeditor5/utils/collection.js';
  11. import Model from '/ckeditor5/ui/model.js';
  12. import View from '/ckeditor5/ui/view.js';
  13. import Template from '/ckeditor5/ui/template.js';
  14. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  15. testUtils.createSinonSandbox();
  16. let ParentView, ItemController, ItemView;
  17. let models;
  18. describe( 'ControllerCollection', () => {
  19. beforeEach( () => {
  20. defineParentViewClass();
  21. defineItemControllerClass();
  22. defineItemViewClass();
  23. createModelCollection();
  24. } );
  25. describe( 'constructor', () => {
  26. it( 'should throw when no name is passed', () => {
  27. expect( () => {
  28. new ControllerCollection();
  29. } ).to.throw( /^ui-controllercollection-no-name/ );
  30. } );
  31. it( 'accepts locale', () => {
  32. const locale = {};
  33. const collection = new ControllerCollection( 'foo', locale );
  34. expect( collection.locale ).to.equal( locale );
  35. } );
  36. } );
  37. describe( 'add', () => {
  38. it( 'should add a child controller and return promise', () => {
  39. const parentController = new Controller();
  40. const childController = new Controller();
  41. const collection = parentController.addCollection( 'x' );
  42. const returned = collection.add( childController );
  43. expect( returned ).to.be.an.instanceof( Promise );
  44. expect( collection.get( 0 ) ).to.be.equal( childController );
  45. } );
  46. it( 'should add a child controller at given position', () => {
  47. const parentController = new Controller();
  48. const childController1 = new Controller();
  49. const childController2 = new Controller();
  50. const collection = parentController.addCollection( 'x' );
  51. collection.add( childController1 );
  52. collection.add( childController2, 0 );
  53. expect( collection.get( 0 ) ).to.be.equal( childController2 );
  54. expect( collection.get( 1 ) ).to.be.equal( childController1 );
  55. } );
  56. it( 'should initialize child controller if parent is ready', () => {
  57. const parentController = new Controller( null, new ParentView() );
  58. const childController = new Controller( null, new View() );
  59. const spy = testUtils.sinon.spy( childController, 'init' );
  60. const collection = parentController.addCollection( 'x' );
  61. collection.add( childController );
  62. collection.remove( childController );
  63. sinon.assert.notCalled( spy );
  64. return parentController.init()
  65. .then( () => {
  66. return collection.add( childController );
  67. } )
  68. .then( () => {
  69. sinon.assert.calledOnce( spy );
  70. } );
  71. } );
  72. it( 'should not initialize child controller twice', () => {
  73. const parentController = new Controller( null, new ParentView() );
  74. const childController = new Controller( null, new View() );
  75. const spy = testUtils.sinon.spy( childController, 'init' );
  76. const collection = parentController.addCollection( 'x' );
  77. return parentController.init()
  78. .then( () => {
  79. return childController.init();
  80. } )
  81. .then( () => {
  82. return collection.add( childController );
  83. } )
  84. .then( () => {
  85. sinon.assert.calledOnce( spy );
  86. } );
  87. } );
  88. } );
  89. describe( 'bind', () => {
  90. it( 'returns object', () => {
  91. expect( new ControllerCollection( 'foo' ).bind( {} ) ).to.be.an( 'object' );
  92. } );
  93. it( 'provides "as" interface', () => {
  94. const bind = new ControllerCollection( 'foo' ).bind( {} );
  95. expect( bind ).to.have.keys( 'as' );
  96. expect( bind.as ).to.be.a( 'function' );
  97. } );
  98. describe( 'as', () => {
  99. it( 'does not chain', () => {
  100. const controllers = new ControllerCollection( 'synced' );
  101. const returned = controllers.bind( models ).as( ItemController, ItemView );
  102. expect( returned ).to.be.undefined;
  103. } );
  104. describe( 'standard handler', () => {
  105. it( 'expands the initial collection of the models', () => {
  106. const controllers = new ControllerCollection( 'synced' );
  107. controllers.bind( models ).as( ItemController, ItemView );
  108. expect( controllers ).to.have.length( 5 );
  109. expect( controllers.get( 0 ).model.uid ).to.equal( '0' );
  110. expect( controllers.get( 4 ).model.uid ).to.equal( '4' );
  111. } );
  112. it( 'uses the controller and view classes to expand the collection', () => {
  113. const controllers = new ControllerCollection( 'synced' );
  114. controllers.bind( models ).as( ItemController, ItemView );
  115. expect( controllers.get( 0 ) ).to.be.instanceOf( ItemController );
  116. expect( controllers.get( 0 ).view ).to.be.instanceOf( ItemView );
  117. } );
  118. it( 'supports adding new models to the collection', () => {
  119. const controllers = new ControllerCollection( 'synced' );
  120. controllers.bind( models ).as( ItemController, ItemView );
  121. models.add( new Model( { uid: '6' } ) );
  122. models.add( new Model( { uid: '5' } ), 5 );
  123. expect( controllers.get( 5 ).model.uid ).to.equal( '5' );
  124. expect( controllers.get( 6 ).model.uid ).to.equal( '6' );
  125. expect( controllers ).to.have.length( 7 );
  126. } );
  127. it( 'supports removing models from the collection', () => {
  128. const controllers = new ControllerCollection( 'synced' );
  129. controllers.bind( models ).as( ItemController, ItemView );
  130. models.remove( 2 );
  131. models.remove( 3 );
  132. expect( controllers.map( c => c.model.uid ) ).to.have.members( [ '0', '1', '3' ] );
  133. } );
  134. it( 'passes controller collection\'s locale to the views', () => {
  135. const locale = {};
  136. const controllers = new ControllerCollection( 'synced', locale );
  137. controllers.bind( models ).as( ItemController, ItemView );
  138. expect( controllers.get( 0 ).view.locale ).to.equal( locale );
  139. } );
  140. } );
  141. describe( 'custom handler', () => {
  142. it( 'expands the initial collection of the models', () => {
  143. const controllers = new ControllerCollection( 'synced' );
  144. controllers.bind( models ).as( ( model, locale ) => {
  145. return new ItemController( model, new ItemView( locale ) );
  146. } );
  147. expect( controllers ).to.have.length( 5 );
  148. expect( controllers.get( 0 ).model.uid ).to.equal( '0' );
  149. expect( controllers.get( 4 ).model.uid ).to.equal( '4' );
  150. } );
  151. it( 'uses the controller and view classes to expand the collection', () => {
  152. const controllers = new ControllerCollection( 'synced' );
  153. controllers.bind( models ).as( ( model, locale ) => {
  154. return new ItemController( model, new ItemView( locale ) );
  155. } );
  156. expect( controllers.get( 0 ) ).to.be.instanceOf( ItemController );
  157. expect( controllers.get( 0 ).view ).to.be.instanceOf( ItemView );
  158. } );
  159. it( 'supports adding new models to the collection', () => {
  160. const controllers = new ControllerCollection( 'synced' );
  161. controllers.bind( models ).as( ( model, locale ) => {
  162. return new ItemController( model, new ItemView( locale ) );
  163. } );
  164. models.add( new Model( { uid: '6' } ) );
  165. models.add( new Model( { uid: '5' } ), 5 );
  166. expect( controllers.get( 5 ).model.uid ).to.equal( '5' );
  167. expect( controllers.get( 6 ).model.uid ).to.equal( '6' );
  168. expect( controllers ).to.have.length( 7 );
  169. } );
  170. it( 'supports removing models from the collection', () => {
  171. const controllers = new ControllerCollection( 'synced' );
  172. controllers.bind( models ).as( ( model, locale ) => {
  173. return new ItemController( model, new ItemView( locale ) );
  174. } );
  175. models.remove( 2 );
  176. models.remove( 3 );
  177. expect( controllers.map( c => c.model.uid ) ).to.have.members( [ '0', '1', '3' ] );
  178. } );
  179. it( 'supports returning null', () => {
  180. const controllers = new ControllerCollection( 'synced' );
  181. controllers.bind( models ).as( ( model, locale ) => {
  182. if ( model.uid == 2 ) {
  183. return null;
  184. } else {
  185. return new ItemController( model, new ItemView( locale ) );
  186. }
  187. } );
  188. expect( controllers.map( c => c.model.uid ) ).to.have.members( [ '0', '1', '3', '4' ] );
  189. } );
  190. it( 'supports adding to structure with missing controller', () => {
  191. const controllers = new ControllerCollection( 'synced' );
  192. controllers.bind( models ).as( ( model, locale ) => {
  193. if ( model.uid == 2 ) {
  194. return null;
  195. } else {
  196. return new ItemController( model, new ItemView( locale ) );
  197. }
  198. } );
  199. models.add( new Model( { uid: '0.5' } ), 1 );
  200. models.add( new Model( { uid: '3.5' } ), 5 );
  201. expect( models.map( c => c.uid ) ).to.have.members( [ '0', '0.5', '1', '2', '3', '3.5', '4' ] );
  202. expect( controllers.map( c => c.model.uid ) ).to.have.members( [ '0', '0.5', '1', '3', '3.5', '4' ] );
  203. } );
  204. it( 'supports removing to structure with missing controller', () => {
  205. const controllers = new ControllerCollection( 'synced' );
  206. controllers.bind( models ).as( ( model, locale ) => {
  207. if ( model.uid == 2 ) {
  208. return null;
  209. } else {
  210. return new ItemController( model, new ItemView( locale ) );
  211. }
  212. } );
  213. models.remove( 3 );
  214. expect( controllers.map( c => c.model.uid ) ).to.have.members( [ '0', '1', '4' ] );
  215. } );
  216. it( 'supports removing model with missing controller', () => {
  217. const controllers = new ControllerCollection( 'synced' );
  218. controllers.bind( models ).as( ( model, locale ) => {
  219. if ( model.uid == 2 ) {
  220. return null;
  221. } else {
  222. return new ItemController( model, new ItemView( locale ) );
  223. }
  224. } );
  225. models.remove( 2 );
  226. expect( controllers.map( c => c.model.uid ) ).to.have.members( [ '0', '1', '3', '4' ] );
  227. } );
  228. it( 'passes controller collection\'s locale to the views', () => {
  229. const locale = {};
  230. const controllers = new ControllerCollection( 'synced', locale );
  231. controllers.bind( models ).as( ( model, locale ) => {
  232. return new ItemController( model, new ItemView( locale ) );
  233. } );
  234. expect( controllers.get( 0 ).view.locale ).to.equal( locale );
  235. } );
  236. } );
  237. describe( 'custom data format with custom handler', () => {
  238. it( 'expands the initial collection of the models', () => {
  239. const controllers = new ControllerCollection( 'synced' );
  240. const data = new Collection();
  241. data.add( { foo: 'a' } );
  242. data.add( { foo: 'b' } );
  243. controllers.bind( data ).as( ( item, locale ) => {
  244. const model = new Model( {
  245. custom: item.foo
  246. } );
  247. return new ItemController( model, new ItemView( locale ) );
  248. } );
  249. expect( controllers ).to.have.length( 2 );
  250. expect( controllers.get( 0 ).model.custom ).to.equal( 'a' );
  251. expect( controllers.get( 1 ).model.custom ).to.equal( 'b' );
  252. expect( controllers.get( 0 ) ).to.be.instanceOf( ItemController );
  253. expect( controllers.get( 0 ).view ).to.be.instanceOf( ItemView );
  254. } );
  255. } );
  256. } );
  257. } );
  258. describe( 'delegate', () => {
  259. it( 'should throw when event names are not strings', () => {
  260. const collection = new ControllerCollection( 'foo' );
  261. expect( () => {
  262. collection.delegate();
  263. } ).to.throw( CKEditorError, /ui-controllercollection-delegate-wrong-events/ );
  264. expect( () => {
  265. collection.delegate( new Date() );
  266. } ).to.throw( CKEditorError, /ui-controllercollection-delegate-wrong-events/ );
  267. expect( () => {
  268. collection.delegate( 'color', new Date() );
  269. } ).to.throw( CKEditorError, /ui-controllercollection-delegate-wrong-events/ );
  270. } );
  271. it( 'returns object', () => {
  272. expect( new ControllerCollection( 'foo' ).delegate( 'foo' ) ).to.be.an( 'object' );
  273. } );
  274. it( 'provides "to" interface', () => {
  275. const delegate = new ControllerCollection( 'foo' ).delegate( 'foo' );
  276. expect( delegate ).to.have.keys( 'to' );
  277. expect( delegate.to ).to.be.a( 'function' );
  278. } );
  279. describe( 'to', () => {
  280. it( 'does not chain', () => {
  281. const collection = new ControllerCollection( 'foo' );
  282. const returned = collection.delegate( 'foo' ).to( {} );
  283. expect( returned ).to.be.undefined;
  284. } );
  285. it( 'forwards an event to another observable – existing controller', ( done ) => {
  286. const target = new Model();
  287. const collection = new ControllerCollection( 'foo' );
  288. const model = new Model();
  289. collection.add( new Controller( model ) );
  290. collection.delegate( 'foo' ).to( target );
  291. target.on( 'foo', ( ...args ) => {
  292. assertDelegated( args, {
  293. expectedName: 'foo',
  294. expectedSource: model,
  295. expectedPath: [ model, target ],
  296. expectedData: []
  297. } );
  298. done();
  299. } );
  300. model.fire( 'foo' );
  301. } );
  302. it( 'forwards an event to another observable – new controller', ( done ) => {
  303. const target = new Model();
  304. const collection = new ControllerCollection( 'foo' );
  305. const model = new Model();
  306. collection.delegate( 'foo' ).to( target );
  307. collection.add( new Controller( model ) );
  308. target.on( 'foo', ( ...args ) => {
  309. assertDelegated( args, {
  310. expectedName: 'foo',
  311. expectedSource: model,
  312. expectedPath: [ model, target ],
  313. expectedData: []
  314. } );
  315. done();
  316. } );
  317. model.fire( 'foo' );
  318. } );
  319. it( 'forwards multiple events to another observable', () => {
  320. const target = new Model();
  321. const collection = new ControllerCollection( 'foo' );
  322. const modelA = new Model();
  323. const modelB = new Model();
  324. const modelC = new Model();
  325. const spyFoo = sinon.spy();
  326. const spyBar = sinon.spy();
  327. const spyBaz = sinon.spy();
  328. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  329. collection.add( new Controller( modelA ) );
  330. collection.add( new Controller( modelB ) );
  331. collection.add( new Controller( modelC ) );
  332. target.on( 'foo', spyFoo );
  333. target.on( 'bar', spyBar );
  334. target.on( 'baz', spyBaz );
  335. modelA.fire( 'foo' );
  336. sinon.assert.calledOnce( spyFoo );
  337. sinon.assert.notCalled( spyBar );
  338. sinon.assert.notCalled( spyBaz );
  339. assertDelegated( spyFoo.args[ 0 ], {
  340. expectedName: 'foo',
  341. expectedSource: modelA,
  342. expectedPath: [ modelA, target ],
  343. expectedData: []
  344. } );
  345. modelB.fire( 'bar' );
  346. sinon.assert.calledOnce( spyFoo );
  347. sinon.assert.calledOnce( spyBar );
  348. sinon.assert.notCalled( spyBaz );
  349. assertDelegated( spyBar.args[ 0 ], {
  350. expectedName: 'bar',
  351. expectedSource: modelB,
  352. expectedPath: [ modelB, target ],
  353. expectedData: []
  354. } );
  355. modelC.fire( 'baz' );
  356. sinon.assert.calledOnce( spyFoo );
  357. sinon.assert.calledOnce( spyBar );
  358. sinon.assert.calledOnce( spyBaz );
  359. assertDelegated( spyBaz.args[ 0 ], {
  360. expectedName: 'baz',
  361. expectedSource: modelC,
  362. expectedPath: [ modelC, target ],
  363. expectedData: []
  364. } );
  365. modelC.fire( 'not-delegated' );
  366. sinon.assert.calledOnce( spyFoo );
  367. sinon.assert.calledOnce( spyBar );
  368. sinon.assert.calledOnce( spyBaz );
  369. } );
  370. it( 'does not forward events which are not supposed to be delegated', () => {
  371. const target = new Model();
  372. const collection = new ControllerCollection( 'foo' );
  373. const model = new Model();
  374. const spyFoo = sinon.spy();
  375. const spyBar = sinon.spy();
  376. const spyBaz = sinon.spy();
  377. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  378. collection.add( new Controller( model ) );
  379. target.on( 'foo', spyFoo );
  380. target.on( 'bar', spyBar );
  381. target.on( 'baz', spyBaz );
  382. model.fire( 'foo' );
  383. model.fire( 'bar' );
  384. model.fire( 'baz' );
  385. model.fire( 'not-delegated' );
  386. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  387. sinon.assert.callCount( spyFoo, 1 );
  388. sinon.assert.callCount( spyBar, 1 );
  389. sinon.assert.callCount( spyBaz, 1 );
  390. } );
  391. it( 'stops forwarding when controller removed from the collection', () => {
  392. const target = new Model();
  393. const collection = new ControllerCollection( 'foo' );
  394. const model = new Model();
  395. const spy = sinon.spy();
  396. collection.delegate( 'foo' ).to( target );
  397. target.on( 'foo', spy );
  398. collection.add( new Controller( model ) );
  399. model.fire( 'foo' );
  400. sinon.assert.callCount( spy, 1 );
  401. collection.remove( 0 );
  402. model.fire( 'foo' );
  403. sinon.assert.callCount( spy, 1 );
  404. } );
  405. it( 'supports deep event delegation', ( done ) => {
  406. const collection = new ControllerCollection( 'foo' );
  407. const target = new Model();
  408. const modelA = new Model();
  409. const modelAA = new Model();
  410. const data = {};
  411. const controllerA = new Controller( modelA );
  412. const controllerAA = new Controller( modelAA );
  413. const barCollection = controllerA.addCollection( 'bar' );
  414. collection.add( controllerA );
  415. collection.delegate( 'foo' ).to( target );
  416. barCollection.add( controllerAA );
  417. barCollection.delegate( 'foo' ).to( modelA );
  418. target.on( 'foo', ( ...args ) => {
  419. assertDelegated( args, {
  420. expectedName: 'foo',
  421. expectedSource: modelAA,
  422. expectedPath: [ modelAA, modelA, target ],
  423. expectedData: [ data ]
  424. } );
  425. done();
  426. } );
  427. modelAA.fire( 'foo', data );
  428. } );
  429. } );
  430. } );
  431. } );
  432. function defineParentViewClass() {
  433. ParentView = class extends View {
  434. constructor() {
  435. super();
  436. this.element = document.createElement( 'span' );
  437. this.register( 'x', true );
  438. }
  439. };
  440. }
  441. function defineItemControllerClass() {
  442. ItemController = class extends Controller {
  443. constructor( model, view ) {
  444. super( model, view );
  445. view.bind( 'uid' ).to( model );
  446. }
  447. };
  448. }
  449. function defineItemViewClass() {
  450. ItemView = class extends View {
  451. constructor( locale ) {
  452. super( locale );
  453. const bind = this.bindTemplate;
  454. this.template = new Template( {
  455. tag: 'li',
  456. attributes: {
  457. id: bind.to( 'uid' )
  458. }
  459. } );
  460. }
  461. };
  462. }
  463. function createModelCollection() {
  464. models = new Collection( { idProperty: 'uid' } );
  465. for ( let i = 0; i < 5; i++ ) {
  466. models.add( new Model( {
  467. uid: Number( i ).toString()
  468. } ) );
  469. }
  470. }
  471. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  472. const evtInfo = evtArgs[ 0 ];
  473. expect( evtInfo.name ).to.equal( expectedName );
  474. expect( evtInfo.source ).to.equal( expectedSource );
  475. expect( evtInfo.path ).to.deep.equal( expectedPath );
  476. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  477. }