Skip to content

Instantly share code, notes, and snippets.

@nilium
Created December 24, 2011 00:22
Show Gist options
  • Select an option

  • Save nilium/1515771 to your computer and use it in GitHub Desktop.

Select an option

Save nilium/1515771 to your computer and use it in GitHub Desktop.
Layout junk
/******************************************************************************
Copyright (c) 2011 Noel R. Cower
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
******************************************************************************/
(function (ns, $, undefined) {
"use strict";
// makes an element occupy the same space horizontally or vertically as its base
ns.kConstraintFillHorizontal = 0; // same as aligning left and right edges to a single base
ns.kConstraintFillVertical = 1; // same as aligning top and bottom edges to a single base
// align element edges to base edges (basically elem.edge = base.edge)
ns.kConstraintAlignTop = 2;
ns.kConstraintAlignBottom = 3;
ns.kConstraintAlignLeft = 4;
ns.kConstraintAlignRight = 5;
// aligns an element's edge to the middle of the base (left/right = horizontal, top/bottom = vertical)
ns.kConstraintCenterTop = 6;
ns.kConstraintCenterBottom = 7;
ns.kConstraintCenterLeft = 8;
ns.kConstraintCenterRight = 9;
// places an element next to its base
ns.kConstraintPutBelow = 10;
ns.kConstraintPutAbove = 11;
ns.kConstraintPutLeftOf = 12;
ns.kConstraintPutRightOf = 13;
// centers an element relative to its base
// these constraints modify two edges at a time
ns.kConstraintCenterVertical = 14;
ns.kConstraintCenterHorizontal = 15;
var kLayoutFunctions = [
// kConstraintFillHorizontal
function (elemq, baseq, elemPos, basePos, mods, margin) {
mods.leftSet = true;
mods.rightSet = true;
elemPos.left = basePos.left + margin;
elemq.offset(elemPos).width(baseq.width() - (margin * 2));
},
// kConstraintFillVertical
function (elemq, baseq, elemPos, basePos, mods, margin) {
mods.topSet = true;
mods.bottomSet = true;
elemPos.top = basePos.top + margin;
elemq.offset(elemPos).height(baseq.height() - (margin * 2));
},
// kConstraintAlignTop
function (elemq, baseq, elemPos, basePos, mods, margin) {
if (mods.bottomSet) {
var bottom = elemPos.top + elemq.height();
elemPos.top = basePos.top + margin;
elemq.offset(elemPos).height(bottom - elemPos.top);
} else {
elemPos.top = basePos.top;
elemq.offset(elemPos);
}
mods.topSet = true;
},
// kConstraintAlignBottom
function (elemq, baseq, elemPos, basePos, mods, margin) {
if (mods.topSet) {
elemq.height((basePos.top + baseq.height()) - elemPos.top - margin);
} else {
elemPos.top = (basePos.top + baseq.height()) - elemq.height() - margin;
elemq.offset(elemPos);
}
mods.bottomSet = true;
},
// kConstraintAlignLeft
function (elemq, baseq, elemPos, basePos, mods, margin) {
if (mods.rightSet) {
var right = elemPos.left + elemq.width();
elemPos.left = basePos.left + margin;
elemq.offset(elemPos).width(right - elemPos.left);
} else {
elemPos.left = basePos.left + margin;
elemq.offset(elemPos);
}
mods.leftSet = true;
},
// kConstraintAlignRight
function (elemq, baseq, elemPos, basePos, mods, margin) {
if (mods.leftSet) {
elemq.width((basePos.left + baseq.width()) - elemPos.left - margin);
} else {
elemPos.left = (basePos.left + baseq.width()) - elemq.width() - margin;
elemq.offset(elemPos);
}
mods.rightSet = true;
},
// kConstraintCenterTop
function (elemq, baseq, elemPos, basePos, mods, margin) {
var middle = (basePos.top + (baseq.height() * 0.5)) + margin;
if (mods.bottomSet) {
var bottom = elemPos.top + elemq.height();
elemPos.top = middle;
elemq.offset(elemPos).height(bottom - middle);
} else {
elemPos.top = middle;
elemq.offset(elemPos);
}
mods.topSet = true;
},
// kConstraintCenterBottom
function (elemq, baseq, elemPos, basePos, mods, margin) {
var middle = (basePos.top + (baseq.height() * 0.5)) - margin;
if (mods.topSet) {
elemq.height(middle - elemPos.top);
} else {
elemPos.top = middle - elemq.height();
elemq.offset(elemPos);
}
mods.bottomSet = true;
},
// kConstraintCenterLeft
function (elemq, baseq, elemPos, basePos, mods, margin) {
var middle = (basePos.left + (baseq.width() * 0.5)) + margin;
if (mods.rightSet) {
var right = elemPos.left + elemq.width();
elemPos.left = middle;
elemq.offset(elemPos).width(right - middle);
} else {
elemPos.left = middle;
elemq.offset(elemPos);
}
mods.leftSet = true;
},
// kConstraintCenterRight
function (elemq, baseq, elemPos, basePos, mods, margin) {
var middle = (basePos.left + (baseq.width() * 0.5)) - margin;
if (mods.topSet) {
elemq.width(middle - elemPos.left);
} else {
elemPos.left = middle - elemq.width();
elemq.offset(elemPos);
}
mods.rightSet = true;
},
// kConstraintPutBelow
function (elemq, baseq, elemPos, basePos, mods, margin) {
var y = (basePos.top + baseq.height()) + margin;
if (mods.bottomSet) {
var bottom = elemPos.top + elemq.height();
elemPos.top = y;
elemq.offset(elemPos).height(bottom - y);
} else {
elemPos.top = y;
elemq.offset(elemPos);
}
mods.topSet = true;
},
// kConstraintPutAbove
function (elemq, baseq, elemPos, basePos, mods, margin) {
var bottomEdge = basePos.top - margin;
if (mods.topSet) {
elemq.height(bottomEdge - elemPos.top);
} else {
elemPos.top = bottomEdge - elemq.height();
elemq.offset(elemPos);
}
mods.bottomSet = true;
},
// kConstraintPutLeftOf
function (elemq, baseq, elemPos, basePos, mods, margin) {
var right = basePos.top - margin;
if (mods.leftSet) {
elemq.height(right - elemPos.left);
} else {
elemPos.top = right - elemq.width();
elemq.offset(elemPos);
}
mods.rightSet = true;
},
// kConstraintPutRightOf
function (elemq, baseq, elemPos, basePos, mods, margin) {
var x = (basePos.left + baseq.width()) + margin;
if (mods.rightSet) {
var right = elemPos.left + elemq.width();
elemPos.left = x;
elemq.offset(elemPos).width(right - x);
} else {
elemPos.left = x;
elemq.offset(elemPos);
}
mods.leftSet = true;
},
// kConstraintCenterVertical
function (elemq, baseq, elemPos, basePos, mods, margin) {
elemPos.top = basePos.top + ((baseq.height() - elemq.height()) * 0.5) + margin;
mods.topSet = true;
mods.bottomSet = true;
elemq.offset(elemPos);
},
// kConstraintCenterHorizontal
function (elemq, baseq, elemPos, basePos, mods, margin) {
elemPos.left = basePos.left + ((baseq.width() - elemq.width()) * 0.5) + margin;
mods.leftSet = true;
mods.rightSet = true;
elemq.offset(elemPos);
}
];
// modification set
/*@ Generates modification sets for all elements in the constraints list */
var ElemModSet = function(constraints) {
var mods = {}
constraints.forEach(function (constraint, index, arr) {
$(constraint.element).each(function (index, elem) {
if (mods[elem] != undefined)
return;
mods[elem] = {
topSet: false,
bottomSet: false,
leftSet: false,
rightSet: false
};
});
});
this.mods = mods
};
/*@ Gets the modifications set for a given element */
ElemModSet.prototype.setForElement = function(elem) {
return this.mods[elem];
};
// constraint container
/*@
Creates a constraint for `domElement` with a given `relation` to `domBase`.
Optionally specifies an inset `margin` (negative values are outset), defaults
to 0.
*/
ns.Constraint = function(domElement, relation, domBase, margin) {
if (margin == undefined) margin = 0;
this.element = domElement;
this.relation = relation;
this.base = domBase;
this.margin = margin;
};
// layout manager
/*@ Creates a layout manager */
ns.LayoutManager = function() {
this.constraints = [];
};
/*@ Adds a `constraint` to the list of constraints the `LayoutManager` maintains */
ns.LayoutManager.prototype.addConstraint = function(constraint) {
this.constraints.push(constraint);
return this;
};
/*@
Removes one or more constraints from the `LayoutManager`.
Function takes one of three prototypes:
1. `removeConstraints()` -- removes all constraints from the `LayoutManager`
2. `removeConstraints(selector)` -- removes all constraints that match the `selector`
3. `removeConstraints([object1], [object2], ...)` -- removes all constraints that are
passed, as well as any constraints for DOM elements that are passed.
*/
ns.LayoutManager.prototype.removeConstraints = function() {
if (arguments.length == 0) {
this.constraints = []
} else {
// this could definitely be optimized..
arguments.forEach(function (arg, index, argsArray) {
var typ = typeof(arg);
if (typ == 'string') {
var elements = $(arg);
this.constraints = this.constraints.filter(function (constraint, index, array) {
return (elements.index(constraint.element) == -1);
}, this);
} else if (typ == 'object') {
this.constraints = this.constraints.filter(function (constraint, index, array) {
return !(constraint == arg || constraint.target == arg);
}, this);
}
}, this);
}
return this;
};
/*@ Causes the `LayoutManager` to layout its views. */
ns.LayoutManager.prototype.doLayout = function() {
var constraints = this.constraints;
var elemMods = new ElemModSet(constraints);
// some terrifying loop-in-loop shit right here
constraints.forEach( function (constraint, index, constraintsArray) {
$(constraint.element).each(function(index, elem) {
var elemq = $(elem);
var baseq = $(constraint.base);
var elemPos = elemq.offset();
var basePos = baseq.offset();
var mods = elemMods.setForElement(elem);
kLayoutFunctions[constraint.relation].call(this, elemq, baseq, elemPos, basePos, mods, constraint.margin);
});
}, this);
return this;
};
})(window.NLM = window.NLM || {}, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment