-
Notifications
You must be signed in to change notification settings - Fork 0
/
recycle.js
458 lines (396 loc) · 14 KB
/
recycle.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/**
* Our rel closure contains functions to show the relationships
* between countries and the barriers indigenous people face there.
* Note terms still refer to actors/shows from the HBOrecycling project the code originated from in GitHub.
* It also handles the
* drawing of all of our objects.
*/
var rel = {
/**
* The first thing we do is set up the options and defaults for
* our drawing.
*/
hitOptions: {
fill: true,
tolerance: 5
},
currentItem: null,
defaultColor: 'lightgray',
defaultTextColor: 'black',
actorHighlight: '#66b95f',
showHighlight: '#f57e20',
actorSelect: '#bb2527',
showSelect: '#30449d',
selectedActors: [],
selectedShows: [],
font: 'ff-basic-gothic-web-pro, Verdana, sans',
/**
* This function clears out the selection indicators of the
* actors and shows.
*/
clearSelection: function() {
rel.clearSelectionPaths();
rel.selectedActors.length = 0;
rel.selectedShows.length = 0;
},
/**
* This function clears out the selection indicators of the
* actors and shows.
*/
clearSelectionPaths: function() {
for (var i = 0; i < rel.selectedActors.length; i++) {
rel.highlight(rel.selectedActors[i], rel.defaultColor, rel.defaultTextColor, 0.5, 1);
}
for (var i = 0; i < rel.selectedShows.length; i++) {
rel.highlight(rel.selectedShows[i], rel.defaultColor, rel.defaultTextColor, 0.5, 1);
}
},
/**
* Toggle the selection of the specific actor
*/
toggleSelected: function(isActor, /*closure*/ object) {
var selectedItems = isActor ? rel.selectedActors : rel.selectedShows;
rel.clearSelectionPaths();
if ($.inArray(object, selectedItems) !== -1) {
rel.removeItem(object, selectedItems);
} else {
selectedItems.push(object);
}
(isActor ? rel.selectedShows : rel.selectedActors).length = 0;
rel.showIntersecting();
},
/**
* Toggle the selection of the specific actor
*/
toggleSelectedActor: function(/*closure*/ actor) {
this.toggleSelected(true, actor);
},
/**
* Toggle the selection of a specific show
*/
toggleSelectedShow: function(/*closure*/ show) {
this.toggleSelected(false, show);
},
/**
* This is some specialized array handling. We could use the
* functions from underscore.js, but these are a little faster
* since we know specifics about our objects.
*/
removeItem: function(object, array) {
var index = rel.indexOf(object, array);
if (index > -1) {
array.splice(index, 1);
}
},
indexOf: function(object, array) {
for (var i = 0; i < array.length; i++) {
if (array[i].name === object.name) {
return i;
}
}
return -1;
},
/**
* Draw the intersection of all the items or shows.
*/
showIntersecting: function() {
showActors = rel.selectedActors.length > 0;
rel.clearSelectionPaths();
var selectColor = showActors ? rel.actorSelect : rel.showSelect;
var selectedItems = showActors ? rel.selectedActors : rel.selectedShows;
var linkedItems = [];
for (var i = 0; i < selectedItems.length; i++) {
var selectedItem = selectedItems[i];
linkedItems.push(selectedItem.linked);
selectedItem.dot.fillColor = selectColor;
selectedItem.text.fillColor = selectColor;
project.activeLayer.addChild(selectedItem.dot);
}
var inter = _.intersection.apply(this, linkedItems);
var relItems = showActors ? rel.selectedActors : rel.selectedShows;
_.each(inter, function(linkedItem) {
linkedItem.dot.fillColor = selectColor;
project.activeLayer.addChild(linkedItem.dot);
for (var i = 0; i < linkedItem.paths.length; i++) {
var path = linkedItem.paths[i];
if ($.inArray(path.linked, relItems) !== -1) {
path.path.strokeColor = selectColor;
path.path.strokeWidth = 2;
path.path.opacity = 1;
}
}
});
},
/**
* Highlight the specified actor or show, the connections, and all of
* the shows this actor was in.
*/
highlight: function(/*closure*/ object, /*string*/ color, /*string*/ textColor, /*int*/ opacity, /*int*/ strokeWidth) {
for (var i = 0; i < object.paths.length; i++) {
var path = object.paths[i].path;
path.strokeColor = color;
path.strokeWidth = strokeWidth;
path.opacity = opacity;
project.activeLayer.addChild(path);
};
_.each(object.linked, function (linkedObject) {
linkedObject.dot.fillColor = color;
linkedObject.text.fillColor = textColor;
project.activeLayer.addChild(linkedObject.dot);
project.activeLayer.addChild(linkedObject.text);
});
object.dot.fillColor = color;
project.activeLayer.addChild(object.dot);
object.text.fillColor = textColor;
},
create: function(isActor, name, point) {
var highlightColor = isActor ? rel.actorHighlight : rel.showHighlight;
return {
linked: [],
dictionary: {},
name: name,
isActor: isActor,
paths: [],
add: function(item) {
if (isActor)
item.linked.push(this);
this.linked.push(item);
},
point: point,
/**
* Create the dot and text items for this item
*/
createItems: function() {
if (this.hasDrawn) {
return;
}
if (isActor)
this.paths = this.createConnections();
this.dot = new Path.Circle(point, isActor ? this.linked.length * 2 : this.linked.length + 2);
this.dot.fillColor = rel.defaultColor;
this.dot.strokeWidth = 0;
this.dot.rel = this;
this.text = new PointText(point + (isActor ? [-20, 5] : [35, 5]));
var size = isActor ? new Size(-150, -10) : new Size(200, -10);
var p = this.text.point + [isActor ? 30 : -40, 0];
this.rect = new Path.Rectangle(p, size);
this.rect.fillColor = new RGBColor(0, 0, 0, 0);
this.rect.rel = this;
// this.rect.selected = true;
this.text.content = name;
this.text.characterStyle = {
fontSize: isActor ? 10 : 14,
fillColor: rel.defaultTextColor,
font: rel.font
};
if (isActor)
this.text.paragraphStyle.justification = 'right';
this.text.rel = this;
this.hasDrawn = true;
},
/**
* Handle mouse over for the dot for this item
*/
mouseover: function() {
rel.highlight(this, highlightColor, highlightColor, 1, 2);
},
/**
* Handle mouse out for the dot for this item
*/
mouseout: function() {
rel.highlight(this, rel.defaultColor, rel.defaultTextColor, 0.5, 1);
rel.showIntersecting();
},
/**
* Handle clicks onthe dot for this item
*/
click: function() {
rel.toggleSelected(isActor, this);
},
/**
* Create the connections for this actor which is a line
* connecting the actor to each show.
*/
createConnections: function() {
var item = this;
var paths = []
_.each(this.linked, function (linked) {
//console.log('draw line: M' + item.x() + ' ' + item.y() + 'L' + linked.x() + ' ' + linked.y());
var p = new Path(item.point, linked.point);
p.strokeColor = rel.defaultColor;
//p.strokeWidth = 1;
//p.opacity = 0.5;
//console.log('p: ' + p);
paths.push({linked: linked, 'path': p});
linked.paths.push({linked: item, 'path': p});
linked.createItems();
});
return paths;
}
};
},
/**
* Create an actor with the specified name at the specified
* position.
*/
createActor: function(/*string*/ name, /*point*/ point) {
return this.create(true, name, point);
},
/**
* Create a new show object with the specified name at the
* specified position.
*/
createShow: function(/*string*/ name, /*point*/ point) {
return this.create(false, name, point);
}
};
/**
* This is our generic mouse move handler. PaperJS doesn't give
* us a simple mouse event for each object so we need to map the
* generic events to our specific object. This handles
* mouseover and mouseout events.
*/
function onMouseMove(event) {
var hitResult = project.hitTest(event.point, rel.hitOptions);
if (hitResult && hitResult.item && hitResult.item.rel) {
if (rel.currentItem === hitResult.item) {
return;
} else {
if (rel.currentItem) {
rel.currentItem.rel.mouseout();
rel.currentItem = null;
}
rel.currentItem = hitResult.item;
rel.currentItem.rel.mouseover();
}
} else {
/*
* This is mouse out
*/
if (rel.currentItem) {
rel.currentItem.rel.mouseout();
rel.currentItem = null;
}
}
};
/**
* Click events are easier. We just find the item which was
* under the mour when the button was released and call its
* click function.
*/
function onMouseUp(event) {
var hitResult = project.hitTest(event.point, rel.hitOptions);
if (hitResult && hitResult.item && hitResult.item.rel) {
hitResult.item.rel.click();
}
}
jQuery(document).ready(function() {
var point = new Point(250, 25);
/*
* Here's where we initialize our data. We keep the mapping
* of actors to shows in a JSON object in the data.json file.
*
* The JSON is a set of actors with each show they've worked
* on. Like this:
*
* "AmyRyan": [
* "In Treatment",
* "The Wire"
* ]
*
* We iterate through the actors and build the show objects.
*/
var actorNames = [];
var showNames = [];
for (var actor in DATA) {
if (DATA.hasOwnProperty(actor)) {
actorNames.push(actor);
var shows = DATA[actor];
_.each(shows, function (show) {
showNames.push(show);
});
}
}
/*
* Now we sort the show names alphabetically. We have some
* specialized handling to properly handle shows which start
* with "The "
*/
showNames = _.sortBy(showNames, function(show) {
if (show.indexOf('The') === 0) {
return show.substring(4);
} else {
return show;
}
});
/*
* Then we prune out the duplicate names.
*/
showNames = _.uniq(showNames, true);
/*
* Now we're ready to build the real objects.
*/
var actors = _.map(actorNames, function (name) {
var actor = rel.createActor(name, point.clone());
point.y += 16;
return actor;
});
var actorY = point.y;
point = new Point(700, 25);
var shows = _.map(showNames, function (name) {
var show = rel.createShow(name, point.clone());
point.y += 46;
return show;
});
/*
* Now we map each actor to the shows they are in.
*/
_.each(shows, function (show) {
_.each(actors, function (actor) {
if (_.indexOf(DATA[actor.name], show.name, false) > -1) {
actor.add(show);
}
});
});
/*
* Then we set the height of our view
*/
view.viewSize = [1000, actorY + 200];
/*
* The last step is to draw each actor which draws the shows
*/
_.each(actors, function (actor) {
actor.createItems();
});
$('.actorLink').each(function() {
var link = $(this);
$(this).click(function(evt) {
evt.preventDefault();
rel.clearSelection();
_.each(actors, function (actor) {
if (actor.name === link.text()) {
/*
* Now we've found the actor from the link which was
* clicked. Next we need to iterate over each show
* that actor was is and select it.
*/
_.each(actor.linked, function (show) {
rel.toggleSelectedShow(show);
});
view.draw();
}
});
});
});
$('#deadwoodjohn').click(function(evt) {
evt.preventDefault();
rel.clearSelection();
_.each(shows, function (show) {
if (show.name === 'Deadwood' ||
show.name === 'John from Cincinnati') {
rel.toggleSelectedShow(show);
}
});
paper.view.draw();
});
});