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