EntityToolbarView.es6.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /**
  2. * @file
  3. * A Backbone View that provides an entity level toolbar.
  4. */
  5. (function ($, _, Backbone, Drupal, debounce) {
  6. Drupal.quickedit.EntityToolbarView = Backbone.View.extend(/** @lends Drupal.quickedit.EntityToolbarView# */{
  7. /**
  8. * @type {jQuery}
  9. */
  10. _fieldToolbarRoot: null,
  11. /**
  12. * @return {object}
  13. * A map of events.
  14. */
  15. events() {
  16. const map = {
  17. 'click button.action-save': 'onClickSave',
  18. 'click button.action-cancel': 'onClickCancel',
  19. mouseenter: 'onMouseenter',
  20. };
  21. return map;
  22. },
  23. /**
  24. * @constructs
  25. *
  26. * @augments Backbone.View
  27. *
  28. * @param {object} options
  29. * Options to construct the view.
  30. * @param {Drupal.quickedit.AppModel} options.appModel
  31. * A quickedit `AppModel` to use in the view.
  32. */
  33. initialize(options) {
  34. const that = this;
  35. this.appModel = options.appModel;
  36. this.$entity = $(this.model.get('el'));
  37. // Rerender whenever the entity state changes.
  38. this.listenTo(this.model, 'change:isActive change:isDirty change:state', this.render);
  39. // Also rerender whenever a different field is highlighted or activated.
  40. this.listenTo(this.appModel, 'change:highlightedField change:activeField', this.render);
  41. // Rerender when a field of the entity changes state.
  42. this.listenTo(this.model.get('fields'), 'change:state', this.fieldStateChange);
  43. // Reposition the entity toolbar as the viewport and the position within
  44. // the viewport changes.
  45. $(window).on('resize.quickedit scroll.quickedit drupalViewportOffsetChange.quickedit', debounce($.proxy(this.windowChangeHandler, this), 150));
  46. // Adjust the fence placement within which the entity toolbar may be
  47. // positioned.
  48. $(document).on('drupalViewportOffsetChange.quickedit', (event, offsets) => {
  49. if (that.$fence) {
  50. that.$fence.css(offsets);
  51. }
  52. });
  53. // Set the entity toolbar DOM element as the el for this view.
  54. const $toolbar = this.buildToolbarEl();
  55. this.setElement($toolbar);
  56. this._fieldToolbarRoot = $toolbar.find('.quickedit-toolbar-field').get(0);
  57. // Initial render.
  58. this.render();
  59. },
  60. /**
  61. * @inheritdoc
  62. *
  63. * @return {Drupal.quickedit.EntityToolbarView}
  64. * The entity toolbar view.
  65. */
  66. render() {
  67. if (this.model.get('isActive')) {
  68. // If the toolbar container doesn't exist, create it.
  69. const $body = $('body');
  70. if ($body.children('#quickedit-entity-toolbar').length === 0) {
  71. $body.append(this.$el);
  72. }
  73. // The fence will define a area on the screen that the entity toolbar
  74. // will be position within.
  75. if ($body.children('#quickedit-toolbar-fence').length === 0) {
  76. this.$fence = $(Drupal.theme('quickeditEntityToolbarFence'))
  77. .css(Drupal.displace())
  78. .appendTo($body);
  79. }
  80. // Adds the entity title to the toolbar.
  81. this.label();
  82. // Show the save and cancel buttons.
  83. this.show('ops');
  84. // If render is being called and the toolbar is already visible, just
  85. // reposition it.
  86. this.position();
  87. }
  88. // The save button text and state varies with the state of the entity
  89. // model.
  90. const $button = this.$el.find('.quickedit-button.action-save');
  91. const isDirty = this.model.get('isDirty');
  92. // Adjust the save button according to the state of the model.
  93. switch (this.model.get('state')) {
  94. // Quick editing is active, but no field is being edited.
  95. case 'opened':
  96. // The saving throbber is not managed by AJAX system. The
  97. // EntityToolbarView manages this visual element.
  98. $button
  99. .removeClass('action-saving icon-throbber icon-end')
  100. .text(Drupal.t('Save'))
  101. .removeAttr('disabled')
  102. .attr('aria-hidden', !isDirty);
  103. break;
  104. // The changes to the fields of the entity are being committed.
  105. case 'committing':
  106. $button
  107. .addClass('action-saving icon-throbber icon-end')
  108. .text(Drupal.t('Saving'))
  109. .attr('disabled', 'disabled');
  110. break;
  111. default:
  112. $button.attr('aria-hidden', true);
  113. break;
  114. }
  115. return this;
  116. },
  117. /**
  118. * @inheritdoc
  119. */
  120. remove() {
  121. // Remove additional DOM elements controlled by this View.
  122. this.$fence.remove();
  123. // Stop listening to additional events.
  124. $(window).off('resize.quickedit scroll.quickedit drupalViewportOffsetChange.quickedit');
  125. $(document).off('drupalViewportOffsetChange.quickedit');
  126. Backbone.View.prototype.remove.call(this);
  127. },
  128. /**
  129. * Repositions the entity toolbar on window scroll and resize.
  130. *
  131. * @param {jQuery.Event} event
  132. * The scroll or resize event.
  133. */
  134. windowChangeHandler(event) {
  135. this.position();
  136. },
  137. /**
  138. * Determines the actions to take given a change of state.
  139. *
  140. * @param {Drupal.quickedit.FieldModel} model
  141. * The `FieldModel` model.
  142. * @param {string} state
  143. * The state of the associated field. One of
  144. * {@link Drupal.quickedit.FieldModel.states}.
  145. */
  146. fieldStateChange(model, state) {
  147. switch (state) {
  148. case 'active':
  149. this.render();
  150. break;
  151. case 'invalid':
  152. this.render();
  153. break;
  154. }
  155. },
  156. /**
  157. * Uses the jQuery.ui.position() method to position the entity toolbar.
  158. *
  159. * @param {HTMLElement} [element]
  160. * The element against which the entity toolbar is positioned.
  161. */
  162. position(element) {
  163. clearTimeout(this.timer);
  164. const that = this;
  165. // Vary the edge of the positioning according to the direction of language
  166. // in the document.
  167. const edge = (document.documentElement.dir === 'rtl') ? 'right' : 'left';
  168. // A time unit to wait until the entity toolbar is repositioned.
  169. let delay = 0;
  170. // Determines what check in the series of checks below should be
  171. // evaluated.
  172. let check = 0;
  173. // When positioned against an active field that has padding, we should
  174. // ignore that padding when positioning the toolbar, to not unnecessarily
  175. // move the toolbar horizontally, which feels annoying.
  176. let horizontalPadding = 0;
  177. let of;
  178. let activeField;
  179. let highlightedField;
  180. // There are several elements in the page that the entity toolbar might be
  181. // positioned against. They are considered below in a priority order.
  182. do {
  183. switch (check) {
  184. case 0:
  185. // Position against a specific element.
  186. of = element;
  187. break;
  188. case 1:
  189. // Position against a form container.
  190. activeField = Drupal.quickedit.app.model.get('activeField');
  191. of = activeField && activeField.editorView && activeField.editorView.$formContainer && activeField.editorView.$formContainer.find('.quickedit-form');
  192. break;
  193. case 2:
  194. // Position against an active field.
  195. of = activeField && activeField.editorView && activeField.editorView.getEditedElement();
  196. if (activeField && activeField.editorView && activeField.editorView.getQuickEditUISettings().padding) {
  197. horizontalPadding = 5;
  198. }
  199. break;
  200. case 3:
  201. // Position against a highlighted field.
  202. highlightedField = Drupal.quickedit.app.model.get('highlightedField');
  203. of = highlightedField && highlightedField.editorView && highlightedField.editorView.getEditedElement();
  204. delay = 250;
  205. break;
  206. default: {
  207. const fieldModels = this.model.get('fields').models;
  208. let topMostPosition = 1000000;
  209. let topMostField = null;
  210. // Position against the topmost field.
  211. for (let i = 0; i < fieldModels.length; i++) {
  212. const pos = fieldModels[i].get('el').getBoundingClientRect().top;
  213. if (pos < topMostPosition) {
  214. topMostPosition = pos;
  215. topMostField = fieldModels[i];
  216. }
  217. }
  218. of = topMostField.get('el');
  219. delay = 50;
  220. break;
  221. }
  222. }
  223. // Prepare to check the next possible element to position against.
  224. check++;
  225. } while (!of);
  226. /**
  227. * Refines the positioning algorithm of jquery.ui.position().
  228. *
  229. * Invoked as the 'using' callback of jquery.ui.position() in
  230. * positionToolbar().
  231. *
  232. * @param {*} view
  233. * The view the positions will be calculated from.
  234. * @param {object} suggested
  235. * A hash of top and left values for the position that should be set. It
  236. * can be forwarded to .css() or .animate().
  237. * @param {object} info
  238. * The position and dimensions of both the 'my' element and the 'of'
  239. * elements, as well as calculations to their relative position. This
  240. * object contains the following properties:
  241. * @param {object} info.element
  242. * A hash that contains information about the HTML element that will be
  243. * positioned. Also known as the 'my' element.
  244. * @param {object} info.target
  245. * A hash that contains information about the HTML element that the
  246. * 'my' element will be positioned against. Also known as the 'of'
  247. * element.
  248. */
  249. function refinePosition(view, suggested, info) {
  250. // Determine if the pointer should be on the top or bottom.
  251. const isBelow = suggested.top > info.target.top;
  252. info.element.element.toggleClass('quickedit-toolbar-pointer-top', isBelow);
  253. // Don't position the toolbar past the first or last editable field if
  254. // the entity is the target.
  255. if (view.$entity[0] === info.target.element[0]) {
  256. // Get the first or last field according to whether the toolbar is
  257. // above or below the entity.
  258. const $field = view.$entity.find('.quickedit-editable').eq((isBelow) ? -1 : 0);
  259. if ($field.length > 0) {
  260. suggested.top = (isBelow) ? ($field.offset().top + $field.outerHeight(true)) : $field.offset().top - info.element.element.outerHeight(true);
  261. }
  262. }
  263. // Don't let the toolbar go outside the fence.
  264. const fenceTop = view.$fence.offset().top;
  265. const fenceHeight = view.$fence.height();
  266. const toolbarHeight = info.element.element.outerHeight(true);
  267. if (suggested.top < fenceTop) {
  268. suggested.top = fenceTop;
  269. }
  270. else if ((suggested.top + toolbarHeight) > (fenceTop + fenceHeight)) {
  271. suggested.top = (fenceTop + fenceHeight) - toolbarHeight;
  272. }
  273. // Position the toolbar.
  274. info.element.element.css({
  275. left: Math.floor(suggested.left),
  276. top: Math.floor(suggested.top),
  277. });
  278. }
  279. /**
  280. * Calls the jquery.ui.position() method on the $el of this view.
  281. */
  282. function positionToolbar() {
  283. that.$el
  284. .position({
  285. my: `${edge} bottom`,
  286. // Move the toolbar 1px towards the start edge of the 'of' element,
  287. // plus any horizontal padding that may have been added to the
  288. // element that is being added, to prevent unwanted horizontal
  289. // movement.
  290. at: `${edge}+${1 + horizontalPadding} top`,
  291. of,
  292. collision: 'flipfit',
  293. using: refinePosition.bind(null, that),
  294. within: that.$fence,
  295. })
  296. // Resize the toolbar to match the dimensions of the field, up to a
  297. // maximum width that is equal to 90% of the field's width.
  298. .css({
  299. 'max-width': (document.documentElement.clientWidth < 450) ? document.documentElement.clientWidth : 450,
  300. // Set a minimum width of 240px for the entity toolbar, or the width
  301. // of the client if it is less than 240px, so that the toolbar
  302. // never folds up into a squashed and jumbled mess.
  303. 'min-width': (document.documentElement.clientWidth < 240) ? document.documentElement.clientWidth : 240,
  304. width: '100%',
  305. });
  306. }
  307. // Uses the jQuery.ui.position() method. Use a timeout to move the toolbar
  308. // only after the user has focused on an editable for 250ms. This prevents
  309. // the toolbar from jumping around the screen.
  310. this.timer = setTimeout(() => {
  311. // Render the position in the next execution cycle, so that animations
  312. // on the field have time to process. This is not strictly speaking, a
  313. // guarantee that all animations will be finished, but it's a simple
  314. // way to get better positioning without too much additional code.
  315. _.defer(positionToolbar);
  316. }, delay);
  317. },
  318. /**
  319. * Set the model state to 'saving' when the save button is clicked.
  320. *
  321. * @param {jQuery.Event} event
  322. * The click event.
  323. */
  324. onClickSave(event) {
  325. event.stopPropagation();
  326. event.preventDefault();
  327. // Save the model.
  328. this.model.set('state', 'committing');
  329. },
  330. /**
  331. * Sets the model state to candidate when the cancel button is clicked.
  332. *
  333. * @param {jQuery.Event} event
  334. * The click event.
  335. */
  336. onClickCancel(event) {
  337. event.preventDefault();
  338. this.model.set('state', 'deactivating');
  339. },
  340. /**
  341. * Clears the timeout that will eventually reposition the entity toolbar.
  342. *
  343. * Without this, it may reposition itself, away from the user's cursor!
  344. *
  345. * @param {jQuery.Event} event
  346. * The mouse event.
  347. */
  348. onMouseenter(event) {
  349. clearTimeout(this.timer);
  350. },
  351. /**
  352. * Builds the entity toolbar HTML; attaches to DOM; sets starting position.
  353. *
  354. * @return {jQuery}
  355. * The toolbar element.
  356. */
  357. buildToolbarEl() {
  358. const $toolbar = $(Drupal.theme('quickeditEntityToolbar', {
  359. id: 'quickedit-entity-toolbar',
  360. }));
  361. $toolbar
  362. .find('.quickedit-toolbar-entity')
  363. // Append the "ops" toolgroup into the toolbar.
  364. .prepend(Drupal.theme('quickeditToolgroup', {
  365. classes: ['ops'],
  366. buttons: [
  367. {
  368. label: Drupal.t('Save'),
  369. type: 'submit',
  370. classes: 'action-save quickedit-button icon',
  371. attributes: {
  372. 'aria-hidden': true,
  373. },
  374. },
  375. {
  376. label: Drupal.t('Close'),
  377. classes: 'action-cancel quickedit-button icon icon-close icon-only',
  378. },
  379. ],
  380. }));
  381. // Give the toolbar a sensible starting position so that it doesn't
  382. // animate on to the screen from a far off corner.
  383. $toolbar
  384. .css({
  385. left: this.$entity.offset().left,
  386. top: this.$entity.offset().top,
  387. });
  388. return $toolbar;
  389. },
  390. /**
  391. * Returns the DOM element that fields will attach their toolbars to.
  392. *
  393. * @return {jQuery}
  394. * The DOM element that fields will attach their toolbars to.
  395. */
  396. getToolbarRoot() {
  397. return this._fieldToolbarRoot;
  398. },
  399. /**
  400. * Generates a state-dependent label for the entity toolbar.
  401. */
  402. label() {
  403. // The entity label.
  404. let label = '';
  405. const entityLabel = this.model.get('label');
  406. // Label of an active field, if it exists.
  407. const activeField = Drupal.quickedit.app.model.get('activeField');
  408. const activeFieldLabel = activeField && activeField.get('metadata').label;
  409. // Label of a highlighted field, if it exists.
  410. const highlightedField = Drupal.quickedit.app.model.get('highlightedField');
  411. const highlightedFieldLabel = highlightedField && highlightedField.get('metadata').label;
  412. // The label is constructed in a priority order.
  413. if (activeFieldLabel) {
  414. label = Drupal.theme('quickeditEntityToolbarLabel', {
  415. entityLabel,
  416. fieldLabel: activeFieldLabel,
  417. });
  418. }
  419. else if (highlightedFieldLabel) {
  420. label = Drupal.theme('quickeditEntityToolbarLabel', {
  421. entityLabel,
  422. fieldLabel: highlightedFieldLabel,
  423. });
  424. }
  425. else {
  426. // @todo Add XSS regression test coverage in https://www.drupal.org/node/2547437
  427. label = Drupal.checkPlain(entityLabel);
  428. }
  429. this.$el
  430. .find('.quickedit-toolbar-label')
  431. .html(label);
  432. },
  433. /**
  434. * Adds classes to a toolgroup.
  435. *
  436. * @param {string} toolgroup
  437. * A toolgroup name.
  438. * @param {string} classes
  439. * A string of space-delimited class names that will be applied to the
  440. * wrapping element of the toolbar group.
  441. */
  442. addClass(toolgroup, classes) {
  443. this._find(toolgroup).addClass(classes);
  444. },
  445. /**
  446. * Removes classes from a toolgroup.
  447. *
  448. * @param {string} toolgroup
  449. * A toolgroup name.
  450. * @param {string} classes
  451. * A string of space-delimited class names that will be removed from the
  452. * wrapping element of the toolbar group.
  453. */
  454. removeClass(toolgroup, classes) {
  455. this._find(toolgroup).removeClass(classes);
  456. },
  457. /**
  458. * Finds a toolgroup.
  459. *
  460. * @param {string} toolgroup
  461. * A toolgroup name.
  462. *
  463. * @return {jQuery}
  464. * The toolgroup DOM element.
  465. */
  466. _find(toolgroup) {
  467. return this.$el.find(`.quickedit-toolbar .quickedit-toolgroup.${toolgroup}`);
  468. },
  469. /**
  470. * Shows a toolgroup.
  471. *
  472. * @param {string} toolgroup
  473. * A toolgroup name.
  474. */
  475. show(toolgroup) {
  476. this.$el.removeClass('quickedit-animate-invisible');
  477. },
  478. });
  479. }(jQuery, _, Backbone, Drupal, Drupal.debounce));