linkformview.js 9.7 KB

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