tabledrag.js 31 KB

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