Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
add ability to define variables within selectors and filters, ref #461
Browse files Browse the repository at this point in the history
  • Loading branch information
nebulon42 committed Jun 2, 2017
1 parent fcf2f7a commit def6de4
Show file tree
Hide file tree
Showing 31 changed files with 501 additions and 77 deletions.
19 changes: 1 addition & 18 deletions lib/carto/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,28 +213,11 @@ carto.Parser = function Parser(env) {

// call populates Invalid-caused errors
var definitions = this.flatten([], [], env);
definitions.sort(specificitySort);
definitions.sort(tree.specificitySort);
return definitions;
};
})();

// Sort rules by specificity: this function expects selectors to be
// split already.
//
// Written to be used as a .sort(Function);
// argument.
//
// [1, 0, 0, 467] > [0, 0, 1, 520]
var specificitySort = function(a, b) {
var as = a.specificity;
var bs = b.specificity;

if (as[0] != bs[0]) return bs[0] - as[0];
if (as[1] != bs[1]) return bs[1] - as[1];
if (as[2] != bs[2]) return bs[2] - as[2];
return bs[3] - as[3];
};

return root;
},

Expand Down
28 changes: 28 additions & 0 deletions lib/carto/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,31 @@ module.exports.find = function (obj, fun) {
}
return null;
};

// Sort rules by specificity: this function expects selectors to be
// split already.
//
// Specificity: [ID, Class, Filters, Zoom, Position in document]
//
// Written to be used as a .sort(Function);
// argument.
//
// [1, 0, 0, 467] > [0, 0, 1, 520]
module.exports.specificitySort = function (a, b) {
var as = a.specificity;
var bs = b.specificity;

if (as[0] != bs[0]) { // ID
return bs[0] - as[0];
}
if (as[1] != bs[1]) { // Class
return bs[1] - as[1];
}
if (as[2] != bs[2]) { // Filters
return bs[2] - as[2];
}
if (as[3] != -1 && bs[3] != -1 && as[3] != bs[3]) { // Zoom
return bs[4] - as[4];
}
return bs[4] - as[4]; // Position in document
};
9 changes: 9 additions & 0 deletions lib/carto/tree/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tree.Call = function Call(name, args, filename, index) {
this.args = args;
this.filename = filename;
this.index = index;
this.rule = null;
};

tree.Call.prototype = {
Expand Down Expand Up @@ -105,6 +106,14 @@ tree.Call.prototype = {
} else {
return this.name;
}
},
setRule: function (rule) {
this.rule = rule;
_.forEach(this.args, function (a) {
if (typeof a.setRule === 'function') {
a.setRule(rule);
}
});
}
};

Expand Down
5 changes: 5 additions & 0 deletions lib/carto/tree/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tree.Color = function Color(hsl, a, perceptual) {
} else {
this.perceptual = false;
}
this.rule = null;
};

