RangeArrayUtils.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. window.RangeArrayUtils = o2.RangeArrayUtils = {
  2. //补集 range [ start, end ] rangeList [ [start1, end1], [ start2, end2 ] ... ]
  3. complementary : function( range, rangeList, type ){
  4. if( !range )return range;
  5. var r = this.getRangeObject( range );
  6. if( !rangeList || rangeList.length == 0 )return this.parse( [r] , type);
  7. var unitedList = this.union( rangeList );
  8. var newRange = {};
  9. if( unitedList[0][0] > r.start ){
  10. newRange.start = r.start;
  11. }else if( r.end > unitedList[0][1] ){
  12. newRange.start = unitedList[0][1];
  13. unitedList.shift();
  14. }else{
  15. return [];
  16. }
  17. var newList = [];
  18. while( unitedList.length > 0 ){
  19. if( unitedList[0][0] >= r.end ){
  20. newRange.end = r.end;
  21. newList.push( Object.clone(newRange) );
  22. return this.parse( newList , type);
  23. }else if( r.end <= unitedList[0][1] ){
  24. newRange.end = unitedList[0][0];
  25. newList.push( Object.clone(newRange) );
  26. return this.parse( newList, type );
  27. }else{
  28. newRange.end = unitedList[0][0];
  29. newList.push( Object.clone(newRange) );
  30. newRange.start = unitedList[0][1];
  31. unitedList.shift();
  32. }
  33. }
  34. newRange.end = r.end;
  35. newList.push( Object.clone(newRange ));
  36. return this.parse( newList, type );
  37. },
  38. //取区域并集rangeList [ [start1, end1], [ start2, end2 ] ... ]
  39. union : function( ranges, type ){
  40. if( !ranges || ranges.length == 0)return ranges; //this.parse(this.getRangeObject( ranges ) ) ;
  41. var rangeList = Array.clone( ranges );
  42. for( var i=0; i<rangeList.length; i++ ){
  43. rangeList[i] = this.getRangeObject( rangeList[i] );
  44. }
  45. rangeList.sort( function( a, b ){
  46. return a.start - b.start;
  47. });
  48. var newRangeList = [];
  49. var newRange = rangeList.shift();
  50. while( rangeList.length > 0 ){
  51. var nextRange = rangeList.shift();
  52. if( this.isIntersection( newRange, nextRange ) ){
  53. newRange.end = Math.max( newRange.end, nextRange.end );
  54. }else{
  55. newRangeList.push( Object.clone( newRange ) );
  56. newRange = nextRange;
  57. }
  58. }
  59. if( !nextRange ){
  60. newRangeList.push( Object.clone( newRange ) );
  61. }else if( this.isIntersection( newRange, nextRange ) ){
  62. newRange.end = Math.max( newRange.end, nextRange.end );
  63. newRangeList.push( Object.clone( newRange ) );
  64. }else{
  65. newRangeList.push( Object.clone( nextRange ) );
  66. }
  67. return this.parse( newRangeList, type );
  68. },
  69. //区域是否相交
  70. isIntersection : function( range1, range2 ){
  71. var r1 = typeOf( range1 ) === "object" ? range1 : this.getRangeObject( range1 );
  72. var r2 = typeOf( range2 ) === "object" ? range2 : this.getRangeObject( range2 );
  73. if( r1.start > r2.end )return false;
  74. if( r2.start > r1.end )return false;
  75. return true;
  76. },
  77. parse: function( objectList, type ){
  78. var list = [];
  79. for( var i=0; i<objectList.length; i++ ){
  80. var range = objectList[i];
  81. if( type && type == "date" ){
  82. list.push( [ Date.parse(range.start), Date.parse(range.end) ] );
  83. }else{
  84. list.push( [range.start, range.end] );
  85. }
  86. }
  87. return list;
  88. },
  89. getRangeObject: function( range ){
  90. return {
  91. start : Math.min( range[0], range[1] ),
  92. end : Math.max( range[0], range[1] )
  93. }
  94. }
  95. };