UNPKG

4.52 kBJavaScriptView Raw
1var BUCKET_SIZE = 4
2
3var d3 = require('d3')
4var _ = require('lodash')
5var aolog = require('../log.js')(null, BUCKET_SIZE)
6
7var WIDTH = document.body.scrollWidth
8var HEIGHT = document.documentElement.clientHeight
9
10var container = d3.select('#canvas').append('svg')
11 .attr('width', WIDTH)
12 .attr('height', HEIGHT)
13 .attr('transform', 'translate(' + WIDTH / 2 + ', 10)')
14
15container.append('g').attr('id', 'buckets')
16container.append('g').attr('id', 'elements')
17
18var ELHEIGHT = 10
19var ELWIDTH = 2
20var PADDING = 4
21
22var BUCKET_WIDTH = ELWIDTH * BUCKET_SIZE + (PADDING * (BUCKET_SIZE + 1))
23var HALF_BUCKET = BUCKET_WIDTH / 2
24
25var ZOOM_DIST = ELWIDTH * 50
26
27var log = aolog.empty()
28
29var State = {bucketCount: 0,
30 elementCount: 0}
31
32var draw = function (tree, offsetx, offsety, format) {
33 if (!format) format = {i: 0, fingerdepth: 0, depth: 0}
34 // sort
35
36 // console.log('draw ' + tree.type)
37
38 if (tree.type === 'Finger') {
39 draw(tree.head.ref,
40 offsetx - HALF_BUCKET - PADDING * 4,
41 offsety,
42 _.assign({}, format, {align: 'left'}))
43
44 draw(tree.rest.ref,
45 offsetx,
46 offsety + (ELHEIGHT + PADDING * 4) * (format.fingerdepth + 1),
47 _.assign({}, format, {align: 'middle', fingerdepth: format.fingerdepth + 1}))
48
49 draw(tree.tail.ref,
50 offsetx + HALF_BUCKET + PADDING * 4,
51 offsety,
52 _.assign({}, format, {align: 'right'}))
53
54 }
55 if (tree.type === 'Bucket') {
56 drawBucket(tree, offsetx, offsety, format)
57 }
58}
59
60var drawBucket = function (bucket, offsetx, offsety, format) {
61 var id = 'bucket' + State.bucketCount++
62
63 // console.log(offsetx, offsety)
64
65 if (format.i !== 'undefined') {
66
67 // console.log('huh')
68 // console.log(format.depth)
69
70 var ofs
71 if (format.align === 'middle') {
72 ofs = ((format.i - BUCKET_SIZE / 2) + 1 / 2) * format.depth
73 } else if (format.align === 'left') {
74 ofs = (format.i - BUCKET_SIZE + 1) * format.depth
75 } else if (format.align === 'right') {
76 ofs = (format.i) * format.depth
77 } else {
78 ofs = 0
79 }
80
81 offsetx += ofs * (BUCKET_WIDTH + PADDING)
82 }
83
84 var x = offsetx - HALF_BUCKET
85 var y = offsety
86
87 // console.log('bucket x,y')
88 // console.log(x,y)
89
90 // bucket background
91
92 var el = State[id]
93
94 if (el) {
95 // moved?
96 if (el.attr('x') !== x ||
97 el.attr('y') !== y) {
98 el.transition()
99 .duration(700)
100 .attr('x', x)
101 .attr('y', y)
102 }
103 } else {
104 // zoom in new
105 el = container.select('#buckets').append('rect')
106 .attr('class', 'bucket')
107 .attr('opacity', 0)
108 .attr('x', x + ZOOM_DIST)
109 .attr('y', y)
110 .attr('stroke', '#000')
111 .attr('fill', '#eae')
112 .attr('width', BUCKET_WIDTH)
113 .attr('height', ELHEIGHT + PADDING * 2)
114
115 el
116 .transition()
117 .duration(700)
118 .attr('opacity', 1)
119 .attr('x', x)
120 State[id] = el
121 }
122
123 for (var i = 0 ; i < bucket.elements.length ; i++) {
124 drawElement(bucket.elements[i],
125 offsetx,
126 offsety,
127 _.assign({}, format, {i: i, depth: format.depth + 1}))
128 }
129}
130
131var drawElement = function (element, offsetx, offsety, format) {
132
133 // console.log('offsetx, offsety element')
134 // console.log(offsetx, offsety)
135
136 var x = offsetx - HALF_BUCKET + PADDING + (format.i * (ELWIDTH + PADDING))
137 var y = offsety + PADDING
138
139 var id
140 if (element.ref) {
141 drawBucket(element.ref,
142 offsetx,
143 offsety + ELHEIGHT + PADDING * 4,
144 format)
145 } else {
146 id = element
147
148 var el = State[id]
149
150 if (el) {
151 // moved?
152 if (el.attr('x') !== x ||
153 el.attr('y') !== y) {
154 el.transition()
155 .duration(700)
156 .attr('x', x)
157 .attr('y', y)
158 }
159
160 } else {
161 // new
162 el = container.select('#elements').append('rect')
163 .attr('class', 'element')
164 .attr('opacity', 0)
165 .attr('x', x + ZOOM_DIST)
166 .attr('y', y)
167 .attr('stroke', '#000')
168 .attr('fill', '#fcf')
169 .attr('width', ELWIDTH)
170 .attr('height', ELHEIGHT)
171
172 el
173 .transition()
174 .duration(700)
175 .attr('opacity', 1)
176 .attr('x', x)
177
178 State[id] = el
179 }
180 }
181}
182
183var i = 0
184
185for (i = 0 ; i < 0 ; i++) {
186 log = log.append(i)
187}
188
189var count = 5000
190
191draw(log, 0, 0)
192
193var interval = setInterval(function () {
194 log = log.append(i++)
195 State.bucketCount = 0
196 State.elementCount = 0
197 draw(log, 0, 0)
198 if (!--count) clearInterval(interval)
199}, 1000)