tabledrag.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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 = null;
  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></tr>').addClass('draggable').appendTo(table);
  56. var testCell = $('<td></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>').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').eq(0);
  230. var previousRow = $previousRow.get(0);
  231. while (previousRow && $previousRow.is(':hidden')) {
  232. $previousRow = $(previousRow).prev('tr').eq(0);
  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').eq(0);
  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').eq(0);
  269. var nextRow = $nextRow.get(0);
  270. while (nextRow && $nextRow.is(':hidden')) {
  271. $nextRow = $(nextRow).next('tr').eq(0);
  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. self.oldY = self.pointerCoords(event).y;
  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 _this3 = this;
  435. var rows = $(this.table.tBodies[0].rows).not(':hidden');
  436. var _loop = function _loop(n) {
  437. var row = rows[n];
  438. var $row = $(row);
  439. var rowY = $row.offset().top;
  440. var rowHeight = void 0;
  441. if (row.offsetHeight === 0) {
  442. rowHeight = parseInt(row.firstChild.offsetHeight, 10) / 2;
  443. } else {
  444. rowHeight = parseInt(row.offsetHeight, 10) / 2;
  445. }
  446. if (y > rowY - rowHeight && y < rowY + rowHeight) {
  447. if (_this3.indentEnabled) {
  448. if (Object.keys(_this3.rowObject.group).some(function (o) {
  449. return _this3.rowObject.group[o] === row;
  450. })) {
  451. return {
  452. v: null
  453. };
  454. }
  455. } else if (row === _this3.rowObject.element) {
  456. return {
  457. v: null
  458. };
  459. }
  460. if (!_this3.rowObject.isValidSwap(row)) {
  461. return {
  462. v: null
  463. };
  464. }
  465. while ($row.is(':hidden') && $row.prev('tr').is(':hidden')) {
  466. $row = $row.prev('tr:first-of-type');
  467. row = $row.get(0);
  468. }
  469. return {
  470. v: row
  471. };
  472. }
  473. };
  474. for (var n = 0; n < rows.length; n++) {
  475. var _ret = _loop(n);
  476. if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
  477. }
  478. return null;
  479. };
  480. Drupal.tableDrag.prototype.updateFields = function (changedRow) {
  481. var _this4 = this;
  482. Object.keys(this.tableSettings || {}).forEach(function (group) {
  483. _this4.updateField(changedRow, group);
  484. });
  485. };
  486. Drupal.tableDrag.prototype.updateField = function (changedRow, group) {
  487. var rowSettings = this.rowSettings(group, changedRow);
  488. var $changedRow = $(changedRow);
  489. var sourceRow = void 0;
  490. var $previousRow = void 0;
  491. var previousRow = void 0;
  492. var useSibling = void 0;
  493. if (rowSettings.relationship === 'self' || rowSettings.relationship === 'group') {
  494. sourceRow = changedRow;
  495. } else if (rowSettings.relationship === 'sibling') {
  496. $previousRow = $changedRow.prev('tr:first-of-type');
  497. previousRow = $previousRow.get(0);
  498. var $nextRow = $changedRow.next('tr:first-of-type');
  499. var nextRow = $nextRow.get(0);
  500. sourceRow = changedRow;
  501. if ($previousRow.is('.draggable') && $previousRow.find('.' + group).length) {
  502. if (this.indentEnabled) {
  503. if ($previousRow.find('.js-indentations').length === $changedRow.find('.js-indentations').length) {
  504. sourceRow = previousRow;
  505. }
  506. } else {
  507. sourceRow = previousRow;
  508. }
  509. } else if ($nextRow.is('.draggable') && $nextRow.find('.' + group).length) {
  510. if (this.indentEnabled) {
  511. if ($nextRow.find('.js-indentations').length === $changedRow.find('.js-indentations').length) {
  512. sourceRow = nextRow;
  513. }
  514. } else {
  515. sourceRow = nextRow;
  516. }
  517. }
  518. } else if (rowSettings.relationship === 'parent') {
  519. $previousRow = $changedRow.prev('tr');
  520. previousRow = $previousRow;
  521. while ($previousRow.length && $previousRow.find('.js-indentation').length >= this.rowObject.indents) {
  522. $previousRow = $previousRow.prev('tr');
  523. previousRow = $previousRow;
  524. }
  525. if ($previousRow.length) {
  526. sourceRow = $previousRow.get(0);
  527. } else {
  528. sourceRow = $(this.table).find('tr.draggable:first-of-type').get(0);
  529. if (sourceRow === this.rowObject.element) {
  530. sourceRow = $(this.rowObject.group[this.rowObject.group.length - 1]).next('tr.draggable').get(0);
  531. }
  532. useSibling = true;
  533. }
  534. }
  535. this.copyDragClasses(sourceRow, changedRow, group);
  536. rowSettings = this.rowSettings(group, changedRow);
  537. if (useSibling) {
  538. rowSettings.relationship = 'sibling';
  539. rowSettings.source = rowSettings.target;
  540. }
  541. var targetClass = '.' + rowSettings.target;
  542. var targetElement = $changedRow.find(targetClass).get(0);
  543. if (targetElement) {
  544. var sourceClass = '.' + rowSettings.source;
  545. var sourceElement = $(sourceClass, sourceRow).get(0);
  546. switch (rowSettings.action) {
  547. case 'depth':
  548. targetElement.value = $(sourceElement).closest('tr').find('.js-indentation').length;
  549. break;
  550. case 'match':
  551. targetElement.value = sourceElement.value;
  552. break;
  553. case 'order':
  554. {
  555. var siblings = this.rowObject.findSiblings(rowSettings);
  556. if ($(targetElement).is('select')) {
  557. var values = [];
  558. $(targetElement).find('option').each(function () {
  559. values.push(this.value);
  560. });
  561. var maxVal = values[values.length - 1];
  562. $(siblings).find(targetClass).each(function () {
  563. if (values.length > 0) {
  564. this.value = values.shift();
  565. } else {
  566. this.value = maxVal;
  567. }
  568. });
  569. } else {
  570. var weight = parseInt($(siblings[0]).find(targetClass).val(), 10) || 0;
  571. $(siblings).find(targetClass).each(function () {
  572. this.value = weight;
  573. weight++;
  574. });
  575. }
  576. break;
  577. }
  578. }
  579. }
  580. };
  581. Drupal.tableDrag.prototype.copyDragClasses = function (sourceRow, targetRow, group) {
  582. var sourceElement = $(sourceRow).find('.' + group);
  583. var targetElement = $(targetRow).find('.' + group);
  584. if (sourceElement.length && targetElement.length) {
  585. targetElement[0].className = sourceElement[0].className;
  586. }
  587. };
  588. Drupal.tableDrag.prototype.checkScroll = function (cursorY) {
  589. var de = document.documentElement;
  590. var b = document.body;
  591. var windowHeight = window.innerHeight || (de.clientHeight && de.clientWidth !== 0 ? de.clientHeight : b.offsetHeight);
  592. this.windowHeight = windowHeight;
  593. var scrollY = void 0;
  594. if (document.all) {
  595. scrollY = !de.scrollTop ? b.scrollTop : de.scrollTop;
  596. } else {
  597. scrollY = window.pageYOffset ? window.pageYOffset : window.scrollY;
  598. }
  599. this.scrollY = scrollY;
  600. var trigger = this.scrollSettings.trigger;
  601. var delta = 0;
  602. if (cursorY - scrollY > windowHeight - trigger) {
  603. delta = trigger / (windowHeight + scrollY - cursorY);
  604. delta = delta > 0 && delta < trigger ? delta : trigger;
  605. return delta * this.scrollSettings.amount;
  606. }
  607. if (cursorY - scrollY < trigger) {
  608. delta = trigger / (cursorY - scrollY);
  609. delta = delta > 0 && delta < trigger ? delta : trigger;
  610. return -delta * this.scrollSettings.amount;
  611. }
  612. };
  613. Drupal.tableDrag.prototype.setScroll = function (scrollAmount) {
  614. var self = this;
  615. this.scrollInterval = setInterval(function () {
  616. self.checkScroll(self.currentPointerCoords.y);
  617. var aboveTable = self.scrollY > self.table.topY;
  618. var belowTable = self.scrollY + self.windowHeight < self.table.bottomY;
  619. if (scrollAmount > 0 && belowTable || scrollAmount < 0 && aboveTable) {
  620. window.scrollBy(0, scrollAmount);
  621. }
  622. }, this.scrollSettings.interval);
  623. };
  624. Drupal.tableDrag.prototype.restripeTable = function () {
  625. $(this.table).find('> tbody > tr.draggable, > tr.draggable').filter(':visible').filter(':odd').removeClass('odd').addClass('even').end().filter(':even').removeClass('even').addClass('odd');
  626. };
  627. Drupal.tableDrag.prototype.onDrag = function () {
  628. return null;
  629. };
  630. Drupal.tableDrag.prototype.onDrop = function () {
  631. return null;
  632. };
  633. Drupal.tableDrag.prototype.row = function (tableRow, method, indentEnabled, maxDepth, addClasses) {
  634. var $tableRow = $(tableRow);
  635. this.element = tableRow;
  636. this.method = method;
  637. this.group = [tableRow];
  638. this.groupDepth = $tableRow.find('.js-indentation').length;
  639. this.changed = false;
  640. this.table = $tableRow.closest('table')[0];
  641. this.indentEnabled = indentEnabled;
  642. this.maxDepth = maxDepth;
  643. this.direction = '';
  644. if (this.indentEnabled) {
  645. this.indents = $tableRow.find('.js-indentation').length;
  646. this.children = this.findChildren(addClasses);
  647. this.group = $.merge(this.group, this.children);
  648. for (var n = 0; n < this.group.length; n++) {
  649. this.groupDepth = Math.max($(this.group[n]).find('.js-indentation').length, this.groupDepth);
  650. }
  651. }
  652. };
  653. Drupal.tableDrag.prototype.row.prototype.findChildren = function (addClasses) {
  654. var parentIndentation = this.indents;
  655. var currentRow = $(this.element, this.table).next('tr.draggable');
  656. var rows = [];
  657. var child = 0;
  658. function rowIndentation(indentNum, el) {
  659. var self = $(el);
  660. if (child === 1 && indentNum === parentIndentation) {
  661. self.addClass('tree-child-first');
  662. }
  663. if (indentNum === parentIndentation) {
  664. self.addClass('tree-child');
  665. } else if (indentNum > parentIndentation) {
  666. self.addClass('tree-child-horizontal');
  667. }
  668. }
  669. while (currentRow.length) {
  670. if (currentRow.find('.js-indentation').length > parentIndentation) {
  671. child++;
  672. rows.push(currentRow[0]);
  673. if (addClasses) {
  674. currentRow.find('.js-indentation').each(rowIndentation);
  675. }
  676. } else {
  677. break;
  678. }
  679. currentRow = currentRow.next('tr.draggable');
  680. }
  681. if (addClasses && rows.length) {
  682. $(rows[rows.length - 1]).find('.js-indentation:nth-child(' + (parentIndentation + 1) + ')').addClass('tree-child-last');
  683. }
  684. return rows;
  685. };
  686. Drupal.tableDrag.prototype.row.prototype.isValidSwap = function (row) {
  687. var $row = $(row);
  688. if (this.indentEnabled) {
  689. var prevRow = void 0;
  690. var nextRow = void 0;
  691. if (this.direction === 'down') {
  692. prevRow = row;
  693. nextRow = $row.next('tr').get(0);
  694. } else {
  695. prevRow = $row.prev('tr').get(0);
  696. nextRow = row;
  697. }
  698. this.interval = this.validIndentInterval(prevRow, nextRow);
  699. if (this.interval.min > this.interval.max) {
  700. return false;
  701. }
  702. }
  703. if (this.table.tBodies[0].rows[0] === row && $row.is(':not(.draggable)')) {
  704. return false;
  705. }
  706. return true;
  707. };
  708. Drupal.tableDrag.prototype.row.prototype.swap = function (position, row) {
  709. this.group.forEach(function (row) {
  710. Drupal.detachBehaviors(row, drupalSettings, 'move');
  711. });
  712. $(row)[position](this.group);
  713. this.group.forEach(function (row) {
  714. Drupal.attachBehaviors(row, drupalSettings);
  715. });
  716. this.changed = true;
  717. this.onSwap(row);
  718. };
  719. Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow, nextRow) {
  720. var $prevRow = $(prevRow);
  721. var maxIndent = void 0;
  722. var minIndent = nextRow ? $(nextRow).find('.js-indentation').length : 0;
  723. if (!prevRow || $prevRow.is(':not(.draggable)') || $(this.element).is('.tabledrag-root')) {
  724. maxIndent = 0;
  725. } else {
  726. maxIndent = $prevRow.find('.js-indentation').length + ($prevRow.is('.tabledrag-leaf') ? 0 : 1);
  727. if (this.maxDepth) {
  728. maxIndent = Math.min(maxIndent, this.maxDepth - (this.groupDepth - this.indents));
  729. }
  730. }
  731. return { min: minIndent, max: maxIndent };
  732. };
  733. Drupal.tableDrag.prototype.row.prototype.indent = function (indentDiff) {
  734. var $group = $(this.group);
  735. if (!this.interval) {
  736. var prevRow = $(this.element).prev('tr').get(0);
  737. var nextRow = $group.eq(-1).next('tr').get(0);
  738. this.interval = this.validIndentInterval(prevRow, nextRow);
  739. }
  740. var indent = this.indents + indentDiff;
  741. indent = Math.max(indent, this.interval.min);
  742. indent = Math.min(indent, this.interval.max);
  743. indentDiff = indent - this.indents;
  744. for (var n = 1; n <= Math.abs(indentDiff); n++) {
  745. if (indentDiff < 0) {
  746. $group.find('.js-indentation:first-of-type').remove();
  747. this.indents--;
  748. } else {
  749. $group.find('td:first-of-type').prepend(Drupal.theme('tableDragIndentation'));
  750. this.indents++;
  751. }
  752. }
  753. if (indentDiff) {
  754. this.changed = true;
  755. this.groupDepth += indentDiff;
  756. this.onIndent();
  757. }
  758. return indentDiff;
  759. };
  760. Drupal.tableDrag.prototype.row.prototype.findSiblings = function (rowSettings) {
  761. var siblings = [];
  762. var directions = ['prev', 'next'];
  763. var rowIndentation = this.indents;
  764. var checkRowIndentation = void 0;
  765. for (var d = 0; d < directions.length; d++) {
  766. var checkRow = $(this.element)[directions[d]]();
  767. while (checkRow.length) {
  768. if (checkRow.find('.' + rowSettings.target)) {
  769. if (this.indentEnabled) {
  770. checkRowIndentation = checkRow.find('.js-indentation').length;
  771. }
  772. if (!this.indentEnabled || checkRowIndentation === rowIndentation) {
  773. siblings.push(checkRow[0]);
  774. } else if (checkRowIndentation < rowIndentation) {
  775. break;
  776. }
  777. } else {
  778. break;
  779. }
  780. checkRow = checkRow[directions[d]]();
  781. }
  782. if (directions[d] === 'prev') {
  783. siblings.reverse();
  784. siblings.push(this.element);
  785. }
  786. }
  787. return siblings;
  788. };
  789. Drupal.tableDrag.prototype.row.prototype.removeIndentClasses = function () {
  790. var _this5 = this;
  791. Object.keys(this.children || {}).forEach(function (n) {
  792. $(_this5.children[n]).find('.js-indentation').removeClass('tree-child').removeClass('tree-child-first').removeClass('tree-child-last').removeClass('tree-child-horizontal');
  793. });
  794. };
  795. Drupal.tableDrag.prototype.row.prototype.markChanged = function () {
  796. var marker = Drupal.theme('tableDragChangedMarker');
  797. var cell = $(this.element).find('td:first-of-type');
  798. if (cell.find('abbr.tabledrag-changed').length === 0) {
  799. cell.append(marker);
  800. }
  801. };
  802. Drupal.tableDrag.prototype.row.prototype.onIndent = function () {
  803. return null;
  804. };
  805. Drupal.tableDrag.prototype.row.prototype.onSwap = function (swappedRow) {
  806. return null;
  807. };
  808. $.extend(Drupal.theme, {
  809. tableDragChangedMarker: function tableDragChangedMarker() {
  810. return '<abbr class="warning tabledrag-changed" title="' + Drupal.t('Changed') + '">*</abbr>';
  811. },
  812. tableDragIndentation: function tableDragIndentation() {
  813. return '<div class="js-indentation indentation">&nbsp;</div>';
  814. },
  815. tableDragChangedWarning: function tableDragChangedWarning() {
  816. return '<div class="tabledrag-changed-warning messages messages--warning" role="alert">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t('You have unsaved changes.') + '</div>';
  817. }
  818. });
  819. })(jQuery, Drupal, drupalSettings);