diff --git a/data/clock.js b/data/clock.js index 2fc58e6..f8f3e1e 100644 --- a/data/clock.js +++ b/data/clock.js @@ -159,8 +159,8 @@ function onConnection(connection){ 'data-content-type': connection.contentType, 'data-how-many': 1 }); - g.onmouseenter = showTooltip; - g.onmouseleave = hideTooltip; + g.onmouseenter = tooltip.show; + g.onmouseleave = tooltip.hide; var x = connectionIdx * 10; var y = 0; g.appendChild(svg('circle', { diff --git a/data/index.html b/data/index.html index 9e56e5a..ce23af5 100644 --- a/data/index.html +++ b/data/index.html @@ -74,8 +74,9 @@ - + + diff --git a/data/svgdataset.js b/data/svgdataset.js new file mode 100644 index 0000000..57dc59b --- /dev/null +++ b/data/svgdataset.js @@ -0,0 +1,42 @@ +(function(global){ + +function dataAttrToKey(attr){ + return attr.slice(5).split('-').map(function(part, index){ + if (index){ + return part[0].toUpperCase() + part.slice(1); + } + return part; + }).join(''); +} + +function dataKeyToAttr(key){ + return 'data-' + key.replace(/([A-Z])/, '-$1').toLowerCase(); +} + +function svgdataset(elem){ + // work around the fact that SVG elements don't have dataset attributes + var ds = function(key, value){ + if (value === undefined){ + // act as getter + return JSON.parse(elem.getAttribute(dataKeyToAttr(key))); + }else{ + elem.setAttribute(dataKeyToAttr(key), JSON.stringify(value)); + } + } + // Create read-only shortcuts for convenience + Array.prototype.forEach.call(elem.attributes, function(attr){ + if (attr.name.startsWith('data-')){ + try{ + ds[dataAttrToKey(attr.name)] = JSON.parse(attr.value); + }catch(e){ + ds[dataAttrToKey(attr.name)] = attr.value; + console.error('unable to parse %s', attr.value); + } + } + }); + return ds; +} + +global.svgdataset = svgdataset; + +})(this); diff --git a/data/tooltip.js b/data/tooltip.js index 472e5ae..698eab1 100644 --- a/data/tooltip.js +++ b/data/tooltip.js @@ -1,4 +1,4 @@ -(function(){ +(function(global){ var tooltipTimer; @@ -10,13 +10,8 @@ function showTooltip(event){ tooltip.innerHTML = event.target.getAttribute('data-target'); var rect = event.target.getClientRects()[0]; var tooltipWidth = tooltip.offsetWidth; - // console.log('rect: %o, width: %s', rect, tooltipWidth); tooltip.style.top = (rect.top - 60) + 'px'; tooltip.style.left = (rect.left + (rect.width / 2) - (tooltipWidth / 2)) + 'px'; - // PLACEHOLDER (FOR DEBUGGING) - // placeholder.style.left = rect.left + 'px'; - // placeholder.style.top = rect.top + 'px'; - // placeholder.style.border = '1px solid red'; return false; } @@ -33,11 +28,14 @@ function timeoutTooltip(){ } function hideTooltip(event){ - // console.log('Hide tooltip for %s: %s', event.target.tagName, event.target.getAttribute('data-target')); - // tooltip.style.display = 'none'; - // placeholder.style.border = '0'; setTooltipTimeout(); return false; } -})(); +global.tooltip = { + show: showTooltip, + hide: hideTooltip +}; + +})(this); +