taxonomy.es6.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @file
  3. * Taxonomy behaviors.
  4. */
  5. (function($, Drupal) {
  6. /**
  7. * Reorder taxonomy terms.
  8. *
  9. * This behavior is dependent on the tableDrag behavior, since it uses the
  10. * objects initialized in that behavior to update the row.
  11. *
  12. * @type {Drupal~behavior}
  13. *
  14. * @prop {Drupal~behaviorAttach} attach
  15. * Attaches the drag behavior to a applicable table element.
  16. */
  17. Drupal.behaviors.termDrag = {
  18. attach(context, settings) {
  19. const backStep = settings.taxonomy.backStep;
  20. const forwardStep = settings.taxonomy.forwardStep;
  21. // Get the taxonomy tableDrag object.
  22. const tableDrag = Drupal.tableDrag.taxonomy;
  23. const $table = $('#taxonomy');
  24. const rows = $table.find('tr').length;
  25. // When a row is swapped, keep previous and next page classes set.
  26. tableDrag.row.prototype.onSwap = function(swappedRow) {
  27. $table
  28. .find('tr.taxonomy-term-preview')
  29. .removeClass('taxonomy-term-preview');
  30. $table
  31. .find('tr.taxonomy-term-divider-top')
  32. .removeClass('taxonomy-term-divider-top');
  33. $table
  34. .find('tr.taxonomy-term-divider-bottom')
  35. .removeClass('taxonomy-term-divider-bottom');
  36. const tableBody = $table[0].tBodies[0];
  37. if (backStep) {
  38. for (let n = 0; n < backStep; n++) {
  39. $(tableBody.rows[n]).addClass('taxonomy-term-preview');
  40. }
  41. $(tableBody.rows[backStep - 1]).addClass('taxonomy-term-divider-top');
  42. $(tableBody.rows[backStep]).addClass('taxonomy-term-divider-bottom');
  43. }
  44. if (forwardStep) {
  45. for (let k = rows - forwardStep - 1; k < rows - 1; k++) {
  46. $(tableBody.rows[k]).addClass('taxonomy-term-preview');
  47. }
  48. $(tableBody.rows[rows - forwardStep - 2]).addClass(
  49. 'taxonomy-term-divider-top',
  50. );
  51. $(tableBody.rows[rows - forwardStep - 1]).addClass(
  52. 'taxonomy-term-divider-bottom',
  53. );
  54. }
  55. };
  56. },
  57. };
  58. })(jQuery, Drupal);