summaryrefslogtreecommitdiff
path: root/scripts/history.js
blob: 02ca8b0f21796ecac88c95ea4f518a8da9468fa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Once upon a time we shall have a versioned property graph from which history
// will be one extractable aspect, much like a git for graphs. Now we just have
// a plain list of events for a specific user.

// Enums and chrome don't play along well. Object.freeze I guess? actually rhizi code cuases
// exceptions but that shouldn't break the console, as evidenced by the '__commandLineAPI is not defined'
// error below.
//
// Uncaught TypeError: Can't add property addednodes, object is not extensible rhizicore.js:4
// Uncaught TypeError: Can't add property text, object is not extensible textanalysis.js:3
// Uncaught TypeError: Can't add property key, object is not extensible buttons.js:5
// Uncaught ReferenceError: sentence is not defined robot.js:11
// Resource interpreted as Font but transferred with MIME type application/font-sfnt: "http://localhost:8000/external/Lato300.ttf". jquery.js:2
// > document
// ReferenceError: __commandLineAPI is not defined
//var ActionEnum = Enum();

function History(user) {
    this.records = [];
    this.user = user;
}

var ACTION_KEYSTROKES = 'ACTION_KEYSTROKES';
var ACTION_MOUSE = 'ACTION_MOUSE';
var ACTION_GRAPH_ADD = 'ACTION_GRAPH_ADD';
var ACTION_GRAPH_DELETE = 'ACTION_GRAPH_DELETE';

var KEYSTROKE_WHERE_TEXTANALYSIS = 'KEYSTROKE_WHERE_TEXTANALYSIS';
var KEYSTROKE_WHERE_DOCUMENT = 'KEYSTROKE_WHERE_DOCUMENT';
var KEYSTROKE_WHERE_EDIT_NODE = 'KEYSTROKE_WHERE_EDIT_NODE';

History.prototype.record = function(d)
{
    if (d === undefined || d.action === undefined) {
        throw "Invalid arguments";
    }
    d['user'] = this.user;
    d['timestamp'] = new Date();
    this.records.push(d);
};

History.prototype.save_to_file = function()
{
    // TODO: filename and chrome support (grep json;base64 for other locations)
    // perhaps has to wait for FileWriter api?
    location.href = 'data:text/json;base64,' + window.btoa(JSON.stringify(this.records, function (k, v) { return v; }, 2));
};

History.prototype.clear_history = function()
{
    this.records = [];
}

History.prototype.record_nodes_removal = function(ids)
{
    this.record({
        'action': ACTION_GRAPH_DELETE,
        'node_ids': ids
    });
}

History.prototype.record_links = function(links)
{
    if (links === undefined || links.length === undefined || links.length <= 0) {
        throw "Invalid arguments"
    }
    this.record({
        'action': ACTION_GRAPH_ADD,
        'nodes': [],
        'links': links
    });
}

History.prototype.record_nodes = function(nodes)
{
    if (nodes === undefined || nodes.length === undefined || nodes.length <= 0) {
        throw "Invalid arguments"
    }
    this.record({'action': ACTION_GRAPH_ADD,
     'nodes': nodes,
     'links': []
    });
}

History.prototype.record_keystrokes = function(where, keys)
{
    if (keys === undefined || keys.length === undefined || keys.length <= 0) {
        throw "Invalid arguments";
    }
    keys = keys.filter(function(k) { return k !== undefined; });
    if (keys.length == 0) {
        return;
    }
    this.record({'action': ACTION_KEYSTROKES,
        'keys': keys,
        'where': where
    });
}