UNPKG

doevisualizations

Version:

Data Visualization Library based on RequireJS and D3.js (v4+)

77 lines (67 loc) 2.46 kB
steal('jquery', function($) { /** * @function jQuery.fn.compare * @parent jQuery.compare * @hide * * Compare two elements and return a bitmask as a number representing the following conditions: * * - `000000` -> __0__: Elements are identical * - `000001` -> __1__: The nodes are in different documents (or one is outside of a document) * - `000010` -> __2__: #bar precedes #foo * - `000100` -> __4__: #foo precedes #bar * - `001000` -> __8__: #bar contains #foo * - `010000` -> __16__: #foo contains #bar * * You can check for any of these conditions using a bitwise AND: * * if( $('#foo').compare($('#bar')) & 2 ) { * console.log("#bar precedes #foo") * } * * @param {HTMLElement|jQuery} element an element or jQuery collection to compare against. * @return {Number} A number representing a bitmask deatiling how the elements are positioned from each other. */ // See http://ejohn.org/blog/comparing-document-position/ $.fn.compare = function(element){ //usually try{ // Firefox 3 throws an error with XUL - we can't use compare then element = element.jquery ? element[0] : element; }catch(e){ return null; } // make sure we aren't coming from XUL element if (window.HTMLElement) { var s = HTMLElement.prototype.toString.call(element) if (s == '[xpconnect wrapped native prototype]' || s == '[object XULElement]' || s === '[object Window]') { return null; } } if(this[0].compareDocumentPosition){ // For browsers that support it, use compareDocumentPosition // https://developer.mozilla.org/en/DOM/Node.compareDocumentPosition return this[0].compareDocumentPosition(element); } // this[0] contains element if(this[0] == document && element != document) return 8; var number = // this[0] contains element (this[0] !== element && this[0].contains(element) && 16) + // element contains this[0] (this[0] != element && element.contains(this[0]) && 8), docEl = document.documentElement; // Use the sourceIndex if(this[0].sourceIndex){ // this[0] precedes element number += (this[0].sourceIndex < element.sourceIndex && 4) // element precedes foo[0] number += (this[0].sourceIndex > element.sourceIndex && 2) // The nodes are in different documents number += (this[0].ownerDocument !== element.ownerDocument || (this[0] != docEl && this[0].sourceIndex <= 0 ) || (element != docEl && element.sourceIndex <= 0 )) && 1 } return number; } return $; });