Debian Codegraph @

dojox/editor/plugins/ResizeTableColumn.js

raw symbols SCIP
1 define([
2 "dojo",
3 "dijit",
4 "dojox",
5 "./TablePlugins"
6 ], function(dojo, dijit, dojox, TablePlugins) {
7
8
9 var ResizeTableColumn = dojo.declare("dojox.editor.plugins.ResizeTableColumn", TablePlugins, {
10
11 constructor: function(){
12 // summary:
13 // Because IE will ignore the cursor style when the editMode of the document is on,
14 // we need to create a div within the outer document to mimic the behavior of drag&drop
15 this.isLtr = this.dir ? (this.dir == "ltr") : dojo._isBodyLtr();
16 this.ruleDiv = dojo.create("div",
17 {style: "top: -10000px; z-index: 10001"},
18 dojo.body(), "last");
19 },
20
21 setEditor: function(editor){
22 // summary:
23 // Handle the drag&drop events
24 // editor:
25 // The editor which this plugin belongs to
26 // tags:
27 // protected
28 var ruleDiv = this.ruleDiv;
29
30 this.editor = editor;
31 this.editor.customUndo = true;
32 this.onEditorLoaded();
33
34 // The content of the editor is loaded asynchronously, so the function
35 // should be called when it is loaded.
36 editor.onLoadDeferred.addCallback(dojo.hitch(this, function(){
37 this.connect(this.editor.editNode, "onmousemove", function(evt){
38 var editorCoords = dojo.position(editor.iframe, true),
39 ex = editorCoords.x, cx = evt.clientX;
40
41 if(!this.isDragging){
42 // If it is just a movement, put the div at the edge of the
43 // target cell so that when the cursor hover on it, it will
44 // change to the col-resize style.
45 var obj = evt.target;
46
47 if(obj.tagName && obj.tagName.toLowerCase() == "td"){
48 var objCoords = dojo.position(obj), ox = objCoords.x, ow = objCoords.w,
49 pos = ex + objCoords.x - 2;
50 if(this.isLtr){
51 ruleDiv.headerColumn = true;
52 if(!isBoundary(obj, "first") || cx > ox + ow / 2){
53 pos += ow;
54 ruleDiv.headerColumn = false;
55 }
56 }else{
57 ruleDiv.headerColumn = false;
58 if(isBoundary(obj, "first") && cx > ox + ow / 2){
59 pos += ow;
60 ruleDiv.headerColumn = true;
61 }
62 }
63 dojo.style(ruleDiv, {
64 position: "absolute",
65 cursor: "col-resize",
66 display: "block",
67 width: "4px",
68 backgroundColor: "transparent",
69 top: editorCoords.y + objCoords.y + "px",
70 left: pos + "px",
71 height: objCoords.h + "px"
72 });
73 this.activeCell = obj;
74 }else{
75 dojo.style(ruleDiv, {display: "none", top: "-10000px"});
76 }
77 }else{
78 // Begin to drag&drop
79 var activeCell = this.activeCell,
80 activeCoords = dojo.position(activeCell), ax = activeCoords.x, aw = activeCoords.w,
81 sibling = nextSibling(activeCell), siblingCoords, sx, sw,
82 containerCoords = dojo.position(getTable(activeCell).parentNode),
83 ctx = containerCoords.x, ctw = containerCoords.w;
84
85 if(sibling){
86 siblingCoords = dojo.position(sibling);
87 sx = siblingCoords.x;
88 sw = siblingCoords.w;
89 }
90
91 // The leading and trailing columns can only be sized to the extent of the containing div.
92 if(this.isLtr &&
93 ((ruleDiv.headerColumn && sibling && ctx < cx && cx < ax + aw) ||
94 ((!sibling && ax < cx && cx < ctx + ctw) || (sibling && ax < cx && cx < sx + sw))) ||
95 !this.isLtr &&
96 ((ruleDiv.headerColumn && sibling && ctx > cx && cx > ax) ||
97 ((!sibling && ax + aw > cx && cx > ctx) || (sibling && ax + aw > cx && cx > sx)))){
98 dojo.style(ruleDiv, {left: ex + cx + "px"});
99 }
100 }
101 });
102
103 this.connect(ruleDiv, "onmousedown", function(evt){
104 var editorCoords = dojo.position(editor.iframe, true),
105 tableCoords = dojo.position(getTable(this.activeCell));
106
107 this.isDragging = true;
108 dojo.style(editor.editNode, {cursor: "col-resize"});
109 dojo.style(ruleDiv, {
110 width: "1px",
111 left: evt.clientX + "px",
112 top: editorCoords.y + tableCoords.y + "px",
113 height: tableCoords.h + "px",
114 backgroundColor: "#777"
115 });
116 });
117
118 this.connect(ruleDiv, "onmouseup", function(evt){
119 var activeCell = this.activeCell,
120 activeCoords = dojo.position(activeCell), aw = activeCoords.w, ax = activeCoords.x,
121 sibling = nextSibling(activeCell), siblingCoords, sx, sw,
122 editorCoords = dojo.position(editor.iframe), ex = editorCoords.x,
123 table = getTable(activeCell), tableCoords = dojo.position(table),
124 cs = table.getAttribute("cellspacing"),
125 cx = evt.clientX,
126 headerCell = getHeaderCell(activeCell), headerSibling,
127 newWidth, newSiblingWidth;
128
129 if(!cs || (cs = parseInt(cs, 10)) < 0){ cs = 2; }
130
131 if(sibling){
132 siblingCoords = dojo.position(sibling);
133 sx = siblingCoords.x;
134 sw = siblingCoords.w;
135 headerSibling = getHeaderCell(sibling);
136 }
137
138 // The delta width is either taken from or added to the adjacent column on the trailing edge.
139 // Sizing the rightmost or leftmost columns affects only those columns.
140 if(this.isLtr){
141 if(ruleDiv.headerColumn){
142 newWidth = ex + ax + aw - cx;
143 }else{
144 newWidth = cx - ex - ax;
145 if(sibling) { newSiblingWidth = ex + sx + sw - cx - cs; }
146 }
147 }else{
148 if(ruleDiv.headerColumn){
149 newWidth = cx - ex - ax;
150 }else{
151 newWidth = ex + ax + aw - cx;
152 if(sibling) { newSiblingWidth = cx - ex - sx - cs; }
153 }
154 }
155
156 this.isDragging = false;
157 marginBox(headerCell, newWidth);
158 if(sibling){
159 if(!ruleDiv.headerColumn){
160 marginBox(headerSibling, newSiblingWidth);
161 }
162 }
163 if(ruleDiv.headerColumn && isBoundary(activeCell, "first") || isBoundary(activeCell, "last")){
164 dojo.marginBox(table, {w: tableCoords.w + newWidth - aw});
165 }
166 // Do it again to consolidate the result,
167 // because maybe the cell cannot be so narrow as you specified.
168 marginBox(headerCell, dojo.position(activeCell).w);
169 if(sibling){
170 marginBox(headerSibling, dojo.position(sibling).w);
171 }
172 dojo.style(editor.editNode, {cursor: "auto"});
173 dojo.style(ruleDiv, {display: "none", top: "-10000px"});
174 this.activeCell = null;
175 });
176 }));
177
178 function isBoundary(/*DomNode*/ n, /*String*/ b){
179 // summary:
180 // Check if the current cell is in the first column or
181 // in the last column.
182 // n:
183 // The node of a table cell
184 // b:
185 // Indicate if the cell node is compared with the first coluln
186 // or the last column
187 var nodes = dojo.query("> td", n.parentNode);
188 switch(b){
189 case "first":
190 return nodes[0] == n;
191 case "last":
192 return nodes[nodes.length - 1] == n;
193 default:
194 return false;
195 }
196 }
197
198 function nextSibling(/*DomNode*/ node){
199 // summary:
200 // Get the next cell in row
201 // node:
202 // The table cell
203 node = node.nextSibling
204 while(node){
205 if(node.tagName && node.tagName.toLowerCase() == "td"){
206 break;
207 }
208 node = node.nextSibling
209 }
210 return node;
211 }
212
213 function getTable(/*DomNode*/ t){
214 // summary:
215 // Get the table that this cell belongs to.
216 // t:
217 // The table cell
218 while((t = t.parentNode) && t.tagName.toLowerCase() != "table"){}
219 return t;
220 }
221
222 function getHeaderCell(/*DomNode*/ t){
223 // summary:
224 // Get the table cell in the first row that shares the same
225 // column with the node t.
226 // t:
227 // The node of the table cell
228 var tds = dojo.query("td", getTable(t)),
229 len = tds.length;
230 for(var i = 0; i < len; i++){
231 if(dojo.position(tds[i]).x == dojo.position(t).x){
232 return tds[i];
233 }
234 }
235 return null;
236 }
237
238 function marginBox(/*DomNode*/ node, /*Number*/ width){
239 // summary:
240 // In IE, if the border width of the td is not specified in table, the default value is 1px,
241 // though it is marked "medium".
242 // node:
243 // The node to be set width
244 // width:
245 // The new width of the node
246 if(dojo.isIE){
247 var s = node.currentStyle,
248 bl = px(node, s.borderLeftWidth), br = px(node, s.borderRightWidth),
249 pl = px(node, s.paddingLeft), pr = px(node, s.paddingRight);
250
251 node.style.width = width - bl - br - pl - pr;
252 }else{
253 dojo.marginBox(node, {w: width});
254 }
255
256 function px(element, avalue){
257 if(!avalue){ return 0; }
258 if(avalue == "medium"){ return 1; }
259 // style values can be floats, client code may
260 // want to round this value for integer pixels.
261 if(avalue.slice && avalue.slice(-2) == 'px'){ return parseFloat(avalue); }
262 with(element){
263 var sLeft = style.left;
264 var rsLeft = runtimeStyle.left;
265 runtimeStyle.left = currentStyle.left;
266 try{
267 // 'avalue' may be incompatible with style.left, which can cause IE to throw
268 // this has been observed for border widths using "thin", "medium", "thick" constants
269 // those particular constants could be trapped by a lookup
270 // but perhaps there are more
271 style.left = avalue;
272 avalue = style.pixelLeft;
273 }catch(e){
274 avalue = 0;
275 }
276 style.left = sLeft;
277 runtimeStyle.left = rsLeft;
278 }
279 return avalue;
280 }
281 }
282 }
283 });
284
285 dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
286 if(o.plugin){ return; }
287 // make first character lower case
288 if(o.args && o.args.command){
289 var cmd = o.args.command.charAt(0).toLowerCase() + o.args.command.substring(1, o.args.command.length);
290 if(cmd == "resizeTableColumn"){
291 o.plugin = new ResizeTableColumn({commandName: cmd});
292 }
293 }
294 });
295
296 return ResizeTableColumn;
297 });