tree.Color.prototype = {
Expand Down Expand Up @@ -149,6 +150,10 @@ tree.Color.prototype = {

round: function(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
},

setRule: function (rule) {
this.rule = rule;
}
};

Expand Down
38 changes: 36 additions & 2 deletions lib/carto/tree/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ var assert = require('assert'),
// }
//
// The selector can have filters
tree.Definition = function Definition(selector, rules) {
tree.Definition = function Definition(selector, rules, env) {
var that = this;

this.elements = selector.elements;
assert.ok(selector.filters instanceof tree.Filterset);
this.rules = rules;
Expand All @@ -19,10 +21,42 @@ tree.Definition = function Definition(selector, rules) {
this.rules[i].zoom = selector.zoom;
this.ruleIndex[this.rules[i].updateID()] = true;
}

this.filters = selector.filters;
if (this.filters) {
if (_.isArray(this.filters)) {
_.forEach(this.filters, function (f) {
if (typeof f.setRule === 'function') {
f.setRule(that);
}
});
}
else {
if (typeof this.filters.setRule === 'function') {
this.filters.setRule(that);
}
}
}

this.zoom = selector.zoom;

if (this.zoom) {
if (_.isArray(this.zoom)) {
_.forEach(this.zoom, function (f) {
if (typeof f.setRule === 'function') {
f.setRule(that);
}
});
}
else {
if (typeof this.zoom.setRule === 'function') {
this.zoom.setRule(that);
}
}
}

this.attachment = selector.attachment || '__default__';
this.specificity = selector.specificity();
this.specificity = selector.specificity(env);
this.matchCount = 0;

// special handling for Map selector
Expand Down
4 changes: 4 additions & 0 deletions lib/carto/tree/dimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ tree.Dimension = function Dimension(value, unit, index, filename) {
this.unit = unit || null;
this.filename = filename;
this.index = index;
this.rule = null;
};

tree.Dimension.prototype = {
Expand Down Expand Up @@ -96,6 +97,9 @@ tree.Dimension.prototype = {
//here the operands are either the same (% or undefined or px), or one is undefined and the other is px
return new tree.Dimension(tree.operate(op, this.value, other.value),
this.unit || other.unit, this.index, this.filename);
},
setRule: function (rule) {
this.rule = rule;
}
};

Expand Down
19 changes: 19 additions & 0 deletions lib/carto/tree/expression.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
(function(tree) {

var _ = require('lodash');

tree.Expression = function Expression(value) {
this.value = value;
this.rule = null;
};

tree.Expression.prototype = {
Expand All @@ -20,6 +23,22 @@ tree.Expression.prototype = {
return this.value.map(function(e) {
return e.toString(env);
}).join(' ');
},

setRule: function (rule) {
this.rule = rule;
if (_.isArray(this.value)) {
_.forEach(this.value, function (v) {
if (typeof v.setRule === 'function') {
v.setRule(rule);
}
});
}
else {
if (typeof this.value.setRule === 'function') {
this.value.setRule(rule);
}
}
}
};

Expand Down
7 changes: 7 additions & 0 deletions lib/carto/tree/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

tree.Field = function Field(content) {
this.value = content || '';
this.rule = null;
};

tree.Field.prototype = {
Expand All @@ -11,6 +12,12 @@ tree.Field.prototype = {
},
'ev': function() {
return this;
},
setRule: function (rule) {
this.rule = rule;
if (typeof this.value.setRule === 'function') {
this.value.setRule(rule);
}
}
};

Expand Down
14 changes: 14 additions & 0 deletions lib/carto/tree/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tree.Filter = function Filter(key, op, val, index, filename) {
this.val = val;
this.index = index;
this.filename = filename;
this.rule = null;

this.id = this.key + this.op + this.val;
};
Expand Down Expand Up @@ -67,4 +68,17 @@ tree.Filter.prototype.toString = function() {
return '[' + this.id + ']';
};

tree.Filter.prototype.setRule = function (rule) {
this.rule = rule;
if (typeof this.key.setRule === 'function') {
this.key.setRule(rule);
}
if (typeof this.op.setRule === 'function') {
this.op.setRule(rule);
}
if (typeof this.val.setRule === 'function') {
this.val.setRule(rule);
}
}

})(require('../tree'));
14 changes: 13 additions & 1 deletion lib/carto/tree/filterset.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var tree = require('../tree'),
util = require('../util');
util = require('../util'),
_ = require('lodash');

tree.Filterset = function Filterset() {
this.filters = {};
this.rule = null;
};

tree.Filterset.prototype.toXML = function(env) {
Expand Down Expand Up @@ -46,6 +48,7 @@ tree.Filterset.prototype.ev = function(env) {

tree.Filterset.prototype.clone = function() {
var clone = new tree.Filterset();
clone.rule = this.rule;
for (var id in this.filters) {
clone.filters[id] = this.filters[id];
}
Expand Down Expand Up @@ -285,3 +288,12 @@ tree.Filterset.prototype.add = function(filter, env) { // eslint-disable-line
}
}
};

tree.Filterset.prototype.setRule = function (rule) {
this.rule = rule;
_.forEach(this.filters, function (f) {
if (typeof f.setRule === 'function') {
f.setRule(rule);
}
});
}
12 changes: 12 additions & 0 deletions lib/carto/tree/fontset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(function(tree) {

var _ = require('lodash');

tree._flattenFontArray = function (fonts) {
var result = [];

Expand Down Expand Up @@ -29,6 +31,7 @@ tree._getFontSet = function(env, fonts) {
tree.FontSet = function FontSet(env, fonts) {
this.fonts = fonts;
this.name = 'fontset-' + env.effects.length;
this.rule = null;
};

tree.FontSet.prototype.toXML = function(env) { // eslint-disable-line
Expand All @@ -41,4 +44,13 @@ tree.FontSet.prototype.toXML = function(env) { // eslint-disable-line
'\n</FontSet>';
};

tree.FontSet.prototype.setRule = function (rule) {
this.rule = rule;
_.forEach(this.fonts, function (f) {
if (typeof f.setRule === 'function') {
f.setRule(rule);
}
});
}

})(require('../tree'));
15 changes: 15 additions & 0 deletions lib/carto/tree/imagefilter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
(function(tree) {

var _ = require('lodash');

tree.ImageFilter = function ImageFilter(filter, args) {
this.filter = filter;
this.args = args || null;
this.rule = null;
};

tree.ImageFilter.prototype = {
Expand All @@ -18,5 +21,17 @@ tree.ImageFilter.prototype = {
}
};

tree.ImageFilter.prototype.setRule = function (rule) {
this.rule = rule;
if (typeof this.filter.setRule === 'function') {
this.filter.setRule(rule);
}
_.forEach(this.args, function (a) {
if (typeof a.setRule === 'function') {
a.setRule(rule);
}
});
}


})(require('../tree'));
5 changes: 5 additions & 0 deletions lib/carto/tree/invalid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tree.Invalid = function Invalid(chunk, index, message, filename) {
this.index = index;
this.message = message || "Invalid code: " + this.chunk;
this.filename = filename;
this.rule = null;
};

tree.Invalid.prototype.is = 'invalid';
Expand All @@ -22,4 +23,8 @@ tree.Invalid.prototype.ev = function(env) {
is: 'undefined'
};
};

tree.Invalid.prototype.setRule = function (rule) {
this.rule = rule;
};
})(require('../tree'));
7 changes: 7 additions & 0 deletions lib/carto/tree/keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ tree.Keyword = function Keyword(value) {
'false': 'boolean'
};
this.is = special[value] ? special[value] : 'keyword';
this.rule = null;
};
tree.Keyword.prototype = {
ev: function() { return this; },
toString: function() { return this.value; }
};
tree.Keyword.prototype.setRule = function (rule) {
this.rule = rule;
if (typeof this.value.setRule === 'function') {
this.value.setRule(rule);
}
}

})(require('../tree'));
Loading

0 comments on commit def6de4

Please sign in to comment.