Carto, with a few small tweaks, starting to operate in-browser
This commit is contained in:
parent
4766e1e6af
commit
a49cc53e79
6
Makefile
6
Makefile
@ -4,6 +4,7 @@
|
||||
|
||||
expresso = ./node_modules/.bin/expresso
|
||||
docco = ./node_modules/.bin/docco
|
||||
uglify = ./node_modules/.bin/uglify
|
||||
|
||||
lint:
|
||||
./node_modules/.bin/jshint lib/carto/*.js lib/carto/tree/*.js
|
||||
@ -19,4 +20,9 @@ endif
|
||||
doc:
|
||||
$(docco) lib/carto/*.js lib/carto/tree/*.js
|
||||
|
||||
dist/carto.js:
|
||||
cat browser/*.js lib/carto/parser.js lib/carto/tree.js lib/carto/tree/*.js > dist/carto.js
|
||||
|
||||
dist: dist/carto.js
|
||||
|
||||
.PHONY: test
|
||||
|
29
browser/init.js
Normal file
29
browser/init.js
Normal file
@ -0,0 +1,29 @@
|
||||
// Reqwest
|
||||
/*!
|
||||
* Reqwest! A x-browser general purpose XHR connection manager
|
||||
* copyright Dustin Diaz 2011
|
||||
* https://github.com/ded/reqwest
|
||||
* license MIT
|
||||
*/
|
||||
!function(window){function serial(a){var b=a.name;if(a.disabled||!b)return"";b=enc(b);switch(a.tagName.toLowerCase()){case"input":switch(a.type){case"reset":case"button":case"image":case"file":return"";case"checkbox":case"radio":return a.checked?b+"="+(a.value?enc(a.value):!0)+"&":"";default:return b+"="+(a.value?enc(a.value):"")+"&"}break;case"textarea":return b+"="+enc(a.value)+"&";case"select":return b+"="+enc(a.options[a.selectedIndex].value)+"&"}return""}function enc(a){return encodeURIComponent(a)}function reqwest(a,b){return new Reqwest(a,b)}function init(o,fn){function error(a){o.error&&o.error(a),complete(a)}function success(resp){o.timeout&&clearTimeout(self.timeout)&&(self.timeout=null);var r=resp.responseText;switch(type){case"json":resp=eval("("+r+")");break;case"js":resp=eval(r);break;case"html":resp=r}fn(resp),o.success&&o.success(resp),complete(resp)}function complete(a){o.complete&&o.complete(a)}this.url=typeof o=="string"?o:o.url,this.timeout=null;var type=o.type||setType(this.url),self=this;fn=fn||function(){},o.timeout&&(this.timeout=setTimeout(function(){self.abort(),error()},o.timeout)),this.request=getRequest(o,success,error)}function setType(a){if(/\.json$/.test(a))return"json";if(/\.jsonp$/.test(a))return"jsonp";if(/\.js$/.test(a))return"js";if(/\.html?$/.test(a))return"html";if(/\.xml$/.test(a))return"xml";return"js"}function Reqwest(a,b){this.o=a,this.fn=b,init.apply(this,arguments)}function getRequest(a,b,c){if(a.type!="jsonp"){var f=xhr();f.open(a.method||"GET",typeof a=="string"?a:a.url,!0),setHeaders(f,a),f.onreadystatechange=readyState(f,b,c),a.before&&a.before(f),f.send(a.data||null);return f}var d=doc.createElement("script");window[getCallbackName(a)]=generalCallback,d.type="text/javascript",d.src=a.url,d.async=!0;var e=function(){a.success&&a.success(lastValue),lastValue=undefined,head.removeChild(d)};d.onload=e,d.onreadystatechange=function(){/^loaded|complete$/.test(d.readyState)&&e()},head.appendChild(d)}function generalCallback(a){lastValue=a}function getCallbackName(a){var b=a.jsonpCallback||"callback";if(a.url.slice(-(b.length+2))==b+"=?"){var c="reqwest_"+uniqid++;a.url=a.url.substr(0,a.url.length-1)+c;return c}var d=new RegExp(b+"=([\\w]+)");return a.url.match(d)[1]}function setHeaders(a,b){var c=b.headers||{};c.Accept="text/javascript, text/html, application/xml, text/xml, */*",c["X-Requested-With"]=c["X-Requested-With"]||"XMLHttpRequest";if(b.data){c["Content-type"]=c["Content-type"]||"application/x-www-form-urlencoded";for(var d in c)c.hasOwnProperty(d)&&a.setRequestHeader(d,c[d],!1)}}function readyState(a,b,c){return function(){a&&a.readyState==4&&(twoHundo.test(a.status)?b(a):c(a))}}var twoHundo=/^20\d$/,doc=document,byTag="getElementsByTagName",head=doc[byTag]("head")[0],xhr="XMLHttpRequest"in window?function(){return new XMLHttpRequest}:function(){return new ActiveXObject("Microsoft.XMLHTTP")},uniqid=0,lastValue;Reqwest.prototype={abort:function(){this.request.abort()},retry:function(){init.call(this,this.o,this.fn)}},reqwest.serialize=function(a){var b=a[byTag]("input"),c=a[byTag]("select"),d=a[byTag]("textarea");return(v(b).chain().toArray().map(serial).value().join("")+v(c).chain().toArray().map(serial).value().join("")+v(d).chain().toArray().map(serial).value().join("")).replace(/&$/,"")},reqwest.serializeArray=function(a){for(var b=this.serialize(a).split("&"),c=0,d=b.length,e=[],f;c<d;c++)b[c]&&(f=b[c].split("="))&&e.push({name:f[0],value:f[1]});return e};var old=window.reqwest;reqwest.noConflict=function(){window.reqwest=old;return this},window.reqwest=reqwest}(this)
|
||||
//
|
||||
|
||||
|
||||
var carto_initialize = function(carto, uri, callback) {
|
||||
reqwest(uri, function (resp) {
|
||||
carto.tree.Reference.data = resp;
|
||||
// regenerate cache
|
||||
carto.tree.Reference.selectors = (function() {
|
||||
var list = [];
|
||||
for (var i in tree.Reference.data.symbolizers) {
|
||||
for (var j in tree.Reference.data.symbolizers[i]) {
|
||||
if (tree.Reference.data.symbolizers[i][j].hasOwnProperty('css')) {
|
||||
list.push(tree.Reference.data.symbolizers[i][j].css);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
})();
|
||||
callback(carto);
|
||||
});
|
||||
};
|
6
browser/require.js
Normal file
6
browser/require.js
Normal file
@ -0,0 +1,6 @@
|
||||
//
|
||||
// Stub out `require` in the browser
|
||||
//
|
||||
function require(arg) {
|
||||
return window.carto[arg.split('/')[1]];
|
||||
};
|
@ -318,7 +318,10 @@ carto.Parser = function Parser(env) {
|
||||
// Get an array of Ruleset objects, flattened
|
||||
// and sorted according to specificitySort
|
||||
root.toList = (function() {
|
||||
var line, lines, column, _ = require('underscore')._;
|
||||
var line, lines, column;
|
||||
if (!(window && window._)) {
|
||||
var _ = require('underscore')._;
|
||||
}
|
||||
return function(env) {
|
||||
env.error = function(e) {
|
||||
if (!env.errors) env.errors = [];
|
||||
|
@ -1,8 +1,11 @@
|
||||
(function(tree) {
|
||||
var fs = require('fs');
|
||||
|
||||
if (!window) {
|
||||
var fs = require('fs');
|
||||
}
|
||||
|
||||
tree.Reference = {
|
||||
data: JSON.parse(fs.readFileSync(__dirname + '/reference.json'))
|
||||
data: (!window) && JSON.parse(fs.readFileSync(__dirname + '/reference.json'))
|
||||
};
|
||||
|
||||
tree.Reference.required_prop_list_cache = {};
|
||||
|
@ -50,6 +50,7 @@
|
||||
"devDependencies": {
|
||||
"expresso" : "= 0.7.x",
|
||||
"docco" : "= 0.3.x",
|
||||
"uglify-js": "1.0.x",
|
||||
"jshint" : "= 0.2.x"
|
||||
},
|
||||
"scripts": {
|
||||
|
45
test/index.html
Normal file
45
test/index.html
Normal file
@ -0,0 +1,45 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
|
||||
<title>Carto Test</title>
|
||||
<script src='underscore.js' type='text/javascript'></script>
|
||||
<script src='../dist/carto.js' type='text/javascript'></script>
|
||||
<script>
|
||||
function test() {
|
||||
carto_initialize(carto, '/~tmcw/carto/lib/carto/tree/reference.json', function(carto) {
|
||||
var style = document.getElementById('style').innerHTML;
|
||||
var parse_env = {
|
||||
error: function(obj) {
|
||||
document.getElementById('error').innerHTML += JSON.stringify(obj);
|
||||
}
|
||||
};
|
||||
(new carto.Parser(parse_env)).parse(style,
|
||||
function(err, ruleset) {
|
||||
for (var i in ruleset.rules) {
|
||||
for (var j in ruleset.rules[i].rules) {
|
||||
document.getElementById('output').innerHTML += ruleset.rules[i].rules[j].eval(parse_env).toXML(parse_env) + ' ';
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body onload='test()'>
|
||||
<h3>Style</h3>
|
||||
<pre id='style'>
|
||||
#world {
|
||||
line-width: 2;
|
||||
line-color: #f00;
|
||||
}
|
||||
</pre>
|
||||
<h3>Error</h3>
|
||||
<pre id='error'>
|
||||
</pre>
|
||||
<h3>Output</h3>
|
||||
<pre id='output'>
|
||||
</pre>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user