From fc2d486384bb5ce2478ebd4df6727734c9ffe592 Mon Sep 17 00:00:00 2001 From: Andy Reagan Date: Wed, 4 Jun 2014 01:00:34 -0400 Subject: [PATCH 1/2] first version added, probably needs tests for primetime --- urllib/README.md | 30 +++++++++++ urllib/urllib.js | 137 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 urllib/README.md create mode 100644 urllib/urllib.js diff --git a/urllib/README.md b/urllib/README.md new file mode 100644 index 0000000..4726fa5 --- /dev/null +++ b/urllib/README.md @@ -0,0 +1,30 @@ +d3.urllib +========= + +a simple d3 plugin to manage pushing and pulling the visualization state to the brower url + +tests +----- +no test suite, I've tested in in Chrome v35 for reasonable use cases + +example +------- +also no simple example, but you can see it in use here: +http://www.uvm.edu/storylab/share/papers/dodds2014a/books.html + +documentation +------------- +right now, only works for strings and lists of strings + +create an instance of urllib to append some variable to the url: (here we're creating it for a varible lens with the value lensExtent, a list of two numbers + +```javascript +lensencoder = d3.urllib.encoder().varname("lens").varval(lensExtent); +``` + +then we update the url (when a slider is dragged, etc) with: +```javascript +lensencoder.varval(lensExtent); +``` + +If you don't want the URL to update until you've called the lensencoder again, don't apply the .varval yet. diff --git a/urllib/urllib.js b/urllib/urllib.js new file mode 100644 index 0000000..038c87b --- /dev/null +++ b/urllib/urllib.js @@ -0,0 +1,137 @@ +/* +d3.urllib +========= + +a simple d3 plugin to manage pushing and pulling the visualization state to the brower url + +tests +----- +no test suite, I've tested in in Chrome v35 for reasonable use cases + +example +------- +also no simple example, but you can see it in use here: +http://www.uvm.edu/storylab/share/papers/dodds2014a/books.html + +documentation +------------- +slightly more documentation in the README + +*/ +(function() { + d3.urllib = { + encoder: function() { + var varname = "tmp" + varval = []; + + function urllib(d) { + // nothing yet + return {current: varval,}; + } + + function parseurl() { + GET = {}; + query = window.location.search.substring(1).split("&"); + // break down the url + for (var i = 0, max = query.length; i < max; i++) + { + if (query[i] === "") // check for trailing & with no param + continue; + var param = query[i].split("="); + GET[decodeURIComponent(param[0])] = decodeURIComponent(param[1] || ""); + } + + baseUrl = window.location.origin+window.location.pathname; + var tmpList = "["+varval[0] + for (var i=1; i 0 && GET[varname][0] === "[") { + var tmpArray = GET[varname].substring(1, GET[varname].length - 1).split(','); + varresult = tmpArray; + } + else { + varresult = GET[varname]; + } + } + return urllib; + } + + urllib.varname = function(_) { + if (!arguments.length) return varname; + varname = _; + return parseurl(); + } + + urllib.varresult = function(_) { + if (!arguments.length) return varresult; + varresult = _; + return parseurl(); + } + + return parseurl(); + } + } +})(); + + + + + + + + + + + From 2fcb7d68ec2e232b43774c0f9935fed0d82717ed Mon Sep 17 00:00:00 2001 From: Andy Reagan Date: Wed, 4 Jun 2014 16:51:04 -0400 Subject: [PATCH 2/2] fixed strings going up, multiple instances with global varval --- urllib/urllib.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/urllib/urllib.js b/urllib/urllib.js index 038c87b..9336dba 100644 --- a/urllib/urllib.js +++ b/urllib/urllib.js @@ -21,11 +21,14 @@ slightly more documentation in the README (function() { d3.urllib = { encoder: function() { - var varname = "tmp" - varval = []; + var varname = "tmp"; + var varval = []; + //var that = this; function urllib(d) { // nothing yet + //console.log(this); + //console.log(that); return {current: varval,}; } @@ -42,10 +45,16 @@ slightly more documentation in the README } baseUrl = window.location.origin+window.location.pathname; - var tmpList = "["+varval[0] - for (var i=1; i