linkformview.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /**
  6. * @module link/ui/linkformview
  7. */
  8. import View from '@ckeditor/ckeditor5-ui/src/view';
  9. import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import SwitchButtonView from '@ckeditor/ckeditor5-ui/src/button/switchbuttonview';
  12. import LabeledFieldView from '@ckeditor/ckeditor5-ui/src/labeledfield/labeledfieldview';
  13. import { createLabeledInputText } from '@ckeditor/ckeditor5-ui/src/labeledfield/utils';
  14. import injectCssTransitionDisabler from '@ckeditor/ckeditor5-ui/src/bindings/injectcsstransitiondisabler';
  15. import submitHandler from '@ckeditor/ckeditor5-ui/src/bindings/submithandler';
  16. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  17. import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
  18. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  19. import checkIcon from '@ckeditor/ckeditor5-core/theme/icons/check.svg';
  20. import cancelIcon from '@ckeditor/ckeditor5-core/theme/icons/cancel.svg';
  21. import '../../theme/linkform.css';
  22. import '@ckeditor/ckeditor5-ui/theme/components/responsive-form/responsiveform.css';
  23. /**
  24. * The link form view controller class.
  25. *
  26. * See {@link module:link/ui/linkformview~LinkFormView}.
  27. *
  28. * @extends module:ui/view~View
  29. */
  30. export default class LinkFormView extends View {
  31. /**
  32. * Creates an instance of the {@link module:link/ui/linkformview~LinkFormView} class.
  33. *
  34. * Also see {@link #render}.
  35. *
  36. * @param {module:utils/locale~Locale} [locale] The localization services instance.
  37. * @param {module:link/linkcommand~LinkCommand} linkCommand Reference to {@link module:link/linkcommand~LinkCommand}.
  38. * @param {String} [protocol] A value of a protocol to be displayed in the input's placeholder.
  39. */
  40. constructor( locale, linkCommand ) {
  41. super( locale );
  42. const t = locale.t;
  43. /**
  44. * Tracks information about DOM focus in the form.
  45. *
  46. * @readonly
  47. * @member {module:utils/focustracker~FocusTracker}
  48. */
  49. this.focusTracker = new FocusTracker();
  50. /**
  51. * An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
  52. *
  53. * @readonly
  54. * @member {module:utils/keystrokehandler~KeystrokeHandler}
  55. */
  56. this.keystrokes = new KeystrokeHandler();
  57. /**
  58. * The URL input view.
  59. *
  60. * @member {module:ui/labeledfield/labeledfieldview~LabeledFieldView}
  61. */
  62. this.urlInputView = this._createUrlInput();
  63. /**
  64. * The Save button view.
  65. *
  66. * @member {module:ui/button/buttonview~ButtonView}
  67. */
  68. this.saveButtonView = this._createButton( t( 'Save' ), checkIcon, 'ck-button-save' );
  69. this.saveButtonView.type = 'submit';
  70. /**
  71. * The Cancel button view.
  72. *
  73. * @member {module:ui/button/buttonview~ButtonView}
  74. */
  75. this.cancelButtonView = this._createButton( t( 'Cancel' ), cancelIcon, 'ck-button-cancel', 'cancel' );
  76. /**
  77. * A collection of {@link module:ui/button/switchbuttonview~SwitchButtonView},
  78. * which corresponds to {@link module:link/linkcommand~LinkCommand#manualDecorators manual decorators}
  79. * configured in the editor.
  80. *
  81. * @private
  82. * @readonly
  83. * @type {module:ui/viewcollection~ViewCollection}
  84. */
  85. this._manualDecoratorSwitches = this._createManualDecoratorSwitches( linkCommand );
  86. /**
  87. * A collection of child views in the form.
  88. *
  89. * @readonly
  90. * @type {module:ui/viewcollection~ViewCollection}
  91. */
  92. this.children = this._createFormChildren( linkCommand.manualDecorators );
  93. /**
  94. * A collection of views that can be focused in the form.
  95. *
  96. * @readonly
  97. * @protected
  98. * @member {module:ui/viewcollection~ViewCollection}
  99. */
  100. this._focusables = new ViewCollection();
  101. /**
  102. * Helps cycling over {@link #_focusables} in the form.
  103. *
  104. * @readonly
  105. * @protected
  106. * @member {module:ui/focuscycler~FocusCycler}
  107. */
  108. this._focusCycler = new FocusCycler( {
  109. focusables: this._focusables,
  110. focusTracker: this.focusTracker,
  111. keystrokeHandler: this.keystrokes,
  112. actions: {
  113. // Navigate form fields backwards using the Shift + Tab keystroke.
  114. focusPrevious: 'shift + tab',
  115. // Navigate form fields forwards using the Tab key.
  116. focusNext: 'tab'
  117. }
  118. } );
  119. const classList = [ 'ck', 'ck-link-form', 'ck-responsive-form' ];
  120. if ( linkCommand.manualDecorators.length ) {
  121. classList.push( 'ck-link-form_layout-vertical', 'ck-vertical-form' );
  122. }
  123. this.setTemplate( {
  124. tag: 'form',
  125. attributes: {
  126. class: classList,
  127. // https://github.com/ckeditor/ckeditor5-link/issues/90
  128. tabindex: '-1'
  129. },
  130. children: this.children
  131. } );
  132. injectCssTransitionDisabler( this );
  133. }
  134. /**
  135. * Obtains the state of the {@link module:ui/button/switchbuttonview~SwitchButtonView switch buttons} representing
  136. * {@link module:link/linkcommand~LinkCommand#manualDecorators manual link decorators}
  137. * in the {@link module:link/ui/linkformview~LinkFormView}.
  138. *
  139. * @returns {Object.<String,Boolean>} Key-value pairs, where the key is the name of the decorator and the value is
  140. * its state.
  141. */
  142. getDecoratorSwitchesState() {
  143. return Array.from( this._manualDecoratorSwitches ).reduce( ( accumulator, switchButton ) => {
  144. accumulator[ switchButton.name ] = switchButton.isOn;
  145. return accumulator;
  146. }, {} );
  147. }
  148. /**
  149. * @inheritDoc
  150. */
  151. render() {
  152. super.render();
  153. submitHandler( {
  154. view: this
  155. } );
  156. const childViews = [
  157. this.urlInputView,
  158. ...this._manualDecoratorSwitches,
  159. this.saveButtonView,
  160. this.cancelButtonView
  161. ];
  162. childViews.forEach( v => {
  163. // Register the view as focusable.
  164. this._focusables.add( v );
  165. // Register the view in the focus tracker.
  166. this.focusTracker.add( v.element );
  167. } );
  168. // Start listening for the keystrokes coming from #element.
  169. this.keystrokes.listenTo( this.element );
  170. }
  171. /**
  172. * Focuses the fist {@link #_focusables} in the form.
  173. */
  174. focus() {
  175. this._focusCycler.focusFirst();
  176. }
  177. /**
  178. * Creates a labeled input view.
  179. *
  180. * @private
  181. * @returns {module:ui/labeledfield/labeledfieldview~LabeledFieldView} Labeled field view instance.
  182. */
  183. _createUrlInput() {
  184. const t = this.locale.t;
  185. const labeledInput = new LabeledFieldView( this.locale, createLabeledInputText );
  186. labeledInput.label = t( 'Link URL' );
  187. return labeledInput;
  188. }
  189. /**
  190. * Creates a button view.
  191. *
  192. * @private
  193. * @param {String} label The button label.
  194. * @param {String} icon The button icon.
  195. * @param {String} className The additional button CSS class name.
  196. * @param {String} [eventName] An event name that the `ButtonView#execute` event will be delegated to.
  197. * @returns {module:ui/button/buttonview~ButtonView} The button view instance.
  198. */
  199. _createButton( label, icon, className, eventName ) {
  200. const button = new ButtonView( this.locale );
  201. button.set( {
  202. label,
  203. icon,
  204. tooltip: true
  205. } );
  206. button.extendTemplate( {
  207. attributes: {
  208. class: className
  209. }
  210. } );
  211. if ( eventName ) {
  212. button.delegate( 'execute' ).to( this, eventName );
  213. }
  214. return button;
  215. }
  216. /**
  217. * Populates {@link module:ui/viewcollection~ViewCollection} of {@link module:ui/button/switchbuttonview~SwitchButtonView}
  218. * made based on {@link module:link/linkcommand~LinkCommand#manualDecorators}.
  219. *
  220. * @private
  221. * @param {module:link/linkcommand~LinkCommand} linkCommand A reference to the link command.
  222. * @returns {module:ui/viewcollection~ViewCollection} of switch buttons.
  223. */
  224. _createManualDecoratorSwitches( linkCommand ) {
  225. const switches = this.createCollection();
  226. for ( const manualDecorator of linkCommand.manualDecorators ) {
  227. const switchButton = new SwitchButtonView( this.locale );
  228. switchButton.set( {
  229. name: manualDecorator.id,
  230. label: manualDecorator.label,
  231. withText: true
  232. } );
  233. switchButton.bind( 'isOn' ).toMany( [ manualDecorator, linkCommand ], 'value', ( decoratorValue, commandValue ) => {
  234. return commandValue === undefined && decoratorValue === undefined ? manualDecorator.defaultValue : decoratorValue;
  235. } );
  236. switchButton.on( 'execute', () => {
  237. manualDecorator.set( 'value', !switchButton.isOn );
  238. } );
  239. switches.add( switchButton );
  240. }
  241. return switches;
  242. }
  243. /**
  244. * Populates the {@link #children} collection of the form.
  245. *
  246. * If {@link module:link/linkcommand~LinkCommand#manualDecorators manual decorators} are configured in the editor, it creates an
  247. * additional `View` wrapping all {@link #_manualDecoratorSwitches} switch buttons corresponding
  248. * to these decorators.
  249. *
  250. * @private
  251. * @param {module:utils/collection~Collection} manualDecorators A reference to
  252. * the collection of manual decorators stored in the link command.
  253. * @returns {module:ui/viewcollection~ViewCollection} The children of link form view.
  254. */
  255. _createFormChildren( manualDecorators ) {
  256. const children = this.createCollection();
  257. children.add( this.urlInputView );
  258. if ( manualDecorators.length ) {
  259. const additionalButtonsView = new View();
  260. additionalButtonsView.setTemplate( {
  261. tag: 'ul',
  262. children: this._manualDecoratorSwitches.map( switchButton => ( {
  263. tag: 'li',
  264. children: [ switchButton ],
  265. attributes: {
  266. class: [
  267. 'ck',
  268. 'ck-list__item'
  269. ]
  270. }
  271. } ) ),
  272. attributes: {
  273. class: [
  274. 'ck',
  275. 'ck-reset',
  276. 'ck-list'
  277. ]
  278. }
  279. } );
  280. children.add( additionalButtonsView );
  281. }
  282. children.add( this.saveButtonView );
  283. children.add( this.cancelButtonView );
  284. return children;
  285. }
  286. }
  287. /**
  288. * Fired when the form view is submitted (when one of the children triggered the submit event),
  289. * for example with a click on {@link #saveButtonView}.
  290. *
  291. * @event submit
  292. */
  293. /**
  294. * Fired when the form view is canceled, for example with a click on {@link #cancelButtonView}.
  295. *
  296. * @event cancel
  297. */