Skip to content

Instantly share code, notes, and snippets.

@SirPepe
Created September 25, 2011 14:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SirPepe/1240684 to your computer and use it in GitHub Desktop.
Save SirPepe/1240684 to your computer and use it in GitHub Desktop.
<component lightWeight="true">
<attach event="onpropertychange" onevent="checkPropertyChange()" />
<attach event="ondetach" onevent="restore()" />
<attach event="onresize" for="window" onevent="restore();init()" />
<script type="text/javascript">
//<![CDATA[
var viewportwidth = (typeof window.innerWidth != 'undefined' ? window.innerWidth : element.document.documentElement.clientWidth);
// Shortcut for the document object
var doc = element.document;
//doc.body.style.width = viewportwidth;
/*
* init gets called once at the start and then never again,
* triggers box-sizing calculations and updates width and height
*/
function init(){
// check for IE8+
if(typeof(element.style.boxSizing) == "undefined"){
updateBorderBoxWidth();
updateBorderBoxHeight();
}
}
/*
* restore gets called when the behavior is being detached (see event binding at the top),
* resets everything like it was before applying the behavior
*/
function restore(){
// check for IE8+
if(typeof(element.style.boxSizing) == "undefined"){
element.runtimeStyle.width = "";
element.runtimeStyle.height = "";
}
}
/*
* checkPropertyChange gets called as soon as an element property changes
* (see event binding at the top), it then checks if any property influencing its
* dimensions was changed and if yes recalculates width and height
*/
function checkPropertyChange(){
// check for IE8+
if(typeof(element.style.boxSizing) == "undefined"){
var pn = event.propertyName;
var undef;
if(pn == "style.boxSizing" && element.style.boxSizing == ""){
element.style.removeAttribute("boxSizing");
element.runtimeStyle.boxSizing = undef;
}
switch (pn){
case "style.width":
case "style.borderLeftWidth":
case "style.borderLeftStyle":
case "style.borderRightWidth":
case "style.borderRightStyle":
case "style.paddingLeft":
case "style.paddingRight":
updateBorderBoxWidth();
break;
case "style.height":
case "style.borderTopWidth":
case "style.borderTopStyle":
case "style.borderBottomWidth":
case "style.borderBottomStyle":
case "style.paddingTop":
case "style.paddingBottom":
updateBorderBoxHeight();
break;
case "className":
case "style.boxSizing":
updateBorderBoxWidth();
updateBorderBoxHeight();
break;
}
}
}
/*
* Helper function, taken from Dean Edward's IE7 framework,
* added by Schepp on 12.06.2010.
* http://code.google.com/p/ie7-js/
* MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* Allows us to convert from relative to pixel-values.
*
* Citing Dean himself:
* "MSIE6+ has special pixelLeft/Width/Height etc properties.
* They represent the current pixel value of the equivalent style setting.
* So, if you have style.width=8em then style.pixelWidth would return the
* pixel equivalent. MSIE also supports an override style called runtimeStyle.
* Setting properties on runtimeStyle overrides all other style properties.
*
* This trick works by setting style.left and then getting MSIE to convert
* it by calling style.pixelLeft. To stop the element moving around the screen
* when we do this, we set runtimeStyle.left with the current left value.
* After we’ve done the conversion we set everything back to the way it was.
*
* http://ajaxian.com/archives/computed-vs-cascaded-style
*/
function getPixelValue(value){
var PIXEL = /^\d+(px)?$/i;
if (PIXEL.test(value)) return parseInt(value);
var style = element.style.left;
var runtimeStyle = element.runtimeStyle.left;
element.runtimeStyle.left = element.currentStyle.left;
element.style.left = value || 0;
value = parseInt(element.style.pixelLeft);
element.style.left = style;
element.runtimeStyle.left = runtimeStyle;
return value;
}
function getPixelWidth(object, value){
// For Pixel Values
var PIXEL = /^\d+(px)?$/i;
if (PIXEL.test(value)) return parseInt(value);
// For Percentage Values
var PERCENT = /^[\d\.]+%$/i;
if (PERCENT.test(value)){
try{
parentWidth = getPixelWidth(object.parentElement,(object.parentElement.currentStyle.width != "auto" ? object.parentElement.currentStyle.width : "100%"));
value = (parseFloat(value) / 100) * parentWidth;
}
catch(e){
value = (parseFloat(value) / 100) * element.document.documentElement.clientWidth;
}
return parseInt(value);
}
// For EM Values
var style = object.style.left;
var runtimeStyle = object.runtimeStyle.left;
object.runtimeStyle.left = object.currentStyle.left;
object.style.left = value || 0;
value = parseInt(object.style.pixelLeft);
object.style.left = style;
object.runtimeStyle.left = runtimeStyle;
return value;
}
/*
* getBorderWidth & friends
* Border width getters
*/
function getBorderWidth(sSide){
if(element.currentStyle["border" + sSide + "Style"] == "none"){
return 0;
}
var n = getPixelValue(element.currentStyle["border" + sSide + "Width"]);
return n || 0;
}
function getBorderLeftWidth() { return getBorderWidth("Left"); }
function getBorderRightWidth() { return getBorderWidth("Right"); }
function getBorderTopWidth() { return getBorderWidth("Top"); }
function getBorderBottomWidth() { return getBorderWidth("Bottom"); }
/*
* getPadding & friends
* Padding width getters
*/
function getPadding(sSide) {
var n = getPixelValue(element.currentStyle["padding" + sSide]);
return n || 0;
}
function getPaddingLeft() { return getPadding("Left"); }
function getPaddingRight() { return getPadding("Right"); }
function getPaddingTop() { return getPadding("Top"); }
function getPaddingBottom() { return getPadding("Bottom"); }
/*
* getBoxSizing
* Get the box-sizing value for the current element
*/
function getBoxSizing(){
var s = element.style;
var cs = element.currentStyle
if(typeof s.boxSizing != "undefined" && s.boxSizing != ""){
return s.boxSizing;
}
if(typeof s["box-sizing"] != "undefined" && s["box-sizing"] != ""){
return s["box-sizing"];
}
if(typeof cs.boxSizing != "undefined" && cs.boxSizing != ""){
return cs.boxSizing;
}
if(typeof cs["box-sizing"] != "undefined" && cs["box-sizing"] != ""){
return cs["box-sizing"];
}
return getDocumentBoxSizing();
}
/*
* getDocumentBoxSizing
* Get the default document box sizing (check for quirks mode)
*/
function getDocumentBoxSizing(){
if(doc.compatMode == null || doc.compatMode == "BackCompat"){
return "border-box";
}
return "content-box"
}
/*
* setBorderBoxWidth & friends
* Width and height setters
*/
function setBorderBoxWidth(n){
element.runtimeStyle.width = Math.max(0, n - getBorderLeftWidth() -
getPaddingLeft() - getPaddingRight() - getBorderRightWidth()) + "px";
}
function setBorderBoxHeight(n){
element.runtimeStyle.height = Math.max(0, n - getBorderTopWidth() -
getPaddingTop() - getPaddingBottom() - getBorderBottomWidth()) + "px";
}
function setContentBoxWidth(n){
element.runtimeStyle.width = Math.max(0, n + getBorderLeftWidth() +
getPaddingLeft() + getPaddingRight() + getBorderRightWidth()) + "px";
}
function setContentBoxHeight(n){
element.runtimeStyle.height = Math.max(0, n + getBorderTopWidth() +
getPaddingTop() + getPaddingBottom() + getBorderBottomWidth()) + "px";
}
/*
* updateBorderBoxWidth & updateBorderBoxHeight
*
*/
function updateBorderBoxWidth() {
if(getDocumentBoxSizing() == getBoxSizing()){
return;
}
var csw = element.currentStyle.width;
if(csw != "auto"){
csw = getPixelWidth(element,csw);
if(getBoxSizing() == "border-box"){
setBorderBoxWidth(parseInt(csw));
}
else{
setContentBoxWidth(parseInt(csw));
}
}
}
function updateBorderBoxHeight() {
if(getDocumentBoxSizing() == getBoxSizing()){
return;
}
var csh = element.currentStyle.height;
if(csh != "auto"){
csh = getPixelValue(csh);
if(getBoxSizing() == "border-box"){
setBorderBoxHeight(parseInt(csh));
}
else{
setContentBoxHeight(parseInt(csh));
}
}
}
// Run the calculations
init();
//]]>
</script>
</component>
=======
<component lightWeight="true">
<attach event="onpropertychange" onevent="checkPropertyChange()" />
<attach event="ondetach" onevent="restore()" />
<attach event="onresize" for="window" onevent="restore();init()" />
<script>
//<![CDATA[
var viewportwidth = (typeof window.innerWidth != 'undefined' ? window.innerWidth : element.document.documentElement.clientWidth);
// Shortcut for the document object
var doc = element.document;
//doc.body.style.width = viewportwidth;
/*
* init gets called once at the start and then never again,
* triggers box-sizing calculations and updates width and height
*/
function init(){
// check for IE8+
if(typeof(element.style.boxSizing) == "undefined"){
updateBorderBoxWidth();
updateBorderBoxHeight();
}
}
/*
* restore gets called when the behavior is being detached (see event binding at the top),
* resets everything like it was before applying the behavior
*/
function restore(){
// check for IE8+
if(typeof(element.style.boxSizing) == "undefined"){
element.runtimeStyle.width = "";
element.runtimeStyle.height = "";
}
}
/*
* checkPropertyChange gets called as soon as an element property changes
* (see event binding at the top), it then checks if any property influencing its
* dimensions was changed and if yes recalculates width and height
*/
function checkPropertyChange(){
// check for IE8+
if(typeof(element.style.boxSizing) == "undefined"){
var pn = event.propertyName;
var undef;
if(pn == "style.boxSizing" && element.style.boxSizing == ""){
element.style.removeAttribute("boxSizing");
element.runtimeStyle.boxSizing = undef;
}
switch (pn){
case "style.width":
case "style.borderLeftWidth":
case "style.borderLeftStyle":
case "style.borderRightWidth":
case "style.borderRightStyle":
case "style.paddingLeft":
case "style.paddingRight":
updateBorderBoxWidth();
break;
case "style.height":
case "style.borderTopWidth":
case "style.borderTopStyle":
case "style.borderBottomWidth":
case "style.borderBottomStyle":
case "style.paddingTop":
case "style.paddingBottom":
updateBorderBoxHeight();
break;
case "className":
case "style.boxSizing":
updateBorderBoxWidth();
updateBorderBoxHeight();
break;
}
}
}
/*
* Helper function, taken from Dean Edward's IE7 framework,
* added by Schepp on 12.06.2010.
* http://code.google.com/p/ie7-js/
* MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* Allows us to convert from relative to pixel-values.
*
* Citing Dean himself:
* "MSIE6+ has special pixelLeft/Width/Height etc properties.
* They represent the current pixel value of the equivalent style setting.
* So, if you have style.width=8em then style.pixelWidth would return the
* pixel equivalent. MSIE also supports an override style called runtimeStyle.
* Setting properties on runtimeStyle overrides all other style properties.
*
* This trick works by setting style.left and then getting MSIE to convert
* it by calling style.pixelLeft. To stop the element moving around the screen
* when we do this, we set runtimeStyle.left with the current left value.
* After we’ve done the conversion we set everything back to the way it was.
*
* http://ajaxian.com/archives/computed-vs-cascaded-style
*/
function getPixelValue(value){
var PIXEL = /^\d+(px)?$/i;
if (PIXEL.test(value)) return parseInt(value);
var style = element.style.left;
var runtimeStyle = element.runtimeStyle.left;
element.runtimeStyle.left = element.currentStyle.left;
element.style.left = value || 0;
value = parseInt(element.style.pixelLeft);
element.style.left = style;
element.runtimeStyle.left = runtimeStyle;
return value;
}
function getPixelWidth(object, value){
// For Pixel Values
var PIXEL = /^\d+(px)?$/i;
if (PIXEL.test(value)) return parseInt(value);
// For Percentage Values
var PERCENT = /^[\d\.]+%$/i;
if (PERCENT.test(value)){
try{
parentWidth = getPixelWidth(object.parentElement,(object.parentElement.currentStyle.width != "auto" ? object.parentElement.currentStyle.width : "100%"));
value = (parseFloat(value) / 100) * parentWidth;
}
catch(e){
value = (parseFloat(value) / 100) * element.document.documentElement.clientWidth;
}
return parseInt(value);
}
// For EM Values
var style = object.style.left;
var runtimeStyle = object.runtimeStyle.left;
object.runtimeStyle.left = object.currentStyle.left;
object.style.left = value || 0;
value = parseInt(object.style.pixelLeft);
object.style.left = style;
object.runtimeStyle.left = runtimeStyle;
return value;
}
/*
* getBorderWidth & friends
* Border width getters
*/
function getBorderWidth(sSide){
if(element.currentStyle["border" + sSide + "Style"] == "none"){
return 0;
}
var n = getPixelValue(element.currentStyle["border" + sSide + "Width"]);
return n || 0;
}
function getBorderLeftWidth() { return getBorderWidth("Left"); }
function getBorderRightWidth() { return getBorderWidth("Right"); }
function getBorderTopWidth() { return getBorderWidth("Top"); }
function getBorderBottomWidth() { return getBorderWidth("Bottom"); }
/*
* getPadding & friends
* Padding width getters
*/
function getPadding(sSide) {
var n = getPixelValue(element.currentStyle["padding" + sSide]);
return n || 0;
}
function getPaddingLeft() { return getPadding("Left"); }
function getPaddingRight() { return getPadding("Right"); }
function getPaddingTop() { return getPadding("Top"); }
function getPaddingBottom() { return getPadding("Bottom"); }
/*
* getBoxSizing
* Get the box-sizing value for the current element
*/
function getBoxSizing(){
var s = element.style;
var cs = element.currentStyle
if(typeof s.boxSizing != "undefined" && s.boxSizing != ""){
return s.boxSizing;
}
if(typeof s["box-sizing"] != "undefined" && s["box-sizing"] != ""){
return s["box-sizing"];
}
if(typeof cs.boxSizing != "undefined" && cs.boxSizing != ""){
return cs.boxSizing;
}
if(typeof cs["box-sizing"] != "undefined" && cs["box-sizing"] != ""){
return cs["box-sizing"];
}
return getDocumentBoxSizing();
}
/*
* getDocumentBoxSizing
* Get the default document box sizing (check for quirks mode)
*/
function getDocumentBoxSizing(){
if(doc.compatMode == null || doc.compatMode == "BackCompat"){
return "border-box";
}
return "content-box"
}
/*
* setBorderBoxWidth & friends
* Width and height setters
*/
function setBorderBoxWidth(n){
element.runtimeStyle.width = Math.max(0, n - getBorderLeftWidth() -
getPaddingLeft() - getPaddingRight() - getBorderRightWidth()) + "px";
}
function setBorderBoxHeight(n){
element.runtimeStyle.height = Math.max(0, n - getBorderTopWidth() -
getPaddingTop() - getPaddingBottom() - getBorderBottomWidth()) + "px";
}
function setContentBoxWidth(n){
element.runtimeStyle.width = Math.max(0, n + getBorderLeftWidth() +
getPaddingLeft() + getPaddingRight() + getBorderRightWidth()) + "px";
}
function setContentBoxHeight(n){
element.runtimeStyle.height = Math.max(0, n + getBorderTopWidth() +
getPaddingTop() + getPaddingBottom() + getBorderBottomWidth()) + "px";
}
/*
* updateBorderBoxWidth & updateBorderBoxHeight
*
*/
function updateBorderBoxWidth() {
if(getDocumentBoxSizing() == getBoxSizing()){
return;
}
var csw = element.currentStyle.width;
if(csw != "auto"){
csw = getPixelWidth(element,csw);
if(getBoxSizing() == "border-box"){
setBorderBoxWidth(parseInt(csw));
}
else{
setContentBoxWidth(parseInt(csw));
}
}
}
function updateBorderBoxHeight() {
if(getDocumentBoxSizing() == getBoxSizing()){
return;
}
var csh = element.currentStyle.height;
if(csh != "auto"){
csh = getPixelValue(csh);
if(getBoxSizing() == "border-box"){
setBorderBoxHeight(parseInt(csh));
}
else{
setContentBoxHeight(parseInt(csh));
}
}
}
// Run the calculations
init();
//]]>
</script>
</component>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment