|
| 1 | +/** |
| 2 | +* Copyright 2012-2016, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +var Plotly = require('../../plotly'); |
| 13 | + |
| 14 | + |
| 15 | +module.exports = { |
| 16 | + hasClickToShow: hasClickToShow, |
| 17 | + onClick: onClick |
| 18 | +}; |
| 19 | + |
| 20 | +/* |
| 21 | + * hasClickToShow: does the given hoverData have ANY annotations which will |
| 22 | + * turn ON if we click here? (used by hover events to set cursor) |
| 23 | + * |
| 24 | + * gd: graphDiv |
| 25 | + * hoverData: a hoverData array, as included with the *plotly_hover* or |
| 26 | + * *plotly_click* events in the `points` attribute |
| 27 | + * |
| 28 | + * returns: boolean |
| 29 | + */ |
| 30 | +function hasClickToShow(gd, hoverData) { |
| 31 | + var sets = getToggleSets(gd, hoverData); |
| 32 | + return sets.on.length > 0 || sets.explicitOff.length > 0; |
| 33 | +} |
| 34 | + |
| 35 | +/* |
| 36 | + * onClick: perform the toggling (via Plotly.update) implied by clicking |
| 37 | + * at this hoverData |
| 38 | + * |
| 39 | + * gd: graphDiv |
| 40 | + * hoverData: a hoverData array, as included with the *plotly_hover* or |
| 41 | + * *plotly_click* events in the `points` attribute |
| 42 | + * |
| 43 | + * returns: Promise that the update is complete |
| 44 | + */ |
| 45 | +function onClick(gd, hoverData) { |
| 46 | + var toggleSets = getToggleSets(gd, hoverData), |
| 47 | + onSet = toggleSets.on, |
| 48 | + offSet = toggleSets.off.concat(toggleSets.explicitOff), |
| 49 | + update = {}, |
| 50 | + i; |
| 51 | + |
| 52 | + if(!(onSet.length || offSet.length)) return; |
| 53 | + |
| 54 | + for(i = 0; i < onSet.length; i++) { |
| 55 | + update['annotations[' + onSet[i] + '].visible'] = true; |
| 56 | + } |
| 57 | + |
| 58 | + for(i = 0; i < offSet.length; i++) { |
| 59 | + update['annotations[' + offSet[i] + '].visible'] = false; |
| 60 | + } |
| 61 | + |
| 62 | + return Plotly.update(gd, {}, update); |
| 63 | +} |
| 64 | + |
| 65 | +/* |
| 66 | + * getToggleSets: find the annotations which will turn on or off at this |
| 67 | + * hoverData |
| 68 | + * |
| 69 | + * gd: graphDiv |
| 70 | + * hoverData: a hoverData array, as included with the *plotly_hover* or |
| 71 | + * *plotly_click* events in the `points` attribute |
| 72 | + * |
| 73 | + * returns: { |
| 74 | + * on: Array (indices of annotations to turn on), |
| 75 | + * off: Array (indices to turn off because you're not hovering on them), |
| 76 | + * explicitOff: Array (indices to turn off because you *are* hovering on them) |
| 77 | + * } |
| 78 | + */ |
| 79 | +function getToggleSets(gd, hoverData) { |
| 80 | + var annotations = gd._fullLayout.annotations, |
| 81 | + onSet = [], |
| 82 | + offSet = [], |
| 83 | + explicitOffSet = [], |
| 84 | + hoverLen = (hoverData || []).length; |
| 85 | + |
| 86 | + var i, j, anni, showMode, pointj, toggleType; |
| 87 | + |
| 88 | + for(i = 0; i < annotations.length; i++) { |
| 89 | + anni = annotations[i]; |
| 90 | + showMode = anni.clicktoshow; |
| 91 | + if(showMode) { |
| 92 | + for(j = 0; j < hoverLen; j++) { |
| 93 | + pointj = hoverData[j]; |
| 94 | + if(pointj.x === anni._xclick && pointj.y === anni._yclick && |
| 95 | + pointj.xaxis._id === anni.xref && |
| 96 | + pointj.yaxis._id === anni.yref) { |
| 97 | + // match! toggle this annotation |
| 98 | + // regardless of its clicktoshow mode |
| 99 | + // but if it's onout mode, off is implicit |
| 100 | + if(anni.visible) { |
| 101 | + if(showMode === 'onout') toggleType = offSet; |
| 102 | + else toggleType = explicitOffSet; |
| 103 | + } |
| 104 | + else { |
| 105 | + toggleType = onSet; |
| 106 | + } |
| 107 | + toggleType.push(i); |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if(j === hoverLen) { |
| 113 | + // no match - only turn this annotation OFF, and only if |
| 114 | + // showmode is 'onout' |
| 115 | + if(anni.visible && showMode === 'onout') offSet.push(i); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return {on: onSet, off: offSet, explicitOff: explicitOffSet}; |
| 121 | +} |
0 commit comments