OLD | NEW |
(Empty) | |
| 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-wor
k/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 <link rel="import" href="../polymer/polymer.html"> |
| 36 |
| 37 <polymer-element name="polymer-anchor-point" attributes="target anchor"> |
| 38 <script> |
| 39 (function() { |
| 40 var DEFAULT_ANCHOR_POINT = 'center center'; |
| 41 |
| 42 function getAnchorPoint(node) { |
| 43 var anchorPt = node.getAttribute('anchor-point'); |
| 44 if (!anchorPt || anchorPt === 'none') { |
| 45 anchorPt = DEFAULT_ANCHOR_POINT; |
| 46 } |
| 47 return anchorPt; |
| 48 }; |
| 49 |
| 50 function lengthIsPx(length) { |
| 51 return length.slice(-2) === 'px'; |
| 52 }; |
| 53 |
| 54 function lengthIsPercent(length) { |
| 55 return length.slice(-1) === '%'; |
| 56 }; |
| 57 |
| 58 function computeLength(length, ref) { |
| 59 var computed = 0; |
| 60 if (lengthIsPx(length)) { |
| 61 computed = length.slice(0, -2); |
| 62 } else if (lengthIsPercent(length)) { |
| 63 computed = ref * length.slice(0, -1) / 100; |
| 64 } |
| 65 return computed; |
| 66 }; |
| 67 |
| 68 function partIsX(part) { |
| 69 return part === 'left' || part === 'right' || part === 'center'; |
| 70 }; |
| 71 |
| 72 function partIsY(part) { |
| 73 return part === 'top' || part === 'bottom' || part === 'center'; |
| 74 }; |
| 75 |
| 76 function partIsKeyword(part) { |
| 77 return part.slice(-1) !== '%' && part.slice(-2) !== 'px'; |
| 78 }; |
| 79 |
| 80 function parsePosition(position) { |
| 81 var parsed = {}; |
| 82 var parts = position.split(' '); |
| 83 var i = 0; |
| 84 var lastEdgeX = true; |
| 85 do { |
| 86 if (partIsX(parts[i])) { |
| 87 parsed.x = parts[i]; |
| 88 lastEdgeX = parts[i] !== 'center'; |
| 89 } else if (partIsY(parts[i])) { |
| 90 parsed.y = parts[i]; |
| 91 lastEdgeX = false; |
| 92 } else if (lastEdgeX) { |
| 93 parsed.xOffset = parts[i]; |
| 94 lastEdgeX = false; |
| 95 } else { |
| 96 parsed.yOffset = parts[i]; |
| 97 } |
| 98 } while (++i < parts.length); |
| 99 return parsed; |
| 100 }; |
| 101 |
| 102 function computeAnchorOffset(rect, anchorPt) { |
| 103 var offset = { |
| 104 left: 0, |
| 105 top: 0 |
| 106 }; |
| 107 var parsed = parsePosition(anchorPt); |
| 108 if (!parsed.x && !parsed.xOffset) { |
| 109 offset.left = rect.width / 2; |
| 110 } else if (parsed.x && !parsed.xOffset) { |
| 111 switch (parsed.x) { |
| 112 case 'left': |
| 113 offset.left = 0; |
| 114 break; |
| 115 case 'right': |
| 116 offset.left = rect.width; |
| 117 break; |
| 118 case 'center': |
| 119 offset.left = rect.width / 2; |
| 120 break; |
| 121 } |
| 122 } else { |
| 123 var computed = computeLength(parsed.xOffset, rect.width); |
| 124 if (parsed.x === 'right') { |
| 125 offset.left = rect.width - computed; |
| 126 } else if (!parsed.x || parsed.x === 'left') { |
| 127 offset.left = computed; |
| 128 } |
| 129 } |
| 130 if (!parsed.y && !parsed.yOffset) { |
| 131 offset.top = rect.height / 2; |
| 132 } else if (parsed.y && !parsed.yOffset) { |
| 133 switch (parsed.y) { |
| 134 case 'top': |
| 135 offset.top = 0; |
| 136 break; |
| 137 case 'bottom': |
| 138 offset.top = rect.height; |
| 139 break; |
| 140 case 'center': |
| 141 offset.top = rect.height / 2; |
| 142 break; |
| 143 } |
| 144 } else { |
| 145 var computed = computeLength(parsed.yOffset, rect.height); |
| 146 if (parsed.y === 'bottom') { |
| 147 offset.top = rect.height - computed; |
| 148 } else if (!parsed.y || parsed.y === 'top') { |
| 149 offset.top = computed; |
| 150 } |
| 151 } |
| 152 return offset; |
| 153 }; |
| 154 |
| 155 Polymer('polymer-anchor-point', { |
| 156 /** |
| 157 * The node to be positioned. |
| 158 * @attribute target |
| 159 * @type Node |
| 160 */ |
| 161 target: null, |
| 162 /** |
| 163 * The node to align the target to. |
| 164 * @attribute anchor |
| 165 * @type node |
| 166 */ |
| 167 anchor: null, |
| 168 canPosition: function() { |
| 169 return this.target && this.anchor; |
| 170 }, |
| 171 apply: function() { |
| 172 if (!this.canPosition()) { |
| 173 return; |
| 174 } |
| 175 var pos = this.computePosition(); |
| 176 this.target.style.position = 'fixed'; |
| 177 this.target.style.top = pos.top + 'px'; |
| 178 this.target.style.left = pos.left + 'px'; |
| 179 }, |
| 180 computePosition: function() { |
| 181 var rect = this.anchor.getBoundingClientRect(); |
| 182 var anchorPt = getAnchorPoint(this.anchor); |
| 183 var anchorOffset = computeAnchorOffset(rect, anchorPt); |
| 184 var targetRect = this.target.getBoundingClientRect(); |
| 185 var targetAnchorPt = getAnchorPoint(this.target); |
| 186 var targetOffset = computeAnchorOffset(targetRect, targetAnchorPt); |
| 187 var pos = { |
| 188 left: rect.left + anchorOffset.left - targetOffset.left, |
| 189 top: rect.top + anchorOffset.top - targetOffset.top |
| 190 }; |
| 191 return pos; |
| 192 } |
| 193 }); |
| 194 })(); |
| 195 </script> |
| 196 </polymer-element> |
OLD | NEW |