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.layout_manager = {
26 addCol: function(col) {
27 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>';
28 col.up('tr').insert({bottom: new_col});
29 new_col = new Element('td');
30 new_col.addClassName("layout-manager-column-add");
32 this.loadColAdd(new_col);
33 col.up('tr').insert({bottom: new_col});
35 this.distributeWidth(col.up('tr'));
36 var rm = col.up('tr').down('.layout-manager-column-remove', col.up('tr').select('.layout-manager-column').size() - 1);
37 this.loadColRemove(rm);
39 addRow: function(row) {
40 var new_row = '<table class="layout-manager-row" cellspacing="5" cellpadding="2" border="0"><tr><td>+</td></tr></table>';
41 row.insert({after: new_row});
43 var new_col = row.next().down('td');
44 new_col.addClassName("layout-manager-column-add");
46 this.loadColAdd(new_col);
50 new_row = new Element('div');
51 new_row.addClassName("layout-manager-row-add");
53 this.loadRowAdd(new_row);
55 row.next().insert({after: new_row});
57 removeCol: function (rm) {
58 var row = rm.up('tr');
60 //Check columns number in layout
61 if ($('layout-manager').select('.layout-manager-column').size() == 1) {
62 alert('You must keep at least one column in your layout.');
63 } else if (row.select('.layout-manager-column').size() == 1) {
65 Element.remove(row.up('table').next());
66 Element.remove(row.up('table'));
69 Element.remove(rm.up('td').next());
70 Element.remove(rm.up('td'));
71 this.distributeWidth(row);
74 distributeWidth: function (row) {
75 var cols = row.select('input[type=text]');
76 var width = Math.round(100 / cols.size());
77 cols.each(function (input) {
82 $$(".layout-manager-column-add").each(this.loadColAdd.bind(this));
83 $$(".layout-manager-column-remove").each(this.loadColRemove.bind(this));
84 $$(".layout-manager-row-add").each(this.loadRowAdd.bind(this));
86 loadColAdd: function(col) {
87 col.observe('mouseover', function() {
88 this.addClassName('layout-manager-column-add_hover');
90 col.observe('mouseout', function() {
91 this.removeClassName('layout-manager-column-add_hover');
93 col.observe('click', function () { this.addCol(col) }.bind(this));
95 loadColRemove: function(rm) {
96 rm.observe('mouseover', function() {
97 this.addClassName('layout-manager-column-remove_hover');
98 }).observe('mouseout', function() {
99 this.removeClassName('layout-manager-column-remove_hover');
100 }).observe('click', function () { this.removeCol(rm); }.bind(this));
102 loadRowAdd: function(row) {
103 row.observe('mouseover', function() {
104 this.addClassName('layout-manager-row-add_hover');
106 row.observe('mouseout', function() {
107 this.removeClassName('layout-manager-row-add_hover');
109 row.observe('click', function () { this.addRow(row); }.bind(this));
113 document.observe('dom:loaded', function() {
114 if ($('layout-manager')) {
115 codendi.layout_manager.load();
116 $('save').observe('click', function(evt) {
117 if ($('layout-manager')) {
119 var invalid = $('layout-manager').select('.layout-manager-column input[type=text]').find(function (element) {
120 return (!reg.test(element.value)) ? element : false;
123 alert(invalid.value+' is not a valid number');
125 var form = $('layout-manager').up('form');
127 $('layout-manager').select('.layout-manager-row').each(function (row) {
128 form.insert(new Element('input', {
130 name: 'new_layout[]',
131 value: row.select('.layout-manager-column input[type=text]').pluck('value').join(',')
138 $$('.layout-manager-chooser').each(function (row) {
139 row.down('input[type=radio]').observe('change', function() {
140 $$('.layout-manager-chooser').each(function (row) {
141 row.removeClassName('layout-manager-chooser_selected');
143 row.addClassName('layout-manager-chooser_selected');
149 var default_categ = location.href.match(/#filter-(widget-categ-[a-zA-Z0-9-_]+)$/);
151 if (default_categ && default_categ[1]) {
152 current_categ = default_categ[1];
154 current_categ = 'widget-categ-general';
156 $$('.widget-categ-switcher').each(function (a) {
157 var scan_id = a.href.match(/#(widget-categ-[a-zA-Z0-9-_]+)$/);
158 if (scan_id && scan_id[1]) {
160 a.href = a.href.gsub(/#(widget-categ-[a-zA-Z0-9-_]+)$/, '#filter-'+id);
161 a.observe('click', function(evt) {
163 //Display widgets of this category
164 $('widget-content-categ').childElements().invoke('hide');
165 a.up('ul').select('.widget-categ-switcher').each(function(other_a) {
166 other_a.up().removeClassName('selected');
168 a.up().addClassName('selected');
171 //remove corresponding table
172 if (id != current_categ) {
175 a.up().addClassName('selected');
177 Element.remove($(id).down('h4'));
178 $('widget-content-categ').appendChild(Element.remove($(id)));