| 1 |
/* |
| 2 |
* Copyright 2004-2006 the Seasar Foundation and the Others. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 13 |
* either express or implied. See the License for the specific language |
| 14 |
* governing permissions and limitations under the License. |
| 15 |
*/ |
| 16 |
if (typeof(Kumu) == 'undefined') { |
| 17 |
Kumu = {}; |
| 18 |
} |
| 19 |
|
| 20 |
if (typeof(Kumu.DHTML) == 'undefined'){ |
| 21 |
Kumu.DHTML = {}; |
| 22 |
} |
| 23 |
|
| 24 |
Kumu.extend(Kumu.DHTML, { |
| 25 |
|
| 26 |
getPosition: function(element){ |
| 27 |
element = $i(element); |
| 28 |
var offsetTop = 0; |
| 29 |
var offsetLeft = 0; |
| 30 |
do { |
| 31 |
offsetLeft += element.offsetLeft || 0; |
| 32 |
offsetTop += element.offsetTop || 0; |
| 33 |
element = element.parentNode; |
| 34 |
} while(element) |
| 35 |
return [offsetLeft, offsetTop]; |
| 36 |
}, |
| 37 |
|
| 38 |
getSize: function(element){ |
| 39 |
element = $i(element) |
| 40 |
var width = element.offsetWidth; |
| 41 |
var height = element.offsetHeight; |
| 42 |
return [width, height]; |
| 43 |
}, |
| 44 |
|
| 45 |
move: function(element, x, y){ |
| 46 |
var styleObj = $i(element).style; |
| 47 |
if(styleObj.position != 'absolute'){ |
| 48 |
styleObj.position = 'absolute'; |
| 49 |
} |
| 50 |
var units = (typeof styleObj.left == 'string') ? 'px' : 0; |
| 51 |
styleObj.left = x + units; |
| 52 |
styleObj.top = y + units; |
| 53 |
}, |
| 54 |
|
| 55 |
moveBy: function(element, deltaX, deltaY){ |
| 56 |
var pos = this.getPosition(element); |
| 57 |
var styleObj = $i(element).style; |
| 58 |
if(styleObj.position != 'absolute'){ |
| 59 |
styleObj.position = 'absolute'; |
| 60 |
} |
| 61 |
var units = (typeof styleObj.left == 'string') ? 'px' : 0; |
| 62 |
styleObj.left = (parseInt(pos[0]) + deltaX) + units; |
| 63 |
styleObj.top = (parseInt(pos[1]) + deltaY) + units; |
| 64 |
}, |
| 65 |
|
| 66 |
setZIndex: function(element, index){ |
| 67 |
var styleObj = $i(element).styel; |
| 68 |
styleObj.zIndex = index; |
| 69 |
}, |
| 70 |
|
| 71 |
clone: function(src, dest){ |
| 72 |
var source = $(src); |
| 73 |
var target = $(dest); |
| 74 |
target.style.position = 'absolute'; |
| 75 |
var offsets = this.getPosition(source); |
| 76 |
target.style.top = offsets[1] + 'px'; |
| 77 |
target.style.left = offsets[0] + 'px'; |
| 78 |
target.style.width = source.offsetWidth + 'px'; |
| 79 |
target.style.height = source.offsetHeight + 'px'; |
| 80 |
}, |
| 81 |
|
| 82 |
insertBefore: function(node, ref){ |
| 83 |
if((!node)||(!ref)){ |
| 84 |
return false; |
| 85 |
} |
| 86 |
node = $i(node); |
| 87 |
ref = $i(ref); |
| 88 |
var parent = ref.parentNode; |
| 89 |
parent.insertBefore(node, ref); |
| 90 |
return true; |
| 91 |
}, |
| 92 |
|
| 93 |
insertAfter: function(node, ref){ |
| 94 |
if((!node)||(!ref)){ |
| 95 |
return false; |
| 96 |
} |
| 97 |
node = $i(node); |
| 98 |
ref = $i(ref); |
| 99 |
var pn = ref.parentNode; |
| 100 |
if(ref == pn.lastChild){ |
| 101 |
pn.appendChild(node); |
| 102 |
}else{ |
| 103 |
return this.insertBefore(node, ref.nextSibling); |
| 104 |
} |
| 105 |
return true; |
| 106 |
}, |
| 107 |
|
| 108 |
insert: function(node, ref, position){ |
| 109 |
if((!node)||(!ref)||(!position)){ |
| 110 |
return false; |
| 111 |
} |
| 112 |
node = $i(node); |
| 113 |
red = $i(ref); |
| 114 |
switch(position.toLowerCase){ |
| 115 |
case 'before': |
| 116 |
return this.insertBefore(node, ref); |
| 117 |
case 'after': |
| 118 |
return this.insertAfter(node, ref); |
| 119 |
case 'first': |
| 120 |
if(ref.firstChild){ |
| 121 |
return this.insertBefore(node, ref); |
| 122 |
}else{ |
| 123 |
ref.appendChild(node); |
| 124 |
return true; |
| 125 |
} |
| 126 |
default: |
| 127 |
ref.appendChild(node); |
| 128 |
return true; |
| 129 |
} |
| 130 |
}, |
| 131 |
|
| 132 |
getScrollPosition: function(){ |
| 133 |
if(typeof window.pageYOffset != 'undefined'){ |
| 134 |
return [window.pageXOffset, window.pageYOffset]; |
| 135 |
}else if(typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0){ |
| 136 |
return [document.documentElement.scrollLeft, document.documentElement.scrollTop]; |
| 137 |
}else if(typeof document.body.scrollTop != 'undefined'){ |
| 138 |
return [document.body.scrollLeft, document.body.scrollTop]; |
| 139 |
}else{ |
| 140 |
return [0, 0]; |
| 141 |
} |
| 142 |
}, |
| 143 |
|
| 144 |
getEventTarget: function(event){ |
| 145 |
var target = (event.target) ? event.target : event.srcElement; |
| 146 |
while(target.nodeType == 3 && target.parentNode != null){ |
| 147 |
target == target.parentNode; |
| 148 |
} |
| 149 |
return taget; |
| 150 |
}, |
| 151 |
|
| 152 |
makeClipping : function(element) { |
| 153 |
var self = Kumu.DHTML; |
| 154 |
element = $i(element); |
| 155 |
if (element._overflow){ |
| 156 |
return; |
| 157 |
} |
| 158 |
element._overflow = element.style.overflow; |
| 159 |
if ((self.getStyle(element, 'overflow') || 'visible') != 'hidden'){ |
| 160 |
element.style.overflow = 'hidden'; |
| 161 |
} |
| 162 |
}, |
| 163 |
|
| 164 |
undoClipping : function(element) { |
| 165 |
element = $i(element); |
| 166 |
if (element._overflow){ |
| 167 |
return; |
| 168 |
} |
| 169 |
element.style.overflow = element._overflow; |
| 170 |
element._overflow = undefined; |
| 171 |
}, |
| 172 |
|
| 173 |
_getStyle : function(element, property){ |
| 174 |
element = Kumu.$i(element); |
| 175 |
var value = element.style[property.camelize]; |
| 176 |
if(!value){ |
| 177 |
if(document.defaultView && document.defaultView.getComputedStyle){ |
| 178 |
var css = document.defaultView.getComputedStyle(element, null); |
| 179 |
value = css ? css.getPropertyValue(property) : null; |
| 180 |
}else if(element.currentStyle){ |
| 181 |
value = element.currentStyle[property.camelize()]; |
| 182 |
} |
| 183 |
} |
| 184 |
return (value == 'auto') ? null : value; |
| 185 |
}, |
| 186 |
|
| 187 |
getOpacity : function(element){ |
| 188 |
var self = Kumu.DHTML; |
| 189 |
element = Kumu.$i(element); |
| 190 |
var opacity; |
| 191 |
if(opacity = self._getStyle(element, 'opacity')){ |
| 192 |
return parseFloat(opacity); |
| 193 |
} |
| 194 |
if(opacity = (self._getStyle(element, 'filter') || '').match(/alpha\(opacity=(.*)\)/)){ |
| 195 |
if(opacity[1]){ |
| 196 |
return parseFloat(opacity[1]) / 100; |
| 197 |
} |
| 198 |
} |
| 199 |
return 1.0; |
| 200 |
}, |
| 201 |
|
| 202 |
getStyle : function (element, style){ |
| 203 |
var self = Kumu.DHTML; |
| 204 |
element = $i(element); |
| 205 |
if(style == 'opacity'){ |
| 206 |
return self.getOpacity(element); |
| 207 |
}else{ |
| 208 |
return self._getStyle(element, style); |
| 209 |
} |
| 210 |
}, |
| 211 |
|
| 212 |
setStyle : function(element, style) { |
| 213 |
var self = Kumu.DHTML; |
| 214 |
element = Kumu.$i(element); |
| 215 |
for(var v in style){ |
| 216 |
if(v == 'opacity'){ |
| 217 |
self.setOpacity(element, style[v]); |
| 218 |
}else{ |
| 219 |
element.style[v.camelize()] = style[v]; |
| 220 |
} |
| 221 |
} |
| 222 |
}, |
| 223 |
|
| 224 |
setOpacity : function(element, value){ |
| 225 |
var self = Kumu.DHTML; |
| 226 |
//element= $i(element); |
| 227 |
if(value == 1){ |
| 228 |
var value = (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 0.999999 : null; |
| 229 |
self.setStyle(element, {opacity:value}); |
| 230 |
if(/MSIE/.test(navigator.userAgent)){ |
| 231 |
self.setStyle(element, {filter:self._getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'')}); |
| 232 |
} |
| 233 |
} else { |
| 234 |
self.setStyle(element, {opacity: value}); |
| 235 |
if(/MSIE/.test(navigator.userAgent)){ |
| 236 |
self.setStyle(element, {filter:self._getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'') + 'alpha(opacity='+value*100+')'}); |
| 237 |
} |
| 238 |
} |
| 239 |
}, |
| 240 |
|
| 241 |
parseColor : function(code) { |
| 242 |
if(!code){ |
| 243 |
return code; |
| 244 |
} |
| 245 |
var self = Kumu.DHTML; |
| 246 |
var i; |
| 247 |
var result = []; |
| 248 |
if(code.slice(0, 4) == 'rgb('){ |
| 249 |
var col = code.slice(4, code.length-1).split(','); |
| 250 |
for(i = 0; i < col.length; i++){ |
| 251 |
result.push(parseInt(col[i]) / 255.0); |
| 252 |
} |
| 253 |
}else{ |
| 254 |
if(code.charAt(0) == '#'){ |
| 255 |
code = code.substring(1); |
| 256 |
} |
| 257 |
var hex; |
| 258 |
if(code.length == 3){ |
| 259 |
for(i = 0; i < 3; i++){ |
| 260 |
hex = code.substr(i, 1); |
| 261 |
result.push(parseInt(hex + hex, 16) / 255.0); |
| 262 |
} |
| 263 |
}else if(code.length == 6){ |
| 264 |
for(i = 0; i < 6; i+=2){ |
| 265 |
hex = code.substr(i, 2); |
| 266 |
result.push(parseInt(hex, 16) / 255.0); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
return result; |
| 271 |
}, |
| 272 |
|
| 273 |
toHexString : function(code){ |
| 274 |
var self = Kumu.DHTML; |
| 275 |
var result; |
| 276 |
|
| 277 |
result = '#' + |
| 278 |
self.toColorPart(self._clampColorComponent(code[0], 255)) + |
| 279 |
self.toColorPart(self._clampColorComponent(code[1], 255)) + |
| 280 |
self.toColorPart(self._clampColorComponent(code[2], 255)); |
| 281 |
return result; |
| 282 |
}, |
| 283 |
|
| 284 |
toColorPart : function(num){ |
| 285 |
num = Math.round(num); |
| 286 |
var digit = num.toString(16); |
| 287 |
if(num < 16){ |
| 288 |
return '0'+digit; |
| 289 |
} |
| 290 |
return digit; |
| 291 |
}, |
| 292 |
|
| 293 |
_clampColorComponent : function(v, scale){ |
| 294 |
v *= scale; |
| 295 |
if(v < 0){ |
| 296 |
return 0; |
| 297 |
}else{ |
| 298 |
return Math.max(v, Math.min(v, scale)); |
| 299 |
} |
| 300 |
}, |
| 301 |
|
| 302 |
blendColor : function(from, to, fraction){ |
| 303 |
var self = Kumu.DHTML; |
| 304 |
var fromColor; |
| 305 |
var toColor; |
| 306 |
var df; |
| 307 |
var sf; |
| 308 |
if(typeof(from) == 'string'){ |
| 309 |
from = self.parseColor(from); |
| 310 |
} |
| 311 |
if(typeof(to) == 'string'){ |
| 312 |
to = self.parseColor(to); |
| 313 |
} |
| 314 |
sf = 1.0 - fraction; |
| 315 |
df = fraction; |
| 316 |
|
| 317 |
if(typeof(from) == 'string'){ |
| 318 |
from = self.parseColor(from); |
| 319 |
} |
| 320 |
if(typeof(to) == 'string'){ |
| 321 |
to = self.parseColor(to); |
| 322 |
} |
| 323 |
return [ |
| 324 |
(from[0] * sf) + (to[0] * df), |
| 325 |
(from[1] * sf) + (to[1] * df), |
| 326 |
(from[2] * sf) + (to[2] * df) |
| 327 |
]; |
| 328 |
} |
| 329 |
|
| 330 |
}); |
| 331 |
|
| 332 |
|