-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards.js
144 lines (144 loc) · 5.36 KB
/
cards.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
///<reference path="d3.d.ts" />
"use strict";
// A & B are required by haste for callbacks. See:
// https://github.com/valderman/haste-compiler/blob/master/doc/js-externals.txt
// for details.
var A;
var B;
// For debugging.
function showAlert_ffi(msg) {
alert(msg);
}
// For debugging.
function consoleLog_ffi(msg) {
console.log(msg);
}
// Global scale to apply to all cards displayed
var cardScale = 0.5;
var drag = d3.behavior.drag()
.on("dragstart", dragstart)
.on("drag", dragmove)
.on("dragend", dragend);
function dragstart() {
d3.event.sourceEvent.stopPropagation();
var selectArg;
// Turn off mouseover across the board while dragging
selectArg = 'g[class*="visible"]';
d3.selectAll(selectArg).on("mouseover", null);
selectArg = 'g[class="solitareDeck"]';
d3.selectAll(selectArg).on("mouseover", null);
selectArg = 'g[class="hiddenReserves"]';
d3.selectAll(selectArg).on("mouseover", null);
}
// Define drag beavior
function dragmove(d) {
d.xtranslate += d3.event.dx / cardScale;
d.ytranslate += d3.event.dy / cardScale;
d3.select(this).attr("transform", "scale (" + cardScale + ")"
+ "translate (" + d.xtranslate + "," + d.ytranslate + ")");
}
// Provide for callback into haskell when object stops being dragged.
var dragEndCallback;
// Called from haskell
function setDragEndCallback_ffi(cb) {
dragEndCallback = cb;
}
// Define dragend behavior - just call back into haskell.
function dragend(d) {
var selectArg;
// Turn on mouseover for all visible objects when done dragging
selectArg = 'g[class*="visible"]';
d3.selectAll(selectArg).on("mouseover", mouseover);
selectArg = 'g[class="solitareDeck"]';
d3.selectAll(selectArg).on("mouseover", mouseover);
selectArg = 'g[class="hiddenReserves"]';
d3.selectAll(selectArg).on("mouseover", mouseover);
var dragged = d3.select(this);
var draggedClassName = dragged.attr("class");
var draggedId = dragged.select("g").attr("id"); // select("g") because card is nested below dragged object
var coordinates = d3.mouse(this.parentNode);
var xCoord = coordinates[0];
var yCoord = coordinates[1];
dragEndCallback(draggedId, draggedClassName, xCoord, yCoord);
}
// Provide for callback into haskell when mouse passes over object
var mouseoverCallback;
// Called from haskell
function setMouseoverCallback_ffi(cb) {
mouseoverCallback = cb;
}
// Define mouseover behavior - just call back into haskell.
function mouseover(d, i) {
var moused = d3.select(this);
var mousedClassName = moused.attr("class");
var mousedId = moused.select("g").attr("id");
var coordinates = d3.mouse(this.parentNode);
var xCoord = coordinates[0];
var yCoord = coordinates[1];
mouseoverCallback(mousedId, mousedClassName, xCoord, yCoord);
}
function placeCard(id, name, classname, x, y) {
// Thanks to :
// http://stackoverflow.com/questions/10337640/how-to-access-the-dom-element-that-correlates-to-a-d3-svg-object
// for telling how to use node() to retrieve DOM element from selection.
// Select to see if the card has already been displayed
var cardSelect = d3.select('body svg g[data-name="' + name + '"]');
var card;
var alreadyDisplayed = !cardSelect.empty();
if (!alreadyDisplayed) {
var documentElement = document.getElementById(id);
cardSelect
= d3.select("body svg")
.append("g")
.each(function (d, i) {
this.appendChild(documentElement.cloneNode(true));
})
.attr("data-name", function (d, i) {
return name;
});
}
// queryString thanks to : http://stackoverflow.com/questions/23034283/is-it-possible-to-use-htmls-queryselector-to-select-by-xlink-attribute-in-an
card = cardSelect.node();
var base = d3.select(card).select('use[*|href="#base"]');
var xOffset = parseInt(base.attr("x"));
var yOffset = parseInt(base.attr("y"));
cardSelect
.data([{ xtranslate: (0 + x / cardScale - xOffset),
ytranslate: (235.27 + y / cardScale - yOffset)
}])
.attr("class", function (d, i) { return classname; });
var transformFunction = function (d, i) {
return "scale (" + cardScale + ")"
+ "translate (" + d.xtranslate + "," + d.ytranslate + ")";
};
if (alreadyDisplayed) {
cardSelect.transition().attr("transform", transformFunction);
}
else {
cardSelect.attr("transform", transformFunction).on("mouseover", mouseover);
// There must be a better way of enabling drag
// for new cards in a visble column.
if ((classname.indexOf("visibleColumn") > -1)
|| (classname == "hiddenReserves")
|| (classname == "solitareDeck")) {
var selectArg = "g[class=" + classname + "]";
d3.selectAll(selectArg).call(drag);
}
}
}
function deleteByClass(cssSelection) {
d3.selectAll("." + cssSelection).remove();
}
function loadCards(cb) {
//Import the full deck of cards.
d3.xml("pretty-svg-cards.svg", "image/svg+xml", function (xml) {
d3.select("body")
.append("div")
.attr("style", "display: none; visibility: hidden")
.each(function (d, i) {
this.appendChild(xml.documentElement.cloneNode(true));
});
// Call back to haskell when done.
cb();
});
}