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