This repository has been archived by the owner on Mar 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (82 loc) · 1.8 KB
/
index.js
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
99
100
var fixtures = require('add-fixtures');
var child = require('reactive-child');
var db = require('db');
var Graph = require('graph-model');
var Item = require('graph-view');
var menu = require('menu');
var reactive = require('reactive');
var series = require('array-series');
var split = require('split-callback');
/**
* Create a graph menu.
*/
var Menu = menu(Item);
/**
* Kick it all off.
*/
series([setup, boot], function (err, res) {
if (err) throw err;
});
/**
* Setup the database.
*
* @param {Function} fn
*/
function setup (fn) {
var fns = split(fn);
db.transaction(function (t) {
var schema = '(id INTEGER PRIMARY KEY AUTOINCREMENT, data)';
var sql = 'CREATE TABLE IF NOT EXISTS graph ' + schema;
t.executeSql(sql);
}, fns.success, fns.error);
}
/**
* Get all graphs an initialize the App.
*
* @param {Function} fn
*/
function boot (fn) {
Graph.all(function (err, collection) {
if (err) return fn(err);
new App(collection, document.body.querySelector('.App'));
fn();
});
}
/**
* Initialize the `App` with `graphs` and `el`.
*
* @param {Collection} graphs
* @param {Element} el
*/
function App (graphs, el) {
this.el = el;
this.collection = graphs;
var menu = this.menu = new Menu();
// TODO pass collection straight to menu constructor
graphs.each(function (graph) {
menu.add(graph);
});
menu.on('select', function (el, model, view) {
view.render();
});
menu.on('add', function (el, model, view) {
view.edit();
});
reactive(this.el, {}, this)
.use(child);
}
/**
* Create a new graph.
*
* @param {Event} e
*/
App.prototype.new = function (e) {
var col = this.collection;
var menu = this.menu;
var model = new Graph();
model.save(function (err) {
if (err) throw err;
col.push(model);
menu.add(model);
});
};