|
9 | 9 |
|
10 | 10 | 'use strict';
|
11 | 11 |
|
| 12 | +var Lib = require('../../lib'); |
| 13 | +var Axes = require('../../plots/cartesian/axes'); |
12 | 14 | var handleArrayContainerDefaults = require('../../plots/array_container_defaults');
|
13 |
| -var handleShapeDefaults = require('./shape_defaults'); |
| 15 | + |
| 16 | +var attributes = require('./attributes'); |
| 17 | +var helpers = require('./helpers'); |
14 | 18 |
|
15 | 19 |
|
16 | 20 | module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
|
17 |
| - var opts = { |
| 21 | + handleArrayContainerDefaults(layoutIn, layoutOut, { |
18 | 22 | name: 'shapes',
|
19 | 23 | handleItemDefaults: handleShapeDefaults
|
20 |
| - }; |
21 |
| - |
22 |
| - handleArrayContainerDefaults(layoutIn, layoutOut, opts); |
| 24 | + }); |
23 | 25 | };
|
| 26 | + |
| 27 | +function handleShapeDefaults(shapeIn, shapeOut, fullLayout, opts, itemOpts) { |
| 28 | + function coerce(attr, dflt) { |
| 29 | + return Lib.coerce(shapeIn, shapeOut, attributes, attr, dflt); |
| 30 | + } |
| 31 | + |
| 32 | + var visible = coerce('visible', !itemOpts.itemIsNotPlainObject); |
| 33 | + |
| 34 | + if(!visible) return shapeOut; |
| 35 | + |
| 36 | + coerce('layer'); |
| 37 | + coerce('opacity'); |
| 38 | + coerce('fillcolor'); |
| 39 | + coerce('line.color'); |
| 40 | + coerce('line.width'); |
| 41 | + coerce('line.dash'); |
| 42 | + |
| 43 | + var dfltType = shapeIn.path ? 'path' : 'rect', |
| 44 | + shapeType = coerce('type', dfltType), |
| 45 | + xSizeMode = coerce('xsizemode'), |
| 46 | + ySizeMode = coerce('ysizemode'); |
| 47 | + |
| 48 | + // positioning |
| 49 | + var axLetters = ['x', 'y']; |
| 50 | + for(var i = 0; i < 2; i++) { |
| 51 | + var axLetter = axLetters[i], |
| 52 | + attrAnchor = axLetter + 'anchor', |
| 53 | + sizeMode = axLetter === 'x' ? xSizeMode : ySizeMode, |
| 54 | + gdMock = {_fullLayout: fullLayout}, |
| 55 | + ax, |
| 56 | + pos2r, |
| 57 | + r2pos; |
| 58 | + |
| 59 | + // xref, yref |
| 60 | + var axRef = Axes.coerceRef(shapeIn, shapeOut, gdMock, axLetter, '', 'paper'); |
| 61 | + |
| 62 | + if(axRef !== 'paper') { |
| 63 | + ax = Axes.getFromId(gdMock, axRef); |
| 64 | + r2pos = helpers.rangeToShapePosition(ax); |
| 65 | + pos2r = helpers.shapePositionToRange(ax); |
| 66 | + } |
| 67 | + else { |
| 68 | + pos2r = r2pos = Lib.identity; |
| 69 | + } |
| 70 | + |
| 71 | + // Coerce x0, x1, y0, y1 |
| 72 | + if(shapeType !== 'path') { |
| 73 | + var dflt0 = 0.25, |
| 74 | + dflt1 = 0.75; |
| 75 | + |
| 76 | + // hack until V2.0 when log has regular range behavior - make it look like other |
| 77 | + // ranges to send to coerce, then put it back after |
| 78 | + // this is all to give reasonable default position behavior on log axes, which is |
| 79 | + // a pretty unimportant edge case so we could just ignore this. |
| 80 | + var attr0 = axLetter + '0', |
| 81 | + attr1 = axLetter + '1', |
| 82 | + in0 = shapeIn[attr0], |
| 83 | + in1 = shapeIn[attr1]; |
| 84 | + shapeIn[attr0] = pos2r(shapeIn[attr0], true); |
| 85 | + shapeIn[attr1] = pos2r(shapeIn[attr1], true); |
| 86 | + |
| 87 | + if(sizeMode === 'pixel') { |
| 88 | + coerce(attr0, 0); |
| 89 | + coerce(attr1, 10); |
| 90 | + } else { |
| 91 | + Axes.coercePosition(shapeOut, gdMock, coerce, axRef, attr0, dflt0); |
| 92 | + Axes.coercePosition(shapeOut, gdMock, coerce, axRef, attr1, dflt1); |
| 93 | + } |
| 94 | + |
| 95 | + // hack part 2 |
| 96 | + shapeOut[attr0] = r2pos(shapeOut[attr0]); |
| 97 | + shapeOut[attr1] = r2pos(shapeOut[attr1]); |
| 98 | + shapeIn[attr0] = in0; |
| 99 | + shapeIn[attr1] = in1; |
| 100 | + } |
| 101 | + |
| 102 | + // Coerce xanchor and yanchor |
| 103 | + if(sizeMode === 'pixel') { |
| 104 | + // Hack for log axis described above |
| 105 | + var inAnchor = shapeIn[attrAnchor]; |
| 106 | + shapeIn[attrAnchor] = pos2r(shapeIn[attrAnchor], true); |
| 107 | + |
| 108 | + Axes.coercePosition(shapeOut, gdMock, coerce, axRef, attrAnchor, 0.25); |
| 109 | + |
| 110 | + // Hack part 2 |
| 111 | + shapeOut[attrAnchor] = r2pos(shapeOut[attrAnchor]); |
| 112 | + shapeIn[attrAnchor] = inAnchor; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if(shapeType === 'path') { |
| 117 | + coerce('path'); |
| 118 | + } |
| 119 | + else { |
| 120 | + Lib.noneOrAll(shapeIn, shapeOut, ['x0', 'x1', 'y0', 'y1']); |
| 121 | + } |
| 122 | + |
| 123 | + return shapeOut; |
| 124 | +} |
0 commit comments