Skip to content

Commit

Permalink
chromaway/ngcccbase#67: remember color set on server
Browse files Browse the repository at this point in the history
user gets color_set_hash for colors he entered

server remembers association of color_set_hash with a set of colors

Page can get color_set_hash in parameter, does lookup on server, gets color set, and proceeds from it
  • Loading branch information
arichnad committed Dec 22, 2013
1 parent 097f456 commit 179e369
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 27 deletions.
60 changes: 56 additions & 4 deletions colorer-b.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,45 @@ var transactionCache = {};
var coinbaseHash = '0000000000000000000000000000000000000000000000000000000000000000';
var defs = {};
var transactionSpentCache = {};
var colorSetHash = '';



function loadDefs(names, colorSet) {
defs = {};
for(i=0;i<names.length;i++) {
addDef(names[i], colorSet[i]);
}
}

function addDef(name, colorDescriptor) {
[coloringScheme, transactionHash, outputIndex, height] = colorDescriptor.split(':');
defs[transactionHash] = {'colorDescriptor': colorDescriptor, 'name': name, 'unit': 1};
$('#colorDescriptors').append("<br />\n"+colorDescriptor);
}

function getColorSetFromServer(colorSetHash, callback) {
$.ajax({
url: "/get_color_set/" + colorSetHash,
dataType: "json"
}).done(function (data) {
loadDefs(data.names, data.color_set);
callback();
}).fail(function (jqXHR, textStatus, errorThrown) { alert('fail:' + jqXHR + ' ' + textStatus);});
}

function addColorSetToServer(callback) {
//the server fills in the heights and returns both the color_set and the color_set_hash.
$.ajax({
url: "/add_color_set/" + getColorSet() + '/' + getNames(),
dataType: "json"
}).done(function (data) {
colorSetHash = data.color_set_hash;
loadDefs(data.names, data.color_set);
callback();
}).fail(function (jqXHR, textStatus, errorThrown) { alert('fail:' + jqXHR + ' ' + textStatus);});
}

function getTransaction(transactionHash, callback) {
var data = transactionCache[transactionHash];
if (data)
Expand Down Expand Up @@ -300,6 +336,14 @@ function testAll() {
"unit": 1
}
};
console.log(getColorSet() + ' / ' + getNames());
addColorSetToServer(function () {
console.log(colorSetHash, '673b13966a0dc47bb71a7e270ec15f7df8addcf0166f492e08ae68c2275324ed');
console.log(getColorSet() + ' / ' + getNames());
});
getColorSetFromServer('673b13966a0dc47bb71a7e270ec15f7df8addcf0166f492e08ae68c2275324ed', function() {
console.log(getColorSet() + ' / ' + getNames());
});

var i = 0;
testNext();
Expand Down Expand Up @@ -399,12 +443,20 @@ function getColorDescriptor(coloringScheme, transactionHash, outputIndex, height
return [coloringScheme, transactionHash, outputIndex, height].join(':');
}

function getColorDescriptors() {
function getColorSet() {
var result = '';
for(key in defs) {
if(result !== '') result += ',';
result += defs[key].colorDescriptor;
}
return result;
}

function getNames() {
var result = '';
for(key in defs) {
if(result !== '') result += '&';
def = defs[key];
result += [def.name, def.colorDescriptor].join(',');
if(result !== '') result += ',';
result += defs[key].name;
}
return result;
}
Expand Down
39 changes: 16 additions & 23 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<div class="input"><label>Output index:</label><input id='addOutputIndex' size='4' value='0' /></div>
<button onClick="addColorUI()">add</button>

<p><div class="box" id='colorDescriptions'>Colors</div></p>
<p><div class="box" id='colorDescriptors'>Colors</div></p>

</div>

Expand All @@ -90,35 +90,28 @@

var coloringScheme = 'obc'; //allow the user to set this?
var height = 0; //allow the user to set this?

addColor(name, getColorDescriptor(coloringScheme, transactionHash, outputIndex, height));
oldUrl = window.location.href.replace(/\?.*/, '');
window.location.href = oldUrl + '?' + getColorDescriptors();
}
function addColor(name, colorDescriptor) {
var colorDescriptor = getColorDescriptor(coloringScheme, transactionHash, outputIndex, height);

if(colorDescriptor.split(':').length != 4) {
window.alert('incorrect color descriptor found');
}
[coloringScheme, transactionHash, outputIndex, height] = colorDescriptor.split(':');
defs[transactionHash] = {'colorDescriptor': colorDescriptor, 'name': name, 'unit': 1};
$('#colorDescriptions').append("<br />\n"+colorDescriptor);
addDef(name, colorDescriptor);
$('#colorDescriptors').empty();
$('#colorDescriptors').append("Colors");
addColorSetToServer(function () {
oldUrl = window.location.href.replace(/\?.*/, '');
window.location.href = oldUrl + '?colorSetHash=' + colorSetHash;
});
}
function parseUrl() {
params=getUrlParams();
var transactionHash;
for(param in params) {
if(param == 'goto') {
goTransaction(params[param]);
return;
}
[name, colorDescriptor] = param.split(',');
if(param.split(',').length != 2) {
window.alert('incorrect parameter found');
}
addColor(name, colorDescriptor);
params=getUrlParams();
if('colorSetHash' in params) {
getColorSetFromServer(params['colorSetHash'], function() {
});
}
if(transactionHash !== undefined) {
goTransaction(transactionHash);
if('goto' in params) {
goTransaction(params['goto']);
}
}
</script>
Expand Down

0 comments on commit 179e369

Please sign in to comment.