Improved min/max function, 2 new Built in function

This commit is contained in:
deviprsd21
2014-01-10 22:48:37 +05:30
parent 3689d81737
commit 08e613709e

View File

@@ -264,24 +264,41 @@ tree.functions = {
_minmax: function (isMin, args) {
args = Array.prototype.slice.call(args);
switch(args.length) {
case 0: throw { type: "Argument", message: "one or more arguments required" };
case 1: return args[0];
case 0: throw { type: "Argument", message: "one or more arguments required" };
}
var i, j, current, currentUnified, referenceUnified, unit,
var i, j, current, currentUnified, referenceUnified, unit, unitStatic,
order = [], // elems only contains original argument values.
values = {}; // key is the unit.toString() for unified tree.Dimension values,
// value is the index into the order array.
for (i = 0; i < args.length; i++) {
current = args[i];
if (!(current instanceof tree.Dimension)) {
order.push(current);
continue;
}
currentUnified = current.unify();
unit = currentUnified.unit.toString();
if(unit === "" && unitStatic !== undefined) {
unit = unitStatic;
} else if(unit === "") {
unit = "variable";
}
unitStatic = unit !== "variable" && unitStatic === undefined ? unit : unitStatic;
values[unit] = values["variable"] !== undefined && unit !== "variable" && unit === unitStatic ? values["variable"] : values[unit];
j = values[unit];
if (j === undefined) {
values[unit] = order.length;
//error handling for incompatible types
if((values["px"] !== undefined && values["em"] !== undefined) || (values["px"] !== undefined && values["%"] !== undefined) ||
(values["px"] !== undefined && values["m"] !== undefined) || (values["px"] !== undefined && values["s"] !== undefined) ||
(values["px"] !== undefined && values["rad"] !== undefined) || (values["em"] !== undefined && values["%"] !== undefined) ||
(values["em"] !== undefined && values["m"] !== undefined) || (values["em"] !== undefined && values["s"] !== undefined) ||
(values["em"] !== undefined && values["rad"] !== undefined) || (values["%"] !== undefined && values["m"] !== undefined) ||
(values["%"] !== undefined && values["s"] !== undefined) || (values["%"] !== undefined && values["rad"] !== undefined) ||
(values["m"] !== undefined && values["s"] !== undefined) || (values["m"] !== undefined && values["rad"] !== undefined) ||
(values["s"] !== undefined && values["rad"] !== undefined))
{
throw { type: "Argument", message: "incompatible types" };
}
order.push(current);
continue;
}
@@ -294,8 +311,7 @@ tree.functions = {
if (order.length == 1) {
return order[0];
}
args = order.map(function (a) { return a.toCSS(this.env); })
.join(this.env.compress ? "," : ", ");
args = order.map(function (a) { return a.toCSS(this.env); }).join(this.env.compress ? "," : ", ");
return new(tree.Anonymous)((isMin ? "min" : "max") + "(" + args + ")");
},
min: function () {
@@ -303,6 +319,12 @@ tree.functions = {
},
max: function () {
return this._minmax(false, arguments);
},
zero: function (n) {
return new(tree.Dimension)(n.value, n.value ? n.unit : '');
},
unitonly: function (n) {
return new(tree.Anonymous)(n.unit);
},
argb: function (color) {
return new(tree.Anonymous)(color.toARGB());