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
19 * along with Codendi; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 var codendi = codendi || { };
27 codendi.layout_manager = {
28 addCol: function(col) {
29 var new_col = '<td class="layout-manager-column"><div class="layout-manager-column-remove">x</div><div class="layout-manager-column-width"><input type="text" value="" autocomplete="off" size="1" maxlength="3" />%</div></td>';
30 col.up('tr').insert({bottom: new_col});
31 new_col = new Element('td');
32 new_col.addClassName("layout-manager-column-add");
34 this.loadColAdd(new_col);
35 col.up('tr').insert({bottom: new_col});
37 this.distributeWidth(col.up('tr'));
38 var rm = col.up('tr').down('.layout-manager-column-remove', col.up('tr').select('.layout-manager-column').size() - 1);
39 this.loadColRemove(rm);
41 addRow: function(row) {
42 var new_row = '<table class="layout-manager-row" cellspacing="5" cellpadding="2" border="0"><tr><td>+</td></tr></table>';
43 row.insert({after: new_row});
45 var new_col = row.next().down('td');
46 new_col.addClassName("layout-manager-column-add");
48 this.loadColAdd(new_col);
52 new_row = new Element('div');
53 new_row.addClassName("layout-manager-row-add");
55 this.loadRowAdd(new_row);
57 row.next().insert({after: new_row});
59 removeCol: function (rm) {
60 var row = rm.up('tr');
62 //Check columns number in layout
63 if ($('layout-manager').select('.layout-manager-column').size() == 1) {
64 alert('You must keep at least one column in your layout.');
65 } else if (row.select('.layout-manager-column').size() == 1) {
67 Element.remove(row.up('table').next());
68 Element.remove(row.up('table'));
71 Element.remove(rm.up('td').next());
72 Element.remove(rm.up('td'));
73 this.distributeWidth(row);
76 distributeWidth: function (row) {
77 var cols = row.select('input[type=text]');
78 var width = Math.round(100 / cols.size());
79 cols.each(function (input) {
84 $$(".layout-manager-column-add").each(this.loadColAdd.bind(this));
85 $$(".layout-manager-column-remove").each(this.loadColRemove.bind(this));
86 $$(".layout-manager-row-add").each(this.loadRowAdd.bind(this));
88 loadColAdd: function(col) {
89 col.observe('mouseover', function() {
90 this.addClassName('layout-manager-column-add_hover');
92 col.observe('mouseout', function() {
93 this.removeClassName('layout-manager-column-add_hover');
95 col.observe('click', function () { this.addCol(col) }.bind(this));
97 loadColRemove: function(rm) {
98 rm.observe('mouseover', function() {
99 this.addClassName('layout-manager-column-remove_hover');
100 }).observe('mouseout', function() {
101 this.removeClassName('layout-manager-column-remove_hover');
102 }).observe('click', function () { this.removeCol(rm); }.bind(this));
104 loadRowAdd: function(row) {
105 row.observe('mouseover', function() {
106 this.addClassName('layout-manager-row-add_hover');
108 row.observe('mouseout', function() {
109 this.removeClassName('layout-manager-row-add_hover');
111 row.observe('click', function () { this.addRow(row); }.bind(this));
115 document.observe('dom:loaded', function() {
116 if ($('layout-manager')) {
117 codendi.layout_manager.load();
118 $('save').observe('click', function(evt) {
119 if ($('layout-manager')) {
121 var invalid = $('layout-manager').select('.layout-manager-column input[type=text]').find(function (element) {
122 return (!reg.test(element.value)) ? element : false;
125 alert(invalid.value+' is not a valid number');
127 var form = $('layout-manager').up('form');
129 $('layout-manager').select('.layout-manager-row').each(function (row) {
130 form.insert(new Element('input', {
132 name: 'new_layout[]',
133 value: row.select('.layout-manager-column input[type=text]').pluck('value').join(',')
140 $$('.layout-manager-chooser').each(function (row) {
141 row.down('input[type=radio]').observe('change', function() {
142 $$('.layout-manager-chooser').each(function (row) {
143 row.removeClassName('layout-manager-chooser_selected');
145 row.addClassName('layout-manager-chooser_selected');
151 var default_categ = location.href.match(/#filter-(widget-categ-[a-zA-Z0-9-_]+)$/);
153 if (default_categ && default_categ[1]) {
154 current_categ = default_categ[1];
156 current_categ = 'widget-categ-general';
158 $$('.widget-categ-switcher').each(function (a) {
159 var scan_id = a.href.match(/#(widget-categ-[a-zA-Z0-9-_]+)$/);
160 if (scan_id && scan_id[1]) {
162 a.href = a.href.gsub(/#(widget-categ-[a-zA-Z0-9-_]+)$/, '#filter-'+id);
163 a.observe('click', function(evt) {
165 //Display widgets of this category
166 $('widget-content-categ').childElements().invoke('hide');
167 a.up('ul').select('.widget-categ-switcher').each(function(other_a) {
168 other_a.up().removeClassName('selected');
170 a.up().addClassName('selected');
173 //remove corresponding table
174 if (id != current_categ) {
177 a.up().addClassName('selected');
179 Element.remove($(id).down('h4'));
180 $('widget-content-categ').appendChild(Element.remove($(id)));