linkformview.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals Event, document */
  6. import LinkFormView from '../../src/ui/linkformview';
  7. import View from '@ckeditor/ckeditor5-ui/src/view';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  10. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
  12. import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import ManualDecorator from '../../src/utils/manualdecorator';
  15. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  16. import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  17. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  18. import Link from '../../src/link';
  19. import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
  20. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  21. describe( 'LinkFormView', () => {
  22. let view;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. view = new LinkFormView( { t: val => val }, { manualDecorators: [] } );
  26. view.render();
  27. } );
  28. afterEach( () => {
  29. view.destroy();
  30. } );
  31. describe( 'constructor()', () => {
  32. it( 'should create element from template', () => {
  33. expect( view.element.classList.contains( 'ck' ) ).to.true;
  34. expect( view.element.classList.contains( 'ck-link-form' ) ).to.true;
  35. expect( view.element.classList.contains( 'ck-responsive-form' ) ).to.true;
  36. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  37. } );
  38. it( 'should create child views', () => {
  39. expect( view.urlInputView ).to.be.instanceOf( View );
  40. expect( view.saveButtonView ).to.be.instanceOf( View );
  41. expect( view.cancelButtonView ).to.be.instanceOf( View );
  42. expect( view.saveButtonView.element.classList.contains( 'ck-button-save' ) ).to.be.true;
  43. expect( view.cancelButtonView.element.classList.contains( 'ck-button-cancel' ) ).to.be.true;
  44. expect( view.children.get( 0 ) ).to.equal( view.urlInputView );
  45. expect( view.children.get( 1 ) ).to.equal( view.saveButtonView );
  46. expect( view.children.get( 2 ) ).to.equal( view.cancelButtonView );
  47. } );
  48. it( 'should create #focusTracker instance', () => {
  49. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  50. } );
  51. it( 'should create #keystrokes instance', () => {
  52. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  53. } );
  54. it( 'should create #_focusCycler instance', () => {
  55. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  56. } );
  57. it( 'should create #_focusables view collection', () => {
  58. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  59. } );
  60. it( 'should fire `cancel` event on cancelButtonView#execute', () => {
  61. const spy = sinon.spy();
  62. view.on( 'cancel', spy );
  63. view.cancelButtonView.fire( 'execute' );
  64. expect( spy.calledOnce ).to.true;
  65. } );
  66. describe( 'template', () => {
  67. it( 'has url input view', () => {
  68. expect( view.template.children[ 0 ].get( 0 ) ).to.equal( view.urlInputView );
  69. } );
  70. it( 'has button views', () => {
  71. expect( view.template.children[ 0 ].get( 1 ) ).to.equal( view.saveButtonView );
  72. expect( view.template.children[ 0 ].get( 2 ) ).to.equal( view.cancelButtonView );
  73. } );
  74. } );
  75. it( 'should implement the CSS transition disabling feature', () => {
  76. expect( view.disableCssTransitions ).to.be.a( 'function' );
  77. } );
  78. } );
  79. describe( 'render()', () => {
  80. it( 'should register child views in #_focusables', () => {
  81. expect( view._focusables.map( f => f ) ).to.have.members( [
  82. view.urlInputView,
  83. view.saveButtonView,
  84. view.cancelButtonView
  85. ] );
  86. } );
  87. it( 'should register child views\' #element in #focusTracker', () => {
  88. view = new LinkFormView( { t: () => {} }, { manualDecorators: [] } );
  89. const spy = testUtils.sinon.spy( view.focusTracker, 'add' );
  90. view.render();
  91. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.urlInputView.element );
  92. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.saveButtonView.element );
  93. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.cancelButtonView.element );
  94. } );
  95. it( 'starts listening for #keystrokes coming from #element', () => {
  96. view = new LinkFormView( { t: () => {} }, { manualDecorators: [] } );
  97. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  98. view.render();
  99. sinon.assert.calledOnce( spy );
  100. sinon.assert.calledWithExactly( spy, view.element );
  101. } );
  102. describe( 'activates keyboard navigation for the toolbar', () => {
  103. it( 'so "tab" focuses the next focusable item', () => {
  104. const keyEvtData = {
  105. keyCode: keyCodes.tab,
  106. preventDefault: sinon.spy(),
  107. stopPropagation: sinon.spy()
  108. };
  109. // Mock the url input is focused.
  110. view.focusTracker.isFocused = true;
  111. view.focusTracker.focusedElement = view.urlInputView.element;
  112. const spy = sinon.spy( view.saveButtonView, 'focus' );
  113. view.keystrokes.press( keyEvtData );
  114. sinon.assert.calledOnce( keyEvtData.preventDefault );
  115. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  116. sinon.assert.calledOnce( spy );
  117. } );
  118. it( 'so "shift + tab" focuses the previous focusable item', () => {
  119. const keyEvtData = {
  120. keyCode: keyCodes.tab,
  121. shiftKey: true,
  122. preventDefault: sinon.spy(),
  123. stopPropagation: sinon.spy()
  124. };
  125. // Mock the cancel button is focused.
  126. view.focusTracker.isFocused = true;
  127. view.focusTracker.focusedElement = view.cancelButtonView.element;
  128. const spy = sinon.spy( view.saveButtonView, 'focus' );
  129. view.keystrokes.press( keyEvtData );
  130. sinon.assert.calledOnce( keyEvtData.preventDefault );
  131. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  132. sinon.assert.calledOnce( spy );
  133. } );
  134. } );
  135. } );
  136. describe( 'DOM bindings', () => {
  137. describe( 'submit event', () => {
  138. it( 'should trigger submit event', () => {
  139. const spy = sinon.spy();
  140. view.on( 'submit', spy );
  141. view.element.dispatchEvent( new Event( 'submit' ) );
  142. expect( spy.calledOnce ).to.true;
  143. } );
  144. } );
  145. } );
  146. describe( 'focus()', () => {
  147. it( 'focuses the #urlInputView', () => {
  148. const spy = sinon.spy( view.urlInputView, 'focus' );
  149. view.focus();
  150. sinon.assert.calledOnce( spy );
  151. } );
  152. } );
  153. describe( 'manual decorators', () => {
  154. let view, collection, linkCommand;
  155. beforeEach( () => {
  156. collection = new Collection();
  157. collection.add( new ManualDecorator( {
  158. id: 'decorator1',
  159. label: 'Foo',
  160. attributes: {
  161. foo: 'bar'
  162. }
  163. } ) );
  164. collection.add( new ManualDecorator( {
  165. id: 'decorator2',
  166. label: 'Download',
  167. attributes: {
  168. download: 'download'
  169. },
  170. defaultValue: true
  171. } ) );
  172. collection.add( new ManualDecorator( {
  173. id: 'decorator3',
  174. label: 'Multi',
  175. attributes: {
  176. class: 'fancy-class',
  177. target: '_blank',
  178. rel: 'noopener noreferrer'
  179. }
  180. } ) );
  181. class LinkCommandMock {
  182. constructor( manualDecorators ) {
  183. this.manualDecorators = manualDecorators;
  184. this.set( 'value' );
  185. }
  186. }
  187. mix( LinkCommandMock, ObservableMixin );
  188. linkCommand = new LinkCommandMock( collection );
  189. view = new LinkFormView( { t: val => val }, linkCommand );
  190. view.render();
  191. } );
  192. afterEach( () => {
  193. view.destroy();
  194. collection.clear();
  195. } );
  196. it( 'switch buttons reflects state of manual decorators', () => {
  197. expect( view._manualDecoratorSwitches.length ).to.equal( 3 );
  198. expect( view._manualDecoratorSwitches.get( 0 ) ).to.deep.include( {
  199. name: 'decorator1',
  200. label: 'Foo',
  201. isOn: undefined
  202. } );
  203. expect( view._manualDecoratorSwitches.get( 1 ) ).to.deep.include( {
  204. name: 'decorator2',
  205. label: 'Download',
  206. isOn: true
  207. } );
  208. expect( view._manualDecoratorSwitches.get( 2 ) ).to.deep.include( {
  209. name: 'decorator3',
  210. label: 'Multi',
  211. isOn: undefined
  212. } );
  213. } );
  214. it( 'reacts on switch button changes', () => {
  215. const modelItem = collection.first;
  216. const viewItem = view._manualDecoratorSwitches.first;
  217. expect( modelItem.value ).to.be.undefined;
  218. expect( viewItem.isOn ).to.be.undefined;
  219. viewItem.element.dispatchEvent( new Event( 'click' ) );
  220. expect( modelItem.value ).to.be.true;
  221. expect( viewItem.isOn ).to.be.true;
  222. viewItem.element.dispatchEvent( new Event( 'click' ) );
  223. expect( modelItem.value ).to.be.false;
  224. expect( viewItem.isOn ).to.be.false;
  225. } );
  226. it( 'reacts on switch button changes for the decorator with defaultValue', () => {
  227. const modelItem = collection.get( 1 );
  228. const viewItem = view._manualDecoratorSwitches.get( 1 );
  229. expect( modelItem.value ).to.be.undefined;
  230. expect( viewItem.isOn ).to.be.true;
  231. viewItem.element.dispatchEvent( new Event( 'click' ) );
  232. expect( modelItem.value ).to.be.false;
  233. expect( viewItem.isOn ).to.be.false;
  234. viewItem.element.dispatchEvent( new Event( 'click' ) );
  235. expect( modelItem.value ).to.be.true;
  236. expect( viewItem.isOn ).to.be.true;
  237. } );
  238. describe( 'getDecoratorSwitchesState()', () => {
  239. it( 'should provide object with decorators states', () => {
  240. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  241. decorator1: undefined,
  242. decorator2: true,
  243. decorator3: undefined
  244. } );
  245. view._manualDecoratorSwitches.map( item => {
  246. item.element.dispatchEvent( new Event( 'click' ) );
  247. } );
  248. view._manualDecoratorSwitches.get( 2 ).element.dispatchEvent( new Event( 'click' ) );
  249. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  250. decorator1: true,
  251. decorator2: false,
  252. decorator3: false
  253. } );
  254. } );
  255. it( 'should use decorator default value if command and decorator values are not set', () => {
  256. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  257. decorator1: undefined,
  258. decorator2: true,
  259. decorator3: undefined
  260. } );
  261. } );
  262. it( 'should use a decorator value if decorator value is set', () => {
  263. for ( const decorator of collection ) {
  264. decorator.value = true;
  265. }
  266. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  267. decorator1: true,
  268. decorator2: true,
  269. decorator3: true
  270. } );
  271. for ( const decorator of collection ) {
  272. decorator.value = false;
  273. }
  274. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  275. decorator1: false,
  276. decorator2: false,
  277. decorator3: false
  278. } );
  279. } );
  280. it( 'should use a decorator value if link command value is set', () => {
  281. linkCommand.value = '';
  282. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  283. decorator1: undefined,
  284. decorator2: undefined,
  285. decorator3: undefined
  286. } );
  287. for ( const decorator of collection ) {
  288. decorator.value = false;
  289. }
  290. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  291. decorator1: false,
  292. decorator2: false,
  293. decorator3: false
  294. } );
  295. for ( const decorator of collection ) {
  296. decorator.value = true;
  297. }
  298. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  299. decorator1: true,
  300. decorator2: true,
  301. decorator3: true
  302. } );
  303. } );
  304. } );
  305. } );
  306. describe( 'localization of manual decorators', () => {
  307. before( () => {
  308. addTranslations( 'pl', {
  309. 'Open in a new tab': 'Otwórz w nowym oknie'
  310. } );
  311. } );
  312. after( () => {
  313. clearTranslations();
  314. } );
  315. let editor, editorElement, linkFormView;
  316. beforeEach( () => {
  317. editorElement = document.createElement( 'div' );
  318. document.body.appendChild( editorElement );
  319. return ClassicTestEditor
  320. .create( editorElement, {
  321. plugins: [ Link ],
  322. toolbar: [ 'link' ],
  323. language: 'pl',
  324. link: {
  325. decorators: {
  326. IsExternal: {
  327. mode: 'manual',
  328. label: 'Open in a new tab',
  329. attributes: {
  330. target: '_blank'
  331. }
  332. }
  333. }
  334. }
  335. } )
  336. .then( newEditor => {
  337. editor = newEditor;
  338. linkFormView = new LinkFormView( editor.locale, editor.commands.get( 'link' ) );
  339. } );
  340. } );
  341. afterEach( () => {
  342. editorElement.remove();
  343. return editor.destroy();
  344. } );
  345. it( 'translates labels of manual decorators UI', () => {
  346. expect( linkFormView._manualDecoratorSwitches.first.label ).to.equal( 'Otwórz w nowym oknie' );
  347. } );
  348. } );
  349. } );