danielebarchiesi@0
|
1 (function ($) {
|
danielebarchiesi@0
|
2
|
danielebarchiesi@0
|
3 /**
|
danielebarchiesi@0
|
4 * Drag and drop table rows with field manipulation.
|
danielebarchiesi@0
|
5 *
|
danielebarchiesi@0
|
6 * Using the drupal_add_tabledrag() function, any table with weights or parent
|
danielebarchiesi@0
|
7 * relationships may be made into draggable tables. Columns containing a field
|
danielebarchiesi@0
|
8 * may optionally be hidden, providing a better user experience.
|
danielebarchiesi@0
|
9 *
|
danielebarchiesi@0
|
10 * Created tableDrag instances may be modified with custom behaviors by
|
danielebarchiesi@0
|
11 * overriding the .onDrag, .onDrop, .row.onSwap, and .row.onIndent methods.
|
danielebarchiesi@0
|
12 * See blocks.js for an example of adding additional functionality to tableDrag.
|
danielebarchiesi@0
|
13 */
|
danielebarchiesi@0
|
14 Drupal.behaviors.tableDrag = {
|
danielebarchiesi@0
|
15 attach: function (context, settings) {
|
danielebarchiesi@0
|
16 for (var base in settings.tableDrag) {
|
danielebarchiesi@0
|
17 $('#' + base, context).once('tabledrag', function () {
|
danielebarchiesi@0
|
18 // Create the new tableDrag instance. Save in the Drupal variable
|
danielebarchiesi@0
|
19 // to allow other scripts access to the object.
|
danielebarchiesi@0
|
20 Drupal.tableDrag[base] = new Drupal.tableDrag(this, settings.tableDrag[base]);
|
danielebarchiesi@0
|
21 });
|
danielebarchiesi@0
|
22 }
|
danielebarchiesi@0
|
23 }
|
danielebarchiesi@0
|
24 };
|
danielebarchiesi@0
|
25
|
danielebarchiesi@0
|
26 /**
|
danielebarchiesi@0
|
27 * Constructor for the tableDrag object. Provides table and field manipulation.
|
danielebarchiesi@0
|
28 *
|
danielebarchiesi@0
|
29 * @param table
|
danielebarchiesi@0
|
30 * DOM object for the table to be made draggable.
|
danielebarchiesi@0
|
31 * @param tableSettings
|
danielebarchiesi@0
|
32 * Settings for the table added via drupal_add_dragtable().
|
danielebarchiesi@0
|
33 */
|
danielebarchiesi@0
|
34 Drupal.tableDrag = function (table, tableSettings) {
|
danielebarchiesi@0
|
35 var self = this;
|
danielebarchiesi@0
|
36
|
danielebarchiesi@0
|
37 // Required object variables.
|
danielebarchiesi@0
|
38 this.table = table;
|
danielebarchiesi@0
|
39 this.tableSettings = tableSettings;
|
danielebarchiesi@0
|
40 this.dragObject = null; // Used to hold information about a current drag operation.
|
danielebarchiesi@0
|
41 this.rowObject = null; // Provides operations for row manipulation.
|
danielebarchiesi@0
|
42 this.oldRowElement = null; // Remember the previous element.
|
danielebarchiesi@0
|
43 this.oldY = 0; // Used to determine up or down direction from last mouse move.
|
danielebarchiesi@0
|
44 this.changed = false; // Whether anything in the entire table has changed.
|
danielebarchiesi@0
|
45 this.maxDepth = 0; // Maximum amount of allowed parenting.
|
danielebarchiesi@0
|
46 this.rtl = $(this.table).css('direction') == 'rtl' ? -1 : 1; // Direction of the table.
|
danielebarchiesi@0
|
47
|
danielebarchiesi@0
|
48 // Configure the scroll settings.
|
danielebarchiesi@0
|
49 this.scrollSettings = { amount: 4, interval: 50, trigger: 70 };
|
danielebarchiesi@0
|
50 this.scrollInterval = null;
|
danielebarchiesi@0
|
51 this.scrollY = 0;
|
danielebarchiesi@0
|
52 this.windowHeight = 0;
|
danielebarchiesi@0
|
53
|
danielebarchiesi@0
|
54 // Check this table's settings to see if there are parent relationships in
|
danielebarchiesi@0
|
55 // this table. For efficiency, large sections of code can be skipped if we
|
danielebarchiesi@0
|
56 // don't need to track horizontal movement and indentations.
|
danielebarchiesi@0
|
57 this.indentEnabled = false;
|
danielebarchiesi@0
|
58 for (var group in tableSettings) {
|
danielebarchiesi@0
|
59 for (var n in tableSettings[group]) {
|
danielebarchiesi@0
|
60 if (tableSettings[group][n].relationship == 'parent') {
|
danielebarchiesi@0
|
61 this.indentEnabled = true;
|
danielebarchiesi@0
|
62 }
|
danielebarchiesi@0
|
63 if (tableSettings[group][n].limit > 0) {
|
danielebarchiesi@0
|
64 this.maxDepth = tableSettings[group][n].limit;
|
danielebarchiesi@0
|
65 }
|
danielebarchiesi@0
|
66 }
|
danielebarchiesi@0
|
67 }
|
danielebarchiesi@0
|
68 if (this.indentEnabled) {
|
danielebarchiesi@0
|
69 this.indentCount = 1; // Total width of indents, set in makeDraggable.
|
danielebarchiesi@0
|
70 // Find the width of indentations to measure mouse movements against.
|
danielebarchiesi@0
|
71 // Because the table doesn't need to start with any indentations, we
|
danielebarchiesi@0
|
72 // manually append 2 indentations in the first draggable row, measure
|
danielebarchiesi@0
|
73 // the offset, then remove.
|
danielebarchiesi@0
|
74 var indent = Drupal.theme('tableDragIndentation');
|
danielebarchiesi@0
|
75 var testRow = $('<tr/>').addClass('draggable').appendTo(table);
|
danielebarchiesi@0
|
76 var testCell = $('<td/>').appendTo(testRow).prepend(indent).prepend(indent);
|
danielebarchiesi@0
|
77 this.indentAmount = $('.indentation', testCell).get(1).offsetLeft - $('.indentation', testCell).get(0).offsetLeft;
|
danielebarchiesi@0
|
78 testRow.remove();
|
danielebarchiesi@0
|
79 }
|
danielebarchiesi@0
|
80
|
danielebarchiesi@0
|
81 // Make each applicable row draggable.
|
danielebarchiesi@0
|
82 // Match immediate children of the parent element to allow nesting.
|
danielebarchiesi@0
|
83 $('> tr.draggable, > tbody > tr.draggable', table).each(function () { self.makeDraggable(this); });
|
danielebarchiesi@0
|
84
|
danielebarchiesi@0
|
85 // Add a link before the table for users to show or hide weight columns.
|
danielebarchiesi@0
|
86 $(table).before($('<a href="#" class="tabledrag-toggle-weight"></a>')
|
danielebarchiesi@0
|
87 .attr('title', Drupal.t('Re-order rows by numerical weight instead of dragging.'))
|
danielebarchiesi@0
|
88 .click(function () {
|
danielebarchiesi@0
|
89 if ($.cookie('Drupal.tableDrag.showWeight') == 1) {
|
danielebarchiesi@0
|
90 self.hideColumns();
|
danielebarchiesi@0
|
91 }
|
danielebarchiesi@0
|
92 else {
|
danielebarchiesi@0
|
93 self.showColumns();
|
danielebarchiesi@0
|
94 }
|
danielebarchiesi@0
|
95 return false;
|
danielebarchiesi@0
|
96 })
|
danielebarchiesi@0
|
97 .wrap('<div class="tabledrag-toggle-weight-wrapper"></div>')
|
danielebarchiesi@0
|
98 .parent()
|
danielebarchiesi@0
|
99 );
|
danielebarchiesi@0
|
100
|
danielebarchiesi@0
|
101 // Initialize the specified columns (for example, weight or parent columns)
|
danielebarchiesi@0
|
102 // to show or hide according to user preference. This aids accessibility
|
danielebarchiesi@0
|
103 // so that, e.g., screen reader users can choose to enter weight values and
|
danielebarchiesi@0
|
104 // manipulate form elements directly, rather than using drag-and-drop..
|
danielebarchiesi@0
|
105 self.initColumns();
|
danielebarchiesi@0
|
106
|
danielebarchiesi@0
|
107 // Add mouse bindings to the document. The self variable is passed along
|
danielebarchiesi@0
|
108 // as event handlers do not have direct access to the tableDrag object.
|
danielebarchiesi@0
|
109 $(document).bind('mousemove', function (event) { return self.dragRow(event, self); });
|
danielebarchiesi@0
|
110 $(document).bind('mouseup', function (event) { return self.dropRow(event, self); });
|
danielebarchiesi@0
|
111 };
|
danielebarchiesi@0
|
112
|
danielebarchiesi@0
|
113 /**
|
danielebarchiesi@0
|
114 * Initialize columns containing form elements to be hidden by default,
|
danielebarchiesi@0
|
115 * according to the settings for this tableDrag instance.
|
danielebarchiesi@0
|
116 *
|
danielebarchiesi@0
|
117 * Identify and mark each cell with a CSS class so we can easily toggle
|
danielebarchiesi@0
|
118 * show/hide it. Finally, hide columns if user does not have a
|
danielebarchiesi@0
|
119 * 'Drupal.tableDrag.showWeight' cookie.
|
danielebarchiesi@0
|
120 */
|
danielebarchiesi@0
|
121 Drupal.tableDrag.prototype.initColumns = function () {
|
danielebarchiesi@0
|
122 for (var group in this.tableSettings) {
|
danielebarchiesi@0
|
123 // Find the first field in this group.
|
danielebarchiesi@0
|
124 for (var d in this.tableSettings[group]) {
|
danielebarchiesi@0
|
125 var field = $('.' + this.tableSettings[group][d].target + ':first', this.table);
|
danielebarchiesi@0
|
126 if (field.length && this.tableSettings[group][d].hidden) {
|
danielebarchiesi@0
|
127 var hidden = this.tableSettings[group][d].hidden;
|
danielebarchiesi@0
|
128 var cell = field.closest('td');
|
danielebarchiesi@0
|
129 break;
|
danielebarchiesi@0
|
130 }
|
danielebarchiesi@0
|
131 }
|
danielebarchiesi@0
|
132
|
danielebarchiesi@0
|
133 // Mark the column containing this field so it can be hidden.
|
danielebarchiesi@0
|
134 if (hidden && cell[0]) {
|
danielebarchiesi@0
|
135 // Add 1 to our indexes. The nth-child selector is 1 based, not 0 based.
|
danielebarchiesi@0
|
136 // Match immediate children of the parent element to allow nesting.
|
danielebarchiesi@0
|
137 var columnIndex = $('> td', cell.parent()).index(cell.get(0)) + 1;
|
danielebarchiesi@0
|
138 $('> thead > tr, > tbody > tr, > tr', this.table).each(function () {
|
danielebarchiesi@0
|
139 // Get the columnIndex and adjust for any colspans in this row.
|
danielebarchiesi@0
|
140 var index = columnIndex;
|
danielebarchiesi@0
|
141 var cells = $(this).children();
|
danielebarchiesi@0
|
142 cells.each(function (n) {
|
danielebarchiesi@0
|
143 if (n < index && this.colSpan && this.colSpan > 1) {
|
danielebarchiesi@0
|
144 index -= this.colSpan - 1;
|
danielebarchiesi@0
|
145 }
|
danielebarchiesi@0
|
146 });
|
danielebarchiesi@0
|
147 if (index > 0) {
|
danielebarchiesi@0
|
148 cell = cells.filter(':nth-child(' + index + ')');
|
danielebarchiesi@0
|
149 if (cell[0].colSpan && cell[0].colSpan > 1) {
|
danielebarchiesi@0
|
150 // If this cell has a colspan, mark it so we can reduce the colspan.
|
danielebarchiesi@0
|
151 cell.addClass('tabledrag-has-colspan');
|
danielebarchiesi@0
|
152 }
|
danielebarchiesi@0
|
153 else {
|
danielebarchiesi@0
|
154 // Mark this cell so we can hide it.
|
danielebarchiesi@0
|
155 cell.addClass('tabledrag-hide');
|
danielebarchiesi@0
|
156 }
|
danielebarchiesi@0
|
157 }
|
danielebarchiesi@0
|
158 });
|
danielebarchiesi@0
|
159 }
|
danielebarchiesi@0
|
160 }
|
danielebarchiesi@0
|
161
|
danielebarchiesi@0
|
162 // Now hide cells and reduce colspans unless cookie indicates previous choice.
|
danielebarchiesi@0
|
163 // Set a cookie if it is not already present.
|
danielebarchiesi@0
|
164 if ($.cookie('Drupal.tableDrag.showWeight') === null) {
|
danielebarchiesi@0
|
165 $.cookie('Drupal.tableDrag.showWeight', 0, {
|
danielebarchiesi@0
|
166 path: Drupal.settings.basePath,
|
danielebarchiesi@0
|
167 // The cookie expires in one year.
|
danielebarchiesi@0
|
168 expires: 365
|
danielebarchiesi@0
|
169 });
|
danielebarchiesi@0
|
170 this.hideColumns();
|
danielebarchiesi@0
|
171 }
|
danielebarchiesi@0
|
172 // Check cookie value and show/hide weight columns accordingly.
|
danielebarchiesi@0
|
173 else {
|
danielebarchiesi@0
|
174 if ($.cookie('Drupal.tableDrag.showWeight') == 1) {
|
danielebarchiesi@0
|
175 this.showColumns();
|
danielebarchiesi@0
|
176 }
|
danielebarchiesi@0
|
177 else {
|
danielebarchiesi@0
|
178 this.hideColumns();
|
danielebarchiesi@0
|
179 }
|
danielebarchiesi@0
|
180 }
|
danielebarchiesi@0
|
181 };
|
danielebarchiesi@0
|
182
|
danielebarchiesi@0
|
183 /**
|
danielebarchiesi@0
|
184 * Hide the columns containing weight/parent form elements.
|
danielebarchiesi@0
|
185 * Undo showColumns().
|
danielebarchiesi@0
|
186 */
|
danielebarchiesi@0
|
187 Drupal.tableDrag.prototype.hideColumns = function () {
|
danielebarchiesi@0
|
188 // Hide weight/parent cells and headers.
|
danielebarchiesi@0
|
189 $('.tabledrag-hide', 'table.tabledrag-processed').css('display', 'none');
|
danielebarchiesi@0
|
190 // Show TableDrag handles.
|
danielebarchiesi@0
|
191 $('.tabledrag-handle', 'table.tabledrag-processed').css('display', '');
|
danielebarchiesi@0
|
192 // Reduce the colspan of any effected multi-span columns.
|
danielebarchiesi@0
|
193 $('.tabledrag-has-colspan', 'table.tabledrag-processed').each(function () {
|
danielebarchiesi@0
|
194 this.colSpan = this.colSpan - 1;
|
danielebarchiesi@0
|
195 });
|
danielebarchiesi@0
|
196 // Change link text.
|
danielebarchiesi@0
|
197 $('.tabledrag-toggle-weight').text(Drupal.t('Show row weights'));
|
danielebarchiesi@0
|
198 // Change cookie.
|
danielebarchiesi@0
|
199 $.cookie('Drupal.tableDrag.showWeight', 0, {
|
danielebarchiesi@0
|
200 path: Drupal.settings.basePath,
|
danielebarchiesi@0
|
201 // The cookie expires in one year.
|
danielebarchiesi@0
|
202 expires: 365
|
danielebarchiesi@0
|
203 });
|
danielebarchiesi@0
|
204 // Trigger an event to allow other scripts to react to this display change.
|
danielebarchiesi@0
|
205 $('table.tabledrag-processed').trigger('columnschange', 'hide');
|
danielebarchiesi@0
|
206 };
|
danielebarchiesi@0
|
207
|
danielebarchiesi@0
|
208 /**
|
danielebarchiesi@0
|
209 * Show the columns containing weight/parent form elements
|
danielebarchiesi@0
|
210 * Undo hideColumns().
|
danielebarchiesi@0
|
211 */
|
danielebarchiesi@0
|
212 Drupal.tableDrag.prototype.showColumns = function () {
|
danielebarchiesi@0
|
213 // Show weight/parent cells and headers.
|
danielebarchiesi@0
|
214 $('.tabledrag-hide', 'table.tabledrag-processed').css('display', '');
|
danielebarchiesi@0
|
215 // Hide TableDrag handles.
|
danielebarchiesi@0
|
216 $('.tabledrag-handle', 'table.tabledrag-processed').css('display', 'none');
|
danielebarchiesi@0
|
217 // Increase the colspan for any columns where it was previously reduced.
|
danielebarchiesi@0
|
218 $('.tabledrag-has-colspan', 'table.tabledrag-processed').each(function () {
|
danielebarchiesi@0
|
219 this.colSpan = this.colSpan + 1;
|
danielebarchiesi@0
|
220 });
|
danielebarchiesi@0
|
221 // Change link text.
|
danielebarchiesi@0
|
222 $('.tabledrag-toggle-weight').text(Drupal.t('Hide row weights'));
|
danielebarchiesi@0
|
223 // Change cookie.
|
danielebarchiesi@0
|
224 $.cookie('Drupal.tableDrag.showWeight', 1, {
|
danielebarchiesi@0
|
225 path: Drupal.settings.basePath,
|
danielebarchiesi@0
|
226 // The cookie expires in one year.
|
danielebarchiesi@0
|
227 expires: 365
|
danielebarchiesi@0
|
228 });
|
danielebarchiesi@0
|
229 // Trigger an event to allow other scripts to react to this display change.
|
danielebarchiesi@0
|
230 $('table.tabledrag-processed').trigger('columnschange', 'show');
|
danielebarchiesi@0
|
231 };
|
danielebarchiesi@0
|
232
|
danielebarchiesi@0
|
233 /**
|
danielebarchiesi@0
|
234 * Find the target used within a particular row and group.
|
danielebarchiesi@0
|
235 */
|
danielebarchiesi@0
|
236 Drupal.tableDrag.prototype.rowSettings = function (group, row) {
|
danielebarchiesi@0
|
237 var field = $('.' + group, row);
|
danielebarchiesi@0
|
238 for (var delta in this.tableSettings[group]) {
|
danielebarchiesi@0
|
239 var targetClass = this.tableSettings[group][delta].target;
|
danielebarchiesi@0
|
240 if (field.is('.' + targetClass)) {
|
danielebarchiesi@0
|
241 // Return a copy of the row settings.
|
danielebarchiesi@0
|
242 var rowSettings = {};
|
danielebarchiesi@0
|
243 for (var n in this.tableSettings[group][delta]) {
|
danielebarchiesi@0
|
244 rowSettings[n] = this.tableSettings[group][delta][n];
|
danielebarchiesi@0
|
245 }
|
danielebarchiesi@0
|
246 return rowSettings;
|
danielebarchiesi@0
|
247 }
|
danielebarchiesi@0
|
248 }
|
danielebarchiesi@0
|
249 };
|
danielebarchiesi@0
|
250
|
danielebarchiesi@0
|
251 /**
|
danielebarchiesi@0
|
252 * Take an item and add event handlers to make it become draggable.
|
danielebarchiesi@0
|
253 */
|
danielebarchiesi@0
|
254 Drupal.tableDrag.prototype.makeDraggable = function (item) {
|
danielebarchiesi@0
|
255 var self = this;
|
danielebarchiesi@0
|
256
|
danielebarchiesi@0
|
257 // Create the handle.
|
danielebarchiesi@0
|
258 var handle = $('<a href="#" class="tabledrag-handle"><div class="handle"> </div></a>').attr('title', Drupal.t('Drag to re-order'));
|
danielebarchiesi@0
|
259 // Insert the handle after indentations (if any).
|
danielebarchiesi@0
|
260 if ($('td:first .indentation:last', item).length) {
|
danielebarchiesi@0
|
261 $('td:first .indentation:last', item).after(handle);
|
danielebarchiesi@0
|
262 // Update the total width of indentation in this entire table.
|
danielebarchiesi@0
|
263 self.indentCount = Math.max($('.indentation', item).length, self.indentCount);
|
danielebarchiesi@0
|
264 }
|
danielebarchiesi@0
|
265 else {
|
danielebarchiesi@0
|
266 $('td:first', item).prepend(handle);
|
danielebarchiesi@0
|
267 }
|
danielebarchiesi@0
|
268
|
danielebarchiesi@0
|
269 // Add hover action for the handle.
|
danielebarchiesi@0
|
270 handle.hover(function () {
|
danielebarchiesi@0
|
271 self.dragObject == null ? $(this).addClass('tabledrag-handle-hover') : null;
|
danielebarchiesi@0
|
272 }, function () {
|
danielebarchiesi@0
|
273 self.dragObject == null ? $(this).removeClass('tabledrag-handle-hover') : null;
|
danielebarchiesi@0
|
274 });
|
danielebarchiesi@0
|
275
|
danielebarchiesi@0
|
276 // Add the mousedown action for the handle.
|
danielebarchiesi@0
|
277 handle.mousedown(function (event) {
|
danielebarchiesi@0
|
278 // Create a new dragObject recording the event information.
|
danielebarchiesi@0
|
279 self.dragObject = {};
|
danielebarchiesi@0
|
280 self.dragObject.initMouseOffset = self.getMouseOffset(item, event);
|
danielebarchiesi@0
|
281 self.dragObject.initMouseCoords = self.mouseCoords(event);
|
danielebarchiesi@0
|
282 if (self.indentEnabled) {
|
danielebarchiesi@0
|
283 self.dragObject.indentMousePos = self.dragObject.initMouseCoords;
|
danielebarchiesi@0
|
284 }
|
danielebarchiesi@0
|
285
|
danielebarchiesi@0
|
286 // If there's a lingering row object from the keyboard, remove its focus.
|
danielebarchiesi@0
|
287 if (self.rowObject) {
|
danielebarchiesi@0
|
288 $('a.tabledrag-handle', self.rowObject.element).blur();
|
danielebarchiesi@0
|
289 }
|
danielebarchiesi@0
|
290
|
danielebarchiesi@0
|
291 // Create a new rowObject for manipulation of this row.
|
danielebarchiesi@0
|
292 self.rowObject = new self.row(item, 'mouse', self.indentEnabled, self.maxDepth, true);
|
danielebarchiesi@0
|
293
|
danielebarchiesi@0
|
294 // Save the position of the table.
|
danielebarchiesi@0
|
295 self.table.topY = $(self.table).offset().top;
|
danielebarchiesi@0
|
296 self.table.bottomY = self.table.topY + self.table.offsetHeight;
|
danielebarchiesi@0
|
297
|
danielebarchiesi@0
|
298 // Add classes to the handle and row.
|
danielebarchiesi@0
|
299 $(this).addClass('tabledrag-handle-hover');
|
danielebarchiesi@0
|
300 $(item).addClass('drag');
|
danielebarchiesi@0
|
301
|
danielebarchiesi@0
|
302 // Set the document to use the move cursor during drag.
|
danielebarchiesi@0
|
303 $('body').addClass('drag');
|
danielebarchiesi@0
|
304 if (self.oldRowElement) {
|
danielebarchiesi@0
|
305 $(self.oldRowElement).removeClass('drag-previous');
|
danielebarchiesi@0
|
306 }
|
danielebarchiesi@0
|
307
|
danielebarchiesi@0
|
308 // Hack for IE6 that flickers uncontrollably if select lists are moved.
|
danielebarchiesi@0
|
309 if (navigator.userAgent.indexOf('MSIE 6.') != -1) {
|
danielebarchiesi@0
|
310 $('select', this.table).css('display', 'none');
|
danielebarchiesi@0
|
311 }
|
danielebarchiesi@0
|
312
|
danielebarchiesi@0
|
313 // Hack for Konqueror, prevent the blur handler from firing.
|
danielebarchiesi@0
|
314 // Konqueror always gives links focus, even after returning false on mousedown.
|
danielebarchiesi@0
|
315 self.safeBlur = false;
|
danielebarchiesi@0
|
316
|
danielebarchiesi@0
|
317 // Call optional placeholder function.
|
danielebarchiesi@0
|
318 self.onDrag();
|
danielebarchiesi@0
|
319 return false;
|
danielebarchiesi@0
|
320 });
|
danielebarchiesi@0
|
321
|
danielebarchiesi@0
|
322 // Prevent the anchor tag from jumping us to the top of the page.
|
danielebarchiesi@0
|
323 handle.click(function () {
|
danielebarchiesi@0
|
324 return false;
|
danielebarchiesi@0
|
325 });
|
danielebarchiesi@0
|
326
|
danielebarchiesi@0
|
327 // Similar to the hover event, add a class when the handle is focused.
|
danielebarchiesi@0
|
328 handle.focus(function () {
|
danielebarchiesi@0
|
329 $(this).addClass('tabledrag-handle-hover');
|
danielebarchiesi@0
|
330 self.safeBlur = true;
|
danielebarchiesi@0
|
331 });
|
danielebarchiesi@0
|
332
|
danielebarchiesi@0
|
333 // Remove the handle class on blur and fire the same function as a mouseup.
|
danielebarchiesi@0
|
334 handle.blur(function (event) {
|
danielebarchiesi@0
|
335 $(this).removeClass('tabledrag-handle-hover');
|
danielebarchiesi@0
|
336 if (self.rowObject && self.safeBlur) {
|
danielebarchiesi@0
|
337 self.dropRow(event, self);
|
danielebarchiesi@0
|
338 }
|
danielebarchiesi@0
|
339 });
|
danielebarchiesi@0
|
340
|
danielebarchiesi@0
|
341 // Add arrow-key support to the handle.
|
danielebarchiesi@0
|
342 handle.keydown(function (event) {
|
danielebarchiesi@0
|
343 // If a rowObject doesn't yet exist and this isn't the tab key.
|
danielebarchiesi@0
|
344 if (event.keyCode != 9 && !self.rowObject) {
|
danielebarchiesi@0
|
345 self.rowObject = new self.row(item, 'keyboard', self.indentEnabled, self.maxDepth, true);
|
danielebarchiesi@0
|
346 }
|
danielebarchiesi@0
|
347
|
danielebarchiesi@0
|
348 var keyChange = false;
|
danielebarchiesi@0
|
349 switch (event.keyCode) {
|
danielebarchiesi@0
|
350 case 37: // Left arrow.
|
danielebarchiesi@0
|
351 case 63234: // Safari left arrow.
|
danielebarchiesi@0
|
352 keyChange = true;
|
danielebarchiesi@0
|
353 self.rowObject.indent(-1 * self.rtl);
|
danielebarchiesi@0
|
354 break;
|
danielebarchiesi@0
|
355 case 38: // Up arrow.
|
danielebarchiesi@0
|
356 case 63232: // Safari up arrow.
|
danielebarchiesi@0
|
357 var previousRow = $(self.rowObject.element).prev('tr').get(0);
|
danielebarchiesi@0
|
358 while (previousRow && $(previousRow).is(':hidden')) {
|
danielebarchiesi@0
|
359 previousRow = $(previousRow).prev('tr').get(0);
|
danielebarchiesi@0
|
360 }
|
danielebarchiesi@0
|
361 if (previousRow) {
|
danielebarchiesi@0
|
362 self.safeBlur = false; // Do not allow the onBlur cleanup.
|
danielebarchiesi@0
|
363 self.rowObject.direction = 'up';
|
danielebarchiesi@0
|
364 keyChange = true;
|
danielebarchiesi@0
|
365
|
danielebarchiesi@0
|
366 if ($(item).is('.tabledrag-root')) {
|
danielebarchiesi@0
|
367 // Swap with the previous top-level row.
|
danielebarchiesi@0
|
368 var groupHeight = 0;
|
danielebarchiesi@0
|
369 while (previousRow && $('.indentation', previousRow).length) {
|
danielebarchiesi@0
|
370 previousRow = $(previousRow).prev('tr').get(0);
|
danielebarchiesi@0
|
371 groupHeight += $(previousRow).is(':hidden') ? 0 : previousRow.offsetHeight;
|
danielebarchiesi@0
|
372 }
|
danielebarchiesi@0
|
373 if (previousRow) {
|
danielebarchiesi@0
|
374 self.rowObject.swap('before', previousRow);
|
danielebarchiesi@0
|
375 // No need to check for indentation, 0 is the only valid one.
|
danielebarchiesi@0
|
376 window.scrollBy(0, -groupHeight);
|
danielebarchiesi@0
|
377 }
|
danielebarchiesi@0
|
378 }
|
danielebarchiesi@0
|
379 else if (self.table.tBodies[0].rows[0] != previousRow || $(previousRow).is('.draggable')) {
|
danielebarchiesi@0
|
380 // Swap with the previous row (unless previous row is the first one
|
danielebarchiesi@0
|
381 // and undraggable).
|
danielebarchiesi@0
|
382 self.rowObject.swap('before', previousRow);
|
danielebarchiesi@0
|
383 self.rowObject.interval = null;
|
danielebarchiesi@0
|
384 self.rowObject.indent(0);
|
danielebarchiesi@0
|
385 window.scrollBy(0, -parseInt(item.offsetHeight, 10));
|
danielebarchiesi@0
|
386 }
|
danielebarchiesi@0
|
387 handle.get(0).focus(); // Regain focus after the DOM manipulation.
|
danielebarchiesi@0
|
388 }
|
danielebarchiesi@0
|
389 break;
|
danielebarchiesi@0
|
390 case 39: // Right arrow.
|
danielebarchiesi@0
|
391 case 63235: // Safari right arrow.
|
danielebarchiesi@0
|
392 keyChange = true;
|
danielebarchiesi@0
|
393 self.rowObject.indent(1 * self.rtl);
|
danielebarchiesi@0
|
394 break;
|
danielebarchiesi@0
|
395 case 40: // Down arrow.
|
danielebarchiesi@0
|
396 case 63233: // Safari down arrow.
|
danielebarchiesi@0
|
397 var nextRow = $(self.rowObject.group).filter(':last').next('tr').get(0);
|
danielebarchiesi@0
|
398 while (nextRow && $(nextRow).is(':hidden')) {
|
danielebarchiesi@0
|
399 nextRow = $(nextRow).next('tr').get(0);
|
danielebarchiesi@0
|
400 }
|
danielebarchiesi@0
|
401 if (nextRow) {
|
danielebarchiesi@0
|
402 self.safeBlur = false; // Do not allow the onBlur cleanup.
|
danielebarchiesi@0
|
403 self.rowObject.direction = 'down';
|
danielebarchiesi@0
|
404 keyChange = true;
|
danielebarchiesi@0
|
405
|
danielebarchiesi@0
|
406 if ($(item).is('.tabledrag-root')) {
|
danielebarchiesi@0
|
407 // Swap with the next group (necessarily a top-level one).
|
danielebarchiesi@0
|
408 var groupHeight = 0;
|
danielebarchiesi@0
|
409 var nextGroup = new self.row(nextRow, 'keyboard', self.indentEnabled, self.maxDepth, false);
|
danielebarchiesi@0
|
410 if (nextGroup) {
|
danielebarchiesi@0
|
411 $(nextGroup.group).each(function () {
|
danielebarchiesi@0
|
412 groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight;
|
danielebarchiesi@0
|
413 });
|
danielebarchiesi@0
|
414 var nextGroupRow = $(nextGroup.group).filter(':last').get(0);
|
danielebarchiesi@0
|
415 self.rowObject.swap('after', nextGroupRow);
|
danielebarchiesi@0
|
416 // No need to check for indentation, 0 is the only valid one.
|
danielebarchiesi@0
|
417 window.scrollBy(0, parseInt(groupHeight, 10));
|
danielebarchiesi@0
|
418 }
|
danielebarchiesi@0
|
419 }
|
danielebarchiesi@0
|
420 else {
|
danielebarchiesi@0
|
421 // Swap with the next row.
|
danielebarchiesi@0
|
422 self.rowObject.swap('after', nextRow);
|
danielebarchiesi@0
|
423 self.rowObject.interval = null;
|
danielebarchiesi@0
|
424 self.rowObject.indent(0);
|
danielebarchiesi@0
|
425 window.scrollBy(0, parseInt(item.offsetHeight, 10));
|
danielebarchiesi@0
|
426 }
|
danielebarchiesi@0
|
427 handle.get(0).focus(); // Regain focus after the DOM manipulation.
|
danielebarchiesi@0
|
428 }
|
danielebarchiesi@0
|
429 break;
|
danielebarchiesi@0
|
430 }
|
danielebarchiesi@0
|
431
|
danielebarchiesi@0
|
432 if (self.rowObject && self.rowObject.changed == true) {
|
danielebarchiesi@0
|
433 $(item).addClass('drag');
|
danielebarchiesi@0
|
434 if (self.oldRowElement) {
|
danielebarchiesi@0
|
435 $(self.oldRowElement).removeClass('drag-previous');
|
danielebarchiesi@0
|
436 }
|
danielebarchiesi@0
|
437 self.oldRowElement = item;
|
danielebarchiesi@0
|
438 self.restripeTable();
|
danielebarchiesi@0
|
439 self.onDrag();
|
danielebarchiesi@0
|
440 }
|
danielebarchiesi@0
|
441
|
danielebarchiesi@0
|
442 // Returning false if we have an arrow key to prevent scrolling.
|
danielebarchiesi@0
|
443 if (keyChange) {
|
danielebarchiesi@0
|
444 return false;
|
danielebarchiesi@0
|
445 }
|
danielebarchiesi@0
|
446 });
|
danielebarchiesi@0
|
447
|
danielebarchiesi@0
|
448 // Compatibility addition, return false on keypress to prevent unwanted scrolling.
|
danielebarchiesi@0
|
449 // IE and Safari will suppress scrolling on keydown, but all other browsers
|
danielebarchiesi@0
|
450 // need to return false on keypress. http://www.quirksmode.org/js/keys.html
|
danielebarchiesi@0
|
451 handle.keypress(function (event) {
|
danielebarchiesi@0
|
452 switch (event.keyCode) {
|
danielebarchiesi@0
|
453 case 37: // Left arrow.
|
danielebarchiesi@0
|
454 case 38: // Up arrow.
|
danielebarchiesi@0
|
455 case 39: // Right arrow.
|
danielebarchiesi@0
|
456 case 40: // Down arrow.
|
danielebarchiesi@0
|
457 return false;
|
danielebarchiesi@0
|
458 }
|
danielebarchiesi@0
|
459 });
|
danielebarchiesi@0
|
460 };
|
danielebarchiesi@0
|
461
|
danielebarchiesi@0
|
462 /**
|
danielebarchiesi@0
|
463 * Mousemove event handler, bound to document.
|
danielebarchiesi@0
|
464 */
|
danielebarchiesi@0
|
465 Drupal.tableDrag.prototype.dragRow = function (event, self) {
|
danielebarchiesi@0
|
466 if (self.dragObject) {
|
danielebarchiesi@0
|
467 self.currentMouseCoords = self.mouseCoords(event);
|
danielebarchiesi@0
|
468
|
danielebarchiesi@0
|
469 var y = self.currentMouseCoords.y - self.dragObject.initMouseOffset.y;
|
danielebarchiesi@0
|
470 var x = self.currentMouseCoords.x - self.dragObject.initMouseOffset.x;
|
danielebarchiesi@0
|
471
|
danielebarchiesi@0
|
472 // Check for row swapping and vertical scrolling.
|
danielebarchiesi@0
|
473 if (y != self.oldY) {
|
danielebarchiesi@0
|
474 self.rowObject.direction = y > self.oldY ? 'down' : 'up';
|
danielebarchiesi@0
|
475 self.oldY = y; // Update the old value.
|
danielebarchiesi@0
|
476
|
danielebarchiesi@0
|
477 // Check if the window should be scrolled (and how fast).
|
danielebarchiesi@0
|
478 var scrollAmount = self.checkScroll(self.currentMouseCoords.y);
|
danielebarchiesi@0
|
479 // Stop any current scrolling.
|
danielebarchiesi@0
|
480 clearInterval(self.scrollInterval);
|
danielebarchiesi@0
|
481 // Continue scrolling if the mouse has moved in the scroll direction.
|
danielebarchiesi@0
|
482 if (scrollAmount > 0 && self.rowObject.direction == 'down' || scrollAmount < 0 && self.rowObject.direction == 'up') {
|
danielebarchiesi@0
|
483 self.setScroll(scrollAmount);
|
danielebarchiesi@0
|
484 }
|
danielebarchiesi@0
|
485
|
danielebarchiesi@0
|
486 // If we have a valid target, perform the swap and restripe the table.
|
danielebarchiesi@0
|
487 var currentRow = self.findDropTargetRow(x, y);
|
danielebarchiesi@0
|
488 if (currentRow) {
|
danielebarchiesi@0
|
489 if (self.rowObject.direction == 'down') {
|
danielebarchiesi@0
|
490 self.rowObject.swap('after', currentRow, self);
|
danielebarchiesi@0
|
491 }
|
danielebarchiesi@0
|
492 else {
|
danielebarchiesi@0
|
493 self.rowObject.swap('before', currentRow, self);
|
danielebarchiesi@0
|
494 }
|
danielebarchiesi@0
|
495 self.restripeTable();
|
danielebarchiesi@0
|
496 }
|
danielebarchiesi@0
|
497 }
|
danielebarchiesi@0
|
498
|
danielebarchiesi@0
|
499 // Similar to row swapping, handle indentations.
|
danielebarchiesi@0
|
500 if (self.indentEnabled) {
|
danielebarchiesi@0
|
501 var xDiff = self.currentMouseCoords.x - self.dragObject.indentMousePos.x;
|
danielebarchiesi@0
|
502 // Set the number of indentations the mouse has been moved left or right.
|
danielebarchiesi@0
|
503 var indentDiff = Math.round(xDiff / self.indentAmount * self.rtl);
|
danielebarchiesi@0
|
504 // Indent the row with our estimated diff, which may be further
|
danielebarchiesi@0
|
505 // restricted according to the rows around this row.
|
danielebarchiesi@0
|
506 var indentChange = self.rowObject.indent(indentDiff);
|
danielebarchiesi@0
|
507 // Update table and mouse indentations.
|
danielebarchiesi@0
|
508 self.dragObject.indentMousePos.x += self.indentAmount * indentChange * self.rtl;
|
danielebarchiesi@0
|
509 self.indentCount = Math.max(self.indentCount, self.rowObject.indents);
|
danielebarchiesi@0
|
510 }
|
danielebarchiesi@0
|
511
|
danielebarchiesi@0
|
512 return false;
|
danielebarchiesi@0
|
513 }
|
danielebarchiesi@0
|
514 };
|
danielebarchiesi@0
|
515
|
danielebarchiesi@0
|
516 /**
|
danielebarchiesi@0
|
517 * Mouseup event handler, bound to document.
|
danielebarchiesi@0
|
518 * Blur event handler, bound to drag handle for keyboard support.
|
danielebarchiesi@0
|
519 */
|
danielebarchiesi@0
|
520 Drupal.tableDrag.prototype.dropRow = function (event, self) {
|
danielebarchiesi@0
|
521 // Drop row functionality shared between mouseup and blur events.
|
danielebarchiesi@0
|
522 if (self.rowObject != null) {
|
danielebarchiesi@0
|
523 var droppedRow = self.rowObject.element;
|
danielebarchiesi@0
|
524 // The row is already in the right place so we just release it.
|
danielebarchiesi@0
|
525 if (self.rowObject.changed == true) {
|
danielebarchiesi@0
|
526 // Update the fields in the dropped row.
|
danielebarchiesi@0
|
527 self.updateFields(droppedRow);
|
danielebarchiesi@0
|
528
|
danielebarchiesi@0
|
529 // If a setting exists for affecting the entire group, update all the
|
danielebarchiesi@0
|
530 // fields in the entire dragged group.
|
danielebarchiesi@0
|
531 for (var group in self.tableSettings) {
|
danielebarchiesi@0
|
532 var rowSettings = self.rowSettings(group, droppedRow);
|
danielebarchiesi@0
|
533 if (rowSettings.relationship == 'group') {
|
danielebarchiesi@0
|
534 for (var n in self.rowObject.children) {
|
danielebarchiesi@0
|
535 self.updateField(self.rowObject.children[n], group);
|
danielebarchiesi@0
|
536 }
|
danielebarchiesi@0
|
537 }
|
danielebarchiesi@0
|
538 }
|
danielebarchiesi@0
|
539
|
danielebarchiesi@0
|
540 self.rowObject.markChanged();
|
danielebarchiesi@0
|
541 if (self.changed == false) {
|
danielebarchiesi@0
|
542 $(Drupal.theme('tableDragChangedWarning')).insertBefore(self.table).hide().fadeIn('slow');
|
danielebarchiesi@0
|
543 self.changed = true;
|
danielebarchiesi@0
|
544 }
|
danielebarchiesi@0
|
545 }
|
danielebarchiesi@0
|
546
|
danielebarchiesi@0
|
547 if (self.indentEnabled) {
|
danielebarchiesi@0
|
548 self.rowObject.removeIndentClasses();
|
danielebarchiesi@0
|
549 }
|
danielebarchiesi@0
|
550 if (self.oldRowElement) {
|
danielebarchiesi@0
|
551 $(self.oldRowElement).removeClass('drag-previous');
|
danielebarchiesi@0
|
552 }
|
danielebarchiesi@0
|
553 $(droppedRow).removeClass('drag').addClass('drag-previous');
|
danielebarchiesi@0
|
554 self.oldRowElement = droppedRow;
|
danielebarchiesi@0
|
555 self.onDrop();
|
danielebarchiesi@0
|
556 self.rowObject = null;
|
danielebarchiesi@0
|
557 }
|
danielebarchiesi@0
|
558
|
danielebarchiesi@0
|
559 // Functionality specific only to mouseup event.
|
danielebarchiesi@0
|
560 if (self.dragObject != null) {
|
danielebarchiesi@0
|
561 $('.tabledrag-handle', droppedRow).removeClass('tabledrag-handle-hover');
|
danielebarchiesi@0
|
562
|
danielebarchiesi@0
|
563 self.dragObject = null;
|
danielebarchiesi@0
|
564 $('body').removeClass('drag');
|
danielebarchiesi@0
|
565 clearInterval(self.scrollInterval);
|
danielebarchiesi@0
|
566
|
danielebarchiesi@0
|
567 // Hack for IE6 that flickers uncontrollably if select lists are moved.
|
danielebarchiesi@0
|
568 if (navigator.userAgent.indexOf('MSIE 6.') != -1) {
|
danielebarchiesi@0
|
569 $('select', this.table).css('display', 'block');
|
danielebarchiesi@0
|
570 }
|
danielebarchiesi@0
|
571 }
|
danielebarchiesi@0
|
572 };
|
danielebarchiesi@0
|
573
|
danielebarchiesi@0
|
574 /**
|
danielebarchiesi@0
|
575 * Get the mouse coordinates from the event (allowing for browser differences).
|
danielebarchiesi@0
|
576 */
|
danielebarchiesi@0
|
577 Drupal.tableDrag.prototype.mouseCoords = function (event) {
|
danielebarchiesi@0
|
578 if (event.pageX || event.pageY) {
|
danielebarchiesi@0
|
579 return { x: event.pageX, y: event.pageY };
|
danielebarchiesi@0
|
580 }
|
danielebarchiesi@0
|
581 return {
|
danielebarchiesi@0
|
582 x: event.clientX + document.body.scrollLeft - document.body.clientLeft,
|
danielebarchiesi@0
|
583 y: event.clientY + document.body.scrollTop - document.body.clientTop
|
danielebarchiesi@0
|
584 };
|
danielebarchiesi@0
|
585 };
|
danielebarchiesi@0
|
586
|
danielebarchiesi@0
|
587 /**
|
danielebarchiesi@0
|
588 * Given a target element and a mouse event, get the mouse offset from that
|
danielebarchiesi@0
|
589 * element. To do this we need the element's position and the mouse position.
|
danielebarchiesi@0
|
590 */
|
danielebarchiesi@0
|
591 Drupal.tableDrag.prototype.getMouseOffset = function (target, event) {
|
danielebarchiesi@0
|
592 var docPos = $(target).offset();
|
danielebarchiesi@0
|
593 var mousePos = this.mouseCoords(event);
|
danielebarchiesi@0
|
594 return { x: mousePos.x - docPos.left, y: mousePos.y - docPos.top };
|
danielebarchiesi@0
|
595 };
|
danielebarchiesi@0
|
596
|
danielebarchiesi@0
|
597 /**
|
danielebarchiesi@0
|
598 * Find the row the mouse is currently over. This row is then taken and swapped
|
danielebarchiesi@0
|
599 * with the one being dragged.
|
danielebarchiesi@0
|
600 *
|
danielebarchiesi@0
|
601 * @param x
|
danielebarchiesi@0
|
602 * The x coordinate of the mouse on the page (not the screen).
|
danielebarchiesi@0
|
603 * @param y
|
danielebarchiesi@0
|
604 * The y coordinate of the mouse on the page (not the screen).
|
danielebarchiesi@0
|
605 */
|
danielebarchiesi@0
|
606 Drupal.tableDrag.prototype.findDropTargetRow = function (x, y) {
|
danielebarchiesi@0
|
607 var rows = $(this.table.tBodies[0].rows).not(':hidden');
|
danielebarchiesi@0
|
608 for (var n = 0; n < rows.length; n++) {
|
danielebarchiesi@0
|
609 var row = rows[n];
|
danielebarchiesi@0
|
610 var indentDiff = 0;
|
danielebarchiesi@0
|
611 var rowY = $(row).offset().top;
|
danielebarchiesi@0
|
612 // Because Safari does not report offsetHeight on table rows, but does on
|
danielebarchiesi@0
|
613 // table cells, grab the firstChild of the row and use that instead.
|
danielebarchiesi@0
|
614 // http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari.
|
danielebarchiesi@0
|
615 if (row.offsetHeight == 0) {
|
danielebarchiesi@0
|
616 var rowHeight = parseInt(row.firstChild.offsetHeight, 10) / 2;
|
danielebarchiesi@0
|
617 }
|
danielebarchiesi@0
|
618 // Other browsers.
|
danielebarchiesi@0
|
619 else {
|
danielebarchiesi@0
|
620 var rowHeight = parseInt(row.offsetHeight, 10) / 2;
|
danielebarchiesi@0
|
621 }
|
danielebarchiesi@0
|
622
|
danielebarchiesi@0
|
623 // Because we always insert before, we need to offset the height a bit.
|
danielebarchiesi@0
|
624 if ((y > (rowY - rowHeight)) && (y < (rowY + rowHeight))) {
|
danielebarchiesi@0
|
625 if (this.indentEnabled) {
|
danielebarchiesi@0
|
626 // Check that this row is not a child of the row being dragged.
|
danielebarchiesi@0
|
627 for (var n in this.rowObject.group) {
|
danielebarchiesi@0
|
628 if (this.rowObject.group[n] == row) {
|
danielebarchiesi@0
|
629 return null;
|
danielebarchiesi@0
|
630 }
|
danielebarchiesi@0
|
631 }
|
danielebarchiesi@0
|
632 }
|
danielebarchiesi@0
|
633 else {
|
danielebarchiesi@0
|
634 // Do not allow a row to be swapped with itself.
|
danielebarchiesi@0
|
635 if (row == this.rowObject.element) {
|
danielebarchiesi@0
|
636 return null;
|
danielebarchiesi@0
|
637 }
|
danielebarchiesi@0
|
638 }
|
danielebarchiesi@0
|
639
|
danielebarchiesi@0
|
640 // Check that swapping with this row is allowed.
|
danielebarchiesi@0
|
641 if (!this.rowObject.isValidSwap(row)) {
|
danielebarchiesi@0
|
642 return null;
|
danielebarchiesi@0
|
643 }
|
danielebarchiesi@0
|
644
|
danielebarchiesi@0
|
645 // We may have found the row the mouse just passed over, but it doesn't
|
danielebarchiesi@0
|
646 // take into account hidden rows. Skip backwards until we find a draggable
|
danielebarchiesi@0
|
647 // row.
|
danielebarchiesi@0
|
648 while ($(row).is(':hidden') && $(row).prev('tr').is(':hidden')) {
|
danielebarchiesi@0
|
649 row = $(row).prev('tr').get(0);
|
danielebarchiesi@0
|
650 }
|
danielebarchiesi@0
|
651 return row;
|
danielebarchiesi@0
|
652 }
|
danielebarchiesi@0
|
653 }
|
danielebarchiesi@0
|
654 return null;
|
danielebarchiesi@0
|
655 };
|
danielebarchiesi@0
|
656
|
danielebarchiesi@0
|
657 /**
|
danielebarchiesi@0
|
658 * After the row is dropped, update the table fields according to the settings
|
danielebarchiesi@0
|
659 * set for this table.
|
danielebarchiesi@0
|
660 *
|
danielebarchiesi@0
|
661 * @param changedRow
|
danielebarchiesi@0
|
662 * DOM object for the row that was just dropped.
|
danielebarchiesi@0
|
663 */
|
danielebarchiesi@0
|
664 Drupal.tableDrag.prototype.updateFields = function (changedRow) {
|
danielebarchiesi@0
|
665 for (var group in this.tableSettings) {
|
danielebarchiesi@0
|
666 // Each group may have a different setting for relationship, so we find
|
danielebarchiesi@0
|
667 // the source rows for each separately.
|
danielebarchiesi@0
|
668 this.updateField(changedRow, group);
|
danielebarchiesi@0
|
669 }
|
danielebarchiesi@0
|
670 };
|
danielebarchiesi@0
|
671
|
danielebarchiesi@0
|
672 /**
|
danielebarchiesi@0
|
673 * After the row is dropped, update a single table field according to specific
|
danielebarchiesi@0
|
674 * settings.
|
danielebarchiesi@0
|
675 *
|
danielebarchiesi@0
|
676 * @param changedRow
|
danielebarchiesi@0
|
677 * DOM object for the row that was just dropped.
|
danielebarchiesi@0
|
678 * @param group
|
danielebarchiesi@0
|
679 * The settings group on which field updates will occur.
|
danielebarchiesi@0
|
680 */
|
danielebarchiesi@0
|
681 Drupal.tableDrag.prototype.updateField = function (changedRow, group) {
|
danielebarchiesi@0
|
682 var rowSettings = this.rowSettings(group, changedRow);
|
danielebarchiesi@0
|
683
|
danielebarchiesi@0
|
684 // Set the row as its own target.
|
danielebarchiesi@0
|
685 if (rowSettings.relationship == 'self' || rowSettings.relationship == 'group') {
|
danielebarchiesi@0
|
686 var sourceRow = changedRow;
|
danielebarchiesi@0
|
687 }
|
danielebarchiesi@0
|
688 // Siblings are easy, check previous and next rows.
|
danielebarchiesi@0
|
689 else if (rowSettings.relationship == 'sibling') {
|
danielebarchiesi@0
|
690 var previousRow = $(changedRow).prev('tr').get(0);
|
danielebarchiesi@0
|
691 var nextRow = $(changedRow).next('tr').get(0);
|
danielebarchiesi@0
|
692 var sourceRow = changedRow;
|
danielebarchiesi@0
|
693 if ($(previousRow).is('.draggable') && $('.' + group, previousRow).length) {
|
danielebarchiesi@0
|
694 if (this.indentEnabled) {
|
danielebarchiesi@0
|
695 if ($('.indentations', previousRow).length == $('.indentations', changedRow)) {
|
danielebarchiesi@0
|
696 sourceRow = previousRow;
|
danielebarchiesi@0
|
697 }
|
danielebarchiesi@0
|
698 }
|
danielebarchiesi@0
|
699 else {
|
danielebarchiesi@0
|
700 sourceRow = previousRow;
|
danielebarchiesi@0
|
701 }
|
danielebarchiesi@0
|
702 }
|
danielebarchiesi@0
|
703 else if ($(nextRow).is('.draggable') && $('.' + group, nextRow).length) {
|
danielebarchiesi@0
|
704 if (this.indentEnabled) {
|
danielebarchiesi@0
|
705 if ($('.indentations', nextRow).length == $('.indentations', changedRow)) {
|
danielebarchiesi@0
|
706 sourceRow = nextRow;
|
danielebarchiesi@0
|
707 }
|
danielebarchiesi@0
|
708 }
|
danielebarchiesi@0
|
709 else {
|
danielebarchiesi@0
|
710 sourceRow = nextRow;
|
danielebarchiesi@0
|
711 }
|
danielebarchiesi@0
|
712 }
|
danielebarchiesi@0
|
713 }
|
danielebarchiesi@0
|
714 // Parents, look up the tree until we find a field not in this group.
|
danielebarchiesi@0
|
715 // Go up as many parents as indentations in the changed row.
|
danielebarchiesi@0
|
716 else if (rowSettings.relationship == 'parent') {
|
danielebarchiesi@0
|
717 var previousRow = $(changedRow).prev('tr');
|
danielebarchiesi@0
|
718 while (previousRow.length && $('.indentation', previousRow).length >= this.rowObject.indents) {
|
danielebarchiesi@0
|
719 previousRow = previousRow.prev('tr');
|
danielebarchiesi@0
|
720 }
|
danielebarchiesi@0
|
721 // If we found a row.
|
danielebarchiesi@0
|
722 if (previousRow.length) {
|
danielebarchiesi@0
|
723 sourceRow = previousRow[0];
|
danielebarchiesi@0
|
724 }
|
danielebarchiesi@0
|
725 // Otherwise we went all the way to the left of the table without finding
|
danielebarchiesi@0
|
726 // a parent, meaning this item has been placed at the root level.
|
danielebarchiesi@0
|
727 else {
|
danielebarchiesi@0
|
728 // Use the first row in the table as source, because it's guaranteed to
|
danielebarchiesi@0
|
729 // be at the root level. Find the first item, then compare this row
|
danielebarchiesi@0
|
730 // against it as a sibling.
|
danielebarchiesi@0
|
731 sourceRow = $(this.table).find('tr.draggable:first').get(0);
|
danielebarchiesi@0
|
732 if (sourceRow == this.rowObject.element) {
|
danielebarchiesi@0
|
733 sourceRow = $(this.rowObject.group[this.rowObject.group.length - 1]).next('tr.draggable').get(0);
|
danielebarchiesi@0
|
734 }
|
danielebarchiesi@0
|
735 var useSibling = true;
|
danielebarchiesi@0
|
736 }
|
danielebarchiesi@0
|
737 }
|
danielebarchiesi@0
|
738
|
danielebarchiesi@0
|
739 // Because we may have moved the row from one category to another,
|
danielebarchiesi@0
|
740 // take a look at our sibling and borrow its sources and targets.
|
danielebarchiesi@0
|
741 this.copyDragClasses(sourceRow, changedRow, group);
|
danielebarchiesi@0
|
742 rowSettings = this.rowSettings(group, changedRow);
|
danielebarchiesi@0
|
743
|
danielebarchiesi@0
|
744 // In the case that we're looking for a parent, but the row is at the top
|
danielebarchiesi@0
|
745 // of the tree, copy our sibling's values.
|
danielebarchiesi@0
|
746 if (useSibling) {
|
danielebarchiesi@0
|
747 rowSettings.relationship = 'sibling';
|
danielebarchiesi@0
|
748 rowSettings.source = rowSettings.target;
|
danielebarchiesi@0
|
749 }
|
danielebarchiesi@0
|
750
|
danielebarchiesi@0
|
751 var targetClass = '.' + rowSettings.target;
|
danielebarchiesi@0
|
752 var targetElement = $(targetClass, changedRow).get(0);
|
danielebarchiesi@0
|
753
|
danielebarchiesi@0
|
754 // Check if a target element exists in this row.
|
danielebarchiesi@0
|
755 if (targetElement) {
|
danielebarchiesi@0
|
756 var sourceClass = '.' + rowSettings.source;
|
danielebarchiesi@0
|
757 var sourceElement = $(sourceClass, sourceRow).get(0);
|
danielebarchiesi@0
|
758 switch (rowSettings.action) {
|
danielebarchiesi@0
|
759 case 'depth':
|
danielebarchiesi@0
|
760 // Get the depth of the target row.
|
danielebarchiesi@0
|
761 targetElement.value = $('.indentation', $(sourceElement).closest('tr')).length;
|
danielebarchiesi@0
|
762 break;
|
danielebarchiesi@0
|
763 case 'match':
|
danielebarchiesi@0
|
764 // Update the value.
|
danielebarchiesi@0
|
765 targetElement.value = sourceElement.value;
|
danielebarchiesi@0
|
766 break;
|
danielebarchiesi@0
|
767 case 'order':
|
danielebarchiesi@0
|
768 var siblings = this.rowObject.findSiblings(rowSettings);
|
danielebarchiesi@0
|
769 if ($(targetElement).is('select')) {
|
danielebarchiesi@0
|
770 // Get a list of acceptable values.
|
danielebarchiesi@0
|
771 var values = [];
|
danielebarchiesi@0
|
772 $('option', targetElement).each(function () {
|
danielebarchiesi@0
|
773 values.push(this.value);
|
danielebarchiesi@0
|
774 });
|
danielebarchiesi@0
|
775 var maxVal = values[values.length - 1];
|
danielebarchiesi@0
|
776 // Populate the values in the siblings.
|
danielebarchiesi@0
|
777 $(targetClass, siblings).each(function () {
|
danielebarchiesi@0
|
778 // If there are more items than possible values, assign the maximum value to the row.
|
danielebarchiesi@0
|
779 if (values.length > 0) {
|
danielebarchiesi@0
|
780 this.value = values.shift();
|
danielebarchiesi@0
|
781 }
|
danielebarchiesi@0
|
782 else {
|
danielebarchiesi@0
|
783 this.value = maxVal;
|
danielebarchiesi@0
|
784 }
|
danielebarchiesi@0
|
785 });
|
danielebarchiesi@0
|
786 }
|
danielebarchiesi@0
|
787 else {
|
danielebarchiesi@0
|
788 // Assume a numeric input field.
|
danielebarchiesi@0
|
789 var weight = parseInt($(targetClass, siblings[0]).val(), 10) || 0;
|
danielebarchiesi@0
|
790 $(targetClass, siblings).each(function () {
|
danielebarchiesi@0
|
791 this.value = weight;
|
danielebarchiesi@0
|
792 weight++;
|
danielebarchiesi@0
|
793 });
|
danielebarchiesi@0
|
794 }
|
danielebarchiesi@0
|
795 break;
|
danielebarchiesi@0
|
796 }
|
danielebarchiesi@0
|
797 }
|
danielebarchiesi@0
|
798 };
|
danielebarchiesi@0
|
799
|
danielebarchiesi@0
|
800 /**
|
danielebarchiesi@0
|
801 * Copy all special tableDrag classes from one row's form elements to a
|
danielebarchiesi@0
|
802 * different one, removing any special classes that the destination row
|
danielebarchiesi@0
|
803 * may have had.
|
danielebarchiesi@0
|
804 */
|
danielebarchiesi@0
|
805 Drupal.tableDrag.prototype.copyDragClasses = function (sourceRow, targetRow, group) {
|
danielebarchiesi@0
|
806 var sourceElement = $('.' + group, sourceRow);
|
danielebarchiesi@0
|
807 var targetElement = $('.' + group, targetRow);
|
danielebarchiesi@0
|
808 if (sourceElement.length && targetElement.length) {
|
danielebarchiesi@0
|
809 targetElement[0].className = sourceElement[0].className;
|
danielebarchiesi@0
|
810 }
|
danielebarchiesi@0
|
811 };
|
danielebarchiesi@0
|
812
|
danielebarchiesi@0
|
813 Drupal.tableDrag.prototype.checkScroll = function (cursorY) {
|
danielebarchiesi@0
|
814 var de = document.documentElement;
|
danielebarchiesi@0
|
815 var b = document.body;
|
danielebarchiesi@0
|
816
|
danielebarchiesi@0
|
817 var windowHeight = this.windowHeight = window.innerHeight || (de.clientHeight && de.clientWidth != 0 ? de.clientHeight : b.offsetHeight);
|
danielebarchiesi@0
|
818 var scrollY = this.scrollY = (document.all ? (!de.scrollTop ? b.scrollTop : de.scrollTop) : (window.pageYOffset ? window.pageYOffset : window.scrollY));
|
danielebarchiesi@0
|
819 var trigger = this.scrollSettings.trigger;
|
danielebarchiesi@0
|
820 var delta = 0;
|
danielebarchiesi@0
|
821
|
danielebarchiesi@0
|
822 // Return a scroll speed relative to the edge of the screen.
|
danielebarchiesi@0
|
823 if (cursorY - scrollY > windowHeight - trigger) {
|
danielebarchiesi@0
|
824 delta = trigger / (windowHeight + scrollY - cursorY);
|
danielebarchiesi@0
|
825 delta = (delta > 0 && delta < trigger) ? delta : trigger;
|
danielebarchiesi@0
|
826 return delta * this.scrollSettings.amount;
|
danielebarchiesi@0
|
827 }
|
danielebarchiesi@0
|
828 else if (cursorY - scrollY < trigger) {
|
danielebarchiesi@0
|
829 delta = trigger / (cursorY - scrollY);
|
danielebarchiesi@0
|
830 delta = (delta > 0 && delta < trigger) ? delta : trigger;
|
danielebarchiesi@0
|
831 return -delta * this.scrollSettings.amount;
|
danielebarchiesi@0
|
832 }
|
danielebarchiesi@0
|
833 };
|
danielebarchiesi@0
|
834
|
danielebarchiesi@0
|
835 Drupal.tableDrag.prototype.setScroll = function (scrollAmount) {
|
danielebarchiesi@0
|
836 var self = this;
|
danielebarchiesi@0
|
837
|
danielebarchiesi@0
|
838 this.scrollInterval = setInterval(function () {
|
danielebarchiesi@0
|
839 // Update the scroll values stored in the object.
|
danielebarchiesi@0
|
840 self.checkScroll(self.currentMouseCoords.y);
|
danielebarchiesi@0
|
841 var aboveTable = self.scrollY > self.table.topY;
|
danielebarchiesi@0
|
842 var belowTable = self.scrollY + self.windowHeight < self.table.bottomY;
|
danielebarchiesi@0
|
843 if (scrollAmount > 0 && belowTable || scrollAmount < 0 && aboveTable) {
|
danielebarchiesi@0
|
844 window.scrollBy(0, scrollAmount);
|
danielebarchiesi@0
|
845 }
|
danielebarchiesi@0
|
846 }, this.scrollSettings.interval);
|
danielebarchiesi@0
|
847 };
|
danielebarchiesi@0
|
848
|
danielebarchiesi@0
|
849 Drupal.tableDrag.prototype.restripeTable = function () {
|
danielebarchiesi@0
|
850 // :even and :odd are reversed because jQuery counts from 0 and
|
danielebarchiesi@0
|
851 // we count from 1, so we're out of sync.
|
danielebarchiesi@0
|
852 // Match immediate children of the parent element to allow nesting.
|
danielebarchiesi@0
|
853 $('> tbody > tr.draggable:visible, > tr.draggable:visible', this.table)
|
danielebarchiesi@0
|
854 .removeClass('odd even')
|
danielebarchiesi@0
|
855 .filter(':odd').addClass('even').end()
|
danielebarchiesi@0
|
856 .filter(':even').addClass('odd');
|
danielebarchiesi@0
|
857 };
|
danielebarchiesi@0
|
858
|
danielebarchiesi@0
|
859 /**
|
danielebarchiesi@0
|
860 * Stub function. Allows a custom handler when a row begins dragging.
|
danielebarchiesi@0
|
861 */
|
danielebarchiesi@0
|
862 Drupal.tableDrag.prototype.onDrag = function () {
|
danielebarchiesi@0
|
863 return null;
|
danielebarchiesi@0
|
864 };
|
danielebarchiesi@0
|
865
|
danielebarchiesi@0
|
866 /**
|
danielebarchiesi@0
|
867 * Stub function. Allows a custom handler when a row is dropped.
|
danielebarchiesi@0
|
868 */
|
danielebarchiesi@0
|
869 Drupal.tableDrag.prototype.onDrop = function () {
|
danielebarchiesi@0
|
870 return null;
|
danielebarchiesi@0
|
871 };
|
danielebarchiesi@0
|
872
|
danielebarchiesi@0
|
873 /**
|
danielebarchiesi@0
|
874 * Constructor to make a new object to manipulate a table row.
|
danielebarchiesi@0
|
875 *
|
danielebarchiesi@0
|
876 * @param tableRow
|
danielebarchiesi@0
|
877 * The DOM element for the table row we will be manipulating.
|
danielebarchiesi@0
|
878 * @param method
|
danielebarchiesi@0
|
879 * The method in which this row is being moved. Either 'keyboard' or 'mouse'.
|
danielebarchiesi@0
|
880 * @param indentEnabled
|
danielebarchiesi@0
|
881 * Whether the containing table uses indentations. Used for optimizations.
|
danielebarchiesi@0
|
882 * @param maxDepth
|
danielebarchiesi@0
|
883 * The maximum amount of indentations this row may contain.
|
danielebarchiesi@0
|
884 * @param addClasses
|
danielebarchiesi@0
|
885 * Whether we want to add classes to this row to indicate child relationships.
|
danielebarchiesi@0
|
886 */
|
danielebarchiesi@0
|
887 Drupal.tableDrag.prototype.row = function (tableRow, method, indentEnabled, maxDepth, addClasses) {
|
danielebarchiesi@0
|
888 this.element = tableRow;
|
danielebarchiesi@0
|
889 this.method = method;
|
danielebarchiesi@0
|
890 this.group = [tableRow];
|
danielebarchiesi@0
|
891 this.groupDepth = $('.indentation', tableRow).length;
|
danielebarchiesi@0
|
892 this.changed = false;
|
danielebarchiesi@0
|
893 this.table = $(tableRow).closest('table').get(0);
|
danielebarchiesi@0
|
894 this.indentEnabled = indentEnabled;
|
danielebarchiesi@0
|
895 this.maxDepth = maxDepth;
|
danielebarchiesi@0
|
896 this.direction = ''; // Direction the row is being moved.
|
danielebarchiesi@0
|
897
|
danielebarchiesi@0
|
898 if (this.indentEnabled) {
|
danielebarchiesi@0
|
899 this.indents = $('.indentation', tableRow).length;
|
danielebarchiesi@0
|
900 this.children = this.findChildren(addClasses);
|
danielebarchiesi@0
|
901 this.group = $.merge(this.group, this.children);
|
danielebarchiesi@0
|
902 // Find the depth of this entire group.
|
danielebarchiesi@0
|
903 for (var n = 0; n < this.group.length; n++) {
|
danielebarchiesi@0
|
904 this.groupDepth = Math.max($('.indentation', this.group[n]).length, this.groupDepth);
|
danielebarchiesi@0
|
905 }
|
danielebarchiesi@0
|
906 }
|
danielebarchiesi@0
|
907 };
|
danielebarchiesi@0
|
908
|
danielebarchiesi@0
|
909 /**
|
danielebarchiesi@0
|
910 * Find all children of rowObject by indentation.
|
danielebarchiesi@0
|
911 *
|
danielebarchiesi@0
|
912 * @param addClasses
|
danielebarchiesi@0
|
913 * Whether we want to add classes to this row to indicate child relationships.
|
danielebarchiesi@0
|
914 */
|
danielebarchiesi@0
|
915 Drupal.tableDrag.prototype.row.prototype.findChildren = function (addClasses) {
|
danielebarchiesi@0
|
916 var parentIndentation = this.indents;
|
danielebarchiesi@0
|
917 var currentRow = $(this.element, this.table).next('tr.draggable');
|
danielebarchiesi@0
|
918 var rows = [];
|
danielebarchiesi@0
|
919 var child = 0;
|
danielebarchiesi@0
|
920 while (currentRow.length) {
|
danielebarchiesi@0
|
921 var rowIndentation = $('.indentation', currentRow).length;
|
danielebarchiesi@0
|
922 // A greater indentation indicates this is a child.
|
danielebarchiesi@0
|
923 if (rowIndentation > parentIndentation) {
|
danielebarchiesi@0
|
924 child++;
|
danielebarchiesi@0
|
925 rows.push(currentRow[0]);
|
danielebarchiesi@0
|
926 if (addClasses) {
|
danielebarchiesi@0
|
927 $('.indentation', currentRow).each(function (indentNum) {
|
danielebarchiesi@0
|
928 if (child == 1 && (indentNum == parentIndentation)) {
|
danielebarchiesi@0
|
929 $(this).addClass('tree-child-first');
|
danielebarchiesi@0
|
930 }
|
danielebarchiesi@0
|
931 if (indentNum == parentIndentation) {
|
danielebarchiesi@0
|
932 $(this).addClass('tree-child');
|
danielebarchiesi@0
|
933 }
|
danielebarchiesi@0
|
934 else if (indentNum > parentIndentation) {
|
danielebarchiesi@0
|
935 $(this).addClass('tree-child-horizontal');
|
danielebarchiesi@0
|
936 }
|
danielebarchiesi@0
|
937 });
|
danielebarchiesi@0
|
938 }
|
danielebarchiesi@0
|
939 }
|
danielebarchiesi@0
|
940 else {
|
danielebarchiesi@0
|
941 break;
|
danielebarchiesi@0
|
942 }
|
danielebarchiesi@0
|
943 currentRow = currentRow.next('tr.draggable');
|
danielebarchiesi@0
|
944 }
|
danielebarchiesi@0
|
945 if (addClasses && rows.length) {
|
danielebarchiesi@0
|
946 $('.indentation:nth-child(' + (parentIndentation + 1) + ')', rows[rows.length - 1]).addClass('tree-child-last');
|
danielebarchiesi@0
|
947 }
|
danielebarchiesi@0
|
948 return rows;
|
danielebarchiesi@0
|
949 };
|
danielebarchiesi@0
|
950
|
danielebarchiesi@0
|
951 /**
|
danielebarchiesi@0
|
952 * Ensure that two rows are allowed to be swapped.
|
danielebarchiesi@0
|
953 *
|
danielebarchiesi@0
|
954 * @param row
|
danielebarchiesi@0
|
955 * DOM object for the row being considered for swapping.
|
danielebarchiesi@0
|
956 */
|
danielebarchiesi@0
|
957 Drupal.tableDrag.prototype.row.prototype.isValidSwap = function (row) {
|
danielebarchiesi@0
|
958 if (this.indentEnabled) {
|
danielebarchiesi@0
|
959 var prevRow, nextRow;
|
danielebarchiesi@0
|
960 if (this.direction == 'down') {
|
danielebarchiesi@0
|
961 prevRow = row;
|
danielebarchiesi@0
|
962 nextRow = $(row).next('tr').get(0);
|
danielebarchiesi@0
|
963 }
|
danielebarchiesi@0
|
964 else {
|
danielebarchiesi@0
|
965 prevRow = $(row).prev('tr').get(0);
|
danielebarchiesi@0
|
966 nextRow = row;
|
danielebarchiesi@0
|
967 }
|
danielebarchiesi@0
|
968 this.interval = this.validIndentInterval(prevRow, nextRow);
|
danielebarchiesi@0
|
969
|
danielebarchiesi@0
|
970 // We have an invalid swap if the valid indentations interval is empty.
|
danielebarchiesi@0
|
971 if (this.interval.min > this.interval.max) {
|
danielebarchiesi@0
|
972 return false;
|
danielebarchiesi@0
|
973 }
|
danielebarchiesi@0
|
974 }
|
danielebarchiesi@0
|
975
|
danielebarchiesi@0
|
976 // Do not let an un-draggable first row have anything put before it.
|
danielebarchiesi@0
|
977 if (this.table.tBodies[0].rows[0] == row && $(row).is(':not(.draggable)')) {
|
danielebarchiesi@0
|
978 return false;
|
danielebarchiesi@0
|
979 }
|
danielebarchiesi@0
|
980
|
danielebarchiesi@0
|
981 return true;
|
danielebarchiesi@0
|
982 };
|
danielebarchiesi@0
|
983
|
danielebarchiesi@0
|
984 /**
|
danielebarchiesi@0
|
985 * Perform the swap between two rows.
|
danielebarchiesi@0
|
986 *
|
danielebarchiesi@0
|
987 * @param position
|
danielebarchiesi@0
|
988 * Whether the swap will occur 'before' or 'after' the given row.
|
danielebarchiesi@0
|
989 * @param row
|
danielebarchiesi@0
|
990 * DOM element what will be swapped with the row group.
|
danielebarchiesi@0
|
991 */
|
danielebarchiesi@0
|
992 Drupal.tableDrag.prototype.row.prototype.swap = function (position, row) {
|
danielebarchiesi@0
|
993 Drupal.detachBehaviors(this.group, Drupal.settings, 'move');
|
danielebarchiesi@0
|
994 $(row)[position](this.group);
|
danielebarchiesi@0
|
995 Drupal.attachBehaviors(this.group, Drupal.settings);
|
danielebarchiesi@0
|
996 this.changed = true;
|
danielebarchiesi@0
|
997 this.onSwap(row);
|
danielebarchiesi@0
|
998 };
|
danielebarchiesi@0
|
999
|
danielebarchiesi@0
|
1000 /**
|
danielebarchiesi@0
|
1001 * Determine the valid indentations interval for the row at a given position
|
danielebarchiesi@0
|
1002 * in the table.
|
danielebarchiesi@0
|
1003 *
|
danielebarchiesi@0
|
1004 * @param prevRow
|
danielebarchiesi@0
|
1005 * DOM object for the row before the tested position
|
danielebarchiesi@0
|
1006 * (or null for first position in the table).
|
danielebarchiesi@0
|
1007 * @param nextRow
|
danielebarchiesi@0
|
1008 * DOM object for the row after the tested position
|
danielebarchiesi@0
|
1009 * (or null for last position in the table).
|
danielebarchiesi@0
|
1010 */
|
danielebarchiesi@0
|
1011 Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow, nextRow) {
|
danielebarchiesi@0
|
1012 var minIndent, maxIndent;
|
danielebarchiesi@0
|
1013
|
danielebarchiesi@0
|
1014 // Minimum indentation:
|
danielebarchiesi@0
|
1015 // Do not orphan the next row.
|
danielebarchiesi@0
|
1016 minIndent = nextRow ? $('.indentation', nextRow).length : 0;
|
danielebarchiesi@0
|
1017
|
danielebarchiesi@0
|
1018 // Maximum indentation:
|
danielebarchiesi@0
|
1019 if (!prevRow || $(prevRow).is(':not(.draggable)') || $(this.element).is('.tabledrag-root')) {
|
danielebarchiesi@0
|
1020 // Do not indent:
|
danielebarchiesi@0
|
1021 // - the first row in the table,
|
danielebarchiesi@0
|
1022 // - rows dragged below a non-draggable row,
|
danielebarchiesi@0
|
1023 // - 'root' rows.
|
danielebarchiesi@0
|
1024 maxIndent = 0;
|
danielebarchiesi@0
|
1025 }
|
danielebarchiesi@0
|
1026 else {
|
danielebarchiesi@0
|
1027 // Do not go deeper than as a child of the previous row.
|
danielebarchiesi@0
|
1028 maxIndent = $('.indentation', prevRow).length + ($(prevRow).is('.tabledrag-leaf') ? 0 : 1);
|
danielebarchiesi@0
|
1029 // Limit by the maximum allowed depth for the table.
|
danielebarchiesi@0
|
1030 if (this.maxDepth) {
|
danielebarchiesi@0
|
1031 maxIndent = Math.min(maxIndent, this.maxDepth - (this.groupDepth - this.indents));
|
danielebarchiesi@0
|
1032 }
|
danielebarchiesi@0
|
1033 }
|
danielebarchiesi@0
|
1034
|
danielebarchiesi@0
|
1035 return { 'min': minIndent, 'max': maxIndent };
|
danielebarchiesi@0
|
1036 };
|
danielebarchiesi@0
|
1037
|
danielebarchiesi@0
|
1038 /**
|
danielebarchiesi@0
|
1039 * Indent a row within the legal bounds of the table.
|
danielebarchiesi@0
|
1040 *
|
danielebarchiesi@0
|
1041 * @param indentDiff
|
danielebarchiesi@0
|
1042 * The number of additional indentations proposed for the row (can be
|
danielebarchiesi@0
|
1043 * positive or negative). This number will be adjusted to nearest valid
|
danielebarchiesi@0
|
1044 * indentation level for the row.
|
danielebarchiesi@0
|
1045 */
|
danielebarchiesi@0
|
1046 Drupal.tableDrag.prototype.row.prototype.indent = function (indentDiff) {
|
danielebarchiesi@0
|
1047 // Determine the valid indentations interval if not available yet.
|
danielebarchiesi@0
|
1048 if (!this.interval) {
|
danielebarchiesi@0
|
1049 var prevRow = $(this.element).prev('tr').get(0);
|
danielebarchiesi@0
|
1050 var nextRow = $(this.group).filter(':last').next('tr').get(0);
|
danielebarchiesi@0
|
1051 this.interval = this.validIndentInterval(prevRow, nextRow);
|
danielebarchiesi@0
|
1052 }
|
danielebarchiesi@0
|
1053
|
danielebarchiesi@0
|
1054 // Adjust to the nearest valid indentation.
|
danielebarchiesi@0
|
1055 var indent = this.indents + indentDiff;
|
danielebarchiesi@0
|
1056 indent = Math.max(indent, this.interval.min);
|
danielebarchiesi@0
|
1057 indent = Math.min(indent, this.interval.max);
|
danielebarchiesi@0
|
1058 indentDiff = indent - this.indents;
|
danielebarchiesi@0
|
1059
|
danielebarchiesi@0
|
1060 for (var n = 1; n <= Math.abs(indentDiff); n++) {
|
danielebarchiesi@0
|
1061 // Add or remove indentations.
|
danielebarchiesi@0
|
1062 if (indentDiff < 0) {
|
danielebarchiesi@0
|
1063 $('.indentation:first', this.group).remove();
|
danielebarchiesi@0
|
1064 this.indents--;
|
danielebarchiesi@0
|
1065 }
|
danielebarchiesi@0
|
1066 else {
|
danielebarchiesi@0
|
1067 $('td:first', this.group).prepend(Drupal.theme('tableDragIndentation'));
|
danielebarchiesi@0
|
1068 this.indents++;
|
danielebarchiesi@0
|
1069 }
|
danielebarchiesi@0
|
1070 }
|
danielebarchiesi@0
|
1071 if (indentDiff) {
|
danielebarchiesi@0
|
1072 // Update indentation for this row.
|
danielebarchiesi@0
|
1073 this.changed = true;
|
danielebarchiesi@0
|
1074 this.groupDepth += indentDiff;
|
danielebarchiesi@0
|
1075 this.onIndent();
|
danielebarchiesi@0
|
1076 }
|
danielebarchiesi@0
|
1077
|
danielebarchiesi@0
|
1078 return indentDiff;
|
danielebarchiesi@0
|
1079 };
|
danielebarchiesi@0
|
1080
|
danielebarchiesi@0
|
1081 /**
|
danielebarchiesi@0
|
1082 * Find all siblings for a row, either according to its subgroup or indentation.
|
danielebarchiesi@0
|
1083 * Note that the passed-in row is included in the list of siblings.
|
danielebarchiesi@0
|
1084 *
|
danielebarchiesi@0
|
1085 * @param settings
|
danielebarchiesi@0
|
1086 * The field settings we're using to identify what constitutes a sibling.
|
danielebarchiesi@0
|
1087 */
|
danielebarchiesi@0
|
1088 Drupal.tableDrag.prototype.row.prototype.findSiblings = function (rowSettings) {
|
danielebarchiesi@0
|
1089 var siblings = [];
|
danielebarchiesi@0
|
1090 var directions = ['prev', 'next'];
|
danielebarchiesi@0
|
1091 var rowIndentation = this.indents;
|
danielebarchiesi@0
|
1092 for (var d = 0; d < directions.length; d++) {
|
danielebarchiesi@0
|
1093 var checkRow = $(this.element)[directions[d]]();
|
danielebarchiesi@0
|
1094 while (checkRow.length) {
|
danielebarchiesi@0
|
1095 // Check that the sibling contains a similar target field.
|
danielebarchiesi@0
|
1096 if ($('.' + rowSettings.target, checkRow)) {
|
danielebarchiesi@0
|
1097 // Either add immediately if this is a flat table, or check to ensure
|
danielebarchiesi@0
|
1098 // that this row has the same level of indentation.
|
danielebarchiesi@0
|
1099 if (this.indentEnabled) {
|
danielebarchiesi@0
|
1100 var checkRowIndentation = $('.indentation', checkRow).length;
|
danielebarchiesi@0
|
1101 }
|
danielebarchiesi@0
|
1102
|
danielebarchiesi@0
|
1103 if (!(this.indentEnabled) || (checkRowIndentation == rowIndentation)) {
|
danielebarchiesi@0
|
1104 siblings.push(checkRow[0]);
|
danielebarchiesi@0
|
1105 }
|
danielebarchiesi@0
|
1106 else if (checkRowIndentation < rowIndentation) {
|
danielebarchiesi@0
|
1107 // No need to keep looking for siblings when we get to a parent.
|
danielebarchiesi@0
|
1108 break;
|
danielebarchiesi@0
|
1109 }
|
danielebarchiesi@0
|
1110 }
|
danielebarchiesi@0
|
1111 else {
|
danielebarchiesi@0
|
1112 break;
|
danielebarchiesi@0
|
1113 }
|
danielebarchiesi@0
|
1114 checkRow = $(checkRow)[directions[d]]();
|
danielebarchiesi@0
|
1115 }
|
danielebarchiesi@0
|
1116 // Since siblings are added in reverse order for previous, reverse the
|
danielebarchiesi@0
|
1117 // completed list of previous siblings. Add the current row and continue.
|
danielebarchiesi@0
|
1118 if (directions[d] == 'prev') {
|
danielebarchiesi@0
|
1119 siblings.reverse();
|
danielebarchiesi@0
|
1120 siblings.push(this.element);
|
danielebarchiesi@0
|
1121 }
|
danielebarchiesi@0
|
1122 }
|
danielebarchiesi@0
|
1123 return siblings;
|
danielebarchiesi@0
|
1124 };
|
danielebarchiesi@0
|
1125
|
danielebarchiesi@0
|
1126 /**
|
danielebarchiesi@0
|
1127 * Remove indentation helper classes from the current row group.
|
danielebarchiesi@0
|
1128 */
|
danielebarchiesi@0
|
1129 Drupal.tableDrag.prototype.row.prototype.removeIndentClasses = function () {
|
danielebarchiesi@0
|
1130 for (var n in this.children) {
|
danielebarchiesi@0
|
1131 $('.indentation', this.children[n])
|
danielebarchiesi@0
|
1132 .removeClass('tree-child')
|
danielebarchiesi@0
|
1133 .removeClass('tree-child-first')
|
danielebarchiesi@0
|
1134 .removeClass('tree-child-last')
|
danielebarchiesi@0
|
1135 .removeClass('tree-child-horizontal');
|
danielebarchiesi@0
|
1136 }
|
danielebarchiesi@0
|
1137 };
|
danielebarchiesi@0
|
1138
|
danielebarchiesi@0
|
1139 /**
|
danielebarchiesi@0
|
1140 * Add an asterisk or other marker to the changed row.
|
danielebarchiesi@0
|
1141 */
|
danielebarchiesi@0
|
1142 Drupal.tableDrag.prototype.row.prototype.markChanged = function () {
|
danielebarchiesi@0
|
1143 var marker = Drupal.theme('tableDragChangedMarker');
|
danielebarchiesi@0
|
1144 var cell = $('td:first', this.element);
|
danielebarchiesi@0
|
1145 if ($('span.tabledrag-changed', cell).length == 0) {
|
danielebarchiesi@0
|
1146 cell.append(marker);
|
danielebarchiesi@0
|
1147 }
|
danielebarchiesi@0
|
1148 };
|
danielebarchiesi@0
|
1149
|
danielebarchiesi@0
|
1150 /**
|
danielebarchiesi@0
|
1151 * Stub function. Allows a custom handler when a row is indented.
|
danielebarchiesi@0
|
1152 */
|
danielebarchiesi@0
|
1153 Drupal.tableDrag.prototype.row.prototype.onIndent = function () {
|
danielebarchiesi@0
|
1154 return null;
|
danielebarchiesi@0
|
1155 };
|
danielebarchiesi@0
|
1156
|
danielebarchiesi@0
|
1157 /**
|
danielebarchiesi@0
|
1158 * Stub function. Allows a custom handler when a row is swapped.
|
danielebarchiesi@0
|
1159 */
|
danielebarchiesi@0
|
1160 Drupal.tableDrag.prototype.row.prototype.onSwap = function (swappedRow) {
|
danielebarchiesi@0
|
1161 return null;
|
danielebarchiesi@0
|
1162 };
|
danielebarchiesi@0
|
1163
|
danielebarchiesi@0
|
1164 Drupal.theme.prototype.tableDragChangedMarker = function () {
|
danielebarchiesi@0
|
1165 return '<span class="warning tabledrag-changed">*</span>';
|
danielebarchiesi@0
|
1166 };
|
danielebarchiesi@0
|
1167
|
danielebarchiesi@0
|
1168 Drupal.theme.prototype.tableDragIndentation = function () {
|
danielebarchiesi@0
|
1169 return '<div class="indentation"> </div>';
|
danielebarchiesi@0
|
1170 };
|
danielebarchiesi@0
|
1171
|
danielebarchiesi@0
|
1172 Drupal.theme.prototype.tableDragChangedWarning = function () {
|
danielebarchiesi@0
|
1173 return '<div class="tabledrag-changed-warning messages warning">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t('Changes made in this table will not be saved until the form is submitted.') + '</div>';
|
danielebarchiesi@0
|
1174 };
|
danielebarchiesi@0
|
1175
|
danielebarchiesi@0
|
1176 })(jQuery);
|