Refactor
This commit is contained in:
parent
43073fa1e8
commit
551571fc17
@ -129,7 +129,7 @@ carto.Renderer.prototype.render = function render(m, callback) {
|
||||
output.push(carto.tree.StyleXML(style_name, rule.attachment, rule, env));
|
||||
}
|
||||
if (styles.length) {
|
||||
output.push(carto.tree.Layer.toXML(l, styles));
|
||||
output.push(carto.tree.LayerXML(l, styles));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,6 @@
|
||||
(function(tree) {
|
||||
|
||||
tree.Layer = function Layer(obj, styles) {
|
||||
};
|
||||
|
||||
tree.Layer.toXML = function(obj, styles) {
|
||||
tree.LayerXML = function(obj, styles) {
|
||||
var dsoptions = [];
|
||||
for (var i in obj.Datasource) {
|
||||
dsoptions.push('<Parameter name="' + i + '"><![CDATA[' +
|
||||
|
@ -5,43 +5,33 @@
|
||||
(function(tree) {
|
||||
|
||||
var _ = require('underscore'),
|
||||
mapnik_reference = require('mapnik-reference');
|
||||
mapnik_reference = require('mapnik-reference'),
|
||||
ref = {};
|
||||
|
||||
tree.Reference = { data: mapnik_reference.version.latest };
|
||||
ref.setData = function(data) {
|
||||
ref.data = data;
|
||||
ref.selector_cache = generateSelectorCache(data);
|
||||
ref.mapnikFunctions = generateMapnikFunctions(data);
|
||||
ref.required_cache = generateRequiredProperties(data);
|
||||
};
|
||||
|
||||
tree.Reference.setVersion = function(version) {
|
||||
ref.setVersion = function(version) {
|
||||
if (mapnik_reference.version.hasOwnProperty(version)) {
|
||||
tree.Reference.data = mapnik_reference.version[version];
|
||||
// caches
|
||||
tree.Reference.selector_cache = generateSelectorCache(tree.Reference.data);
|
||||
tree.Reference.mapnikFunctions = generateMapnikFunctions(tree.Reference.data);
|
||||
ref.setData(mapnik_reference.version[version]);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
tree.Reference.validSelector = function(selector) {
|
||||
return !!tree.Reference.selector_cache[selector];
|
||||
ref.selectorData = function(selector, i) {
|
||||
if (ref.selector_cache[selector]) return ref.selector_cache[selector][i];
|
||||
};
|
||||
|
||||
tree.Reference.selectorName = function(selector) {
|
||||
if (tree.Reference.selector_cache[selector]) {
|
||||
return tree.Reference.selector_cache[selector][2];
|
||||
}
|
||||
};
|
||||
|
||||
tree.Reference.selector = function(selector) {
|
||||
if (tree.Reference.selector_cache[selector]) {
|
||||
return tree.Reference.selector_cache[selector][0];
|
||||
}
|
||||
};
|
||||
|
||||
tree.Reference.symbolizer = function(selector) {
|
||||
if (tree.Reference.selector_cache[selector]) {
|
||||
return tree.Reference.selector_cache[selector][1];
|
||||
}
|
||||
};
|
||||
ref.validSelector = function(selector) { return !!ref.selector_cache[selector]; };
|
||||
ref.selectorName = function(selector) { return ref.selectorData(selector, 2); };
|
||||
ref.selector = function(selector) { return ref.selectorData(selector, 0); };
|
||||
ref.symbolizer = function(selector) { return ref.selectorData(selector, 1); };
|
||||
|
||||
function generateSelectorCache(data) {
|
||||
var index = {};
|
||||
@ -83,12 +73,8 @@ function generateRequiredProperties(data) {
|
||||
return cache;
|
||||
}
|
||||
|
||||
tree.Reference.selector_cache = generateSelectorCache(tree.Reference.data);
|
||||
tree.Reference.mapnikFunctions = generateMapnikFunctions(tree.Reference.data);
|
||||
tree.Reference.required_cache = generateRequiredProperties(tree.Reference.data);
|
||||
|
||||
tree.Reference.requiredProperties = function(symbolizer_name, rules) {
|
||||
var req = tree.Reference.required_cache[symbolizer_name];
|
||||
ref.requiredProperties = function(symbolizer_name, rules) {
|
||||
var req = ref.required_cache[symbolizer_name];
|
||||
for (var i in req) {
|
||||
if (!(req[i] in rules)) {
|
||||
return 'Property ' + req[i] + ' required for defining ' +
|
||||
@ -98,7 +84,7 @@ tree.Reference.requiredProperties = function(symbolizer_name, rules) {
|
||||
};
|
||||
|
||||
// TODO: finish implementation - this is dead code
|
||||
tree.Reference._validateValue = {
|
||||
ref._validateValue = {
|
||||
'font': function(env, value) {
|
||||
if (env.validation_data && env.validation_data.fonts) {
|
||||
return env.validation_data.fonts.indexOf(value) != -1;
|
||||
@ -108,20 +94,17 @@ tree.Reference._validateValue = {
|
||||
}
|
||||
};
|
||||
|
||||
tree.Reference.isFont = function(selector) {
|
||||
return tree.Reference.selector(selector).validate == 'font';
|
||||
ref.isFont = function(selector) {
|
||||
return ref.selector(selector).validate == 'font';
|
||||
};
|
||||
|
||||
// https://gist.github.com/982927
|
||||
tree.Reference.editDistance = function(a, b){
|
||||
ref.editDistance = function(a, b){
|
||||
if (a.length === 0) return b.length;
|
||||
if (b.length === 0) return a.length;
|
||||
|
||||
var matrix = [];
|
||||
|
||||
for (var i = 0; i <= b.length; i++) { matrix[i] = [i]; }
|
||||
for (var j = 0; j <= a.length; j++) { matrix[0][j] = j; }
|
||||
|
||||
for (i = 1; i <= b.length; i++) {
|
||||
for (j = 1; j <= a.length; j++) {
|
||||
if (b.charAt(i-1) == a.charAt(j-1)) {
|
||||
@ -133,64 +116,60 @@ tree.Reference.editDistance = function(a, b){
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return matrix[b.length][a.length];
|
||||
};
|
||||
|
||||
tree.Reference.validValue = function(env, selector, value) {
|
||||
function validateFunctions(value, selector) {
|
||||
if (value.value[0].is === 'string') return true;
|
||||
for (var i in value.value) {
|
||||
for (var j in value.value[i].value) {
|
||||
if (value.value[i].value[j].is !== 'call') return false;
|
||||
var f = _.find(ref
|
||||
.selector(selector).functions, function(x) {
|
||||
return x[0] == value.value[i].value[j].name;
|
||||
});
|
||||
// This filter is unknown or given an incorrect number of arguments
|
||||
if (!f || f[1] !== value.value[i].value[j].args.length) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function validateKeyword(value, selector) {
|
||||
if (typeof ref.selector(selector).type === 'object') {
|
||||
return ref.selector(selector).type
|
||||
.indexOf(value.value[0].value) !== -1;
|
||||
} else {
|
||||
// allow unquoted keywords as strings
|
||||
return ref.selector(selector).type === 'string';
|
||||
}
|
||||
}
|
||||
|
||||
ref.validValue = function(env, selector, value) {
|
||||
var i, j;
|
||||
// TODO: handle in reusable way
|
||||
if (!tree.Reference.selector(selector)) {
|
||||
if (!ref.selector(selector)) {
|
||||
return false;
|
||||
} else if (value.value[0].is == 'keyword') {
|
||||
if (typeof tree.Reference.selector(selector).type === 'object') {
|
||||
return tree.Reference
|
||||
.selector(selector).type
|
||||
.indexOf(value.value[0].value) !== -1;
|
||||
// Lax permissions for single strings. If you provide a keyword
|
||||
// aka unquoted string to a property that is a string, it'll be fine.
|
||||
} else if (tree.Reference.selector(selector).type === 'string') {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return validateKeyword(value, selector);
|
||||
} else if (value.value[0].is == 'undefined') {
|
||||
// caught earlier in the chain - ignore here so that
|
||||
// error is not overridden
|
||||
return true;
|
||||
} else if (tree.Reference.selector(selector).type == 'numbers') {
|
||||
} else if (ref.selector(selector).type == 'numbers') {
|
||||
for (i in value.value) {
|
||||
if (value.value[i].is !== 'float') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else if (tree.Reference.selector(selector).type == 'functions') {
|
||||
} else if (ref.selector(selector).type == 'functions') {
|
||||
// For backwards compatibility, you can specify a string for `functions`-compatible
|
||||
// values, though they will not be validated.
|
||||
if (value.value[0].is === 'string') {
|
||||
return true;
|
||||
} else {
|
||||
for (i in value.value) {
|
||||
for (j in value.value[i].value) {
|
||||
if (value.value[i].value[j].is !== 'call') {
|
||||
return false;
|
||||
}
|
||||
var f = _.find(tree.Reference
|
||||
.selector(selector).functions, function(x) {
|
||||
return x[0] == value.value[i].value[j].name;
|
||||
});
|
||||
// This filter is unknown
|
||||
if (!f) return false;
|
||||
// The filter has been given an incorrect number of arguments
|
||||
if (f[1] !== value.value[i].value[j].args.length) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} else if (tree.Reference.selector(selector).type === 'expression') {
|
||||
return validateFunctions(value, selector);
|
||||
} else if (ref.selector(selector).type === 'expression') {
|
||||
return true;
|
||||
} else if (tree.Reference.selector(selector).type === 'unsigned') {
|
||||
} else if (ref.selector(selector).type === 'unsigned') {
|
||||
if (value.value[0].is === 'float') {
|
||||
value.value[0].round();
|
||||
return true;
|
||||
@ -198,22 +177,26 @@ tree.Reference.validValue = function(env, selector, value) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (tree.Reference.selector(selector).validate) {
|
||||
if (ref.selector(selector).validate) {
|
||||
var valid = false;
|
||||
for (i = 0; i < value.value.length; i++) {
|
||||
if (tree.Reference.selector(selector).type == value.value[i].is &&
|
||||
tree.Reference
|
||||
if (ref.selector(selector).type == value.value[i].is &&
|
||||
ref
|
||||
._validateValue
|
||||
[tree.Reference.selector(selector).validate]
|
||||
[ref.selector(selector).validate]
|
||||
(env, value.value[i].value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return valid;
|
||||
} else {
|
||||
return tree.Reference.selector(selector).type == value.value[0].is;
|
||||
return ref.selector(selector).type == value.value[0].is;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ref.setVersion('latest');
|
||||
|
||||
tree.Reference = ref;
|
||||
|
||||
})(require('../tree'));
|
||||
|
Loading…
Reference in New Issue
Block a user