2 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2008. All rights reserved
4 * Originally written by Nicolas Terray, 2008
6 * This file is a part of Codendi.
8 * Codendi is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Codendi is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 var codendi = codendi || { };
25 codendi.ReorderColumns = Class.create({
26 initialize: function(table) {
27 this.has_just_been_dragged = {};
28 //Take the first row, and register the cells
29 Element.childElements(table.rows[0]).map(this.register.bind(this));
31 register: function (cell) {
32 this.registerDraggables(cell);
33 this.registerDroppables(cell);
35 registerDraggables: function (cell) {
36 this.has_just_been_dragged[cell.identify()] = false;
37 cell.observe('click', (function(evt) {
38 if (this.has_just_been_dragged[cell.identify()]) {
39 this.has_just_been_dragged[cell.identify()] = false;
43 new Draggable(cell.down(), {
45 onStart: function () {
46 Element.setStyle(cell, { cursor:"move" });
49 this.has_just_been_dragged[cell.identify()] = true;
50 Element.setStyle(cell, { cursor:"auto" });
54 registerDroppables: function (cell) {
55 Droppables.add(cell, {
56 hoverclass: 'drop-over',
57 onDrop: (function(dragged, dropped, evt) {
58 dragged.undoPositioned();
59 var from = dragged.up('th').cellIndex;
60 var to = dropped.cellIndex;
62 //don't change column order if it is not necessary
64 var form = dropped.up('form');
65 var input = new Element('input', {
67 name: 'reordercolumns['+dragged.id+']',
68 value: (from < to ? (dropped.next() ? dropped.next().down().id : '-1') : dropped.down().id)
70 form.appendChild(input);
72 this.reorder(dropped.up('table'), from, to);
74 //save the new column order
77 //remove the input for other requests
78 Element.remove(input);
83 reorder: function(table, from, to) {
84 var i = table.rows.length;
86 var row = table.rows[i];
87 var cell = row.removeChild(row.cells[from]);
88 if (to < row.cells.length) {
89 row.insertBefore(cell, row.cells[to]);
91 row.appendChild(cell);
97 document.observe('dom:loaded', function() {
98 $$('table.reorderable').each(function (table) {
99 new codendi.ReorderColumns(table);