-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
86 lines (65 loc) · 2.82 KB
/
graph.js
File metadata and controls
86 lines (65 loc) · 2.82 KB
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
// a node that connects to other nodes via 'plugs'
// plug is just a name of collection that contains other nodes
// GraphNode specializes GenericGraphNode by adding 'children' and 'parents' plugs
var GenericGraphNode = Backbone.Model.extend({
defaults: { name: 'node' },
initialize: function() {
var self = this
// simple decorator to check if plug exists for plug accepting functions
var plugfundecorator = function() {
var args = toArray(arguments); var f = args.shift(); var plugname = _.first(args);
if (!self.plugs[plugname]) { throw "graph node can't find plug named '" + plugname + "'"; return; }
return f(args)
}
_.map(['get', 'add', 'remove', 'has'], function(fname) {
self.fname = decorate(plugfundecorator,self.fname)
})
this.plugs = {}
},
name: function() {
return this.get('name')
},
addplug: function(plugplural, plugsingular, nomodels) {
var self = this;
if (!plugsingular) { plugsingular = plugplural }
this.plugs[plugplural] = true
this[ plugplural ] = new Backbone.Collection()
this[ 'add' + plugsingular ] = decorate(multiArg,function(obj) { return self.plugadd.call(self,plugplural,obj) })
this[ 'del' + plugsingular ] = decorate(multiArg,function(obj) { return self.plugremove.call(self,plugplural,obj) })
this[ 'del' + plugplural ] = function() { return self.plugremoveall.call(self,plugplural) }
this[ 'has' + plugsingular ] = function(obj) { return self.plughas.call(self,plugplural,obj) }
this[ 'get' + plugsingular ] = function() { return _.first(self.plugget.call(self,plugplural)) }
this[ 'get' + plugplural ] = function() { return self.plugget.call(self,plugplural) }
},
plugget: function(plug) {
return this[plug].models
},
plugadd: function(plug,obj) {
//console.log(this.get('name'), 'add', plug,obj.get('name'))
if (!this.plughas(plug,obj)) { this[plug].add(obj) }
},
plugremove: function(plug,obj) {
this[plug].remove(obj)
},
plugremoveall: function(plug,obj) {
var plug = this[plug]
plug.map(function(obj) { plug.remove(obj) })
},
plughas: function(plug, obj) {
return (this[plug].indexOf(obj) != -1)
}
})
// GraphNode specializes GenericGraphNode by adding 'children' and 'parents' plugs
var GraphNode = GenericGraphNode.extend4000({
initialize: function() {
var self = this;
this.addplug('parents','parent')
this.addplug('children','child')
this.parents.bind('add',function(obj) {
obj.addchild(self)
})
this.children.bind('add',function(obj) {
obj.addparent(self)
})
}
})