Skip to content

Commit

Permalink
Allowing loading of serialized binary (some tweaks needed). Also twea…
Browse files Browse the repository at this point in the history
…king tool styles.
  • Loading branch information
pieman72 committed Jul 14, 2014
1 parent 5422adb commit 6e3dd0f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 30 deletions.
5 changes: 5 additions & 0 deletions load/index.php
@@ -0,0 +1,5 @@
<?
$hash = $_POST['hash'];

header('Content-type: application/adhoc');
readfile("../generate/$hash.adh");
17 changes: 8 additions & 9 deletions static/css/style.css
Expand Up @@ -184,19 +184,17 @@ label {
min-height:24px;
padding:0 8px;
}
.Action .toolboxTab[data-target=Action],
.Group .toolboxTab[data-target=Group],
.Control .toolboxTab[data-target=Control],
.Operator .toolboxTab[data-target=Operator],
.Variable .toolboxTab[data-target=Variable],
.Literal .toolboxTab[data-target=Literal] {
border:1px solid #6FdF00;
color:#6FdF00;
}
.dark .toolboxTab {
background:#333333;
box-shadow:0 0 4px 3px #111111 inset;
}
.Action .toolboxTab[data-target=Action] { border:1px solid #6FdF00; color:#6FdF00; }
.Group .toolboxTab[data-target=Group] { border:1px solid #808080; color:#808080; }
.Control .toolboxTab[data-target=Control] { border:1px solid #D7005F; color:#D7005F; }
.Operator .toolboxTab[data-target=Operator] { border:1px solid #AF5FFF; color:#AF5FFF; }
.Variable .toolboxTab[data-target=Variable] { border:1px solid #000000; }
.dark .Variable .toolboxTab[data-target=Variable] { border:1px solid #EEEEEE; }
.Literal .toolboxTab[data-target=Literal] { border:1px solid #FF8700; color:#FF8700; }
.toolboxItem {
cursor:pointer;
display:none;
Expand All @@ -216,6 +214,7 @@ label {
.Group .toolboxItem.Group ,
.Control .toolboxItem.Control ,
.Operator .toolboxItem.Operator ,
.Operator .toolboxItem.Assignment ,
.Variable .toolboxItem.Variable ,
.Literal .toolboxItem.Literal {
display:block;
Expand Down
116 changes: 95 additions & 21 deletions static/js/adhoc.js
Expand Up @@ -4,6 +4,10 @@ Event.KEY_COMMAND1 = 91;
Event.KEY_COMMAND2 = 93;
Event.KEY_COMMAND3 = 224;

// Not sure why JavaScript doesn't include this...
String.prototype.ltrim = function(s){ return this.replace(new RegExp("^"+s+"+", "")); }
String.prototype.rtrim = function(s){ return this.replace(new RegExp(s+"+$", "")); }

// Set up everything only after the page loads
Event.observe(window, 'load', function(){
var adhoc = {};
Expand Down Expand Up @@ -838,11 +842,15 @@ Event.observe(window, 'load', function(){
// Convert an int to a 3-byte string
adhoc.intTo3Byte = function(i){
var out = String.fromCharCode(i%256);
i = (i/256)>>0;
i >>= 8;
out = String.fromCharCode(i%256) + out;
i = (i/256)>>0;
i >>= 8;
return String.fromCharCode(i%256) + out;
}
// Convert a 3-byte string to an int
adhoc.intFrom3Byte = function(s){
return (((s.charCodeAt(0)<<8)+s.charCodeAt(1))<<8)+s.charCodeAt(2);
}

// Validate integer values
adhoc.validateInt = function(v){
Expand Down Expand Up @@ -894,7 +902,7 @@ Event.observe(window, 'load', function(){

// Set the value in memory, and in the cookie, then return it
adhoc.settings[s] = v;
document.cookie = 'adhocSettings='+Object.toJSON(adhoc.settings);
document.cookie = 'adhocSettings='+Object.toJSON(adhoc.settings)+';path=/adhoc_demo/';
return v;
}

Expand Down Expand Up @@ -1467,7 +1475,7 @@ Event.observe(window, 'load', function(){
adhoc.setting(i, loadedSettings[i]);
}
}else{
document.cookie = 'adhocSettings='+Object.toJSON(adhoc.settings);
document.cookie = 'adhocSettings='+Object.toJSON(adhoc.settings)+';path=/adhoc_demo/';
}

// Activate package name input
Expand Down Expand Up @@ -1496,6 +1504,7 @@ Event.observe(window, 'load', function(){
adhoc.rootNode = adhoc.createNode(
null
,null
,null
,adhoc.nodeTypes.ACTION
,adhoc.nodeWhich.ACTION_DEFIN
,adhoc.nodeChildType.STATEMENT
Expand Down Expand Up @@ -1773,15 +1782,15 @@ Event.observe(window, 'load', function(){
// Prompt for an action name
adhoc.promptValue('Enter an action name:', adhoc.validateActionDefName, false, function(val){
adhoc.deactivateAllTools();
adhoc.createNode(prnt, repl, type, which, childType, null, val);
adhoc.createNode(null, prnt, repl, type, which, childType, null, val);
});
break;

case adhoc.nodeWhich.ACTION_CALL:
// Prompt for an action name
adhoc.promptValue('Enter an action name:', adhoc.validateActionName, false, function(val, rem, hid){
adhoc.deactivateAllTools();
adhoc.createNode(prnt, repl, type, which, childType, rem, val, null, hid);
adhoc.createNode(null, prnt, repl, type, which, childType, rem, val, null, hid);
}, adhoc.actionSearch, 'Not found in loaded projects');
break;

Expand All @@ -1790,39 +1799,39 @@ Event.observe(window, 'load', function(){
// Prompt for a variable name
adhoc.promptValue('Enter a variable name:', adhoc.validateIdentifier, false, function(val, rem, hid){
adhoc.deactivateAllTools();
adhoc.createNode(prnt, repl, type, which, childType, null, val, null, hid);
adhoc.createNode(null, prnt, repl, type, which, childType, null, val, null, hid);
}, adhoc.genScopeSearch(prnt, false), 'New variable');
break;

case adhoc.nodeWhich.LITERAL_BOOL:
// Prompt for a boolean value
adhoc.promptFlag('Select a boolean value:', ['true', 'false'], function(val){
adhoc.deactivateAllTools();
adhoc.createNode(prnt, repl, type, which, childType, null, null, !val);
adhoc.createNode(null, prnt, repl, type, which, childType, null, null, !val);
});
break;

case adhoc.nodeWhich.LITERAL_INT:
// Prompt for an integer value
adhoc.promptValue('Enter an integer:', adhoc.validateInt, true, function(val){
adhoc.deactivateAllTools();
adhoc.createNode(prnt, repl, type, which, childType, null, null, parseInt(val));
adhoc.createNode(null, prnt, repl, type, which, childType, null, null, parseInt(val));
});
break;

case adhoc.nodeWhich.LITERAL_FLOAT:
// Prompt for a float value
adhoc.promptValue('Enter a float:', adhoc.validateFloat, true, function(val){
adhoc.deactivateAllTools();
adhoc.createNode(prnt, repl, type, which, childType, null, null, parseFloat(val));
adhoc.createNode(null, prnt, repl, type, which, childType, null, null, parseFloat(val));
});
break;

case adhoc.nodeWhich.LITERAL_STRNG:
// Prompt for a string value
adhoc.promptValue('Enter a string:', adhoc.validateString, false, function(val){
adhoc.deactivateAllTools();
adhoc.createNode(prnt, repl, type, which, childType, null, null, val);
adhoc.createNode(null, prnt, repl, type, which, childType, null, null, val);
});
break;

Expand All @@ -1832,12 +1841,12 @@ Event.observe(window, 'load', function(){
adhoc.deactivateAllTools();
// TODO: Prompt for literal value
adhoc.message('This type of literal is not yet implemented'); break;
adhoc.createNode(prnt, repl, type, which, childType);
adhoc.createNode(null, prnt, repl, type, which, childType);
break;

default:
adhoc.deactivateAllTools();
adhoc.createNode(prnt, repl, type, which, childType);
adhoc.createNode(null, prnt, repl, type, which, childType);
}
}

Expand Down Expand Up @@ -2003,6 +2012,25 @@ adhoc.createNode(prnt, repl, type, which, childType);
}
break;

// (CTRL+m) Test unserialize
case 77:
adhoc.rootNode = null;
adhoc.lastId = 0;
adhoc.allNodes = [];
new Ajax.Request('load/', {
parameters: {
hash: 'bfb850ee4a0f106496df865a247383ef'
}
,onSuccess: function(t){
try{
adhoc.rootNode = adhoc.unserialize(t.responseText);
console.log(adhoc.rootNode);
adhoc.refreshRender();
}catch(e){ console.log("Line "+e.lineNumber+"\n"+e); }
}
});
break;

// (CTRL+y) Redo
case 89:
if(adhoc.alternateKeys) adhoc.history.redo();
Expand All @@ -2022,15 +2050,15 @@ adhoc.createNode(prnt, repl, type, which, childType);
var keyUpFunc = function(e){
var key = e.which || window.event.keyCode;
switch(key){
// If various CTRL or CMD keys, set alternamte key mode off
// (CTRL/CMD) Set alternamte key mode off
case Event.KEY_CONTROL:
case Event.KEY_COMMAND1:
case Event.KEY_COMMAND2:
case Event.KEY_COMMAND3:
adhoc.alternateKeys = false;
break;

// If DEL, remove the selected node and it's children
// (DEL) Remove the selected node and it's children
case Event.KEY_DELETE:
// Edge cases
if(!adhoc.selectedNode) break;
Expand All @@ -2050,7 +2078,7 @@ adhoc.createNode(prnt, repl, type, which, childType);
adhoc.refreshRender();
break;

// Do nothing if the key is unknown
// (Unknown) Do nothing
default:
if(adhoc.setting('dbg')) console.log(key);
}
Expand Down Expand Up @@ -2081,6 +2109,7 @@ adhoc.createNode(prnt, repl, type, which, childType);
adhoc.rootNode = adhoc.createNode(
null
,null
,null
,adhoc.nodeTypes.ACTION
,adhoc.nodeWhich.ACTION_DEFIN
,adhoc.nodeChildType.STATEMENT
Expand All @@ -2101,15 +2130,15 @@ adhoc.rootNode = adhoc.createNode(
return ++adhoc.lastId;
}
// Create a new node with just a type and empty contents
adhoc.createNode = function(p, r, t, w, c, k, n, v, f){
adhoc.createNode = function(i, p, r, t, w, c, k, n, v, f){
// Set the type, which, and childType if they're not passed
if(!t) t = adhoc.nodeTypes.TYPE_NULL;
if(!w) w = adhoc.nodeWhich.WHICH_NULL;
if(!c) c = adhoc.nodeChildType.CHILD_NULL;

// Create the object with its params
var newNode = {
id: (t == adhoc.nodeTypes.TYPE_NULL) ? null : adhoc.nextId()
id: (i ? i : ((t == adhoc.nodeTypes.TYPE_NULL) ? null : adhoc.nextId()))
,parent: p
,scope: null
,referenceId: f ? parseInt(f) : null
Expand Down Expand Up @@ -2201,7 +2230,8 @@ adhoc.rootNode = adhoc.createNode(
for(var i=0; i<neededChildren.length; ++i){
for(var j=0; j<neededChildren[i].min; ++j){
adhoc.createNode(
newNode
null
,newNode
,null
,adhoc.nodeTypes.TYPE_NULL
,adhoc.nodeWhich.WHICH_NULL
Expand Down Expand Up @@ -2946,6 +2976,48 @@ adhoc.rootNode = adhoc.createNode(
}
return out;
}
// Function to unserialize binary into a node
adhoc.unserialize = function(s){
// Get the node parts from the input string
var tempNode = {};
tempNode.id = adhoc.intFrom3Byte(s.substr(0, 3));
tempNode.parentId = adhoc.intFrom3Byte(s.substr(3, 3));
tempNode.nodeType = adhoc.intFrom3Byte(s.substr(6, 3));
tempNode.which = adhoc.intFrom3Byte(s.substr(9, 3));
tempNode.childType = adhoc.intFrom3Byte(s.substr(12, 3));
var found = 1;
var offset = 16;
while(found<6){
offset = s.indexOf('"', offset+1);
if(s.charAt(offset-1) == "\\") continue;
++found;
}
var parts = s.substring(16, offset).split('""');
tempNode.package = (parts[0]=="NULL" ? null : parts[0]);
tempNode.name = (parts[1]=="NULL" ? null : parts[1]);
tempNode.value = (parts[2]=="NULL" ? null : parts[2]);

// Create a new node from the parts
var newNode = adhoc.createNode(
tempNode.id
,tempNode.parentId ? adhoc.allNodes[tempNode.parentId] : null
,null
,tempNode.nodeType
,tempNode.which
,tempNode.childType
,tempNode.package
,tempNode.name
,tempNode.value
//,f // Reference Id
);
adhoc.lastId = Math.max(newNode.id+1, adhoc.lastId);

// Continue until we've exhaused the string
s = s.substr(offset+1);
if(!s) return newNode;
adhoc.unserialize(s);
return newNode;
}
// Function to serialize a node and its children for the history manager
adhoc.serializeComplete = function(n, deep){
var out = {
Expand Down Expand Up @@ -3055,7 +3127,8 @@ adhoc.rootNode = adhoc.createNode(
if(neededChildren[i].childType != n.childType) continue;
if(adhoc.countChildrenOfType(p1, n.childType) < neededChildren[i].min){
adhoc.createNode(
p1
null
,p1
,null
,adhoc.nodeTypes.TYPE_NULL
,adhoc.nodeWhich.WHICH_NULL
Expand Down Expand Up @@ -3134,7 +3207,8 @@ adhoc.rootNode = adhoc.createNode(
if(neededChildren[i].childType != n.childType) continue;
if(adhoc.countChildrenOfType(n.parent, n.childType) < neededChildren[i].min){
adhoc.createNode(
n.parent
null
,n.parent
,null
,adhoc.nodeTypes.TYPE_NULL
,adhoc.nodeWhich.WHICH_NULL
Expand Down

0 comments on commit 6e3dd0f

Please sign in to comment.