tabledrag.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal, drupalSettings) {
  8. var showWeight = JSON.parse(localStorage.getItem('Drupal.tableDrag.showWeight'));
  9. Drupal.behaviors.tableDrag = {
  10. attach: function attach(context, settings) {
  11. function initTableDrag(table, base) {
  12. if (table.length) {
  13. Drupal.tableDrag[base] = new Drupal.tableDrag(table[0], settings.tableDrag[base]);
  14. }
  15. }
  16. Object.keys(settings.tableDrag || {}).forEach(function (base) {
  17. initTableDrag($(context).find('#' + base).once('tabledrag'), base);
  18. });
  19. }
  20. };
  21. Drupal.tableDrag = function (table, tableSettings) {
  22. var _this = this;
  23. var self = this;
  24. var $table = $(table);
  25. this.$table = $(table);
  26. this.table = table;
  27. this.tableSettings = tableSettings;
  28. this.dragObject = null;
  29. this.rowObject = null;
  30. this.oldRowElement = null;
  31. this.oldY = 0;
  32. this.changed = false;
  33. this.maxDepth = 0;
  34. this.rtl = $(this.table).css('direction') === 'rtl' ? -1 : 1;
  35. this.striping = $(this.table).data('striping') === 1;
  36. this.scrollSettings = { amount: 4, interval: 50, trigger: 70 };
  37. this.scrollInterval = null;
  38. this.scrollY = 0;
  39. this.windowHeight = 0;
  40. this.indentEnabled = false;
  41. Object.keys(tableSettings || {}).forEach(function (group) {
  42. Object.keys(tableSettings[group] || {}).forEach(function (n) {
  43. if (tableSettings[group][n].relationship === 'parent') {
  44. _this.indentEnabled = true;
  45. }
  46. if (tableSettings[group][n].limit > 0) {
  47. _this.maxDepth = tableSettings[group][n].limit;
  48. }
  49. });
  50. });
  51. if (this.indentEnabled) {
  52. this.indentCount = 1;
  53. var indent = Drupal.theme('tableDragIndentation');
  54. var testRow = $('<tr/>').addClass('draggable').appendTo(table);
  55. var testCell = $('<td/>').appendTo(testRow).prepend(indent).prepend(indent);
  56. var $indentation = testCell.find('.js-indentation');
  57. this.indentAmount = $indentation.get(1).offsetLeft - $indentation.get(0).offsetLeft;
  58. testRow.remove();
  59. }
  60. $table.find('> tr.draggable, > tbody > tr.draggable').each(function () {
  61. self.makeDraggable(this);
  62. });
  63. $table.before($('<button type="button" class="link tabledrag-toggle-weight"></button>').attr('title', Drupal.t('Re-order rows by numerical weight instead of dragging.')).on('click', $.proxy(function (e) {
  64. e.preventDefault();
  65. this.toggleColumns();
  66. }, this)).wrap('<div class="tabledrag-toggle-weight-wrapper"></div>').parent());
  67. self.initColumns();
  68. $(document).on('touchmove', function (event) {
  69. return self.dragRow(event.originalEvent.touches[0], self);
  70. });
  71. $(document).on('touchend', function (event) {
  72. return self.dropRow(event.originalEvent.touches[0], self);
  73. });
  74. $(document).on('mousemove pointermove', function (event) {
  75. return self.dragRow(event, self);
  76. });
  77. $(document).on('mouseup pointerup', function (event) {
  78. return self.dropRow(event, self);
  79. });
  80. $(window).on('storage', $.proxy(function (e) {
  81. if (e.originalEvent.key === 'Drupal.tableDrag.showWeight') {
  82. showWeight = JSON.parse(e.originalEvent.newValue);
  83. this.displayColumns(showWeight);
  84. }
  85. }, this));
  86. };
  87. Drupal.tableDrag.prototype.initColumns = function () {
  88. var _this2 = this;
  89. var $table = this.$table;
  90. var hidden = void 0;
  91. var cell = void 0;
  92. var columnIndex = void 0;
  93. Object.keys(this.tableSettings || {}).forEach(function (group) {
  94. for (var d in _this2.tableSettings[group]) {
  95. if (_this2.tableSettings[group].hasOwnProperty(d)) {
  96. var field = $table.find('.' + _this2.tableSettings[group][d].target).eq(0);
  97. if (field.length && _this2.tableSettings[group][d].hidden) {
  98. hidden = _this2.tableSettings[group][d].hidden;
  99. cell = field.closest('td');
  100. break;
  101. }
  102. }
  103. }
  104. if (hidden && cell[0]) {
  105. columnIndex = cell.parent().find('> td').index(cell.get(0)) + 1;
  106. $table.find('> thead > tr, > tbody > tr, > tr').each(_this2.addColspanClass(columnIndex));
  107. }
  108. });
  109. this.displayColumns(showWeight);
  110. };
  111. Drupal.tableDrag.prototype.addColspanClass = function (columnIndex) {
  112. return function () {
  113. var $row = $(this);
  114. var index = columnIndex;
  115. var cells = $row.children();
  116. var cell = void 0;
  117. cells.each(function (n) {
  118. if (n < index && this.colSpan && this.colSpan > 1) {
  119. index -= this.colSpan - 1;
  120. }
  121. });
  122. if (index > 0) {
  123. cell = cells.filter(':nth-child(' + index + ')');
  124. if (cell[0].colSpan && cell[0].colSpan > 1) {
  125. cell.addClass('tabledrag-has-colspan');
  126. } else {
  127. cell.addClass('tabledrag-hide');
  128. }
  129. }
  130. };
  131. };
  132. Drupal.tableDrag.prototype.displayColumns = function (displayWeight) {
  133. if (displayWeight) {
  134. this.showColumns();
  135. } else {
  136. this.hideColumns();
  137. }
  138. $('table').findOnce('tabledrag').trigger('columnschange', !!displayWeight);
  139. };
  140. Drupal.tableDrag.prototype.toggleColumns = function () {
  141. showWeight = !showWeight;
  142. this.displayColumns(showWeight);
  143. if (showWeight) {
  144. localStorage.setItem('Drupal.tableDrag.showWeight', showWeight);
  145. } else {
  146. localStorage.removeItem('Drupal.tableDrag.showWeight');
  147. }
  148. };
  149. Drupal.tableDrag.prototype.hideColumns = function () {
  150. var $tables = $('table').findOnce('tabledrag');
  151. $tables.find('.tabledrag-hide').css('display', 'none');
  152. $tables.find('.tabledrag-handle').css('display', '');
  153. $tables.find('.tabledrag-has-colspan').each(function () {
  154. this.colSpan = this.colSpan - 1;
  155. });
  156. $('.tabledrag-toggle-weight').text(Drupal.t('Show row weights'));
  157. };
  158. Drupal.tableDrag.prototype.showColumns = function () {
  159. var $tables = $('table').findOnce('tabledrag');
  160. $tables.find('.tabledrag-hide').css('display', '');
  161. $tables.find('.tabledrag-handle').css('display', 'none');
  162. $tables.find('.tabledrag-has-colspan').each(function () {
  163. this.colSpan = this.colSpan + 1;
  164. });
  165. $('.tabledrag-toggle-weight').text(Drupal.t('Hide row weights'));
  166. };
  167. Drupal.tableDrag.prototype.rowSettings = function (group, row) {
  168. var field = $(row).find('.' + group);
  169. var tableSettingsGroup = this.tableSettings[group];
  170. for (var delta in tableSettingsGroup) {
  171. if (tableSettingsGroup.hasOwnProperty(delta)) {
  172. var targetClass = tableSettingsGroup[delta].target;
  173. if (field.is('.' + targetClass)) {
  174. var rowSettings = {};
  175. for (var n in tableSettingsGroup[delta]) {
  176. if (tableSettingsGroup[delta].hasOwnProperty(n)) {
  177. rowSettings[n] = tableSettingsGroup[delta][n];
  178. }
  179. }
  180. return rowSettings;
  181. }
  182. }
  183. }
  184. };
  185. Drupal.tableDrag.prototype.makeDraggable = function (item) {
  186. var self = this;
  187. var $item = $(item);
  188. $item.find('td:first-of-type').find('a').addClass('menu-item__link');
  189. var handle = $('<a href="#" class="tabledrag-handle"><div class="handle">&nbsp;</div></a>').attr('title', Drupal.t('Drag to re-order'));
  190. var $indentationLast = $item.find('td:first-of-type').find('.js-indentation').eq(-1);
  191. if ($indentationLast.length) {
  192. $indentationLast.after(handle);
  193. self.indentCount = Math.max($item.find('.js-indentation').length, self.indentCount);
  194. } else {
  195. $item.find('td').eq(0).prepend(handle);
  196. }
  197. handle.on('mousedown touchstart pointerdown', function (event) {
  198. event.preventDefault();
  199. if (event.originalEvent.type === 'touchstart') {
  200. event = event.originalEvent.touches[0];
  201. }
  202. self.dragStart(event, self, item);
  203. });
  204. handle.on('click', function (e) {
  205. e.preventDefault();
  206. });
  207. handle.on('focus', function () {
  208. self.safeBlur = true;
  209. });
  210. handle.on('blur', function (event) {
  211. if (self.rowObject && self.safeBlur) {
  212. self.dropRow(event, self);
  213. }
  214. });
  215. handle.on('keydown', function (event) {
  216. if (event.keyCode !== 9 && !self.rowObject) {
  217. self.rowObject = new self.row(item, 'keyboard', self.indentEnabled, self.maxDepth, true);
  218. }
  219. var keyChange = false;
  220. var groupHeight = void 0;
  221. switch (event.keyCode) {
  222. case 37:
  223. case 63234:
  224. keyChange = true;
  225. self.rowObject.indent(-1 * self.rtl);
  226. break;
  227. case 38:
  228. case 63232:
  229. {
  230. var $previousRow = $(self.rowObject.element).prev('tr:first-of-type');
  231. var previousRow = $previousRow.get(0);
  232. while (previousRow && $previousRow.is(':hidden')) {
  233. $previousRow = $(previousRow).prev('tr:first-of-type');
  234. previousRow = $previousRow.get(0);
  235. }
  236. if (previousRow) {
  237. self.safeBlur = false;
  238. self.rowObject.direction = 'up';
  239. keyChange = true;
  240. if ($(item).is('.tabledrag-root')) {
  241. groupHeight = 0;
  242. while (previousRow && $previousRow.find('.js-indentation').length) {
  243. $previousRow = $(previousRow).prev('tr:first-of-type');
  244. previousRow = $previousRow.get(0);
  245. groupHeight += $previousRow.is(':hidden') ? 0 : previousRow.offsetHeight;
  246. }
  247. if (previousRow) {
  248. self.rowObject.swap('before', previousRow);
  249. window.scrollBy(0, -groupHeight);
  250. }
  251. } else if (self.table.tBodies[0].rows[0] !== previousRow || $previousRow.is('.draggable')) {
  252. self.rowObject.swap('before', previousRow);
  253. self.rowObject.interval = null;
  254. self.rowObject.indent(0);
  255. window.scrollBy(0, -parseInt(item.offsetHeight, 10));
  256. }
  257. handle.trigger('focus');
  258. }
  259. break;
  260. }
  261. case 39:
  262. case 63235:
  263. keyChange = true;
  264. self.rowObject.indent(self.rtl);
  265. break;
  266. case 40:
  267. case 63233:
  268. {
  269. var $nextRow = $(self.rowObject.group).eq(-1).next('tr:first-of-type');
  270. var nextRow = $nextRow.get(0);
  271. while (nextRow && $nextRow.is(':hidden')) {
  272. $nextRow = $(nextRow).next('tr:first-of-type');
  273. nextRow = $nextRow.get(0);
  274. }
  275. if (nextRow) {
  276. self.safeBlur = false;
  277. self.rowObject.direction = 'down';
  278. keyChange = true;
  279. if ($(item).is('.tabledrag-root')) {
  280. groupHeight = 0;
  281. var nextGroup = new self.row(nextRow, 'keyboard', self.indentEnabled, self.maxDepth, false);
  282. if (nextGroup) {
  283. $(nextGroup.group).each(function () {
  284. groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight;
  285. });
  286. var nextGroupRow = $(nextGroup.group).eq(-1).get(0);
  287. self.rowObject.swap('after', nextGroupRow);
  288. window.scrollBy(0, parseInt(groupHeight, 10));
  289. }
  290. } else {
  291. self.rowObject.swap('after', nextRow);
  292. self.rowObject.interval = null;
  293. self.rowObject.indent(0);
  294. window.scrollBy(0, parseInt(item.offsetHeight, 10));
  295. }
  296. handle.trigger('focus');
  297. }
  298. break;
  299. }
  300. }
  301. if (self.rowObject && self.rowObject.changed === true) {
  302. $(item).addClass('drag');
  303. if (self.oldRowElement) {
  304. $(self.oldRowElement).removeClass('drag-previous');
  305. }
  306. self.oldRowElement = item;
  307. if (self.striping === true) {
  308. self.restripeTable();
  309. }
  310. self.onDrag();
  311. }
  312. if (keyChange) {
  313. return false;
  314. }
  315. });
  316. handle.on('keypress', function (event) {
  317. switch (event.keyCode) {
  318. case 37:
  319. case 38:
  320. case 39:
  321. case 40:
  322. return false;
  323. }
  324. });
  325. };
  326. Drupal.tableDrag.prototype.dragStart = function (event, self, item) {
  327. self.dragObject = {};
  328. self.dragObject.initOffset = self.getPointerOffset(item, event);
  329. self.dragObject.initPointerCoords = self.pointerCoords(event);
  330. if (self.indentEnabled) {
  331. self.dragObject.indentPointerPos = self.dragObject.initPointerCoords;
  332. }
  333. if (self.rowObject) {
  334. $(self.rowObject.element).find('a.tabledrag-handle').trigger('blur');
  335. }
  336. self.rowObject = new self.row(item, 'pointer', self.indentEnabled, self.maxDepth, true);
  337. self.table.topY = $(self.table).offset().top;
  338. self.table.bottomY = self.table.topY + self.table.offsetHeight;
  339. $(item).addClass('drag');
  340. $('body').addClass('drag');
  341. if (self.oldRowElement) {
  342. $(self.oldRowElement).removeClass('drag-previous');
  343. }
  344. };
  345. Drupal.tableDrag.prototype.dragRow = function (event, self) {
  346. if (self.dragObject) {
  347. self.currentPointerCoords = self.pointerCoords(event);
  348. var y = self.currentPointerCoords.y - self.dragObject.initOffset.y;
  349. var x = self.currentPointerCoords.x - self.dragObject.initOffset.x;
  350. if (y !== self.oldY) {
  351. self.rowObject.direction = y > self.oldY ? 'down' : 'up';
  352. self.oldY = y;
  353. var scrollAmount = self.checkScroll(self.currentPointerCoords.y);
  354. clearInterval(self.scrollInterval);
  355. if (scrollAmount > 0 && self.rowObject.direction === 'down' || scrollAmount < 0 && self.rowObject.direction === 'up') {
  356. self.setScroll(scrollAmount);
  357. }
  358. var currentRow = self.findDropTargetRow(x, y);
  359. if (currentRow) {
  360. if (self.rowObject.direction === 'down') {
  361. self.rowObject.swap('after', currentRow, self);
  362. } else {
  363. self.rowObject.swap('before', currentRow, self);
  364. }
  365. if (self.striping === true) {
  366. self.restripeTable();
  367. }
  368. }
  369. }
  370. if (self.indentEnabled) {
  371. var xDiff = self.currentPointerCoords.x - self.dragObject.indentPointerPos.x;
  372. var indentDiff = Math.round(xDiff / self.indentAmount);
  373. var indentChange = self.rowObject.indent(indentDiff);
  374. self.dragObject.indentPointerPos.x += self.indentAmount * indentChange * self.rtl;
  375. self.indentCount = Math.max(self.indentCount, self.rowObject.indents);
  376. }
  377. return false;
  378. }
  379. };
  380. Drupal.tableDrag.prototype.dropRow = function (event, self) {
  381. var droppedRow = void 0;
  382. var $droppedRow = void 0;
  383. if (self.rowObject !== null) {
  384. droppedRow = self.rowObject.element;
  385. $droppedRow = $(droppedRow);
  386. if (self.rowObject.changed === true) {
  387. self.updateFields(droppedRow);
  388. Object.keys(self.tableSettings || {}).forEach(function (group) {
  389. var rowSettings = self.rowSettings(group, droppedRow);
  390. if (rowSettings.relationship === 'group') {
  391. Object.keys(self.rowObject.children || {}).forEach(function (n) {
  392. self.updateField(self.rowObject.children[n], group);
  393. });
  394. }
  395. });
  396. self.rowObject.markChanged();
  397. if (self.changed === false) {
  398. $(Drupal.theme('tableDragChangedWarning')).insertBefore(self.table).hide().fadeIn('slow');
  399. self.changed = true;
  400. }
  401. }
  402. if (self.indentEnabled) {
  403. self.rowObject.removeIndentClasses();
  404. }
  405. if (self.oldRowElement) {
  406. $(self.oldRowElement).removeClass('drag-previous');
  407. }
  408. $droppedRow.removeClass('drag').addClass('drag-previous');
  409. self.oldRowElement = droppedRow;
  410. self.onDrop();
  411. self.rowObject = null;
  412. }
  413. if (self.dragObject !== null) {
  414. self.dragObject = null;
  415. $('body').removeClass('drag');
  416. clearInterval(self.scrollInterval);
  417. }
  418. };
  419. Drupal.tableDrag.prototype.pointerCoords = function (event) {
  420. if (event.pageX || event.pageY) {
  421. return { x: event.pageX, y: event.pageY };
  422. }
  423. return {
  424. x: event.clientX + document.body.scrollLeft - document.body.clientLeft,
  425. y: event.clientY + document.body.scrollTop - document.body.clientTop
  426. };
  427. };
  428. Drupal.tableDrag.prototype.getPointerOffset = function (target, event) {
  429. var docPos = $(target).offset();
  430. var pointerPos = this.pointerCoords(event);
  431. return { x: pointerPos.x - docPos.left, y: pointerPos.y - docPos.top };
  432. };
  433. Drupal.tableDrag.prototype.findDropTargetRow = function (x, y) {
  434. var rows = $(this.table.tBodies[0].rows).not(':hidden');
  435. for (var n = 0; n < rows.length; n++) {
  436. var row = rows[n];
  437. var $row = $(row);
  438. var rowY = $row.offset().top;
  439. var rowHeight = void 0;
  440. if (row.offsetHeight === 0) {
  441. rowHeight = parseInt(row.firstChild.offsetHeight, 10) / 2;
  442. } else {
  443. rowHeight = parseInt(row.offsetHeight, 10) / 2;
  444. }
  445. if (y > rowY - rowHeight && y < rowY + rowHeight) {
  446. if (this.indentEnabled) {
  447. for (n in this.rowObject.group) {
  448. if (this.rowObject.group[n] === row) {
  449. return null;
  450. }
  451. }
  452. } else if (row === this.rowObject.element) {
  453. return null;
  454. }
  455. if (!this.rowObject.isValidSwap(row)) {
  456. return null;
  457. }
  458. while ($row.is(':hidden') && $row.prev('tr').is(':hidden')) {
  459. $row = $row.prev('tr:first-of-type');
  460. row = $row.get(0);
  461. }
  462. return row;
  463. }
  464. }
  465. return null;
  466. };
  467. Drupal.tableDrag.prototype.updateFields = function (changedRow) {
  468. var _this3 = this;
  469. Object.keys(this.tableSettings || {}).forEach(function (group) {
  470. _this3.updateField(changedRow, group);
  471. });
  472. };
  473. Drupal.tableDrag.prototype.updateField = function (changedRow, group) {
  474. var rowSettings = this.rowSettings(group, changedRow);
  475. var $changedRow = $(changedRow);
  476. var sourceRow = void 0;
  477. var $previousRow = void 0;
  478. var previousRow = void 0;
  479. var useSibling = void 0;
  480. if (rowSettings.relationship === 'self' || rowSettings.relationship === 'group') {
  481. sourceRow = changedRow;
  482. } else if (rowSettings.relationship === 'sibling') {
  483. $previousRow = $changedRow.prev('tr:first-of-type');
  484. previousRow = $previousRow.get(0);
  485. var $nextRow = $changedRow.next('tr:first-of-type');
  486. var nextRow = $nextRow.get(0);
  487. sourceRow = changedRow;
  488. if ($previousRow.is('.draggable') && $previousRow.find('.' + group).length) {
  489. if (this.indentEnabled) {
  490. if ($previousRow.find('.js-indentations').length === $changedRow.find('.js-indentations').length) {
  491. sourceRow = previousRow;
  492. }
  493. } else {
  494. sourceRow = previousRow;
  495. }
  496. } else if ($nextRow.is('.draggable') && $nextRow.find('.' + group).length) {
  497. if (this.indentEnabled) {
  498. if ($nextRow.find('.js-indentations').length === $changedRow.find('.js-indentations').length) {
  499. sourceRow = nextRow;
  500. }
  501. } else {
  502. sourceRow = nextRow;
  503. }
  504. }
  505. } else if (rowSettings.relationship === 'parent') {
  506. $previousRow = $changedRow.prev('tr');
  507. previousRow = $previousRow;
  508. while ($previousRow.length && $previousRow.find('.js-indentation').length >= this.rowObject.indents) {
  509. $previousRow = $previousRow.prev('tr');
  510. previousRow = $previousRow;
  511. }
  512. if ($previousRow.length) {
  513. sourceRow = $previousRow.get(0);
  514. } else {
  515. sourceRow = $(this.table).find('tr.draggable:first-of-type').get(0);
  516. if (sourceRow === this.rowObject.element) {
  517. sourceRow = $(this.rowObject.group[this.rowObject.group.length - 1]).next('tr.draggable').get(0);
  518. }
  519. useSibling = true;
  520. }
  521. }
  522. this.copyDragClasses(sourceRow, changedRow, group);
  523. rowSettings = this.rowSettings(group, changedRow);
  524. if (useSibling) {
  525. rowSettings.relationship = 'sibling';
  526. rowSettings.source = rowSettings.target;
  527. }
  528. var targetClass = '.' + rowSettings.target;
  529. var targetElement = $changedRow.find(targetClass).get(0);
  530. if (targetElement) {
  531. var sourceClass = '.' + rowSettings.source;
  532. var sourceElement = $(sourceClass, sourceRow).get(0);
  533. switch (rowSettings.action) {
  534. case 'depth':
  535. targetElement.value = $(sourceElement).closest('tr').find('.js-indentation').length;
  536. break;
  537. case 'match':
  538. targetElement.value = sourceElement.value;
  539. break;
  540. case 'order':
  541. {
  542. var siblings = this.rowObject.findSiblings(rowSettings);
  543. if ($(targetElement).is('select')) {
  544. var values = [];
  545. $(targetElement).find('option').each(function () {
  546. values.push(this.value);
  547. });
  548. var maxVal = values[values.length - 1];
  549. $(siblings).find(targetClass).each(function () {
  550. if (values.length > 0) {
  551. this.value = values.shift();
  552. } else {
  553. this.value = maxVal;
  554. }
  555. });
  556. } else {
  557. var weight = parseInt($(siblings[0]).find(targetClass).val(), 10) || 0;
  558. $(siblings).find(targetClass).each(function () {
  559. this.value = weight;
  560. weight++;
  561. });
  562. }
  563. break;
  564. }
  565. }
  566. }
  567. };
  568. Drupal.tableDrag.prototype.copyDragClasses = function (sourceRow, targetRow, group) {
  569. var sourceElement = $(sourceRow).find('.' + group);
  570. var targetElement = $(targetRow).find('.' + group);
  571. if (sourceElement.length && targetElement.length) {
  572. targetElement[0].className = sourceElement[0].className;
  573. }
  574. };
  575. Drupal.tableDrag.prototype.checkScroll = function (cursorY) {
  576. var de = document.documentElement;
  577. var b = document.body;
  578. var windowHeight = window.innerHeight || (de.clientHeight && de.clientWidth !== 0 ? de.clientHeight : b.offsetHeight);
  579. this.windowHeight = windowHeight;
  580. var scrollY = void 0;
  581. if (document.all) {
  582. scrollY = !de.scrollTop ? b.scrollTop : de.scrollTop;
  583. } else {
  584. scrollY = window.pageYOffset ? window.pageYOffset : window.scrollY;
  585. }
  586. this.scrollY = scrollY;
  587. var trigger = this.scrollSettings.trigger;
  588. var delta = 0;
  589. if (cursorY - scrollY > windowHeight - trigger) {
  590. delta = trigger / (windowHeight + scrollY - cursorY);
  591. delta = delta > 0 && delta < trigger ? delta : trigger;
  592. return delta * this.scrollSettings.amount;
  593. } else if (cursorY - scrollY < trigger) {
  594. delta = trigger / (cursorY - scrollY);
  595. delta = delta > 0 && delta < trigger ? delta : trigger;
  596. return -delta * this.scrollSettings.amount;
  597. }
  598. };
  599. Drupal.tableDrag.prototype.setScroll = function (scrollAmount) {
  600. var self = this;
  601. this.scrollInterval = setInterval(function () {
  602. self.checkScroll(self.currentPointerCoords.y);
  603. var aboveTable = self.scrollY > self.table.topY;
  604. var belowTable = self.scrollY + self.windowHeight < self.table.bottomY;
  605. if (scrollAmount > 0 && belowTable || scrollAmount < 0 && aboveTable) {
  606. window.scrollBy(0, scrollAmount);
  607. }
  608. }, this.scrollSettings.interval);
  609. };
  610. Drupal.tableDrag.prototype.restripeTable = function () {
  611. $(this.table).find('> tbody > tr.draggable, > tr.draggable').filter(':visible').filter(':odd').removeClass('odd').addClass('even').end().filter(':even').removeClass('even').addClass('odd');
  612. };
  613. Drupal.tableDrag.prototype.onDrag = function () {
  614. return null;
  615. };
  616. Drupal.tableDrag.prototype.onDrop = function () {
  617. return null;
  618. };
  619. Drupal.tableDrag.prototype.row = function (tableRow, method, indentEnabled, maxDepth, addClasses) {
  620. var $tableRow = $(tableRow);
  621. this.element = tableRow;
  622. this.method = method;
  623. this.group = [tableRow];
  624. this.groupDepth = $tableRow.find('.js-indentation').length;
  625. this.changed = false;
  626. this.table = $tableRow.closest('table')[0];
  627. this.indentEnabled = indentEnabled;
  628. this.maxDepth = maxDepth;
  629. this.direction = '';
  630. if (this.indentEnabled) {
  631. this.indents = $tableRow.find('.js-indentation').length;
  632. this.children = this.findChildren(addClasses);
  633. this.group = $.merge(this.group, this.children);
  634. for (var n = 0; n < this.group.length; n++) {
  635. this.groupDepth = Math.max($(this.group[n]).find('.js-indentation').length, this.groupDepth);
  636. }
  637. }
  638. };
  639. Drupal.tableDrag.prototype.row.prototype.findChildren = function (addClasses) {
  640. var parentIndentation = this.indents;
  641. var currentRow = $(this.element, this.table).next('tr.draggable');
  642. var rows = [];
  643. var child = 0;
  644. function rowIndentation(indentNum, el) {
  645. var self = $(el);
  646. if (child === 1 && indentNum === parentIndentation) {
  647. self.addClass('tree-child-first');
  648. }
  649. if (indentNum === parentIndentation) {
  650. self.addClass('tree-child');
  651. } else if (indentNum > parentIndentation) {
  652. self.addClass('tree-child-horizontal');
  653. }
  654. }
  655. while (currentRow.length) {
  656. if (currentRow.find('.js-indentation').length > parentIndentation) {
  657. child++;
  658. rows.push(currentRow[0]);
  659. if (addClasses) {
  660. currentRow.find('.js-indentation').each(rowIndentation);
  661. }
  662. } else {
  663. break;
  664. }
  665. currentRow = currentRow.next('tr.draggable');
  666. }
  667. if (addClasses && rows.length) {
  668. $(rows[rows.length - 1]).find('.js-indentation:nth-child(' + (parentIndentation + 1) + ')').addClass('tree-child-last');
  669. }
  670. return rows;
  671. };
  672. Drupal.tableDrag.prototype.row.prototype.isValidSwap = function (row) {
  673. var $row = $(row);
  674. if (this.indentEnabled) {
  675. var prevRow = void 0;
  676. var nextRow = void 0;
  677. if (this.direction === 'down') {
  678. prevRow = row;
  679. nextRow = $row.next('tr').get(0);
  680. } else {
  681. prevRow = $row.prev('tr').get(0);
  682. nextRow = row;
  683. }
  684. this.interval = this.validIndentInterval(prevRow, nextRow);
  685. if (this.interval.min > this.interval.max) {
  686. return false;
  687. }
  688. }
  689. if (this.table.tBodies[0].rows[0] === row && $row.is(':not(.draggable)')) {
  690. return false;
  691. }
  692. return true;
  693. };
  694. Drupal.tableDrag.prototype.row.prototype.swap = function (position, row) {
  695. this.group.forEach(function (row) {
  696. Drupal.detachBehaviors(row, drupalSettings, 'move');
  697. });
  698. $(row)[position](this.group);
  699. this.group.forEach(function (row) {
  700. Drupal.attachBehaviors(row, drupalSettings);
  701. });
  702. this.changed = true;
  703. this.onSwap(row);
  704. };
  705. Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow, nextRow) {
  706. var $prevRow = $(prevRow);
  707. var maxIndent = void 0;
  708. var minIndent = nextRow ? $(nextRow).find('.js-indentation').length : 0;
  709. if (!prevRow || $prevRow.is(':not(.draggable)') || $(this.element).is('.tabledrag-root')) {
  710. maxIndent = 0;
  711. } else {
  712. maxIndent = $prevRow.find('.js-indentation').length + ($prevRow.is('.tabledrag-leaf') ? 0 : 1);
  713. if (this.maxDepth) {
  714. maxIndent = Math.min(maxIndent, this.maxDepth - (this.groupDepth - this.indents));
  715. }
  716. }
  717. return { min: minIndent, max: maxIndent };
  718. };
  719. Drupal.tableDrag.prototype.row.prototype.indent = function (indentDiff) {
  720. var $group = $(this.group);
  721. if (!this.interval) {
  722. var prevRow = $(this.element).prev('tr').get(0);
  723. var nextRow = $group.eq(-1).next('tr').get(0);
  724. this.interval = this.validIndentInterval(prevRow, nextRow);
  725. }
  726. var indent = this.indents + indentDiff;
  727. indent = Math.max(indent, this.interval.min);
  728. indent = Math.min(indent, this.interval.max);
  729. indentDiff = indent - this.indents;
  730. for (var n = 1; n <= Math.abs(indentDiff); n++) {
  731. if (indentDiff < 0) {
  732. $group.find('.js-indentation:first-of-type').remove();
  733. this.indents--;
  734. } else {
  735. $group.find('td:first-of-type').prepend(Drupal.theme('tableDragIndentation'));
  736. this.indents++;
  737. }
  738. }
  739. if (indentDiff) {
  740. this.changed = true;
  741. this.groupDepth += indentDiff;
  742. this.onIndent();
  743. }
  744. return indentDiff;
  745. };
  746. Drupal.tableDrag.prototype.row.prototype.findSiblings = function (rowSettings) {
  747. var siblings = [];
  748. var directions = ['prev', 'next'];
  749. var rowIndentation = this.indents;
  750. var checkRowIndentation = void 0;
  751. for (var d = 0; d < directions.length; d++) {
  752. var checkRow = $(this.element)[directions[d]]();
  753. while (checkRow.length) {
  754. if (checkRow.find('.' + rowSettings.target)) {
  755. if (this.indentEnabled) {
  756. checkRowIndentation = checkRow.find('.js-indentation').length;
  757. }
  758. if (!this.indentEnabled || checkRowIndentation === rowIndentation) {
  759. siblings.push(checkRow[0]);
  760. } else if (checkRowIndentation < rowIndentation) {
  761. break;
  762. }
  763. } else {
  764. break;
  765. }
  766. checkRow = checkRow[directions[d]]();
  767. }
  768. if (directions[d] === 'prev') {
  769. siblings.reverse();
  770. siblings.push(this.element);
  771. }
  772. }
  773. return siblings;
  774. };
  775. Drupal.tableDrag.prototype.row.prototype.removeIndentClasses = function () {
  776. var _this4 = this;
  777. Object.keys(this.children || {}).forEach(function (n) {
  778. $(_this4.children[n]).find('.js-indentation').removeClass('tree-child').removeClass('tree-child-first').removeClass('tree-child-last').removeClass('tree-child-horizontal');
  779. });
  780. };
  781. Drupal.tableDrag.prototype.row.prototype.markChanged = function () {
  782. var marker = Drupal.theme('tableDragChangedMarker');
  783. var cell = $(this.element).find('td:first-of-type');
  784. if (cell.find('abbr.tabledrag-changed').length === 0) {
  785. cell.append(marker);
  786. }
  787. };
  788. Drupal.tableDrag.prototype.row.prototype.onIndent = function () {
  789. return null;
  790. };
  791. Drupal.tableDrag.prototype.row.prototype.onSwap = function (swappedRow) {
  792. return null;
  793. };
  794. $.extend(Drupal.theme, {
  795. tableDragChangedMarker: function tableDragChangedMarker() {
  796. return '<abbr class="warning tabledrag-changed" title="' + Drupal.t('Changed') + '">*</abbr>';
  797. },
  798. tableDragIndentation: function tableDragIndentation() {
  799. return '<div class="js-indentation indentation">&nbsp;</div>';
  800. },
  801. tableDragChangedWarning: function tableDragChangedWarning() {
  802. return '<div class="tabledrag-changed-warning messages messages--warning" role="alert">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t('You have unsaved changes.') + '</div>';
  803. }
  804. });
  805. })(jQuery, Drupal, drupalSettings);