]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/tablekit-trueresize.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1294 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / tablekit-trueresize.js
1 /**\r
2  * This work is licensed under a Creative Commons Attribution 3.0 License\r
3  * (http://creativecommons.org/licenses/by/3.0/).\r
4  *\r
5  * You are free:\r
6  *    to Share - to copy, distribute and transmit the work\r
7  *    to Remix - to adapt the work\r
8  * Under the following conditions:\r
9  *    Attribution. You must attribute the work in the manner specified\r
10  *    by the author or licensor (but not in any way that suggests that\r
11  *    they endorse you or your use of the work).\r
12  *\r
13  * For any reuse or distribution, you must make clear to others the license\r
14  * terms of this work. The best way to do this is with a link to the\r
15  * Creative Commons web page.\r
16  *\r
17  * Any of the above conditions can be waived if you get permission from\r
18  * the copyright holder. Nothing in this license impairs or restricts\r
19  * the author's moral rights.\r
20  *\r
21  * Disclaimer\r
22  *\r
23  * Your fair dealing and other rights are in no way affected by the  above.\r
24  * This is a human-readable summary of the Legal Code (the full license).\r
25  *\r
26  * The author of this work is Vlad Bailescu (http://vlad.bailescu.ro). No\r
27  * warranty or support will be provided for this work, although updates\r
28  * might be made available at http://vlad.bailescu.ro/javascript/tablekit .\r
29  *\r
30  * Licence code and basic description provided by Creative Commons.\r
31  *\r
32  */\r
33 Object.extend(TableKit.options || {}, {\r
34         // If true table width gets recalculated on column resize\r
35         trueResize : false,\r
36         // If true table width will be kept constant on column resize\r
37         keepWidth : false\r
38 });\r
39         \r
40 Object.extend(TableKit.Resizable, {\r
41         resize : function(table, index, w) {\r
42                 // This part is taken from Andrew Tetlaw's original TableKit.Resizable.resize\r
43                 var cell;\r
44                 if(typeof index === 'number') {\r
45                         if(!table || (table.tagName && table.tagName !== "TABLE")) {return;}\r
46                         table = $(table);\r
47                         index = Math.min(table.rows[0].cells.length, index);\r
48                         index = Math.max(1, index);\r
49                         index -= 1;\r
50                         cell = (table.tHead && table.tHead.rows.length > 0) ? $(table.tHead.rows[table.tHead.rows.length-1].cells[index]) : $(table.rows[0].cells[index]);\r
51                 } else {\r
52                         cell = $(index);\r
53                         table = table ? $(table) : cell.up('table');\r
54                         index = TableKit.getCellIndex(cell);\r
55                 }\r
56                 // And now, for the fun stuff\r
57                 // Read the new options values for the given table\r
58                 var op = TableKit.option('trueResize keepWidth minWidth', table.id);\r
59                 // Took also the minWidth as we're gonna use it later anyway\r
60                 var pad = parseInt(cell.getStyle('paddingLeft'),10) + parseInt(cell.getStyle('paddingRight'),10);\r
61                 // Improvement: add cell border to padding as width incorporates both\r
62                 pad += parseInt(cell.getStyle('borderLeftWidth'),10) + parseInt(cell.getStyle('borderRightWidth'),10);\r
63                 w = Math.max(w-pad, op.minWidth); // This will also be used later\r
64                 if (!op.trueResize) {\r
65                         // Original resize method\r
66                         cell.setStyle({'width' : w + 'px'});  // Using previously read minWidth instead of the old way\r
67                 } else {\r
68                         // New stuff\r
69                         var delta = (w + pad - parseInt(cell.getWidth()));\r
70                         if (!op.keepWidth) {\r
71                                 // We'll be updating the table width\r
72                                 var tableWidth = parseInt(table.getWidth()) + delta;\r
73                                 cell.setStyle({'width' : w + 'px'});\r
74                                 table.setStyle({'width' : tableWidth + 'px'});\r
75                         } else {\r
76                                 // Let's see what we have to do to keep the table width constant\r
77                                 var cells = TableKit.getHeaderCells(table);\r
78                                 if (index < 0 || index > cells.length) { return; }\r
79                                 var nbour;\r
80                                 if (index == cells.length - 1) {        // Rightmost cell\r
81                                         nbour = cells[index - 1];\r
82                                 } else {        // Left or inner cell\r
83                                         nbour = cells[index + 1];\r
84                                 }\r
85                                 var nbourWidth = parseInt(nbour.getWidth());\r
86                                 var nbourPad = parseInt(nbour.getStyle('paddingLeft'),10) + parseInt(nbour.getStyle('paddingRight'),10);\r
87                                 var proposedNbourWidth = nbourWidth - nbourPad  - delta;\r
88                                 var newNbourWidth;\r
89                                 if (proposedNbourWidth < op.minWidth) {\r
90                                         // Don't be mean to neighbours. Step off their porch.\r
91                                         newNbourWidth = Math.min(nbourWidth - nbourPad, op.minWidth);\r
92                                         w -= newNbourWidth - proposedNbourWidth;\r
93                                 } else {\r
94                                         newNbourWidth = proposedNbourWidth;\r
95                                 }\r
96                                 nbour.setStyle({'width' : newNbourWidth + 'px'});\r
97                                 cell.setStyle({'width' : w + 'px'});\r
98                         }\r
99                 }\r
100         }\r
101 });