From ddb4bd338bec8ec991bc65e3101a2b9e4e832db0 Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 16 Jun 2014 12:54:42 +0200 Subject: [PATCH] moved code where it belongs --- lib/carto/renderer_js.js | 74 +----------------------------------- lib/carto/tree/definition.js | 38 ++++++++++++++++++ lib/carto/tree/filterset.js | 19 +++++++++ lib/carto/tree/value.js | 14 +++++++ test/rendering_js.test.js | 8 ++++ 5 files changed, 80 insertions(+), 73 deletions(-) diff --git a/lib/carto/renderer_js.js b/lib/carto/renderer_js.js index 570830c..cf87965 100644 --- a/lib/carto/renderer_js.js +++ b/lib/carto/renderer_js.js @@ -2,79 +2,6 @@ var tree = require('./tree'); var _ = require('underscore'); -// monkey patch less classes -tree.Value.prototype.toJS = function() { - //var v = this.value[0].value[0]; - var val = this.ev(); - var v = val.toString(); - if(val.is === "color" || val.is === 'uri' || val.is === 'string' || val.is === 'keyword') { - v = "'" + v + "'"; - } else if (val.is === 'field') { - // replace [varuable] by ctx['variable'] - v = v.replace(/\[(.*)\]/g, "data['\$1']"); - } - return "_value = " + v + ";"; -}; - -Object.defineProperty(tree.Filterset.prototype, 'toJS', { - enumerable: false, - value: function(env) { - var opMap = { - '=': '===' - }; - return _.map(this.filters, function(filter) { - var op = filter.op; - if(op in opMap) { - op = opMap[op]; - } - var val = filter.val; - if(filter._val !== undefined) { - val = filter._val.toString(true); - } - - var attrs = "data"; - return attrs + "." + filter.key + " " + op + " " + val; - }).join(' && '); - } -}); - -tree.Definition.prototype.toJS = function() { - var shaderAttrs = {}; - - // merge conditions from filters with zoom condition of the - // definition - var zoom = "(" + this.zoom + " & (1 << ctx.zoom))"; - var frame_offset = this.frame_offset; - var _if = this.filters.toJS(); - var filters = [zoom]; - if(_if) filters.push(_if); - if(frame_offset) filters.push('ctx["frame-offset"] === ' + frame_offset); - _if = filters.join(" && "); - _.each(this.rules, function(rule) { - if(rule instanceof tree.Rule) { - shaderAttrs[rule.name] = shaderAttrs[rule.name] || []; - if (_if) { - shaderAttrs[rule.name].push( - "if(" + _if + "){" + rule.value.toJS() + "}" - ); - } else { - shaderAttrs[rule.name].push(rule.value.toJS()); - } - } else { - if (rule instanceof tree.Ruleset) { - var sh = rule.toJS(); - for(var v in sh) { - shaderAttrs[v] = shaderAttrs[v] || []; - for(var attr in sh[v]) { - shaderAttrs[v].push(sh[v][attr]); - } - } - } - } - }); - return shaderAttrs; -}; - function CartoCSS(style, options) { this.options = options || {}; @@ -290,6 +217,7 @@ carto.RendererJS.prototype.render = function render(cartocss, callback) { tree.Reference.setVersion(this.options.mapnik_version); return new CartoCSS(cartocss, this.options); } + if(typeof(module) !== 'undefined') { module.exports = carto.RendererJS; } diff --git a/lib/carto/tree/definition.js b/lib/carto/tree/definition.js index f7bc60f..b4b0dbf 100644 --- a/lib/carto/tree/definition.js +++ b/lib/carto/tree/definition.js @@ -208,4 +208,42 @@ tree.Definition.prototype.toXML = function(env, existing) { return xml; }; +tree.Definition.prototype.toJS = function() { + var shaderAttrs = {}; + + // merge conditions from filters with zoom condition of the + // definition + var zoom = "(" + this.zoom + " & (1 << ctx.zoom))"; + var frame_offset = this.frame_offset; + var _if = this.filters.toJS(); + var filters = [zoom]; + if(_if) filters.push(_if); + if(frame_offset) filters.push('ctx["frame-offset"] === ' + frame_offset); + _if = filters.join(" && "); + _.each(this.rules, function(rule) { + if(rule instanceof tree.Rule) { + shaderAttrs[rule.name] = shaderAttrs[rule.name] || []; + if (_if) { + shaderAttrs[rule.name].push( + "if(" + _if + "){" + rule.value.toJS() + "}" + ); + } else { + shaderAttrs[rule.name].push(rule.value.toJS()); + } + } else { + if (rule instanceof tree.Ruleset) { + var sh = rule.toJS(); + for(var v in sh) { + shaderAttrs[v] = shaderAttrs[v] || []; + for(var attr in sh[v]) { + shaderAttrs[v].push(sh[v][attr]); + } + } + } + } + }); + return shaderAttrs; +}; + + })(require('../tree')); diff --git a/lib/carto/tree/filterset.js b/lib/carto/tree/filterset.js index 4e3642b..fa57dc0 100644 --- a/lib/carto/tree/filterset.js +++ b/lib/carto/tree/filterset.js @@ -77,6 +77,25 @@ tree.Filterset.prototype.cloneWith = function(other) { return clone; }; +tree.Filterset.prototype.toJS = function(env) { + var opMap = { + '=': '===' + }; + return _.map(this.filters, function(filter) { + var op = filter.op; + if(op in opMap) { + op = opMap[op]; + } + var val = filter.val; + if(filter._val !== undefined) { + val = filter._val.toString(true); + } + + var attrs = "data"; + return attrs + "." + filter.key + " " + op + " " + val; + }).join(' && '); +} + // Returns true when the new filter can be added, false otherwise. // It can also return null, and on the other side we test for === true or // false diff --git a/lib/carto/tree/value.js b/lib/carto/tree/value.js index 5fb0a39..2ecc2b9 100644 --- a/lib/carto/tree/value.js +++ b/lib/carto/tree/value.js @@ -26,7 +26,21 @@ tree.Value.prototype = { else obj.value = this.value; obj.is = this.is; return obj; + }, + + toJS: function() { + //var v = this.value[0].value[0]; + var val = this.ev(); + var v = val.toString(); + if(val.is === "color" || val.is === 'uri' || val.is === 'string' || val.is === 'keyword') { + v = "'" + v + "'"; + } else if (val.is === 'field') { + // replace [variable] by ctx['variable'] + v = v.replace(/\[(.*)\]/g, "data['$1']"); + } + return "_value = " + v + ";"; } + }; })(require('../tree')); diff --git a/test/rendering_js.test.js b/test/rendering_js.test.js index 519203d..4d1c857 100644 --- a/test/rendering_js.test.js +++ b/test/rendering_js.test.js @@ -42,4 +42,12 @@ describe('RenderingJS', function() { var props = layer.getStyle({}, { 'zoom': 0, 'frame-offset': 10 }); assert( props['line-width'] === 4); }); + + it ("shold render variables", function() { + var style = '#test { marker-width: [testing]; }'; + shader = (new carto.RendererJS({ debug: true })).render(style); + var layer = shader.getLayers()[0]; + var props = layer.getStyle({testing: 2}, { 'zoom': 0, 'frame-offset': 10 }); + assert( props['marker-width'] === 2); + }); });