view.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, HTMLElement */
  6. /* bender-tags: ui */
  7. 'use strict';
  8. const modules = bender.amd.require(
  9. 'core/ui/view',
  10. 'core/ui/region',
  11. 'core/ckeditorerror',
  12. 'core/model'
  13. );
  14. let View, TestView, Model, Region, CKEditorError;
  15. let view;
  16. bender.tools.createSinonSandbox();
  17. describe( 'View', () => {
  18. beforeEach( updateModuleReference );
  19. describe( 'constructor', () => {
  20. beforeEach( () => {
  21. setTestViewClass();
  22. setTestViewInstance();
  23. } );
  24. it( 'accepts the model', () => {
  25. setTestViewInstance( { a: 'foo', b: 42 } );
  26. expect( view.model ).to.be.an.instanceof( Model );
  27. expect( view ).to.have.deep.property( 'model.a', 'foo' );
  28. expect( view ).to.have.deep.property( 'model.b', 42 );
  29. } );
  30. it( 'defines basic view properties', () => {
  31. view = new View();
  32. expect( view.model ).to.be.null;
  33. expect( view.regions.length ).to.be.equal( 0 );
  34. expect( view.template ).to.be.null;
  35. expect( view._regionsSelectors ).to.be.empty;
  36. expect( view._el ).to.be.null;
  37. expect( view._template ).to.be.null;
  38. expect( () => {
  39. view.el;
  40. } ).to.throw( CKEditorError, /ui-view-notemplate/ );
  41. } );
  42. } );
  43. describe( 'init', () => {
  44. beforeEach( () => {
  45. setTestViewClass( () => ( {
  46. tag: 'p',
  47. children: [
  48. { tag: 'span' },
  49. { tag: 'strong' }
  50. ]
  51. } ) );
  52. } );
  53. it( 'calls child regions #init', () => {
  54. setTestViewInstance();
  55. const region1 = new Region( 'x' );
  56. const region2 = new Region( 'y' );
  57. view.register( region1, el => el );
  58. view.register( region2, el => el );
  59. const spy1 = bender.sinon.spy( region1, 'init' );
  60. const spy2 = bender.sinon.spy( region2, 'init' );
  61. view.init();
  62. sinon.assert.calledOnce( spy1 );
  63. sinon.assert.calledOnce( spy2 );
  64. } );
  65. it( 'initializes view regions with string selector', () => {
  66. setTestViewInstance();
  67. const region1 = new Region( 'x' );
  68. const region2 = new Region( 'y' );
  69. view.register( region1, 'span' );
  70. view.register( region2, 'strong' );
  71. view.init();
  72. expect( region1.el ).to.be.equal( view.el.firstChild );
  73. expect( region2.el ).to.be.equal( view.el.lastChild );
  74. } );
  75. it( 'initializes view regions with function selector', () => {
  76. setTestViewInstance();
  77. const region1 = new Region( 'x' );
  78. const region2 = new Region( 'y' );
  79. view.register( region1, el => el.firstChild );
  80. view.register( region2, el => el.lastChild );
  81. view.init();
  82. expect( region1.el ).to.be.equal( view.el.firstChild );
  83. expect( region2.el ).to.be.equal( view.el.lastChild );
  84. } );
  85. it( 'initializes view regions with boolean selector', () => {
  86. setTestViewInstance();
  87. const region1 = new Region( 'x' );
  88. const region2 = new Region( 'y' );
  89. view.register( region1, true );
  90. view.register( region2, true );
  91. view.init();
  92. expect( region1.el ).to.be.null;
  93. expect( region2.el ).to.be.null;
  94. } );
  95. } );
  96. describe( 'addChild', () => {
  97. beforeEach( () => {
  98. setTestViewClass(
  99. () => ( { tag: 'p', text: 'x' } ),
  100. function() {
  101. this.register( 'x', el => el );
  102. }
  103. );
  104. setTestViewInstance();
  105. } );
  106. it( 'should throw when no region name', () => {
  107. expect( () => {
  108. view.addChild();
  109. } ).to.throw( CKEditorError, /ui-view-addchild-badrname/ );
  110. } );
  111. it( 'should throw when region does not exist', () => {
  112. expect( () => {
  113. view.addChild( 'nonexistent' );
  114. } ).to.throw( CKEditorError, /ui-view-addchild-noreg/ );
  115. } );
  116. it( 'should throw when no child view passed', () => {
  117. expect( () => {
  118. view.addChild( 'x' );
  119. } ).to.throw( CKEditorError, /ui-view-addchild-no-view/ );
  120. } );
  121. it( 'should add a child to the region views', () => {
  122. const childView = new View();
  123. view.addChild( 'x', childView );
  124. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView );
  125. } );
  126. it( 'should add a child to the region views at the right index', () => {
  127. const childView1 = new View();
  128. const childView2 = new View();
  129. view.addChild( 'x', childView1 );
  130. view.addChild( 'x', childView2, 0 );
  131. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  132. expect( view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  133. } );
  134. } );
  135. describe( 'removeChild', () => {
  136. beforeEach( () => {
  137. setTestViewClass(
  138. () => ( { tag: 'p', text: 'x' } ),
  139. function() {
  140. this.register( 'x', el => el );
  141. }
  142. );
  143. setTestViewInstance();
  144. } );
  145. it( 'should throw when no region name', () => {
  146. expect( () => {
  147. view.removeChild();
  148. } ).to.throw( CKEditorError, /ui-view-removechild-badrname/ );
  149. } );
  150. it( 'should throw when child view passed', () => {
  151. expect( () => {
  152. view.removeChild( 'x' );
  153. } ).to.throw( CKEditorError, /ui-view-removechild-no-view/ );
  154. } );
  155. it( 'should throw when region does not exist', () => {
  156. expect( () => {
  157. view.removeChild( 'nonexistent', new View() );
  158. } ).to.throw( CKEditorError, /ui-view-removechild-noreg/ );
  159. } );
  160. it( 'should remove a child from the region views', () => {
  161. const childView1 = new View();
  162. const childView2 = new View();
  163. view.addChild( 'x', childView1 );
  164. view.addChild( 'x', childView2, 0 );
  165. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  166. expect( view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  167. view.removeChild( 'x', childView2 );
  168. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView1 );
  169. view.removeChild( 'x', childView1 );
  170. expect( view.regions.get( 'x' ).views.length ).to.be.equal( 0 );
  171. } );
  172. } );
  173. describe( 'getChild', () => {
  174. beforeEach( () => {
  175. setTestViewClass(
  176. () => ( { tag: 'p', text: 'x' } ),
  177. function() {
  178. this.register( 'x', el => el );
  179. }
  180. );
  181. setTestViewInstance();
  182. } );
  183. it( 'should throw when region does not exist', () => {
  184. expect( () => {
  185. view.getChild( 'nonexistent', 0 );
  186. } ).to.throw( CKEditorError, /ui-view-getchild-noreg/ );
  187. } );
  188. it( 'should get a child from the region views', () => {
  189. const childView1 = new View();
  190. const childView2 = new View();
  191. view.addChild( 'x', childView1 );
  192. view.addChild( 'x', childView2, 0 );
  193. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  194. expect( view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  195. } );
  196. } );
  197. describe( 'register', () => {
  198. beforeEach( () => {
  199. setTestViewClass();
  200. setTestViewInstance();
  201. } );
  202. it( 'should throw when first argument is neither Region instance nor string', () => {
  203. expect( () => {
  204. view.register( new Date() );
  205. } ).to.throw( CKEditorError, /ui-view-register-wrongtype/ );
  206. } );
  207. it( 'should throw when missing the selector argument', () => {
  208. expect( () => {
  209. view.register( 'x' );
  210. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  211. } );
  212. it( 'should throw when selector argument is of a wrong type', () => {
  213. expect( () => {
  214. view.register( 'x', new Date() );
  215. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  216. expect( () => {
  217. view.register( 'x', false );
  218. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  219. } );
  220. it( 'should throw when overriding an existing region but without override flag set', () => {
  221. expect( () => {
  222. view.register( 'x', true );
  223. view.register( new Region( 'x' ), true );
  224. } ).to.throw( CKEditorError, /ui-view-register-override/ );
  225. } );
  226. it( 'should register a new region with region name as a first argument', () => {
  227. view.register( 'x', true );
  228. expect( view.regions.get( 'x' ) ).to.be.an.instanceof( Region );
  229. } );
  230. it( 'should register a new region with Region instance as a first argument', () => {
  231. view.register( new Region( 'y' ), true );
  232. expect( view.regions.get( 'y' ) ).to.be.an.instanceof( Region );
  233. } );
  234. it( 'should override an existing region with override flag', () => {
  235. const region1 = new Region( 'x' );
  236. const region2 = new Region( 'x' );
  237. view.register( region1, true );
  238. view.register( region2, true, true );
  239. view.register( 'x', 'span', true );
  240. expect( view.regions.get( 'x' ) ).to.be.equal( region2 );
  241. expect( view._regionsSelectors.x ).to.be.equal( 'span' );
  242. } );
  243. it( 'should not override an existing region with the same region with override flag', () => {
  244. const region = new Region( 'x' );
  245. const spy = bender.sinon.spy( view.regions, 'remove' );
  246. view.register( region, true );
  247. view.register( region, true, true );
  248. sinon.assert.notCalled( spy );
  249. } );
  250. } );
  251. describe( 'el', () => {
  252. beforeEach( createViewInstanceWithTemplate );
  253. it( 'invokes out of #template', () => {
  254. setTestViewInstance( { a: 1 } );
  255. expect( view.el ).to.be.an.instanceof( HTMLElement );
  256. expect( view.el.nodeName ).to.be.equal( 'A' );
  257. } );
  258. it( 'can be explicitly declared', () => {
  259. class CustomView extends View {
  260. constructor() {
  261. super();
  262. this.el = document.createElement( 'span' );
  263. }
  264. }
  265. view = new CustomView();
  266. expect( view.el ).to.be.an.instanceof( HTMLElement );
  267. } );
  268. } );
  269. describe( 'bindToAttribute', () => {
  270. beforeEach( createViewInstanceWithTemplate );
  271. it( 'returns a function that passes arguments', () => {
  272. setTestViewInstance( { a: 'foo' } );
  273. let spy = bender.sinon.spy();
  274. let callback = view.bindToAttribute( 'a', spy );
  275. expect( spy.called ).to.be.false;
  276. callback( 'el', 'updater' );
  277. sinon.assert.calledOnce( spy );
  278. sinon.assert.calledWithExactly( spy, 'el', 'foo' );
  279. view.model.a = 'bar';
  280. sinon.assert.calledTwice( spy );
  281. expect( spy.secondCall.calledWithExactly( 'el', 'bar' ) ).to.be.true;
  282. } );
  283. it( 'allows binding attribute to the model', () => {
  284. setTestViewClass( function() {
  285. return {
  286. tag: 'p',
  287. attrs: {
  288. 'class': this.bindToAttribute( 'foo' )
  289. },
  290. text: 'abc'
  291. };
  292. } );
  293. setTestViewInstance( { foo: 'bar' } );
  294. expect( view.el.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  295. view.model.foo = 'baz';
  296. expect( view.el.outerHTML ).to.be.equal( '<p class="baz">abc</p>' );
  297. } );
  298. it( 'allows binding "text" to the model', () => {
  299. setTestViewClass( function() {
  300. return {
  301. tag: 'p',
  302. children: [
  303. {
  304. tag: 'b',
  305. text: 'baz'
  306. }
  307. ],
  308. text: this.bindToAttribute( 'foo' )
  309. };
  310. } );
  311. setTestViewInstance( { foo: 'bar' } );
  312. expect( view.el.outerHTML ).to.be.equal( '<p>bar<b>baz</b></p>' );
  313. // TODO: A solution to avoid nuking the children?
  314. view.model.foo = 'qux';
  315. expect( view.el.outerHTML ).to.be.equal( '<p>qux</p>' );
  316. } );
  317. it( 'allows binding to the model with value processing', () => {
  318. let callback = ( el, value ) =>
  319. ( value > 0 ? 'positive' : 'negative' );
  320. setTestViewClass( function() {
  321. return {
  322. tag: 'p',
  323. attrs: {
  324. 'class': this.bindToAttribute( 'foo', callback )
  325. },
  326. text: this.bindToAttribute( 'foo', callback )
  327. };
  328. } );
  329. setTestViewInstance( { foo: 3 } );
  330. expect( view.el.outerHTML ).to.be.equal( '<p class="positive">positive</p>' );
  331. view.model.foo = -7;
  332. expect( view.el.outerHTML ).to.be.equal( '<p class="negative">negative</p>' );
  333. } );
  334. it( 'allows binding to the model with custom callback', () => {
  335. setTestViewClass( function() {
  336. return {
  337. tag: 'p',
  338. attrs: {
  339. 'class': this.bindToAttribute( 'foo', ( el, value ) => {
  340. el.innerHTML = value;
  341. if ( value == 'changed' ) {
  342. return value;
  343. }
  344. } )
  345. },
  346. text: 'bar'
  347. };
  348. } );
  349. setTestViewInstance( { foo: 'moo' } );
  350. expect( view.el.outerHTML ).to.be.equal( '<p>moo</p>' );
  351. view.model.foo = 'changed';
  352. expect( view.el.outerHTML ).to.be.equal( '<p class="changed">changed</p>' );
  353. } );
  354. } );
  355. describe( 'on', () => {
  356. it( 'accepts plain binding', () => {
  357. let spy = bender.sinon.spy();
  358. setTestViewClass( function() {
  359. return {
  360. tag: 'p',
  361. on: {
  362. x: 'a',
  363. }
  364. };
  365. } );
  366. setTestViewInstance();
  367. view.on( 'a', spy );
  368. dispatchEvent( view.el, 'x' );
  369. sinon.assert.calledWithExactly( spy,
  370. sinon.match.has( 'name', 'a' ),
  371. sinon.match.has( 'target', view.el )
  372. );
  373. } );
  374. it( 'accepts an array of event bindings', () => {
  375. let spy1 = bender.sinon.spy();
  376. let spy2 = bender.sinon.spy();
  377. setTestViewClass( function() {
  378. return {
  379. tag: 'p',
  380. on: {
  381. x: [ 'a', 'b' ]
  382. }
  383. };
  384. } );
  385. setTestViewInstance();
  386. view.on( 'a', spy1 );
  387. view.on( 'b', spy2 );
  388. dispatchEvent( view.el, 'x' );
  389. sinon.assert.calledWithExactly( spy1,
  390. sinon.match.has( 'name', 'a' ),
  391. sinon.match.has( 'target', view.el )
  392. );
  393. sinon.assert.calledWithExactly( spy2,
  394. sinon.match.has( 'name', 'b' ),
  395. sinon.match.has( 'target', view.el )
  396. );
  397. } );
  398. it( 'accepts DOM selectors', () => {
  399. let spy1 = bender.sinon.spy();
  400. let spy2 = bender.sinon.spy();
  401. let spy3 = bender.sinon.spy();
  402. setTestViewClass( function() {
  403. return {
  404. tag: 'p',
  405. children: [
  406. {
  407. tag: 'span',
  408. attrs: {
  409. 'class': 'y',
  410. },
  411. on: {
  412. 'test@p': 'c'
  413. }
  414. },
  415. {
  416. tag: 'div',
  417. children: [
  418. {
  419. tag: 'span',
  420. attrs: {
  421. 'class': 'y',
  422. }
  423. }
  424. ],
  425. }
  426. ],
  427. on: {
  428. 'test@.y': 'a',
  429. 'test@div': 'b'
  430. }
  431. };
  432. } );
  433. setTestViewInstance();
  434. view.on( 'a', spy1 );
  435. view.on( 'b', spy2 );
  436. view.on( 'c', spy3 );
  437. // Test "test@p".
  438. dispatchEvent( view.el, 'test' );
  439. sinon.assert.callCount( spy1, 0 );
  440. sinon.assert.callCount( spy2, 0 );
  441. sinon.assert.callCount( spy3, 0 );
  442. // Test "test@.y".
  443. dispatchEvent( view.el.firstChild, 'test' );
  444. expect( spy1.firstCall.calledWithExactly(
  445. sinon.match.has( 'name', 'a' ),
  446. sinon.match.has( 'target', view.el.firstChild )
  447. ) ).to.be.true;
  448. sinon.assert.callCount( spy2, 0 );
  449. sinon.assert.callCount( spy3, 0 );
  450. // Test "test@div".
  451. dispatchEvent( view.el.lastChild, 'test' );
  452. sinon.assert.callCount( spy1, 1 );
  453. expect( spy2.firstCall.calledWithExactly(
  454. sinon.match.has( 'name', 'b' ),
  455. sinon.match.has( 'target', view.el.lastChild )
  456. ) ).to.be.true;
  457. sinon.assert.callCount( spy3, 0 );
  458. // Test "test@.y".
  459. dispatchEvent( view.el.lastChild.firstChild, 'test' );
  460. expect( spy1.secondCall.calledWithExactly(
  461. sinon.match.has( 'name', 'a' ),
  462. sinon.match.has( 'target', view.el.lastChild.firstChild )
  463. ) ).to.be.true;
  464. sinon.assert.callCount( spy2, 1 );
  465. sinon.assert.callCount( spy3, 0 );
  466. } );
  467. it( 'accepts function callbacks', () => {
  468. let spy1 = bender.sinon.spy();
  469. let spy2 = bender.sinon.spy();
  470. setTestViewClass( function() {
  471. return {
  472. tag: 'p',
  473. children: [
  474. {
  475. tag: 'span'
  476. }
  477. ],
  478. on: {
  479. x: spy1,
  480. 'y@span': [ spy2, 'c' ],
  481. }
  482. };
  483. } );
  484. setTestViewInstance();
  485. dispatchEvent( view.el, 'x' );
  486. dispatchEvent( view.el.firstChild, 'y' );
  487. sinon.assert.calledWithExactly( spy1,
  488. sinon.match.has( 'target', view.el )
  489. );
  490. sinon.assert.calledWithExactly( spy2,
  491. sinon.match.has( 'target', view.el.firstChild )
  492. );
  493. } );
  494. it( 'supports event delegation', () => {
  495. let spy = bender.sinon.spy();
  496. setTestViewClass( function() {
  497. return {
  498. tag: 'p',
  499. children: [
  500. {
  501. tag: 'span'
  502. }
  503. ],
  504. on: {
  505. x: 'a',
  506. }
  507. };
  508. } );
  509. setTestViewInstance();
  510. view.on( 'a', spy );
  511. dispatchEvent( view.el.firstChild, 'x' );
  512. sinon.assert.calledWithExactly( spy,
  513. sinon.match.has( 'name', 'a' ),
  514. sinon.match.has( 'target', view.el.firstChild )
  515. );
  516. } );
  517. it( 'works for future elements', () => {
  518. let spy = bender.sinon.spy();
  519. setTestViewClass( function() {
  520. return {
  521. tag: 'p',
  522. on: {
  523. 'test@div': 'a'
  524. }
  525. };
  526. } );
  527. setTestViewInstance();
  528. view.on( 'a', spy );
  529. let div = document.createElement( 'div' );
  530. view.el.appendChild( div );
  531. dispatchEvent( div, 'test' );
  532. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  533. } );
  534. } );
  535. describe( 'destroy', () => {
  536. beforeEach( createViewInstanceWithTemplate );
  537. it( 'should destroy the view', () => {
  538. view.destroy();
  539. expect( view.model ).to.be.null;
  540. expect( view.regions ).to.be.null;
  541. expect( view.template ).to.be.null;
  542. expect( view._regionsSelectors ).to.be.null;
  543. expect( view._el ).to.be.null;
  544. expect( view._template ).to.be.null;
  545. } );
  546. it( 'detaches the element from DOM', () => {
  547. const elRef = view.el;
  548. document.createElement( 'div' ).appendChild( view.el );
  549. view.destroy();
  550. expect( elRef.parentNode ).to.be.null;
  551. } );
  552. it( 'destroys child regions', () => {
  553. const region = new Region( 'x' );
  554. const spy = bender.sinon.spy( region, 'destroy' );
  555. const regionsRef = view.regions;
  556. const regionViewsRef = region.views;
  557. view.register( region, true );
  558. view.addChild( 'x', new View() );
  559. view.destroy();
  560. expect( regionsRef.length ).to.be.equal( 0 );
  561. expect( regionViewsRef.length ).to.be.equal( 0 );
  562. expect( spy.calledOnce ).to.be.true;
  563. } );
  564. it( 'detaches bound model listeners', () => {
  565. setTestViewClass( function() {
  566. return {
  567. tag: 'p',
  568. text: this.bindToAttribute( 'foo' )
  569. };
  570. } );
  571. setTestViewInstance( { foo: 'bar' } );
  572. const modelRef = view.model;
  573. const elRef = view.el;
  574. expect( view.el.outerHTML ).to.be.equal( '<p>bar</p>' );
  575. modelRef.foo = 'baz';
  576. expect( view.el.outerHTML ).to.be.equal( '<p>baz</p>' );
  577. view.destroy();
  578. modelRef.foo = 'abc';
  579. expect( elRef.outerHTML ).to.be.equal( '<p>baz</p>' );
  580. } );
  581. it( 'destroy a template–less view', () => {
  582. view = new View();
  583. expect( () => {
  584. view.destroy();
  585. } ).to.not.throw();
  586. } );
  587. } );
  588. } );
  589. function updateModuleReference() {
  590. View = modules[ 'core/ui/view' ];
  591. Region = modules[ 'core/ui/region' ];
  592. Model = modules[ 'core/model' ];
  593. CKEditorError = modules[ 'core/ckeditorerror' ];
  594. }
  595. function createViewInstanceWithTemplate() {
  596. setTestViewClass( () => ( { tag: 'a' } ) );
  597. setTestViewInstance();
  598. }
  599. function setTestViewClass( templateFn, regionsFn ) {
  600. TestView = class V extends View {
  601. constructor( model ) {
  602. super( model );
  603. if ( templateFn ) {
  604. this.template = templateFn.call( this );
  605. }
  606. if ( templateFn && regionsFn ) {
  607. regionsFn.call( this );
  608. }
  609. }
  610. };
  611. }
  612. function setTestViewInstance( model ) {
  613. view = new TestView( new Model( model ) );
  614. if ( view.template ) {
  615. document.body.appendChild( view.el );
  616. }
  617. }
  618. function dispatchEvent( el, domEvtName ) {
  619. if ( !el.parentNode ) {
  620. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  621. }
  622. el.dispatchEvent( new Event( domEvtName, {
  623. bubbles: true
  624. } ) );
  625. }