| 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 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
var ruleDiv = this.ruleDiv; |
| 29 |
|
| 30 |
this.editor = editor; |
| 31 |
this.editor.customUndo = true; |
| 32 |
this.onEditorLoaded(); |
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 139 |
|
| 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 |
|
| 167 |
|
| 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( n, b){ |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 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( node){ |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 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( t){ |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
while((t = t.parentNode) && t.tagName.toLowerCase() != "table"){} |
| 219 |
return t; |
| 220 |
} |
| 221 |
|
| 222 |
function getHeaderCell( t){ |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 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( node, width){ |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 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 |
|
| 260 |
|
| 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 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 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 |
|
| 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 |
}); |