|
| 1 | +/** |
| 2 | +* Copyright 2012-2015, 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 | +var createModeBar = require('./'); |
| 15 | +var modeBarButtons = require('./buttons'); |
| 16 | + |
| 17 | +/** |
| 18 | + * ModeBar wrapper around 'create' and 'update', |
| 19 | + * chooses buttons to pass to ModeBar constructor based on |
| 20 | + * plot type and plot config. |
| 21 | + * |
| 22 | + * @param {object} gd main plot object |
| 23 | + * |
| 24 | + */ |
| 25 | +module.exports = function manageModeBar(gd) { |
| 26 | + var fullLayout = gd._fullLayout, |
| 27 | + context = gd._context, |
| 28 | + modeBar = fullLayout._modeBar; |
| 29 | + |
| 30 | + if(!context.displayModeBar && modeBar) { |
| 31 | + modeBar.destroy(); |
| 32 | + delete fullLayout._modeBar; |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + if(!Array.isArray(context.modeBarButtonsToRemove)) { |
| 37 | + throw new Error([ |
| 38 | + '*modeBarButtonsToRemove* configuration options', |
| 39 | + 'must be an array.' |
| 40 | + ].join(' ')); |
| 41 | + } |
| 42 | + |
| 43 | + if(!Array.isArray(context.modeBarButtonsToAdd)) { |
| 44 | + throw new Error([ |
| 45 | + '*modeBarButtonsToAdd* configuration options', |
| 46 | + 'must be an array.' |
| 47 | + ].join(' ')); |
| 48 | + } |
| 49 | + |
| 50 | + var customButtons = context.modeBarButtons; |
| 51 | + var buttonGroups; |
| 52 | + |
| 53 | + if(Array.isArray(customButtons) && customButtons.length) { |
| 54 | + buttonGroups = fillCustomButton(customButtons); |
| 55 | + } |
| 56 | + else { |
| 57 | + buttonGroups = getButtonGroups( |
| 58 | + fullLayout, |
| 59 | + context.modeBarButtonsToRemove, |
| 60 | + context.modeBarButtonsToAdd |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + if(modeBar) modeBar.update(gd, buttonGroups); |
| 65 | + else fullLayout._modeBar = createModeBar(gd, buttonGroups); |
| 66 | +}; |
| 67 | + |
| 68 | +// logic behind which buttons are displayed by default |
| 69 | +function getButtonGroups(fullLayout, buttonsToRemove, buttonsToAdd) { |
| 70 | + var groups = []; |
| 71 | + |
| 72 | + function addGroup(newGroup) { |
| 73 | + var out = []; |
| 74 | + |
| 75 | + for(var i = 0; i < newGroup.length; i++) { |
| 76 | + var button = newGroup[i]; |
| 77 | + if(buttonsToRemove.indexOf(button) !== -1) continue; |
| 78 | + out.push(modeBarButtons[button]); |
| 79 | + } |
| 80 | + |
| 81 | + groups.push(out); |
| 82 | + } |
| 83 | + |
| 84 | + // buttons common to all plot types |
| 85 | + addGroup(['toImage', 'sendDataToCloud']); |
| 86 | + |
| 87 | + if(fullLayout._hasGL3D) { |
| 88 | + addGroup(['zoom3d', 'pan3d', 'orbitRotation', 'tableRotation']); |
| 89 | + addGroup(['resetCameraDefault3d', 'resetCameraLastSave3d']); |
| 90 | + addGroup(['hoverClosest3d']); |
| 91 | + } |
| 92 | + |
| 93 | + if(fullLayout._hasGeo) { |
| 94 | + addGroup(['zoomInGeo', 'zoomOutGeo', 'resetGeo']); |
| 95 | + addGroup(['hoverClosestGeo']); |
| 96 | + } |
| 97 | + |
| 98 | + var hasCartesian = fullLayout._hasCartesian, |
| 99 | + hasGL2D = fullLayout._hasGL2D, |
| 100 | + allAxesFixed = areAllAxesFixed(fullLayout); |
| 101 | + |
| 102 | + if((hasCartesian || hasGL2D) && !allAxesFixed) { |
| 103 | + addGroup(['zoom2d', 'pan2d']); |
| 104 | + addGroup(['zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d']); |
| 105 | + } |
| 106 | + |
| 107 | + if(hasCartesian) { |
| 108 | + addGroup(['hoverClosestCartesian', 'hoverCompareCartesian']); |
| 109 | + } |
| 110 | + if(hasGL2D) { |
| 111 | + addGroup(['hoverClosestGl2d']); |
| 112 | + } |
| 113 | + if(fullLayout._hasPie) { |
| 114 | + addGroup(['hoverClosestPie']); |
| 115 | + } |
| 116 | + |
| 117 | + // append buttonsToAdd to the groups |
| 118 | + if(buttonsToAdd.length) { |
| 119 | + if(Array.isArray(buttonsToAdd[0])) { |
| 120 | + for(var i = 0; i < buttonsToAdd.length; i++) { |
| 121 | + groups.push(buttonsToAdd[i]); |
| 122 | + } |
| 123 | + } |
| 124 | + else groups.push(buttonsToAdd); |
| 125 | + } |
| 126 | + |
| 127 | + return groups; |
| 128 | +} |
| 129 | + |
| 130 | +function areAllAxesFixed(fullLayout) { |
| 131 | + var axList = Plotly.Axes.list({_fullLayout: fullLayout}, null, true); |
| 132 | + var allFixed = true; |
| 133 | + |
| 134 | + for(var i = 0; i < axList.length; i++) { |
| 135 | + if(!axList[i].fixedrange) { |
| 136 | + allFixed = false; |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return allFixed; |
| 142 | +} |
| 143 | + |
| 144 | +// fill in custom buttons referring to default mode bar buttons |
| 145 | +function fillCustomButton(customButtons) { |
| 146 | + for(var i = 0; i < customButtons.length; i++) { |
| 147 | + var buttonGroup = customButtons[i]; |
| 148 | + |
| 149 | + for(var j = 0; j < buttonGroup.length; j++) { |
| 150 | + var button = buttonGroup[j]; |
| 151 | + |
| 152 | + if(typeof button === 'string') |
| 153 | + if(modeBarButtons[button] !== undefined) { |
| 154 | + customButtons[i][j] = modeBarButtons[button]; |
| 155 | + } |
| 156 | + else { |
| 157 | + throw new Error([ |
| 158 | + '*modeBarButtons* configuration options', |
| 159 | + 'invalid button name' |
| 160 | + ].join(' ')); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return customButtons; |
| 166 | +} |
0 commit comments