|
| 1 | +<!-- |
| 2 | +/** |
| 3 | + * @module Polymer Elements |
| 4 | + */ |
| 5 | +/** |
| 6 | + * polymer-anchor-point can be used to align two nodes. The node to |
| 7 | + * use as the reference position is the anchor node, and the node to |
| 8 | + * be positioned is the target node. |
| 9 | + * |
| 10 | + * Both the anchor and target nodes should have an anchor-point |
| 11 | + * attribute. The target node is positioned such that its anchor-point |
| 12 | + * aligns with the anchor node's anchor-point. |
| 13 | + * |
| 14 | + * Note: The target node is positioned with position: fixed, and will not |
| 15 | + * scroll with the page. |
| 16 | + * |
| 17 | + * Note: This is meant to polyfill the <dialog> positioning behavior when |
| 18 | + * an anchor is provided. Spec: http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#the-dialog-element |
| 19 | + * |
| 20 | + * Example: |
| 21 | + * |
| 22 | + * <div id="anchor" anchor-point="bottom left"></div> |
| 23 | + * <div id="target" anchor-point="top left"></div> |
| 24 | + * <polymer-anchor-point id="anchor-helper"></polymer-anchor-point> |
| 25 | + * <script> |
| 26 | + * var helper = document.querySelector('#anchor-helper'); |
| 27 | + * helper.anchor = document.querySelector('#anchor'); |
| 28 | + * helper.target = document.querySelector('#target'); |
| 29 | + * helper.apply(); |
| 30 | + * </script> |
| 31 | + * |
| 32 | + * @class polymer-anchor-point |
| 33 | + */ |
| 34 | + --> |
| 35 | +<polymer-element name="polymer-anchor-point" attributes="target anchor"> |
| 36 | + <script> |
| 37 | + (function() { |
| 38 | + var DEFAULT_ANCHOR_POINT = 'center center'; |
| 39 | + |
| 40 | + function getAnchorPoint(node) { |
| 41 | + var anchorPt = node.getAttribute('anchor-point'); |
| 42 | + if (!anchorPt || anchorPt === 'none') { |
| 43 | + anchorPt = DEFAULT_ANCHOR_POINT; |
| 44 | + } |
| 45 | + return anchorPt; |
| 46 | + }; |
| 47 | + |
| 48 | + function lengthIsPx(length) { |
| 49 | + return length.slice(-2) === 'px'; |
| 50 | + }; |
| 51 | + |
| 52 | + function lengthIsPercent(length) { |
| 53 | + return length.slice(-1) === '%'; |
| 54 | + }; |
| 55 | + |
| 56 | + function computeLength(length, ref) { |
| 57 | + var computed = 0; |
| 58 | + if (lengthIsPx(length)) { |
| 59 | + computed = length.slice(0, -2); |
| 60 | + } else if (lengthIsPercent(length)) { |
| 61 | + computed = ref * length.slice(0, -1) / 100; |
| 62 | + } |
| 63 | + return computed; |
| 64 | + }; |
| 65 | + |
| 66 | + function partIsX(part) { |
| 67 | + return part === 'left' || part === 'right' || part === 'center'; |
| 68 | + }; |
| 69 | + |
| 70 | + function partIsY(part) { |
| 71 | + return part === 'top' || part === 'bottom' || part === 'center'; |
| 72 | + }; |
| 73 | + |
| 74 | + function partIsKeyword(part) { |
| 75 | + return part.slice(-1) !== '%' && part.slice(-2) !== 'px'; |
| 76 | + }; |
| 77 | + |
| 78 | + function parsePosition(position) { |
| 79 | + var parsed = {}; |
| 80 | + var parts = position.split(' '); |
| 81 | + var i = 0; |
| 82 | + var lastEdgeX = true; |
| 83 | + do { |
| 84 | + if (partIsX(parts[i])) { |
| 85 | + parsed.x = parts[i]; |
| 86 | + lastEdgeX = parts[i] !== 'center'; |
| 87 | + } else if (partIsY(parts[i])) { |
| 88 | + parsed.y = parts[i]; |
| 89 | + lastEdgeX = false; |
| 90 | + } else if (lastEdgeX) { |
| 91 | + parsed.xOffset = parts[i]; |
| 92 | + lastEdgeX = false; |
| 93 | + } else { |
| 94 | + parsed.yOffset = parts[i]; |
| 95 | + } |
| 96 | + } while (++i < parts.length); |
| 97 | + return parsed; |
| 98 | + }; |
| 99 | + |
| 100 | + function computeAnchorOffset(rect, anchorPt) { |
| 101 | + var offset = { |
| 102 | + left: 0, |
| 103 | + top: 0 |
| 104 | + }; |
| 105 | + var parsed = parsePosition(anchorPt); |
| 106 | + if (!parsed.x && !parsed.xOffset) { |
| 107 | + offset.left = rect.width / 2; |
| 108 | + } else if (parsed.x && !parsed.xOffset) { |
| 109 | + switch (parsed.x) { |
| 110 | + case 'left': |
| 111 | + offset.left = 0; |
| 112 | + break; |
| 113 | + case 'right': |
| 114 | + offset.left = rect.width; |
| 115 | + break; |
| 116 | + case 'center': |
| 117 | + offset.left = rect.width / 2; |
| 118 | + break; |
| 119 | + } |
| 120 | + } else { |
| 121 | + var computed = computeLength(parsed.xOffset, rect.width); |
| 122 | + if (parsed.x === 'right') { |
| 123 | + offset.left = rect.width - computed; |
| 124 | + } else if (!parsed.x || parsed.x === 'left') { |
| 125 | + offset.left = computed; |
| 126 | + } |
| 127 | + } |
| 128 | + if (!parsed.y && !parsed.yOffset) { |
| 129 | + offset.top = rect.height / 2; |
| 130 | + } else if (parsed.y && !parsed.yOffset) { |
| 131 | + switch (parsed.y) { |
| 132 | + case 'top': |
| 133 | + offset.top = 0; |
| 134 | + break; |
| 135 | + case 'bottom': |
| 136 | + offset.top = rect.height; |
| 137 | + break; |
| 138 | + case 'center': |
| 139 | + offset.top = rect.height / 2; |
| 140 | + break; |
| 141 | + } |
| 142 | + } else { |
| 143 | + var computed = computeLength(parsed.yOffset, rect.height); |
| 144 | + if (parsed.y === 'bottom') { |
| 145 | + offset.top = rect.height - computed; |
| 146 | + } else if (!parsed.y || parsed.y === 'top') { |
| 147 | + offset.top = computed; |
| 148 | + } |
| 149 | + } |
| 150 | + return offset; |
| 151 | + }; |
| 152 | + |
| 153 | + Polymer('polymer-anchor-point', { |
| 154 | + /** |
| 155 | + * The node to be positioned. |
| 156 | + * @attribute target |
| 157 | + * @type Node |
| 158 | + */ |
| 159 | + target: null, |
| 160 | + /** |
| 161 | + * The node to align the target to. |
| 162 | + * @attribute anchor |
| 163 | + * @type node |
| 164 | + */ |
| 165 | + anchor: null, |
| 166 | + canPosition: function() { |
| 167 | + return this.target && this.anchor; |
| 168 | + }, |
| 169 | + apply: function() { |
| 170 | + if (!this.canPosition()) { |
| 171 | + return; |
| 172 | + } |
| 173 | + var pos = this.computePosition(); |
| 174 | + this.target.style.position = 'fixed'; |
| 175 | + this.target.style.top = pos.top + 'px'; |
| 176 | + this.target.style.left = pos.left + 'px'; |
| 177 | + }, |
| 178 | + computePosition: function() { |
| 179 | + var rect = this.anchor.getBoundingClientRect(); |
| 180 | + var anchorPt = getAnchorPoint(this.anchor); |
| 181 | + var anchorOffset = computeAnchorOffset(rect, anchorPt); |
| 182 | + var targetRect = this.target.getBoundingClientRect(); |
| 183 | + var targetAnchorPt = getAnchorPoint(this.target); |
| 184 | + var targetOffset = computeAnchorOffset(targetRect, targetAnchorPt); |
| 185 | + var pos = { |
| 186 | + left: rect.left + anchorOffset.left - targetOffset.left, |
| 187 | + top: rect.top + anchorOffset.top - targetOffset.top |
| 188 | + }; |
| 189 | + return pos; |
| 190 | + } |
| 191 | + }); |
| 192 | + })(); |
| 193 | + </script> |
| 194 | +</polymer-element> |
0 commit comments