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