|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @ngdoc object |
| 5 | + * @module ui.drop |
| 6 | + * @name ui.drop.$dndDOM |
| 7 | + * @requires $document |
| 8 | + * @requires $window |
| 9 | + * |
| 10 | + * @description |
| 11 | + * |
| 12 | + * A set of utility methods that can be use to retrieve and manipulate DOM properties. |
| 13 | + * |
| 14 | + * (based on https://github.com/angular-ui/bootstrap/blob/master/src/position/position.js) |
| 15 | + * |
| 16 | + */ |
| 17 | +var $dndDOMFactory = ['$document', '$window', function ($document, $window) { |
| 18 | + |
| 19 | + function getStyle(el, cssprop) { |
| 20 | + if (el.currentStyle) { //IE |
| 21 | + return el.currentStyle[cssprop]; |
| 22 | + } else if ($window.getComputedStyle) { |
| 23 | + return $window.getComputedStyle(el)[cssprop]; |
| 24 | + } |
| 25 | + // finally try and get inline style |
| 26 | + return el.style[cssprop]; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Checks if a given element is statically positioned |
| 31 | + * @param element - raw DOM element |
| 32 | + */ |
| 33 | + function isStaticPositioned(element) { |
| 34 | + return (getStyle(element, 'position') || 'static' ) === 'static'; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * returns the closest, non-statically positioned parentOffset of a given element |
| 39 | + * @param element |
| 40 | + */ |
| 41 | + var parentOffsetEl = function (element) { |
| 42 | + var docDomEl = $document[0]; |
| 43 | + var offsetParent = element.offsetParent || docDomEl; |
| 44 | + while (offsetParent && offsetParent !== docDomEl && isStaticPositioned(offsetParent) ) { |
| 45 | + offsetParent = offsetParent.offsetParent; |
| 46 | + } |
| 47 | + return offsetParent || docDomEl; |
| 48 | + }; |
| 49 | + |
| 50 | + function contains(a, b) { |
| 51 | + var adown = a.nodeType === 9 ? a.documentElement : a, |
| 52 | + bup = b && b.parentNode; |
| 53 | + return a === bup || !!( bup && bup.nodeType === 1 && contains(adown, bup) ); |
| 54 | + }; |
| 55 | + |
| 56 | + function swapCss(element, css, callback, args) { |
| 57 | + var ret, prop, old = {}; |
| 58 | + for (prop in css) { |
| 59 | + old[prop] = element.style[prop]; |
| 60 | + element.style[prop] = css[prop]; |
| 61 | + } |
| 62 | + |
| 63 | + ret = callback.apply(element, args || []); |
| 64 | + |
| 65 | + for (prop in css) { |
| 66 | + element.style[prop] = old[prop]; |
| 67 | + } |
| 68 | + |
| 69 | + return ret; |
| 70 | + }; |
| 71 | + |
| 72 | + var swapDisplay = /^(none|table(?!-c[ea]).+)/; |
| 73 | + |
| 74 | + var cssShow = { |
| 75 | + position: 'absolute', |
| 76 | + visibility: 'hidden', |
| 77 | + display: 'block' |
| 78 | + }; |
| 79 | + |
| 80 | + return { |
| 81 | + /** |
| 82 | + * Provides read-only equivalent of jQuery's position function: |
| 83 | + * http://api.jquery.com/position/ |
| 84 | + */ |
| 85 | + position: function (element) { |
| 86 | + var elBCR = this.offset(element); |
| 87 | + var offsetParentBCR = { top: 0, left: 0 }; |
| 88 | + var offsetParentEl = parentOffsetEl(element[0]); |
| 89 | + if (offsetParentEl != $document[0]) { |
| 90 | + offsetParentBCR = this.offset(angular.element(offsetParentEl)); |
| 91 | + offsetParentBCR.top += offsetParentEl.clientTop - offsetParentEl.scrollTop; |
| 92 | + offsetParentBCR.left += offsetParentEl.clientLeft - offsetParentEl.scrollLeft; |
| 93 | + } |
| 94 | + |
| 95 | + var boundingClientRect = element[0].getBoundingClientRect(); |
| 96 | + return { |
| 97 | + width: boundingClientRect.width || element.prop('offsetWidth'), |
| 98 | + height: boundingClientRect.height || element.prop('offsetHeight'), |
| 99 | + top: elBCR.top - offsetParentBCR.top, |
| 100 | + left: elBCR.left - offsetParentBCR.left |
| 101 | + }; |
| 102 | + }, |
| 103 | + |
| 104 | + /** |
| 105 | + * Provides read-only equivalent of jQuery's offset function: |
| 106 | + * http://api.jquery.com/offset/ |
| 107 | + */ |
| 108 | + offset: function (element) { |
| 109 | + |
| 110 | + var doc = element[0] && element[0].ownerDocument; |
| 111 | + |
| 112 | + if (!doc) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + doc = doc.documentElement; |
| 117 | + |
| 118 | + if (!contains(doc, element[0])) { |
| 119 | + return { top: 0, left: 0 }; |
| 120 | + } |
| 121 | + |
| 122 | + var boundingClientRect = element[0].getBoundingClientRect(); |
| 123 | + return { |
| 124 | + width: boundingClientRect.width || element.prop('offsetWidth'), |
| 125 | + height: boundingClientRect.height || element.prop('offsetHeight'), |
| 126 | + top: boundingClientRect.top + ($window.pageYOffset || $document[0].documentElement.scrollTop), |
| 127 | + left: boundingClientRect.left + ($window.pageXOffset || $document[0].documentElement.scrollLeft) |
| 128 | + }; |
| 129 | + }, |
| 130 | + |
| 131 | + /** |
| 132 | + * Partial implementation of https://api.jquery.com/closest/ |
| 133 | + * @param element |
| 134 | + * @param value |
| 135 | + * @returns {angular.element} |
| 136 | + */ |
| 137 | + closest: function(element, value) { |
| 138 | + element = angular.element(element); |
| 139 | + if ($.fn && angular.isFunction($.fn.closest)) { |
| 140 | + return element.closest(value); |
| 141 | + } |
| 142 | + // Otherwise, assume it's a tag name... |
| 143 | + element = element[0]; |
| 144 | + value = value.toLowerCase(); |
| 145 | + do { |
| 146 | + if (element.nodeName.toLowerCase() === value) { |
| 147 | + return angular.element(element); |
| 148 | + } |
| 149 | + } while (element = element.parentNode); |
| 150 | + }, |
| 151 | + |
| 152 | + size: function(element) { |
| 153 | + var jq = angular.element(element); |
| 154 | + element = element.nodeName ? element : element[0]; |
| 155 | + if (element.offsetWidth === 0 && swapDisplay.test(jq.css('display'))) { |
| 156 | + return swapCss(element, cssShow, getHeightAndWidth, [element]); |
| 157 | + } |
| 158 | + return getHeightAndWidth(element); |
| 159 | + |
| 160 | + function getHeightAndWidth(element) { |
| 161 | + return { |
| 162 | + width: element.offsetWidth, |
| 163 | + height: element.offsetHeight |
| 164 | + }; |
| 165 | + } |
| 166 | + }, |
| 167 | + |
| 168 | + keepSize: function(element) { |
| 169 | + var css = this.size(element); |
| 170 | + css.width = css.width + 'px'; |
| 171 | + css.height = css.height + 'px'; |
| 172 | + return css; |
| 173 | + } |
| 174 | + }; |
| 175 | +}]; |
0 commit comments