I'm redisigning an old script, making it OOP first and then later use jQuery for it. I know this question is asked before, but the answers don't seem to fit in my situation. Or I don't get it without jQuery knowledge.
This working script resizes a DIV based on mousevents, that pass a boolean property between them. But the methods reference properties by object name which is bad OOP I understand. I see a lot of answers with a "init" function and renaming "this" that I understand, but I can't relate those solutions to my script:
PositionXY = {
baseX : undefined,
baseY : undefined,
isRepositionable : true,
detectBasePosition : function (e) {
"use strict";
PositionXY.isRepositionable = true; // bad OOP
e = e || window.event; // crossbrowser event acces
if (this.currentStyle){ // IE
if (e.clientX || e.clientY) {
PositionXY.baseX = e.clientX - this.currentStyle.width.replace(/px/,"");
PositionXY.baseY = e.clientY - this.currentStyle.height.replace(/px/,"");
}
} else if (window.getComputedStyle){ // Mozilla
if (e.pageX || e.pageY) {
PositionXY.baseX = e.pageX - document.defaultView.getComputedStyle(this,null).getPropertyValue("width").replace(/px/,"");
PositionXY.baseY = e.pageY - document.defaultView.getComputedStyle(this,null).getPropertyValue("height").replace(/px/,"");
}
}
},
startPositionAdjustment : function (e) {
"use strict";
if (PositionXY.isRepositionable) { // bad OOP
e = e || window.event; // crossbrowser event acces
if (e.clientX || e.clientY) { // IE
this.style.width = e.clientX - PositionXY.baseX + "px";
this.style.height = e.clientY - PositionXY.baseY + "px";
} else if (e.pageX || e.pageY) { // Mozilla
this.style.width = e.pageX - PositionXY.baseX + "px";
this.style.height = e.pageY - PositionXY.baseY + "px";
}
} else return;
},
stopPositionAdjustment : function () {
"use strict";
PositionXY.isRepositionable = false; // bad OOP
}
};
var elm = document.getElementById("x");
elm.onmousedown = PositionXY.detectBasePosition;
elm.onmousemove = PositionXY.startPositionAdjustment;
elm.onmouseup = PositionXY.stopPositionAdjustment;