Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * Provide dragging capabilities to admin uis.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@0
|
6 /**
|
Chris@0
|
7 * Triggers when weights columns are toggled.
|
Chris@0
|
8 *
|
Chris@0
|
9 * @event columnschange
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 (function ($, Drupal, drupalSettings) {
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Store the state of weight columns display for all tables.
|
Chris@0
|
15 *
|
Chris@0
|
16 * Default value is to hide weight columns.
|
Chris@0
|
17 */
|
Chris@0
|
18 let showWeight = JSON.parse(localStorage.getItem('Drupal.tableDrag.showWeight'));
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Drag and drop table rows with field manipulation.
|
Chris@0
|
22 *
|
Chris@0
|
23 * Using the drupal_attach_tabledrag() function, any table with weights or
|
Chris@0
|
24 * parent relationships may be made into draggable tables. Columns containing
|
Chris@0
|
25 * a field may optionally be hidden, providing a better user experience.
|
Chris@0
|
26 *
|
Chris@0
|
27 * Created tableDrag instances may be modified with custom behaviors by
|
Chris@0
|
28 * overriding the .onDrag, .onDrop, .row.onSwap, and .row.onIndent methods.
|
Chris@0
|
29 * See blocks.js for an example of adding additional functionality to
|
Chris@0
|
30 * tableDrag.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @type {Drupal~behavior}
|
Chris@0
|
33 */
|
Chris@0
|
34 Drupal.behaviors.tableDrag = {
|
Chris@0
|
35 attach(context, settings) {
|
Chris@0
|
36 function initTableDrag(table, base) {
|
Chris@0
|
37 if (table.length) {
|
Chris@0
|
38 // Create the new tableDrag instance. Save in the Drupal variable
|
Chris@0
|
39 // to allow other scripts access to the object.
|
Chris@0
|
40 Drupal.tableDrag[base] = new Drupal.tableDrag(table[0], settings.tableDrag[base]);
|
Chris@0
|
41 }
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 Object.keys(settings.tableDrag || {}).forEach((base) => {
|
Chris@0
|
45 initTableDrag($(context).find(`#${base}`).once('tabledrag'), base);
|
Chris@0
|
46 });
|
Chris@0
|
47 },
|
Chris@0
|
48 };
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Provides table and field manipulation.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @constructor
|
Chris@0
|
54 *
|
Chris@0
|
55 * @param {HTMLElement} table
|
Chris@0
|
56 * DOM object for the table to be made draggable.
|
Chris@0
|
57 * @param {object} tableSettings
|
Chris@0
|
58 * Settings for the table added via drupal_add_dragtable().
|
Chris@0
|
59 */
|
Chris@0
|
60 Drupal.tableDrag = function (table, tableSettings) {
|
Chris@0
|
61 const self = this;
|
Chris@0
|
62 const $table = $(table);
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * @type {jQuery}
|
Chris@0
|
66 */
|
Chris@0
|
67 this.$table = $(table);
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 *
|
Chris@0
|
71 * @type {HTMLElement}
|
Chris@0
|
72 */
|
Chris@0
|
73 this.table = table;
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * @type {object}
|
Chris@0
|
77 */
|
Chris@0
|
78 this.tableSettings = tableSettings;
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Used to hold information about a current drag operation.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @type {?HTMLElement}
|
Chris@0
|
84 */
|
Chris@0
|
85 this.dragObject = null;
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * Provides operations for row manipulation.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @type {?HTMLElement}
|
Chris@0
|
91 */
|
Chris@0
|
92 this.rowObject = null;
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Remember the previous element.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @type {?HTMLElement}
|
Chris@0
|
98 */
|
Chris@0
|
99 this.oldRowElement = null;
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * Used to determine up or down direction from last mouse move.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @type {number}
|
Chris@0
|
105 */
|
Chris@0
|
106 this.oldY = 0;
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * Whether anything in the entire table has changed.
|
Chris@0
|
110 *
|
Chris@0
|
111 * @type {bool}
|
Chris@0
|
112 */
|
Chris@0
|
113 this.changed = false;
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * Maximum amount of allowed parenting.
|
Chris@0
|
117 *
|
Chris@0
|
118 * @type {number}
|
Chris@0
|
119 */
|
Chris@0
|
120 this.maxDepth = 0;
|
Chris@0
|
121
|
Chris@0
|
122 /**
|
Chris@0
|
123 * Direction of the table.
|
Chris@0
|
124 *
|
Chris@0
|
125 * @type {number}
|
Chris@0
|
126 */
|
Chris@0
|
127 this.rtl = $(this.table).css('direction') === 'rtl' ? -1 : 1;
|
Chris@0
|
128
|
Chris@0
|
129 /**
|
Chris@0
|
130 *
|
Chris@0
|
131 * @type {bool}
|
Chris@0
|
132 */
|
Chris@0
|
133 this.striping = $(this.table).data('striping') === 1;
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Configure the scroll settings.
|
Chris@0
|
137 *
|
Chris@0
|
138 * @type {object}
|
Chris@0
|
139 *
|
Chris@0
|
140 * @prop {number} amount
|
Chris@0
|
141 * @prop {number} interval
|
Chris@0
|
142 * @prop {number} trigger
|
Chris@0
|
143 */
|
Chris@0
|
144 this.scrollSettings = { amount: 4, interval: 50, trigger: 70 };
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 *
|
Chris@0
|
148 * @type {?number}
|
Chris@0
|
149 */
|
Chris@0
|
150 this.scrollInterval = null;
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 *
|
Chris@0
|
154 * @type {number}
|
Chris@0
|
155 */
|
Chris@0
|
156 this.scrollY = 0;
|
Chris@0
|
157
|
Chris@0
|
158 /**
|
Chris@0
|
159 *
|
Chris@0
|
160 * @type {number}
|
Chris@0
|
161 */
|
Chris@0
|
162 this.windowHeight = 0;
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * Check this table's settings for parent relationships.
|
Chris@0
|
166 *
|
Chris@0
|
167 * For efficiency, large sections of code can be skipped if we don't need to
|
Chris@0
|
168 * track horizontal movement and indentations.
|
Chris@0
|
169 *
|
Chris@0
|
170 * @type {bool}
|
Chris@0
|
171 */
|
Chris@0
|
172 this.indentEnabled = false;
|
Chris@0
|
173 Object.keys(tableSettings || {}).forEach((group) => {
|
Chris@0
|
174 Object.keys(tableSettings[group] || {}).forEach((n) => {
|
Chris@0
|
175 if (tableSettings[group][n].relationship === 'parent') {
|
Chris@0
|
176 this.indentEnabled = true;
|
Chris@0
|
177 }
|
Chris@0
|
178 if (tableSettings[group][n].limit > 0) {
|
Chris@0
|
179 this.maxDepth = tableSettings[group][n].limit;
|
Chris@0
|
180 }
|
Chris@0
|
181 });
|
Chris@0
|
182 });
|
Chris@0
|
183 if (this.indentEnabled) {
|
Chris@0
|
184 /**
|
Chris@0
|
185 * Total width of indents, set in makeDraggable.
|
Chris@0
|
186 *
|
Chris@0
|
187 * @type {number}
|
Chris@0
|
188 */
|
Chris@0
|
189 this.indentCount = 1;
|
Chris@0
|
190 // Find the width of indentations to measure mouse movements against.
|
Chris@0
|
191 // Because the table doesn't need to start with any indentations, we
|
Chris@0
|
192 // manually append 2 indentations in the first draggable row, measure
|
Chris@0
|
193 // the offset, then remove.
|
Chris@0
|
194 const indent = Drupal.theme('tableDragIndentation');
|
Chris@0
|
195 const testRow = $('<tr/>').addClass('draggable').appendTo(table);
|
Chris@0
|
196 const testCell = $('<td/>').appendTo(testRow).prepend(indent).prepend(indent);
|
Chris@0
|
197 const $indentation = testCell.find('.js-indentation');
|
Chris@0
|
198
|
Chris@0
|
199 /**
|
Chris@0
|
200 *
|
Chris@0
|
201 * @type {number}
|
Chris@0
|
202 */
|
Chris@0
|
203 this.indentAmount = $indentation.get(1).offsetLeft - $indentation.get(0).offsetLeft;
|
Chris@0
|
204 testRow.remove();
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 // Make each applicable row draggable.
|
Chris@0
|
208 // Match immediate children of the parent element to allow nesting.
|
Chris@0
|
209 $table.find('> tr.draggable, > tbody > tr.draggable').each(function () {
|
Chris@0
|
210 self.makeDraggable(this);
|
Chris@0
|
211 });
|
Chris@0
|
212
|
Chris@0
|
213 // Add a link before the table for users to show or hide weight columns.
|
Chris@0
|
214 $table.before($('<button type="button" class="link tabledrag-toggle-weight"></button>')
|
Chris@0
|
215 .attr('title', Drupal.t('Re-order rows by numerical weight instead of dragging.'))
|
Chris@0
|
216 .on('click', $.proxy(function (e) {
|
Chris@0
|
217 e.preventDefault();
|
Chris@0
|
218 this.toggleColumns();
|
Chris@0
|
219 }, this))
|
Chris@0
|
220 .wrap('<div class="tabledrag-toggle-weight-wrapper"></div>')
|
Chris@0
|
221 .parent(),
|
Chris@0
|
222 );
|
Chris@0
|
223
|
Chris@0
|
224 // Initialize the specified columns (for example, weight or parent columns)
|
Chris@0
|
225 // to show or hide according to user preference. This aids accessibility
|
Chris@0
|
226 // so that, e.g., screen reader users can choose to enter weight values and
|
Chris@0
|
227 // manipulate form elements directly, rather than using drag-and-drop..
|
Chris@0
|
228 self.initColumns();
|
Chris@0
|
229
|
Chris@0
|
230 // Add event bindings to the document. The self variable is passed along
|
Chris@0
|
231 // as event handlers do not have direct access to the tableDrag object.
|
Chris@0
|
232 $(document).on('touchmove', event => self.dragRow(event.originalEvent.touches[0], self));
|
Chris@0
|
233 $(document).on('touchend', event => self.dropRow(event.originalEvent.touches[0], self));
|
Chris@0
|
234 $(document).on('mousemove pointermove', event => self.dragRow(event, self));
|
Chris@0
|
235 $(document).on('mouseup pointerup', event => self.dropRow(event, self));
|
Chris@0
|
236
|
Chris@0
|
237 // React to localStorage event showing or hiding weight columns.
|
Chris@0
|
238 $(window).on('storage', $.proxy(function (e) {
|
Chris@0
|
239 // Only react to 'Drupal.tableDrag.showWeight' value change.
|
Chris@0
|
240 if (e.originalEvent.key === 'Drupal.tableDrag.showWeight') {
|
Chris@0
|
241 // This was changed in another window, get the new value for this
|
Chris@0
|
242 // window.
|
Chris@0
|
243 showWeight = JSON.parse(e.originalEvent.newValue);
|
Chris@0
|
244 this.displayColumns(showWeight);
|
Chris@0
|
245 }
|
Chris@0
|
246 }, this));
|
Chris@0
|
247 };
|
Chris@0
|
248
|
Chris@0
|
249 /**
|
Chris@0
|
250 * Initialize columns containing form elements to be hidden by default.
|
Chris@0
|
251 *
|
Chris@0
|
252 * Identify and mark each cell with a CSS class so we can easily toggle
|
Chris@0
|
253 * show/hide it. Finally, hide columns if user does not have a
|
Chris@0
|
254 * 'Drupal.tableDrag.showWeight' localStorage value.
|
Chris@0
|
255 */
|
Chris@0
|
256 Drupal.tableDrag.prototype.initColumns = function () {
|
Chris@0
|
257 const $table = this.$table;
|
Chris@0
|
258 let hidden;
|
Chris@0
|
259 let cell;
|
Chris@0
|
260 let columnIndex;
|
Chris@0
|
261 Object.keys(this.tableSettings || {}).forEach((group) => {
|
Chris@0
|
262 // Find the first field in this group.
|
Chris@0
|
263 // eslint-disable-next-line no-restricted-syntax
|
Chris@0
|
264 for (const d in this.tableSettings[group]) {
|
Chris@0
|
265 if (this.tableSettings[group].hasOwnProperty(d)) {
|
Chris@0
|
266 const field = $table.find(`.${this.tableSettings[group][d].target}`).eq(0);
|
Chris@0
|
267 if (field.length && this.tableSettings[group][d].hidden) {
|
Chris@0
|
268 hidden = this.tableSettings[group][d].hidden;
|
Chris@0
|
269 cell = field.closest('td');
|
Chris@0
|
270 break;
|
Chris@0
|
271 }
|
Chris@0
|
272 }
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 // Mark the column containing this field so it can be hidden.
|
Chris@0
|
276 if (hidden && cell[0]) {
|
Chris@0
|
277 // Add 1 to our indexes. The nth-child selector is 1 based, not 0
|
Chris@0
|
278 // based. Match immediate children of the parent element to allow
|
Chris@0
|
279 // nesting.
|
Chris@0
|
280 columnIndex = cell.parent().find('> td').index(cell.get(0)) + 1;
|
Chris@0
|
281 $table.find('> thead > tr, > tbody > tr, > tr').each(this.addColspanClass(columnIndex));
|
Chris@0
|
282 }
|
Chris@0
|
283 });
|
Chris@0
|
284 this.displayColumns(showWeight);
|
Chris@0
|
285 };
|
Chris@0
|
286
|
Chris@0
|
287 /**
|
Chris@0
|
288 * Mark cells that have colspan.
|
Chris@0
|
289 *
|
Chris@0
|
290 * In order to adjust the colspan instead of hiding them altogether.
|
Chris@0
|
291 *
|
Chris@0
|
292 * @param {number} columnIndex
|
Chris@0
|
293 * The column index to add colspan class to.
|
Chris@0
|
294 *
|
Chris@0
|
295 * @return {function}
|
Chris@0
|
296 * Function to add colspan class.
|
Chris@0
|
297 */
|
Chris@0
|
298 Drupal.tableDrag.prototype.addColspanClass = function (columnIndex) {
|
Chris@0
|
299 return function () {
|
Chris@0
|
300 // Get the columnIndex and adjust for any colspans in this row.
|
Chris@0
|
301 const $row = $(this);
|
Chris@0
|
302 let index = columnIndex;
|
Chris@0
|
303 const cells = $row.children();
|
Chris@0
|
304 let cell;
|
Chris@0
|
305 cells.each(function (n) {
|
Chris@0
|
306 if (n < index && this.colSpan && this.colSpan > 1) {
|
Chris@0
|
307 index -= this.colSpan - 1;
|
Chris@0
|
308 }
|
Chris@0
|
309 });
|
Chris@0
|
310 if (index > 0) {
|
Chris@0
|
311 cell = cells.filter(`:nth-child(${index})`);
|
Chris@0
|
312 if (cell[0].colSpan && cell[0].colSpan > 1) {
|
Chris@0
|
313 // If this cell has a colspan, mark it so we can reduce the colspan.
|
Chris@0
|
314 cell.addClass('tabledrag-has-colspan');
|
Chris@0
|
315 }
|
Chris@0
|
316 else {
|
Chris@0
|
317 // Mark this cell so we can hide it.
|
Chris@0
|
318 cell.addClass('tabledrag-hide');
|
Chris@0
|
319 }
|
Chris@0
|
320 }
|
Chris@0
|
321 };
|
Chris@0
|
322 };
|
Chris@0
|
323
|
Chris@0
|
324 /**
|
Chris@0
|
325 * Hide or display weight columns. Triggers an event on change.
|
Chris@0
|
326 *
|
Chris@0
|
327 * @fires event:columnschange
|
Chris@0
|
328 *
|
Chris@0
|
329 * @param {bool} displayWeight
|
Chris@0
|
330 * 'true' will show weight columns.
|
Chris@0
|
331 */
|
Chris@0
|
332 Drupal.tableDrag.prototype.displayColumns = function (displayWeight) {
|
Chris@0
|
333 if (displayWeight) {
|
Chris@0
|
334 this.showColumns();
|
Chris@0
|
335 }
|
Chris@0
|
336 // Default action is to hide columns.
|
Chris@0
|
337 else {
|
Chris@0
|
338 this.hideColumns();
|
Chris@0
|
339 }
|
Chris@0
|
340 // Trigger an event to allow other scripts to react to this display change.
|
Chris@0
|
341 // Force the extra parameter as a bool.
|
Chris@0
|
342 $('table').findOnce('tabledrag').trigger('columnschange', !!displayWeight);
|
Chris@0
|
343 };
|
Chris@0
|
344
|
Chris@0
|
345 /**
|
Chris@0
|
346 * Toggle the weight column depending on 'showWeight' value.
|
Chris@0
|
347 *
|
Chris@0
|
348 * Store only default override.
|
Chris@0
|
349 */
|
Chris@0
|
350 Drupal.tableDrag.prototype.toggleColumns = function () {
|
Chris@0
|
351 showWeight = !showWeight;
|
Chris@0
|
352 this.displayColumns(showWeight);
|
Chris@0
|
353 if (showWeight) {
|
Chris@0
|
354 // Save default override.
|
Chris@0
|
355 localStorage.setItem('Drupal.tableDrag.showWeight', showWeight);
|
Chris@0
|
356 }
|
Chris@0
|
357 else {
|
Chris@0
|
358 // Reset the value to its default.
|
Chris@0
|
359 localStorage.removeItem('Drupal.tableDrag.showWeight');
|
Chris@0
|
360 }
|
Chris@0
|
361 };
|
Chris@0
|
362
|
Chris@0
|
363 /**
|
Chris@0
|
364 * Hide the columns containing weight/parent form elements.
|
Chris@0
|
365 *
|
Chris@0
|
366 * Undo showColumns().
|
Chris@0
|
367 */
|
Chris@0
|
368 Drupal.tableDrag.prototype.hideColumns = function () {
|
Chris@0
|
369 const $tables = $('table').findOnce('tabledrag');
|
Chris@0
|
370 // Hide weight/parent cells and headers.
|
Chris@0
|
371 $tables.find('.tabledrag-hide').css('display', 'none');
|
Chris@0
|
372 // Show TableDrag handles.
|
Chris@0
|
373 $tables.find('.tabledrag-handle').css('display', '');
|
Chris@0
|
374 // Reduce the colspan of any effected multi-span columns.
|
Chris@0
|
375 $tables.find('.tabledrag-has-colspan').each(function () {
|
Chris@0
|
376 this.colSpan = this.colSpan - 1;
|
Chris@0
|
377 });
|
Chris@0
|
378 // Change link text.
|
Chris@0
|
379 $('.tabledrag-toggle-weight').text(Drupal.t('Show row weights'));
|
Chris@0
|
380 };
|
Chris@0
|
381
|
Chris@0
|
382 /**
|
Chris@0
|
383 * Show the columns containing weight/parent form elements.
|
Chris@0
|
384 *
|
Chris@0
|
385 * Undo hideColumns().
|
Chris@0
|
386 */
|
Chris@0
|
387 Drupal.tableDrag.prototype.showColumns = function () {
|
Chris@0
|
388 const $tables = $('table').findOnce('tabledrag');
|
Chris@0
|
389 // Show weight/parent cells and headers.
|
Chris@0
|
390 $tables.find('.tabledrag-hide').css('display', '');
|
Chris@0
|
391 // Hide TableDrag handles.
|
Chris@0
|
392 $tables.find('.tabledrag-handle').css('display', 'none');
|
Chris@0
|
393 // Increase the colspan for any columns where it was previously reduced.
|
Chris@0
|
394 $tables.find('.tabledrag-has-colspan').each(function () {
|
Chris@0
|
395 this.colSpan = this.colSpan + 1;
|
Chris@0
|
396 });
|
Chris@0
|
397 // Change link text.
|
Chris@0
|
398 $('.tabledrag-toggle-weight').text(Drupal.t('Hide row weights'));
|
Chris@0
|
399 };
|
Chris@0
|
400
|
Chris@0
|
401 /**
|
Chris@0
|
402 * Find the target used within a particular row and group.
|
Chris@0
|
403 *
|
Chris@0
|
404 * @param {string} group
|
Chris@0
|
405 * Group selector.
|
Chris@0
|
406 * @param {HTMLElement} row
|
Chris@0
|
407 * The row HTML element.
|
Chris@0
|
408 *
|
Chris@0
|
409 * @return {object}
|
Chris@0
|
410 * The table row settings.
|
Chris@0
|
411 */
|
Chris@0
|
412 Drupal.tableDrag.prototype.rowSettings = function (group, row) {
|
Chris@0
|
413 const field = $(row).find(`.${group}`);
|
Chris@0
|
414 const tableSettingsGroup = this.tableSettings[group];
|
Chris@0
|
415 // eslint-disable-next-line no-restricted-syntax
|
Chris@0
|
416 for (const delta in tableSettingsGroup) {
|
Chris@0
|
417 if (tableSettingsGroup.hasOwnProperty(delta)) {
|
Chris@0
|
418 const targetClass = tableSettingsGroup[delta].target;
|
Chris@0
|
419 if (field.is(`.${targetClass}`)) {
|
Chris@0
|
420 // Return a copy of the row settings.
|
Chris@0
|
421 const rowSettings = {};
|
Chris@0
|
422 // eslint-disable-next-line no-restricted-syntax
|
Chris@0
|
423 for (const n in tableSettingsGroup[delta]) {
|
Chris@0
|
424 if (tableSettingsGroup[delta].hasOwnProperty(n)) {
|
Chris@0
|
425 rowSettings[n] = tableSettingsGroup[delta][n];
|
Chris@0
|
426 }
|
Chris@0
|
427 }
|
Chris@0
|
428 return rowSettings;
|
Chris@0
|
429 }
|
Chris@0
|
430 }
|
Chris@0
|
431 }
|
Chris@0
|
432 };
|
Chris@0
|
433
|
Chris@0
|
434 /**
|
Chris@0
|
435 * Take an item and add event handlers to make it become draggable.
|
Chris@0
|
436 *
|
Chris@0
|
437 * @param {HTMLElement} item
|
Chris@0
|
438 * The item to add event handlers to.
|
Chris@0
|
439 */
|
Chris@0
|
440 Drupal.tableDrag.prototype.makeDraggable = function (item) {
|
Chris@0
|
441 const self = this;
|
Chris@0
|
442 const $item = $(item);
|
Chris@0
|
443 // Add a class to the title link.
|
Chris@0
|
444 $item.find('td:first-of-type').find('a').addClass('menu-item__link');
|
Chris@0
|
445 // Create the handle.
|
Chris@0
|
446 const handle = $('<a href="#" class="tabledrag-handle"><div class="handle"> </div></a>').attr('title', Drupal.t('Drag to re-order'));
|
Chris@0
|
447 // Insert the handle after indentations (if any).
|
Chris@0
|
448 const $indentationLast = $item.find('td:first-of-type').find('.js-indentation').eq(-1);
|
Chris@0
|
449 if ($indentationLast.length) {
|
Chris@0
|
450 $indentationLast.after(handle);
|
Chris@0
|
451 // Update the total width of indentation in this entire table.
|
Chris@0
|
452 self.indentCount = Math.max($item.find('.js-indentation').length, self.indentCount);
|
Chris@0
|
453 }
|
Chris@0
|
454 else {
|
Chris@0
|
455 $item.find('td').eq(0).prepend(handle);
|
Chris@0
|
456 }
|
Chris@0
|
457
|
Chris@0
|
458 handle.on('mousedown touchstart pointerdown', (event) => {
|
Chris@0
|
459 event.preventDefault();
|
Chris@0
|
460 if (event.originalEvent.type === 'touchstart') {
|
Chris@0
|
461 event = event.originalEvent.touches[0];
|
Chris@0
|
462 }
|
Chris@0
|
463 self.dragStart(event, self, item);
|
Chris@0
|
464 });
|
Chris@0
|
465
|
Chris@0
|
466 // Prevent the anchor tag from jumping us to the top of the page.
|
Chris@0
|
467 handle.on('click', (e) => {
|
Chris@0
|
468 e.preventDefault();
|
Chris@0
|
469 });
|
Chris@0
|
470
|
Chris@0
|
471 // Set blur cleanup when a handle is focused.
|
Chris@0
|
472 handle.on('focus', () => {
|
Chris@0
|
473 self.safeBlur = true;
|
Chris@0
|
474 });
|
Chris@0
|
475
|
Chris@0
|
476 // On blur, fire the same function as a touchend/mouseup. This is used to
|
Chris@0
|
477 // update values after a row has been moved through the keyboard support.
|
Chris@0
|
478 handle.on('blur', (event) => {
|
Chris@0
|
479 if (self.rowObject && self.safeBlur) {
|
Chris@0
|
480 self.dropRow(event, self);
|
Chris@0
|
481 }
|
Chris@0
|
482 });
|
Chris@0
|
483
|
Chris@0
|
484 // Add arrow-key support to the handle.
|
Chris@0
|
485 handle.on('keydown', (event) => {
|
Chris@0
|
486 // If a rowObject doesn't yet exist and this isn't the tab key.
|
Chris@0
|
487 if (event.keyCode !== 9 && !self.rowObject) {
|
Chris@0
|
488 self.rowObject = new self.row(item, 'keyboard', self.indentEnabled, self.maxDepth, true);
|
Chris@0
|
489 }
|
Chris@0
|
490
|
Chris@0
|
491 let keyChange = false;
|
Chris@0
|
492 let groupHeight;
|
Chris@0
|
493
|
Chris@0
|
494 /* eslint-disable no-fallthrough */
|
Chris@0
|
495
|
Chris@0
|
496 switch (event.keyCode) {
|
Chris@0
|
497 // Left arrow.
|
Chris@0
|
498 case 37:
|
Chris@0
|
499 // Safari left arrow.
|
Chris@0
|
500 case 63234:
|
Chris@0
|
501 keyChange = true;
|
Chris@0
|
502 self.rowObject.indent(-1 * self.rtl);
|
Chris@0
|
503 break;
|
Chris@0
|
504
|
Chris@0
|
505 // Up arrow.
|
Chris@0
|
506 case 38:
|
Chris@0
|
507 // Safari up arrow.
|
Chris@0
|
508 case 63232: {
|
Chris@0
|
509 let $previousRow = $(self.rowObject.element).prev('tr:first-of-type');
|
Chris@0
|
510 let previousRow = $previousRow.get(0);
|
Chris@0
|
511 while (previousRow && $previousRow.is(':hidden')) {
|
Chris@0
|
512 $previousRow = $(previousRow).prev('tr:first-of-type');
|
Chris@0
|
513 previousRow = $previousRow.get(0);
|
Chris@0
|
514 }
|
Chris@0
|
515 if (previousRow) {
|
Chris@0
|
516 // Do not allow the onBlur cleanup.
|
Chris@0
|
517 self.safeBlur = false;
|
Chris@0
|
518 self.rowObject.direction = 'up';
|
Chris@0
|
519 keyChange = true;
|
Chris@0
|
520
|
Chris@0
|
521 if ($(item).is('.tabledrag-root')) {
|
Chris@0
|
522 // Swap with the previous top-level row.
|
Chris@0
|
523 groupHeight = 0;
|
Chris@0
|
524 while (previousRow && $previousRow.find('.js-indentation').length) {
|
Chris@0
|
525 $previousRow = $(previousRow).prev('tr:first-of-type');
|
Chris@0
|
526 previousRow = $previousRow.get(0);
|
Chris@0
|
527 groupHeight += $previousRow.is(':hidden') ? 0 : previousRow.offsetHeight;
|
Chris@0
|
528 }
|
Chris@0
|
529 if (previousRow) {
|
Chris@0
|
530 self.rowObject.swap('before', previousRow);
|
Chris@0
|
531 // No need to check for indentation, 0 is the only valid one.
|
Chris@0
|
532 window.scrollBy(0, -groupHeight);
|
Chris@0
|
533 }
|
Chris@0
|
534 }
|
Chris@0
|
535 else if (self.table.tBodies[0].rows[0] !== previousRow || $previousRow.is('.draggable')) {
|
Chris@0
|
536 // Swap with the previous row (unless previous row is the first
|
Chris@0
|
537 // one and undraggable).
|
Chris@0
|
538 self.rowObject.swap('before', previousRow);
|
Chris@0
|
539 self.rowObject.interval = null;
|
Chris@0
|
540 self.rowObject.indent(0);
|
Chris@0
|
541 window.scrollBy(0, -parseInt(item.offsetHeight, 10));
|
Chris@0
|
542 }
|
Chris@0
|
543 // Regain focus after the DOM manipulation.
|
Chris@0
|
544 handle.trigger('focus');
|
Chris@0
|
545 }
|
Chris@0
|
546 break;
|
Chris@0
|
547 }
|
Chris@0
|
548 // Right arrow.
|
Chris@0
|
549 case 39:
|
Chris@0
|
550 // Safari right arrow.
|
Chris@0
|
551 case 63235:
|
Chris@0
|
552 keyChange = true;
|
Chris@0
|
553 self.rowObject.indent(self.rtl);
|
Chris@0
|
554 break;
|
Chris@0
|
555
|
Chris@0
|
556 // Down arrow.
|
Chris@0
|
557 case 40:
|
Chris@0
|
558 // Safari down arrow.
|
Chris@0
|
559 case 63233: {
|
Chris@0
|
560 let $nextRow = $(self.rowObject.group).eq(-1).next('tr:first-of-type');
|
Chris@0
|
561 let nextRow = $nextRow.get(0);
|
Chris@0
|
562 while (nextRow && $nextRow.is(':hidden')) {
|
Chris@0
|
563 $nextRow = $(nextRow).next('tr:first-of-type');
|
Chris@0
|
564 nextRow = $nextRow.get(0);
|
Chris@0
|
565 }
|
Chris@0
|
566 if (nextRow) {
|
Chris@0
|
567 // Do not allow the onBlur cleanup.
|
Chris@0
|
568 self.safeBlur = false;
|
Chris@0
|
569 self.rowObject.direction = 'down';
|
Chris@0
|
570 keyChange = true;
|
Chris@0
|
571
|
Chris@0
|
572 if ($(item).is('.tabledrag-root')) {
|
Chris@0
|
573 // Swap with the next group (necessarily a top-level one).
|
Chris@0
|
574 groupHeight = 0;
|
Chris@0
|
575 const nextGroup = new self.row(nextRow, 'keyboard', self.indentEnabled, self.maxDepth, false);
|
Chris@0
|
576 if (nextGroup) {
|
Chris@0
|
577 $(nextGroup.group).each(function () {
|
Chris@0
|
578 groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight;
|
Chris@0
|
579 });
|
Chris@0
|
580 const nextGroupRow = $(nextGroup.group).eq(-1).get(0);
|
Chris@0
|
581 self.rowObject.swap('after', nextGroupRow);
|
Chris@0
|
582 // No need to check for indentation, 0 is the only valid one.
|
Chris@0
|
583 window.scrollBy(0, parseInt(groupHeight, 10));
|
Chris@0
|
584 }
|
Chris@0
|
585 }
|
Chris@0
|
586 else {
|
Chris@0
|
587 // Swap with the next row.
|
Chris@0
|
588 self.rowObject.swap('after', nextRow);
|
Chris@0
|
589 self.rowObject.interval = null;
|
Chris@0
|
590 self.rowObject.indent(0);
|
Chris@0
|
591 window.scrollBy(0, parseInt(item.offsetHeight, 10));
|
Chris@0
|
592 }
|
Chris@0
|
593 // Regain focus after the DOM manipulation.
|
Chris@0
|
594 handle.trigger('focus');
|
Chris@0
|
595 }
|
Chris@0
|
596 break;
|
Chris@0
|
597 }
|
Chris@0
|
598 }
|
Chris@0
|
599
|
Chris@0
|
600 /* eslint-enable no-fallthrough */
|
Chris@0
|
601
|
Chris@0
|
602 if (self.rowObject && self.rowObject.changed === true) {
|
Chris@0
|
603 $(item).addClass('drag');
|
Chris@0
|
604 if (self.oldRowElement) {
|
Chris@0
|
605 $(self.oldRowElement).removeClass('drag-previous');
|
Chris@0
|
606 }
|
Chris@0
|
607 self.oldRowElement = item;
|
Chris@0
|
608 if (self.striping === true) {
|
Chris@0
|
609 self.restripeTable();
|
Chris@0
|
610 }
|
Chris@0
|
611 self.onDrag();
|
Chris@0
|
612 }
|
Chris@0
|
613
|
Chris@0
|
614 // Returning false if we have an arrow key to prevent scrolling.
|
Chris@0
|
615 if (keyChange) {
|
Chris@0
|
616 return false;
|
Chris@0
|
617 }
|
Chris@0
|
618 });
|
Chris@0
|
619
|
Chris@0
|
620 // Compatibility addition, return false on keypress to prevent unwanted
|
Chris@0
|
621 // scrolling. IE and Safari will suppress scrolling on keydown, but all
|
Chris@0
|
622 // other browsers need to return false on keypress.
|
Chris@0
|
623 // http://www.quirksmode.org/js/keys.html
|
Chris@0
|
624 handle.on('keypress', (event) => {
|
Chris@0
|
625 /* eslint-disable no-fallthrough */
|
Chris@0
|
626
|
Chris@0
|
627 switch (event.keyCode) {
|
Chris@0
|
628 // Left arrow.
|
Chris@0
|
629 case 37:
|
Chris@0
|
630 // Up arrow.
|
Chris@0
|
631 case 38:
|
Chris@0
|
632 // Right arrow.
|
Chris@0
|
633 case 39:
|
Chris@0
|
634 // Down arrow.
|
Chris@0
|
635 case 40:
|
Chris@0
|
636 return false;
|
Chris@0
|
637 }
|
Chris@0
|
638
|
Chris@0
|
639 /* eslint-enable no-fallthrough */
|
Chris@0
|
640 });
|
Chris@0
|
641 };
|
Chris@0
|
642
|
Chris@0
|
643 /**
|
Chris@0
|
644 * Pointer event initiator, creates drag object and information.
|
Chris@0
|
645 *
|
Chris@0
|
646 * @param {jQuery.Event} event
|
Chris@0
|
647 * The event object that trigger the drag.
|
Chris@0
|
648 * @param {Drupal.tableDrag} self
|
Chris@0
|
649 * The drag handle.
|
Chris@0
|
650 * @param {HTMLElement} item
|
Chris@0
|
651 * The item that that is being dragged.
|
Chris@0
|
652 */
|
Chris@0
|
653 Drupal.tableDrag.prototype.dragStart = function (event, self, item) {
|
Chris@0
|
654 // Create a new dragObject recording the pointer information.
|
Chris@0
|
655 self.dragObject = {};
|
Chris@0
|
656 self.dragObject.initOffset = self.getPointerOffset(item, event);
|
Chris@0
|
657 self.dragObject.initPointerCoords = self.pointerCoords(event);
|
Chris@0
|
658 if (self.indentEnabled) {
|
Chris@0
|
659 self.dragObject.indentPointerPos = self.dragObject.initPointerCoords;
|
Chris@0
|
660 }
|
Chris@0
|
661
|
Chris@0
|
662 // If there's a lingering row object from the keyboard, remove its focus.
|
Chris@0
|
663 if (self.rowObject) {
|
Chris@0
|
664 $(self.rowObject.element).find('a.tabledrag-handle').trigger('blur');
|
Chris@0
|
665 }
|
Chris@0
|
666
|
Chris@0
|
667 // Create a new rowObject for manipulation of this row.
|
Chris@0
|
668 self.rowObject = new self.row(item, 'pointer', self.indentEnabled, self.maxDepth, true);
|
Chris@0
|
669
|
Chris@0
|
670 // Save the position of the table.
|
Chris@0
|
671 self.table.topY = $(self.table).offset().top;
|
Chris@0
|
672 self.table.bottomY = self.table.topY + self.table.offsetHeight;
|
Chris@0
|
673
|
Chris@0
|
674 // Add classes to the handle and row.
|
Chris@0
|
675 $(item).addClass('drag');
|
Chris@0
|
676
|
Chris@0
|
677 // Set the document to use the move cursor during drag.
|
Chris@0
|
678 $('body').addClass('drag');
|
Chris@0
|
679 if (self.oldRowElement) {
|
Chris@0
|
680 $(self.oldRowElement).removeClass('drag-previous');
|
Chris@0
|
681 }
|
Chris@0
|
682 };
|
Chris@0
|
683
|
Chris@0
|
684 /**
|
Chris@0
|
685 * Pointer movement handler, bound to document.
|
Chris@0
|
686 *
|
Chris@0
|
687 * @param {jQuery.Event} event
|
Chris@0
|
688 * The pointer event.
|
Chris@0
|
689 * @param {Drupal.tableDrag} self
|
Chris@0
|
690 * The tableDrag instance.
|
Chris@0
|
691 *
|
Chris@0
|
692 * @return {bool|undefined}
|
Chris@0
|
693 * Undefined if no dragObject is defined, false otherwise.
|
Chris@0
|
694 */
|
Chris@0
|
695 Drupal.tableDrag.prototype.dragRow = function (event, self) {
|
Chris@0
|
696 if (self.dragObject) {
|
Chris@0
|
697 self.currentPointerCoords = self.pointerCoords(event);
|
Chris@0
|
698 const y = self.currentPointerCoords.y - self.dragObject.initOffset.y;
|
Chris@0
|
699 const x = self.currentPointerCoords.x - self.dragObject.initOffset.x;
|
Chris@0
|
700
|
Chris@0
|
701 // Check for row swapping and vertical scrolling.
|
Chris@0
|
702 if (y !== self.oldY) {
|
Chris@0
|
703 self.rowObject.direction = y > self.oldY ? 'down' : 'up';
|
Chris@0
|
704 // Update the old value.
|
Chris@0
|
705 self.oldY = y;
|
Chris@0
|
706 // Check if the window should be scrolled (and how fast).
|
Chris@0
|
707 const scrollAmount = self.checkScroll(self.currentPointerCoords.y);
|
Chris@0
|
708 // Stop any current scrolling.
|
Chris@0
|
709 clearInterval(self.scrollInterval);
|
Chris@0
|
710 // Continue scrolling if the mouse has moved in the scroll direction.
|
Chris@0
|
711 if ((scrollAmount > 0 && self.rowObject.direction === 'down')
|
Chris@0
|
712 || (scrollAmount < 0 && self.rowObject.direction === 'up')) {
|
Chris@0
|
713 self.setScroll(scrollAmount);
|
Chris@0
|
714 }
|
Chris@0
|
715
|
Chris@0
|
716 // If we have a valid target, perform the swap and restripe the table.
|
Chris@0
|
717 const currentRow = self.findDropTargetRow(x, y);
|
Chris@0
|
718 if (currentRow) {
|
Chris@0
|
719 if (self.rowObject.direction === 'down') {
|
Chris@0
|
720 self.rowObject.swap('after', currentRow, self);
|
Chris@0
|
721 }
|
Chris@0
|
722 else {
|
Chris@0
|
723 self.rowObject.swap('before', currentRow, self);
|
Chris@0
|
724 }
|
Chris@0
|
725 if (self.striping === true) {
|
Chris@0
|
726 self.restripeTable();
|
Chris@0
|
727 }
|
Chris@0
|
728 }
|
Chris@0
|
729 }
|
Chris@0
|
730
|
Chris@0
|
731 // Similar to row swapping, handle indentations.
|
Chris@0
|
732 if (self.indentEnabled) {
|
Chris@0
|
733 const xDiff = self.currentPointerCoords.x - self.dragObject.indentPointerPos.x;
|
Chris@0
|
734 // Set the number of indentations the pointer has been moved left or
|
Chris@0
|
735 // right.
|
Chris@0
|
736 const indentDiff = Math.round(xDiff / self.indentAmount);
|
Chris@0
|
737 // Indent the row with our estimated diff, which may be further
|
Chris@0
|
738 // restricted according to the rows around this row.
|
Chris@0
|
739 const indentChange = self.rowObject.indent(indentDiff);
|
Chris@0
|
740 // Update table and pointer indentations.
|
Chris@0
|
741 self.dragObject.indentPointerPos.x += self.indentAmount * indentChange * self.rtl;
|
Chris@0
|
742 self.indentCount = Math.max(self.indentCount, self.rowObject.indents);
|
Chris@0
|
743 }
|
Chris@0
|
744
|
Chris@0
|
745 return false;
|
Chris@0
|
746 }
|
Chris@0
|
747 };
|
Chris@0
|
748
|
Chris@0
|
749 /**
|
Chris@0
|
750 * Pointerup behavior.
|
Chris@0
|
751 *
|
Chris@0
|
752 * @param {jQuery.Event} event
|
Chris@0
|
753 * The pointer event.
|
Chris@0
|
754 * @param {Drupal.tableDrag} self
|
Chris@0
|
755 * The tableDrag instance.
|
Chris@0
|
756 */
|
Chris@0
|
757 Drupal.tableDrag.prototype.dropRow = function (event, self) {
|
Chris@0
|
758 let droppedRow;
|
Chris@0
|
759 let $droppedRow;
|
Chris@0
|
760
|
Chris@0
|
761 // Drop row functionality.
|
Chris@0
|
762 if (self.rowObject !== null) {
|
Chris@0
|
763 droppedRow = self.rowObject.element;
|
Chris@0
|
764 $droppedRow = $(droppedRow);
|
Chris@0
|
765 // The row is already in the right place so we just release it.
|
Chris@0
|
766 if (self.rowObject.changed === true) {
|
Chris@0
|
767 // Update the fields in the dropped row.
|
Chris@0
|
768 self.updateFields(droppedRow);
|
Chris@0
|
769
|
Chris@0
|
770 // If a setting exists for affecting the entire group, update all the
|
Chris@0
|
771 // fields in the entire dragged group.
|
Chris@0
|
772 Object.keys(self.tableSettings || {}).forEach((group) => {
|
Chris@0
|
773 const rowSettings = self.rowSettings(group, droppedRow);
|
Chris@0
|
774 if (rowSettings.relationship === 'group') {
|
Chris@0
|
775 Object.keys(self.rowObject.children || {}).forEach((n) => {
|
Chris@0
|
776 self.updateField(self.rowObject.children[n], group);
|
Chris@0
|
777 });
|
Chris@0
|
778 }
|
Chris@0
|
779 });
|
Chris@0
|
780
|
Chris@0
|
781 self.rowObject.markChanged();
|
Chris@0
|
782 if (self.changed === false) {
|
Chris@0
|
783 $(Drupal.theme('tableDragChangedWarning')).insertBefore(self.table).hide().fadeIn('slow');
|
Chris@0
|
784 self.changed = true;
|
Chris@0
|
785 }
|
Chris@0
|
786 }
|
Chris@0
|
787
|
Chris@0
|
788 if (self.indentEnabled) {
|
Chris@0
|
789 self.rowObject.removeIndentClasses();
|
Chris@0
|
790 }
|
Chris@0
|
791 if (self.oldRowElement) {
|
Chris@0
|
792 $(self.oldRowElement).removeClass('drag-previous');
|
Chris@0
|
793 }
|
Chris@0
|
794 $droppedRow.removeClass('drag').addClass('drag-previous');
|
Chris@0
|
795 self.oldRowElement = droppedRow;
|
Chris@0
|
796 self.onDrop();
|
Chris@0
|
797 self.rowObject = null;
|
Chris@0
|
798 }
|
Chris@0
|
799
|
Chris@0
|
800 // Functionality specific only to pointerup events.
|
Chris@0
|
801 if (self.dragObject !== null) {
|
Chris@0
|
802 self.dragObject = null;
|
Chris@0
|
803 $('body').removeClass('drag');
|
Chris@0
|
804 clearInterval(self.scrollInterval);
|
Chris@0
|
805 }
|
Chris@0
|
806 };
|
Chris@0
|
807
|
Chris@0
|
808 /**
|
Chris@0
|
809 * Get the coordinates from the event (allowing for browser differences).
|
Chris@0
|
810 *
|
Chris@0
|
811 * @param {jQuery.Event} event
|
Chris@0
|
812 * The pointer event.
|
Chris@0
|
813 *
|
Chris@0
|
814 * @return {object}
|
Chris@0
|
815 * An object with `x` and `y` keys indicating the position.
|
Chris@0
|
816 */
|
Chris@0
|
817 Drupal.tableDrag.prototype.pointerCoords = function (event) {
|
Chris@0
|
818 if (event.pageX || event.pageY) {
|
Chris@0
|
819 return { x: event.pageX, y: event.pageY };
|
Chris@0
|
820 }
|
Chris@0
|
821 return {
|
Chris@0
|
822 x: (event.clientX + document.body.scrollLeft) - document.body.clientLeft,
|
Chris@0
|
823 y: (event.clientY + document.body.scrollTop) - document.body.clientTop,
|
Chris@0
|
824 };
|
Chris@0
|
825 };
|
Chris@0
|
826
|
Chris@0
|
827 /**
|
Chris@0
|
828 * Get the event offset from the target element.
|
Chris@0
|
829 *
|
Chris@0
|
830 * Given a target element and a pointer event, get the event offset from that
|
Chris@0
|
831 * element. To do this we need the element's position and the target position.
|
Chris@0
|
832 *
|
Chris@0
|
833 * @param {HTMLElement} target
|
Chris@0
|
834 * The target HTML element.
|
Chris@0
|
835 * @param {jQuery.Event} event
|
Chris@0
|
836 * The pointer event.
|
Chris@0
|
837 *
|
Chris@0
|
838 * @return {object}
|
Chris@0
|
839 * An object with `x` and `y` keys indicating the position.
|
Chris@0
|
840 */
|
Chris@0
|
841 Drupal.tableDrag.prototype.getPointerOffset = function (target, event) {
|
Chris@0
|
842 const docPos = $(target).offset();
|
Chris@0
|
843 const pointerPos = this.pointerCoords(event);
|
Chris@0
|
844 return { x: pointerPos.x - docPos.left, y: pointerPos.y - docPos.top };
|
Chris@0
|
845 };
|
Chris@0
|
846
|
Chris@0
|
847 /**
|
Chris@0
|
848 * Find the row the mouse is currently over.
|
Chris@0
|
849 *
|
Chris@0
|
850 * This row is then taken and swapped with the one being dragged.
|
Chris@0
|
851 *
|
Chris@0
|
852 * @param {number} x
|
Chris@0
|
853 * The x coordinate of the mouse on the page (not the screen).
|
Chris@0
|
854 * @param {number} y
|
Chris@0
|
855 * The y coordinate of the mouse on the page (not the screen).
|
Chris@0
|
856 *
|
Chris@0
|
857 * @return {*}
|
Chris@0
|
858 * The drop target row, if found.
|
Chris@0
|
859 */
|
Chris@0
|
860 Drupal.tableDrag.prototype.findDropTargetRow = function (x, y) {
|
Chris@0
|
861 const rows = $(this.table.tBodies[0].rows).not(':hidden');
|
Chris@0
|
862 for (let n = 0; n < rows.length; n++) {
|
Chris@0
|
863 let row = rows[n];
|
Chris@0
|
864 let $row = $(row);
|
Chris@0
|
865 const rowY = $row.offset().top;
|
Chris@0
|
866 let rowHeight;
|
Chris@0
|
867 // Because Safari does not report offsetHeight on table rows, but does on
|
Chris@0
|
868 // table cells, grab the firstChild of the row and use that instead.
|
Chris@0
|
869 // http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari.
|
Chris@0
|
870 if (row.offsetHeight === 0) {
|
Chris@0
|
871 rowHeight = parseInt(row.firstChild.offsetHeight, 10) / 2;
|
Chris@0
|
872 }
|
Chris@0
|
873 // Other browsers.
|
Chris@0
|
874 else {
|
Chris@0
|
875 rowHeight = parseInt(row.offsetHeight, 10) / 2;
|
Chris@0
|
876 }
|
Chris@0
|
877
|
Chris@0
|
878 // Because we always insert before, we need to offset the height a bit.
|
Chris@0
|
879 if ((y > (rowY - rowHeight)) && (y < (rowY + rowHeight))) {
|
Chris@0
|
880 if (this.indentEnabled) {
|
Chris@0
|
881 // Check that this row is not a child of the row being dragged.
|
Chris@0
|
882 // eslint-disable-next-line no-restricted-syntax
|
Chris@0
|
883 for (n in this.rowObject.group) {
|
Chris@0
|
884 if (this.rowObject.group[n] === row) {
|
Chris@0
|
885 return null;
|
Chris@0
|
886 }
|
Chris@0
|
887 }
|
Chris@0
|
888 }
|
Chris@0
|
889 // Do not allow a row to be swapped with itself.
|
Chris@0
|
890 else if (row === this.rowObject.element) {
|
Chris@0
|
891 return null;
|
Chris@0
|
892 }
|
Chris@0
|
893
|
Chris@0
|
894 // Check that swapping with this row is allowed.
|
Chris@0
|
895 if (!this.rowObject.isValidSwap(row)) {
|
Chris@0
|
896 return null;
|
Chris@0
|
897 }
|
Chris@0
|
898
|
Chris@0
|
899 // We may have found the row the mouse just passed over, but it doesn't
|
Chris@0
|
900 // take into account hidden rows. Skip backwards until we find a
|
Chris@0
|
901 // draggable row.
|
Chris@0
|
902 while ($row.is(':hidden') && $row.prev('tr').is(':hidden')) {
|
Chris@0
|
903 $row = $row.prev('tr:first-of-type');
|
Chris@0
|
904 row = $row.get(0);
|
Chris@0
|
905 }
|
Chris@0
|
906 return row;
|
Chris@0
|
907 }
|
Chris@0
|
908 }
|
Chris@0
|
909 return null;
|
Chris@0
|
910 };
|
Chris@0
|
911
|
Chris@0
|
912 /**
|
Chris@0
|
913 * After the row is dropped, update the table fields.
|
Chris@0
|
914 *
|
Chris@0
|
915 * @param {HTMLElement} changedRow
|
Chris@0
|
916 * DOM object for the row that was just dropped.
|
Chris@0
|
917 */
|
Chris@0
|
918 Drupal.tableDrag.prototype.updateFields = function (changedRow) {
|
Chris@0
|
919 Object.keys(this.tableSettings || {}).forEach((group) => {
|
Chris@0
|
920 // Each group may have a different setting for relationship, so we find
|
Chris@0
|
921 // the source rows for each separately.
|
Chris@0
|
922 this.updateField(changedRow, group);
|
Chris@0
|
923 });
|
Chris@0
|
924 };
|
Chris@0
|
925
|
Chris@0
|
926 /**
|
Chris@0
|
927 * After the row is dropped, update a single table field.
|
Chris@0
|
928 *
|
Chris@0
|
929 * @param {HTMLElement} changedRow
|
Chris@0
|
930 * DOM object for the row that was just dropped.
|
Chris@0
|
931 * @param {string} group
|
Chris@0
|
932 * The settings group on which field updates will occur.
|
Chris@0
|
933 */
|
Chris@0
|
934 Drupal.tableDrag.prototype.updateField = function (changedRow, group) {
|
Chris@0
|
935 let rowSettings = this.rowSettings(group, changedRow);
|
Chris@0
|
936 const $changedRow = $(changedRow);
|
Chris@0
|
937 let sourceRow;
|
Chris@0
|
938 let $previousRow;
|
Chris@0
|
939 let previousRow;
|
Chris@0
|
940 let useSibling;
|
Chris@0
|
941 // Set the row as its own target.
|
Chris@0
|
942 if (rowSettings.relationship === 'self' || rowSettings.relationship === 'group') {
|
Chris@0
|
943 sourceRow = changedRow;
|
Chris@0
|
944 }
|
Chris@0
|
945 // Siblings are easy, check previous and next rows.
|
Chris@0
|
946 else if (rowSettings.relationship === 'sibling') {
|
Chris@0
|
947 $previousRow = $changedRow.prev('tr:first-of-type');
|
Chris@0
|
948 previousRow = $previousRow.get(0);
|
Chris@0
|
949 const $nextRow = $changedRow.next('tr:first-of-type');
|
Chris@0
|
950 const nextRow = $nextRow.get(0);
|
Chris@0
|
951 sourceRow = changedRow;
|
Chris@0
|
952 if ($previousRow.is('.draggable') && $previousRow.find(`.${group}`).length) {
|
Chris@0
|
953 if (this.indentEnabled) {
|
Chris@0
|
954 if ($previousRow.find('.js-indentations').length === $changedRow.find('.js-indentations').length) {
|
Chris@0
|
955 sourceRow = previousRow;
|
Chris@0
|
956 }
|
Chris@0
|
957 }
|
Chris@0
|
958 else {
|
Chris@0
|
959 sourceRow = previousRow;
|
Chris@0
|
960 }
|
Chris@0
|
961 }
|
Chris@0
|
962 else if ($nextRow.is('.draggable') && $nextRow.find(`.${group}`).length) {
|
Chris@0
|
963 if (this.indentEnabled) {
|
Chris@0
|
964 if ($nextRow.find('.js-indentations').length === $changedRow.find('.js-indentations').length) {
|
Chris@0
|
965 sourceRow = nextRow;
|
Chris@0
|
966 }
|
Chris@0
|
967 }
|
Chris@0
|
968 else {
|
Chris@0
|
969 sourceRow = nextRow;
|
Chris@0
|
970 }
|
Chris@0
|
971 }
|
Chris@0
|
972 }
|
Chris@0
|
973 // Parents, look up the tree until we find a field not in this group.
|
Chris@0
|
974 // Go up as many parents as indentations in the changed row.
|
Chris@0
|
975 else if (rowSettings.relationship === 'parent') {
|
Chris@0
|
976 $previousRow = $changedRow.prev('tr');
|
Chris@0
|
977 previousRow = $previousRow;
|
Chris@0
|
978 while ($previousRow.length && $previousRow.find('.js-indentation').length >= this.rowObject.indents) {
|
Chris@0
|
979 $previousRow = $previousRow.prev('tr');
|
Chris@0
|
980 previousRow = $previousRow;
|
Chris@0
|
981 }
|
Chris@0
|
982 // If we found a row.
|
Chris@0
|
983 if ($previousRow.length) {
|
Chris@0
|
984 sourceRow = $previousRow.get(0);
|
Chris@0
|
985 }
|
Chris@0
|
986 // Otherwise we went all the way to the left of the table without finding
|
Chris@0
|
987 // a parent, meaning this item has been placed at the root level.
|
Chris@0
|
988 else {
|
Chris@0
|
989 // Use the first row in the table as source, because it's guaranteed to
|
Chris@0
|
990 // be at the root level. Find the first item, then compare this row
|
Chris@0
|
991 // against it as a sibling.
|
Chris@0
|
992 sourceRow = $(this.table).find('tr.draggable:first-of-type').get(0);
|
Chris@0
|
993 if (sourceRow === this.rowObject.element) {
|
Chris@0
|
994 sourceRow = $(this.rowObject.group[this.rowObject.group.length - 1]).next('tr.draggable').get(0);
|
Chris@0
|
995 }
|
Chris@0
|
996 useSibling = true;
|
Chris@0
|
997 }
|
Chris@0
|
998 }
|
Chris@0
|
999
|
Chris@0
|
1000 // Because we may have moved the row from one category to another,
|
Chris@0
|
1001 // take a look at our sibling and borrow its sources and targets.
|
Chris@0
|
1002 this.copyDragClasses(sourceRow, changedRow, group);
|
Chris@0
|
1003 rowSettings = this.rowSettings(group, changedRow);
|
Chris@0
|
1004
|
Chris@0
|
1005 // In the case that we're looking for a parent, but the row is at the top
|
Chris@0
|
1006 // of the tree, copy our sibling's values.
|
Chris@0
|
1007 if (useSibling) {
|
Chris@0
|
1008 rowSettings.relationship = 'sibling';
|
Chris@0
|
1009 rowSettings.source = rowSettings.target;
|
Chris@0
|
1010 }
|
Chris@0
|
1011
|
Chris@0
|
1012 const targetClass = `.${rowSettings.target}`;
|
Chris@0
|
1013 const targetElement = $changedRow.find(targetClass).get(0);
|
Chris@0
|
1014
|
Chris@0
|
1015 // Check if a target element exists in this row.
|
Chris@0
|
1016 if (targetElement) {
|
Chris@0
|
1017 const sourceClass = `.${rowSettings.source}`;
|
Chris@0
|
1018 const sourceElement = $(sourceClass, sourceRow).get(0);
|
Chris@0
|
1019 switch (rowSettings.action) {
|
Chris@0
|
1020 case 'depth':
|
Chris@0
|
1021 // Get the depth of the target row.
|
Chris@0
|
1022 targetElement.value = $(sourceElement).closest('tr').find('.js-indentation').length;
|
Chris@0
|
1023 break;
|
Chris@0
|
1024
|
Chris@0
|
1025 case 'match':
|
Chris@0
|
1026 // Update the value.
|
Chris@0
|
1027 targetElement.value = sourceElement.value;
|
Chris@0
|
1028 break;
|
Chris@0
|
1029
|
Chris@0
|
1030 case 'order': {
|
Chris@0
|
1031 const siblings = this.rowObject.findSiblings(rowSettings);
|
Chris@0
|
1032 if ($(targetElement).is('select')) {
|
Chris@0
|
1033 // Get a list of acceptable values.
|
Chris@0
|
1034 const values = [];
|
Chris@0
|
1035 $(targetElement).find('option').each(function () {
|
Chris@0
|
1036 values.push(this.value);
|
Chris@0
|
1037 });
|
Chris@0
|
1038 const maxVal = values[values.length - 1];
|
Chris@0
|
1039 // Populate the values in the siblings.
|
Chris@0
|
1040 $(siblings).find(targetClass).each(function () {
|
Chris@0
|
1041 // If there are more items than possible values, assign the
|
Chris@0
|
1042 // maximum value to the row.
|
Chris@0
|
1043 if (values.length > 0) {
|
Chris@0
|
1044 this.value = values.shift();
|
Chris@0
|
1045 }
|
Chris@0
|
1046 else {
|
Chris@0
|
1047 this.value = maxVal;
|
Chris@0
|
1048 }
|
Chris@0
|
1049 });
|
Chris@0
|
1050 }
|
Chris@0
|
1051 else {
|
Chris@0
|
1052 // Assume a numeric input field.
|
Chris@0
|
1053 let weight = parseInt($(siblings[0]).find(targetClass).val(), 10) || 0;
|
Chris@0
|
1054 $(siblings).find(targetClass).each(function () {
|
Chris@0
|
1055 this.value = weight;
|
Chris@0
|
1056 weight++;
|
Chris@0
|
1057 });
|
Chris@0
|
1058 }
|
Chris@0
|
1059 break;
|
Chris@0
|
1060 }
|
Chris@0
|
1061 }
|
Chris@0
|
1062 }
|
Chris@0
|
1063 };
|
Chris@0
|
1064
|
Chris@0
|
1065 /**
|
Chris@0
|
1066 * Copy all tableDrag related classes from one row to another.
|
Chris@0
|
1067 *
|
Chris@0
|
1068 * Copy all special tableDrag classes from one row's form elements to a
|
Chris@0
|
1069 * different one, removing any special classes that the destination row
|
Chris@0
|
1070 * may have had.
|
Chris@0
|
1071 *
|
Chris@0
|
1072 * @param {HTMLElement} sourceRow
|
Chris@0
|
1073 * The element for the source row.
|
Chris@0
|
1074 * @param {HTMLElement} targetRow
|
Chris@0
|
1075 * The element for the target row.
|
Chris@0
|
1076 * @param {string} group
|
Chris@0
|
1077 * The group selector.
|
Chris@0
|
1078 */
|
Chris@0
|
1079 Drupal.tableDrag.prototype.copyDragClasses = function (sourceRow, targetRow, group) {
|
Chris@0
|
1080 const sourceElement = $(sourceRow).find(`.${group}`);
|
Chris@0
|
1081 const targetElement = $(targetRow).find(`.${group}`);
|
Chris@0
|
1082 if (sourceElement.length && targetElement.length) {
|
Chris@0
|
1083 targetElement[0].className = sourceElement[0].className;
|
Chris@0
|
1084 }
|
Chris@0
|
1085 };
|
Chris@0
|
1086
|
Chris@0
|
1087 /**
|
Chris@0
|
1088 * Check the suggested scroll of the table.
|
Chris@0
|
1089 *
|
Chris@0
|
1090 * @param {number} cursorY
|
Chris@0
|
1091 * The Y position of the cursor.
|
Chris@0
|
1092 *
|
Chris@0
|
1093 * @return {number}
|
Chris@0
|
1094 * The suggested scroll.
|
Chris@0
|
1095 */
|
Chris@0
|
1096 Drupal.tableDrag.prototype.checkScroll = function (cursorY) {
|
Chris@0
|
1097 const de = document.documentElement;
|
Chris@0
|
1098 const b = document.body;
|
Chris@0
|
1099
|
Chris@0
|
1100 const windowHeight = window.innerHeight || (de.clientHeight && de.clientWidth !== 0 ? de.clientHeight : b.offsetHeight);
|
Chris@0
|
1101 this.windowHeight = windowHeight;
|
Chris@0
|
1102 let scrollY;
|
Chris@0
|
1103 if (document.all) {
|
Chris@0
|
1104 scrollY = !de.scrollTop ? b.scrollTop : de.scrollTop;
|
Chris@0
|
1105 }
|
Chris@0
|
1106 else {
|
Chris@0
|
1107 scrollY = window.pageYOffset ? window.pageYOffset : window.scrollY;
|
Chris@0
|
1108 }
|
Chris@0
|
1109 this.scrollY = scrollY;
|
Chris@0
|
1110 const trigger = this.scrollSettings.trigger;
|
Chris@0
|
1111 let delta = 0;
|
Chris@0
|
1112
|
Chris@0
|
1113 // Return a scroll speed relative to the edge of the screen.
|
Chris@0
|
1114 if (cursorY - scrollY > windowHeight - trigger) {
|
Chris@0
|
1115 delta = trigger / ((windowHeight + scrollY) - cursorY);
|
Chris@0
|
1116 delta = (delta > 0 && delta < trigger) ? delta : trigger;
|
Chris@0
|
1117 return delta * this.scrollSettings.amount;
|
Chris@0
|
1118 }
|
Chris@0
|
1119 else if (cursorY - scrollY < trigger) {
|
Chris@0
|
1120 delta = trigger / (cursorY - scrollY);
|
Chris@0
|
1121 delta = (delta > 0 && delta < trigger) ? delta : trigger;
|
Chris@0
|
1122 return -delta * this.scrollSettings.amount;
|
Chris@0
|
1123 }
|
Chris@0
|
1124 };
|
Chris@0
|
1125
|
Chris@0
|
1126 /**
|
Chris@0
|
1127 * Set the scroll for the table.
|
Chris@0
|
1128 *
|
Chris@0
|
1129 * @param {number} scrollAmount
|
Chris@0
|
1130 * The amount of scroll to apply to the window.
|
Chris@0
|
1131 */
|
Chris@0
|
1132 Drupal.tableDrag.prototype.setScroll = function (scrollAmount) {
|
Chris@0
|
1133 const self = this;
|
Chris@0
|
1134
|
Chris@0
|
1135 this.scrollInterval = setInterval(() => {
|
Chris@0
|
1136 // Update the scroll values stored in the object.
|
Chris@0
|
1137 self.checkScroll(self.currentPointerCoords.y);
|
Chris@0
|
1138 const aboveTable = self.scrollY > self.table.topY;
|
Chris@0
|
1139 const belowTable = self.scrollY + self.windowHeight < self.table.bottomY;
|
Chris@0
|
1140 if ((scrollAmount > 0 && belowTable)
|
Chris@0
|
1141 || (scrollAmount < 0 && aboveTable)) {
|
Chris@0
|
1142 window.scrollBy(0, scrollAmount);
|
Chris@0
|
1143 }
|
Chris@0
|
1144 }, this.scrollSettings.interval);
|
Chris@0
|
1145 };
|
Chris@0
|
1146
|
Chris@0
|
1147 /**
|
Chris@0
|
1148 * Command to restripe table properly.
|
Chris@0
|
1149 */
|
Chris@0
|
1150 Drupal.tableDrag.prototype.restripeTable = function () {
|
Chris@0
|
1151 // :even and :odd are reversed because jQuery counts from 0 and
|
Chris@0
|
1152 // we count from 1, so we're out of sync.
|
Chris@0
|
1153 // Match immediate children of the parent element to allow nesting.
|
Chris@0
|
1154 $(this.table)
|
Chris@0
|
1155 .find('> tbody > tr.draggable, > tr.draggable')
|
Chris@0
|
1156 .filter(':visible')
|
Chris@0
|
1157 .filter(':odd')
|
Chris@0
|
1158 .removeClass('odd')
|
Chris@0
|
1159 .addClass('even')
|
Chris@0
|
1160 .end()
|
Chris@0
|
1161 .filter(':even')
|
Chris@0
|
1162 .removeClass('even')
|
Chris@0
|
1163 .addClass('odd');
|
Chris@0
|
1164 };
|
Chris@0
|
1165
|
Chris@0
|
1166 /**
|
Chris@0
|
1167 * Stub function. Allows a custom handler when a row begins dragging.
|
Chris@0
|
1168 *
|
Chris@0
|
1169 * @return {null}
|
Chris@0
|
1170 * Returns null when the stub function is used.
|
Chris@0
|
1171 */
|
Chris@0
|
1172 Drupal.tableDrag.prototype.onDrag = function () {
|
Chris@0
|
1173 return null;
|
Chris@0
|
1174 };
|
Chris@0
|
1175
|
Chris@0
|
1176 /**
|
Chris@0
|
1177 * Stub function. Allows a custom handler when a row is dropped.
|
Chris@0
|
1178 *
|
Chris@0
|
1179 * @return {null}
|
Chris@0
|
1180 * Returns null when the stub function is used.
|
Chris@0
|
1181 */
|
Chris@0
|
1182 Drupal.tableDrag.prototype.onDrop = function () {
|
Chris@0
|
1183 return null;
|
Chris@0
|
1184 };
|
Chris@0
|
1185
|
Chris@0
|
1186 /**
|
Chris@0
|
1187 * Constructor to make a new object to manipulate a table row.
|
Chris@0
|
1188 *
|
Chris@0
|
1189 * @param {HTMLElement} tableRow
|
Chris@0
|
1190 * The DOM element for the table row we will be manipulating.
|
Chris@0
|
1191 * @param {string} method
|
Chris@0
|
1192 * The method in which this row is being moved. Either 'keyboard' or
|
Chris@0
|
1193 * 'mouse'.
|
Chris@0
|
1194 * @param {bool} indentEnabled
|
Chris@0
|
1195 * Whether the containing table uses indentations. Used for optimizations.
|
Chris@0
|
1196 * @param {number} maxDepth
|
Chris@0
|
1197 * The maximum amount of indentations this row may contain.
|
Chris@0
|
1198 * @param {bool} addClasses
|
Chris@0
|
1199 * Whether we want to add classes to this row to indicate child
|
Chris@0
|
1200 * relationships.
|
Chris@0
|
1201 */
|
Chris@0
|
1202 Drupal.tableDrag.prototype.row = function (tableRow, method, indentEnabled, maxDepth, addClasses) {
|
Chris@0
|
1203 const $tableRow = $(tableRow);
|
Chris@0
|
1204
|
Chris@0
|
1205 this.element = tableRow;
|
Chris@0
|
1206 this.method = method;
|
Chris@0
|
1207 this.group = [tableRow];
|
Chris@0
|
1208 this.groupDepth = $tableRow.find('.js-indentation').length;
|
Chris@0
|
1209 this.changed = false;
|
Chris@0
|
1210 this.table = $tableRow.closest('table')[0];
|
Chris@0
|
1211 this.indentEnabled = indentEnabled;
|
Chris@0
|
1212 this.maxDepth = maxDepth;
|
Chris@0
|
1213 // Direction the row is being moved.
|
Chris@0
|
1214 this.direction = '';
|
Chris@0
|
1215 if (this.indentEnabled) {
|
Chris@0
|
1216 this.indents = $tableRow.find('.js-indentation').length;
|
Chris@0
|
1217 this.children = this.findChildren(addClasses);
|
Chris@0
|
1218 this.group = $.merge(this.group, this.children);
|
Chris@0
|
1219 // Find the depth of this entire group.
|
Chris@0
|
1220 for (let n = 0; n < this.group.length; n++) {
|
Chris@0
|
1221 this.groupDepth = Math.max($(this.group[n]).find('.js-indentation').length, this.groupDepth);
|
Chris@0
|
1222 }
|
Chris@0
|
1223 }
|
Chris@0
|
1224 };
|
Chris@0
|
1225
|
Chris@0
|
1226 /**
|
Chris@0
|
1227 * Find all children of rowObject by indentation.
|
Chris@0
|
1228 *
|
Chris@0
|
1229 * @param {bool} addClasses
|
Chris@0
|
1230 * Whether we want to add classes to this row to indicate child
|
Chris@0
|
1231 * relationships.
|
Chris@0
|
1232 *
|
Chris@0
|
1233 * @return {Array}
|
Chris@0
|
1234 * An array of children of the row.
|
Chris@0
|
1235 */
|
Chris@0
|
1236 Drupal.tableDrag.prototype.row.prototype.findChildren = function (addClasses) {
|
Chris@0
|
1237 const parentIndentation = this.indents;
|
Chris@0
|
1238 let currentRow = $(this.element, this.table).next('tr.draggable');
|
Chris@0
|
1239 const rows = [];
|
Chris@0
|
1240 let child = 0;
|
Chris@0
|
1241
|
Chris@0
|
1242 function rowIndentation(indentNum, el) {
|
Chris@0
|
1243 const self = $(el);
|
Chris@0
|
1244 if (child === 1 && (indentNum === parentIndentation)) {
|
Chris@0
|
1245 self.addClass('tree-child-first');
|
Chris@0
|
1246 }
|
Chris@0
|
1247 if (indentNum === parentIndentation) {
|
Chris@0
|
1248 self.addClass('tree-child');
|
Chris@0
|
1249 }
|
Chris@0
|
1250 else if (indentNum > parentIndentation) {
|
Chris@0
|
1251 self.addClass('tree-child-horizontal');
|
Chris@0
|
1252 }
|
Chris@0
|
1253 }
|
Chris@0
|
1254
|
Chris@0
|
1255 while (currentRow.length) {
|
Chris@0
|
1256 // A greater indentation indicates this is a child.
|
Chris@0
|
1257 if (currentRow.find('.js-indentation').length > parentIndentation) {
|
Chris@0
|
1258 child++;
|
Chris@0
|
1259 rows.push(currentRow[0]);
|
Chris@0
|
1260 if (addClasses) {
|
Chris@0
|
1261 currentRow.find('.js-indentation').each(rowIndentation);
|
Chris@0
|
1262 }
|
Chris@0
|
1263 }
|
Chris@0
|
1264 else {
|
Chris@0
|
1265 break;
|
Chris@0
|
1266 }
|
Chris@0
|
1267 currentRow = currentRow.next('tr.draggable');
|
Chris@0
|
1268 }
|
Chris@0
|
1269 if (addClasses && rows.length) {
|
Chris@0
|
1270 $(rows[rows.length - 1]).find(`.js-indentation:nth-child(${parentIndentation + 1})`).addClass('tree-child-last');
|
Chris@0
|
1271 }
|
Chris@0
|
1272 return rows;
|
Chris@0
|
1273 };
|
Chris@0
|
1274
|
Chris@0
|
1275 /**
|
Chris@0
|
1276 * Ensure that two rows are allowed to be swapped.
|
Chris@0
|
1277 *
|
Chris@0
|
1278 * @param {HTMLElement} row
|
Chris@0
|
1279 * DOM object for the row being considered for swapping.
|
Chris@0
|
1280 *
|
Chris@0
|
1281 * @return {bool}
|
Chris@0
|
1282 * Whether the swap is a valid swap or not.
|
Chris@0
|
1283 */
|
Chris@0
|
1284 Drupal.tableDrag.prototype.row.prototype.isValidSwap = function (row) {
|
Chris@0
|
1285 const $row = $(row);
|
Chris@0
|
1286 if (this.indentEnabled) {
|
Chris@0
|
1287 let prevRow;
|
Chris@0
|
1288 let nextRow;
|
Chris@0
|
1289 if (this.direction === 'down') {
|
Chris@0
|
1290 prevRow = row;
|
Chris@0
|
1291 nextRow = $row.next('tr').get(0);
|
Chris@0
|
1292 }
|
Chris@0
|
1293 else {
|
Chris@0
|
1294 prevRow = $row.prev('tr').get(0);
|
Chris@0
|
1295 nextRow = row;
|
Chris@0
|
1296 }
|
Chris@0
|
1297 this.interval = this.validIndentInterval(prevRow, nextRow);
|
Chris@0
|
1298
|
Chris@0
|
1299 // We have an invalid swap if the valid indentations interval is empty.
|
Chris@0
|
1300 if (this.interval.min > this.interval.max) {
|
Chris@0
|
1301 return false;
|
Chris@0
|
1302 }
|
Chris@0
|
1303 }
|
Chris@0
|
1304
|
Chris@0
|
1305 // Do not let an un-draggable first row have anything put before it.
|
Chris@0
|
1306 if (this.table.tBodies[0].rows[0] === row && $row.is(':not(.draggable)')) {
|
Chris@0
|
1307 return false;
|
Chris@0
|
1308 }
|
Chris@0
|
1309
|
Chris@0
|
1310 return true;
|
Chris@0
|
1311 };
|
Chris@0
|
1312
|
Chris@0
|
1313 /**
|
Chris@0
|
1314 * Perform the swap between two rows.
|
Chris@0
|
1315 *
|
Chris@0
|
1316 * @param {string} position
|
Chris@0
|
1317 * Whether the swap will occur 'before' or 'after' the given row.
|
Chris@0
|
1318 * @param {HTMLElement} row
|
Chris@0
|
1319 * DOM element what will be swapped with the row group.
|
Chris@0
|
1320 */
|
Chris@0
|
1321 Drupal.tableDrag.prototype.row.prototype.swap = function (position, row) {
|
Chris@0
|
1322 // Makes sure only DOM object are passed to Drupal.detachBehaviors().
|
Chris@0
|
1323 this.group.forEach((row) => {
|
Chris@0
|
1324 Drupal.detachBehaviors(row, drupalSettings, 'move');
|
Chris@0
|
1325 });
|
Chris@0
|
1326 $(row)[position](this.group);
|
Chris@0
|
1327 // Makes sure only DOM object are passed to Drupal.attachBehaviors()s.
|
Chris@0
|
1328 this.group.forEach((row) => {
|
Chris@0
|
1329 Drupal.attachBehaviors(row, drupalSettings);
|
Chris@0
|
1330 });
|
Chris@0
|
1331 this.changed = true;
|
Chris@0
|
1332 this.onSwap(row);
|
Chris@0
|
1333 };
|
Chris@0
|
1334
|
Chris@0
|
1335 /**
|
Chris@0
|
1336 * Determine the valid indentations interval for the row at a given position.
|
Chris@0
|
1337 *
|
Chris@0
|
1338 * @param {?HTMLElement} prevRow
|
Chris@0
|
1339 * DOM object for the row before the tested position
|
Chris@0
|
1340 * (or null for first position in the table).
|
Chris@0
|
1341 * @param {?HTMLElement} nextRow
|
Chris@0
|
1342 * DOM object for the row after the tested position
|
Chris@0
|
1343 * (or null for last position in the table).
|
Chris@0
|
1344 *
|
Chris@0
|
1345 * @return {object}
|
Chris@0
|
1346 * An object with the keys `min` and `max` to indicate the valid indent
|
Chris@0
|
1347 * interval.
|
Chris@0
|
1348 */
|
Chris@0
|
1349 Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow, nextRow) {
|
Chris@0
|
1350 const $prevRow = $(prevRow);
|
Chris@0
|
1351 let maxIndent;
|
Chris@0
|
1352
|
Chris@0
|
1353 // Minimum indentation:
|
Chris@0
|
1354 // Do not orphan the next row.
|
Chris@0
|
1355 const minIndent = nextRow ? $(nextRow).find('.js-indentation').length : 0;
|
Chris@0
|
1356
|
Chris@0
|
1357 // Maximum indentation:
|
Chris@0
|
1358 if (!prevRow || $prevRow.is(':not(.draggable)') || $(this.element).is('.tabledrag-root')) {
|
Chris@0
|
1359 // Do not indent:
|
Chris@0
|
1360 // - the first row in the table,
|
Chris@0
|
1361 // - rows dragged below a non-draggable row,
|
Chris@0
|
1362 // - 'root' rows.
|
Chris@0
|
1363 maxIndent = 0;
|
Chris@0
|
1364 }
|
Chris@0
|
1365 else {
|
Chris@0
|
1366 // Do not go deeper than as a child of the previous row.
|
Chris@0
|
1367 maxIndent = $prevRow.find('.js-indentation').length + ($prevRow.is('.tabledrag-leaf') ? 0 : 1);
|
Chris@0
|
1368 // Limit by the maximum allowed depth for the table.
|
Chris@0
|
1369 if (this.maxDepth) {
|
Chris@0
|
1370 maxIndent = Math.min(maxIndent, this.maxDepth - (this.groupDepth - this.indents));
|
Chris@0
|
1371 }
|
Chris@0
|
1372 }
|
Chris@0
|
1373
|
Chris@0
|
1374 return { min: minIndent, max: maxIndent };
|
Chris@0
|
1375 };
|
Chris@0
|
1376
|
Chris@0
|
1377 /**
|
Chris@0
|
1378 * Indent a row within the legal bounds of the table.
|
Chris@0
|
1379 *
|
Chris@0
|
1380 * @param {number} indentDiff
|
Chris@0
|
1381 * The number of additional indentations proposed for the row (can be
|
Chris@0
|
1382 * positive or negative). This number will be adjusted to nearest valid
|
Chris@0
|
1383 * indentation level for the row.
|
Chris@0
|
1384 *
|
Chris@0
|
1385 * @return {number}
|
Chris@0
|
1386 * The number of indentations applied.
|
Chris@0
|
1387 */
|
Chris@0
|
1388 Drupal.tableDrag.prototype.row.prototype.indent = function (indentDiff) {
|
Chris@0
|
1389 const $group = $(this.group);
|
Chris@0
|
1390 // Determine the valid indentations interval if not available yet.
|
Chris@0
|
1391 if (!this.interval) {
|
Chris@0
|
1392 const prevRow = $(this.element).prev('tr').get(0);
|
Chris@0
|
1393 const nextRow = $group.eq(-1).next('tr').get(0);
|
Chris@0
|
1394 this.interval = this.validIndentInterval(prevRow, nextRow);
|
Chris@0
|
1395 }
|
Chris@0
|
1396
|
Chris@0
|
1397 // Adjust to the nearest valid indentation.
|
Chris@0
|
1398 let indent = this.indents + indentDiff;
|
Chris@0
|
1399 indent = Math.max(indent, this.interval.min);
|
Chris@0
|
1400 indent = Math.min(indent, this.interval.max);
|
Chris@0
|
1401 indentDiff = indent - this.indents;
|
Chris@0
|
1402
|
Chris@0
|
1403 for (let n = 1; n <= Math.abs(indentDiff); n++) {
|
Chris@0
|
1404 // Add or remove indentations.
|
Chris@0
|
1405 if (indentDiff < 0) {
|
Chris@0
|
1406 $group.find('.js-indentation:first-of-type').remove();
|
Chris@0
|
1407 this.indents--;
|
Chris@0
|
1408 }
|
Chris@0
|
1409 else {
|
Chris@0
|
1410 $group.find('td:first-of-type').prepend(Drupal.theme('tableDragIndentation'));
|
Chris@0
|
1411 this.indents++;
|
Chris@0
|
1412 }
|
Chris@0
|
1413 }
|
Chris@0
|
1414 if (indentDiff) {
|
Chris@0
|
1415 // Update indentation for this row.
|
Chris@0
|
1416 this.changed = true;
|
Chris@0
|
1417 this.groupDepth += indentDiff;
|
Chris@0
|
1418 this.onIndent();
|
Chris@0
|
1419 }
|
Chris@0
|
1420
|
Chris@0
|
1421 return indentDiff;
|
Chris@0
|
1422 };
|
Chris@0
|
1423
|
Chris@0
|
1424 /**
|
Chris@0
|
1425 * Find all siblings for a row.
|
Chris@0
|
1426 *
|
Chris@0
|
1427 * According to its subgroup or indentation. Note that the passed-in row is
|
Chris@0
|
1428 * included in the list of siblings.
|
Chris@0
|
1429 *
|
Chris@0
|
1430 * @param {object} rowSettings
|
Chris@0
|
1431 * The field settings we're using to identify what constitutes a sibling.
|
Chris@0
|
1432 *
|
Chris@0
|
1433 * @return {Array}
|
Chris@0
|
1434 * An array of siblings.
|
Chris@0
|
1435 */
|
Chris@0
|
1436 Drupal.tableDrag.prototype.row.prototype.findSiblings = function (rowSettings) {
|
Chris@0
|
1437 const siblings = [];
|
Chris@0
|
1438 const directions = ['prev', 'next'];
|
Chris@0
|
1439 const rowIndentation = this.indents;
|
Chris@0
|
1440 let checkRowIndentation;
|
Chris@0
|
1441 for (let d = 0; d < directions.length; d++) {
|
Chris@0
|
1442 let checkRow = $(this.element)[directions[d]]();
|
Chris@0
|
1443 while (checkRow.length) {
|
Chris@0
|
1444 // Check that the sibling contains a similar target field.
|
Chris@0
|
1445 if (checkRow.find(`.${rowSettings.target}`)) {
|
Chris@0
|
1446 // Either add immediately if this is a flat table, or check to ensure
|
Chris@0
|
1447 // that this row has the same level of indentation.
|
Chris@0
|
1448 if (this.indentEnabled) {
|
Chris@0
|
1449 checkRowIndentation = checkRow.find('.js-indentation').length;
|
Chris@0
|
1450 }
|
Chris@0
|
1451
|
Chris@0
|
1452 if (!(this.indentEnabled) || (checkRowIndentation === rowIndentation)) {
|
Chris@0
|
1453 siblings.push(checkRow[0]);
|
Chris@0
|
1454 }
|
Chris@0
|
1455 else if (checkRowIndentation < rowIndentation) {
|
Chris@0
|
1456 // No need to keep looking for siblings when we get to a parent.
|
Chris@0
|
1457 break;
|
Chris@0
|
1458 }
|
Chris@0
|
1459 }
|
Chris@0
|
1460 else {
|
Chris@0
|
1461 break;
|
Chris@0
|
1462 }
|
Chris@0
|
1463 checkRow = checkRow[directions[d]]();
|
Chris@0
|
1464 }
|
Chris@0
|
1465 // Since siblings are added in reverse order for previous, reverse the
|
Chris@0
|
1466 // completed list of previous siblings. Add the current row and continue.
|
Chris@0
|
1467 if (directions[d] === 'prev') {
|
Chris@0
|
1468 siblings.reverse();
|
Chris@0
|
1469 siblings.push(this.element);
|
Chris@0
|
1470 }
|
Chris@0
|
1471 }
|
Chris@0
|
1472 return siblings;
|
Chris@0
|
1473 };
|
Chris@0
|
1474
|
Chris@0
|
1475 /**
|
Chris@0
|
1476 * Remove indentation helper classes from the current row group.
|
Chris@0
|
1477 */
|
Chris@0
|
1478 Drupal.tableDrag.prototype.row.prototype.removeIndentClasses = function () {
|
Chris@0
|
1479 Object.keys(this.children || {}).forEach((n) => {
|
Chris@0
|
1480 $(this.children[n]).find('.js-indentation')
|
Chris@0
|
1481 .removeClass('tree-child')
|
Chris@0
|
1482 .removeClass('tree-child-first')
|
Chris@0
|
1483 .removeClass('tree-child-last')
|
Chris@0
|
1484 .removeClass('tree-child-horizontal');
|
Chris@0
|
1485 });
|
Chris@0
|
1486 };
|
Chris@0
|
1487
|
Chris@0
|
1488 /**
|
Chris@0
|
1489 * Add an asterisk or other marker to the changed row.
|
Chris@0
|
1490 */
|
Chris@0
|
1491 Drupal.tableDrag.prototype.row.prototype.markChanged = function () {
|
Chris@0
|
1492 const marker = Drupal.theme('tableDragChangedMarker');
|
Chris@0
|
1493 const cell = $(this.element).find('td:first-of-type');
|
Chris@0
|
1494 if (cell.find('abbr.tabledrag-changed').length === 0) {
|
Chris@0
|
1495 cell.append(marker);
|
Chris@0
|
1496 }
|
Chris@0
|
1497 };
|
Chris@0
|
1498
|
Chris@0
|
1499 /**
|
Chris@0
|
1500 * Stub function. Allows a custom handler when a row is indented.
|
Chris@0
|
1501 *
|
Chris@0
|
1502 * @return {null}
|
Chris@0
|
1503 * Returns null when the stub function is used.
|
Chris@0
|
1504 */
|
Chris@0
|
1505 Drupal.tableDrag.prototype.row.prototype.onIndent = function () {
|
Chris@0
|
1506 return null;
|
Chris@0
|
1507 };
|
Chris@0
|
1508
|
Chris@0
|
1509 /**
|
Chris@0
|
1510 * Stub function. Allows a custom handler when a row is swapped.
|
Chris@0
|
1511 *
|
Chris@0
|
1512 * @param {HTMLElement} swappedRow
|
Chris@0
|
1513 * The element for the swapped row.
|
Chris@0
|
1514 *
|
Chris@0
|
1515 * @return {null}
|
Chris@0
|
1516 * Returns null when the stub function is used.
|
Chris@0
|
1517 */
|
Chris@0
|
1518 Drupal.tableDrag.prototype.row.prototype.onSwap = function (swappedRow) {
|
Chris@0
|
1519 return null;
|
Chris@0
|
1520 };
|
Chris@0
|
1521
|
Chris@0
|
1522 $.extend(Drupal.theme, /** @lends Drupal.theme */{
|
Chris@0
|
1523
|
Chris@0
|
1524 /**
|
Chris@0
|
1525 * @return {string}
|
Chris@0
|
1526 * Markup for the marker.
|
Chris@0
|
1527 */
|
Chris@0
|
1528 tableDragChangedMarker() {
|
Chris@0
|
1529 return `<abbr class="warning tabledrag-changed" title="${Drupal.t('Changed')}">*</abbr>`;
|
Chris@0
|
1530 },
|
Chris@0
|
1531
|
Chris@0
|
1532 /**
|
Chris@0
|
1533 * @return {string}
|
Chris@0
|
1534 * Markup for the indentation.
|
Chris@0
|
1535 */
|
Chris@0
|
1536 tableDragIndentation() {
|
Chris@0
|
1537 return '<div class="js-indentation indentation"> </div>';
|
Chris@0
|
1538 },
|
Chris@0
|
1539
|
Chris@0
|
1540 /**
|
Chris@0
|
1541 * @return {string}
|
Chris@0
|
1542 * Markup for the warning.
|
Chris@0
|
1543 */
|
Chris@0
|
1544 tableDragChangedWarning() {
|
Chris@0
|
1545 return `<div class="tabledrag-changed-warning messages messages--warning" role="alert">${Drupal.theme('tableDragChangedMarker')} ${Drupal.t('You have unsaved changes.')}</div>`;
|
Chris@0
|
1546 },
|
Chris@0
|
1547 });
|
Chris@0
|
1548 }(jQuery, Drupal, drupalSettings));
|