1const IndexUtils = new function () { 2 const this_ = this; 3 this.eRANGES_TYPE = { 4 DISCRETE: 0, 5 NON_DISCRETE: 1, 6 }; 7 8 this.createRanges = function (nonFixedRanges, usefixRangesByIndexOrRangeType = false, rangeType = undefined, indexType = undefined, addOpacity = false, fixColor = true, colorStyle = undefined) { 9 var unMergedRanges; 10 // remove the range that indicates there is no data about index. 11 nonFixedRanges = jQuery.grep(nonFixedRanges, function (item) { 12 return item.Range != -9999; 13 }); 14 15 var isDescrete = isDescreteRange(nonFixedRanges) 16 if (isDescrete) { 17 unMergedRanges = createDescreteRanges(nonFixedRanges, addOpacity); 18 } 19 else { 20 unMergedRanges = createNonDescreteRanges(nonFixedRanges, addOpacity, fixColor, colorStyle) 21 } 22 let mergedRanges = mergeRangesWithSameColor(unMergedRanges, addOpacity); 23 24 if (usefixRangesByIndexOrRangeType) { 25 mergedRanges = fixRangesByIndexOrRangeType(mergedRanges, rangeType, indexType); 26 } 27 return mergedRanges; 28 29 function isDescreteRange(nonFixedRanges) { 30 31 var isDiscrete = true; //The difference between any two consecutive values is 1 32 for (var i = 0; i < nonFixedRanges.length - 1; i++) { 33 if (nonFixedRanges[i + 1].Range - nonFixedRanges[i].Range != 1) { 34 isDiscrete = false 35 break; 36 } 37 } 38 39 return isDiscrete; 40 } 41 42 function createNonDescreteRanges(ranges, addOpacity = false, fixColor = true, colorStyle = undefined) { 43 44 var ranges = ranges.sort(function (aRangeItem, bRangeItem) { 45 return aRangeItem.Range - bRangeItem.Range; 46 }) 47 48 let rangesExist = [] 49 let distinctRanges = [] 50 ranges.forEach(function (rangeItem) { 51 if ($.inArray(rangeItem.Range, rangesExist) == -1) { 52 rangesExist.push(rangeItem.Range) 53 distinctRanges.push(rangeItem) 54 } 55 }) 56 57 58 var fixedRanges = []; 59 for (var i = 0; i < distinctRanges.length - 1; i++) { 60 let range = { 61 from: distinctRanges[i].Range, 62 to: distinctRanges[i + 1].Range, 63 64 }; 65 if (fixColor) { 66 range.color = getFixedColor(distinctRanges[i].ColorString, colorStyle); 67 } else { 68 range.color = distinctRanges[i].ColorString; 69 } 70 71 if (addOpacity) { 72 range.opacity = 0.5; 73 } 74 75 fixedRanges.push(range); 76 77 } 78 79 return fixedRanges; 80 } 81 82 function createDescreteRanges(nonFixedRanges, addOpacity = false) { 83 var ranges = []; 84 85 //DESCRETE VERSION 86 //In case of 1,2,3,4,5,6,7,8,9,10 87 for (var i = 0; i < nonFixedRanges.length; i++) { 88 let range = { 89 from: nonFixedRanges[i].Range - 1, 90 to: nonFixedRanges[i].Range, 91 color: nonFixedRanges[i].ColorString 92 } 93 94 if (addOpacity) { 95 range.opacity = 0.5; 96 } 97 ranges.push(range); 98 99 } 100 101 return ranges; 102 } 103 104 function fixRangesByIndexOrRangeType(mergedRanges, rangeType, indexType) {
105 mergedRanges.forEach(range => { 106 range.from = fixRangeValue(range.from); 107 range.to = fixRangeValue(range.to); 108 if (indexType == INDEX_TYPES.Israel) {//israel scale is negative and needs to be reversed. 109 let tmp = range.from; 110 range.from = range.to; 111 range.to = tmp; 112 } 113 }); 114 return mergedRanges; 115 116 function fixRangeValue(value) { 117 118 if (rangeType == this_.eRANGES_TYPE.DISCRETE) { 119 if ((value - 0.5) % 1 == 0) { 120 return value + 0.5; 121 } 122 } 123 else { 124 if (indexType == INDEX_TYPES.Israel) { 125 return 100 - value; 126 } 127 else { 128 return value; 129 } 130 } 131 } 132 } 133 } 134 135 this.getRangesType = function(ranges) { 136 var rangesType = this_.eRANGES_TYPE.DISCRETE 137 138 $.each(ranges, function (index, range) { 139 if (range.to - range.from != 1 && range.to - range.from != 0) { 140 rangesType = this_.eRANGES_TYPE.NON_DISCRETE; 141 } 142 }) 143 144 return rangesType; 145 } 146 147 function getFixedColor(color, style) { 148 if (style == "grayscale") { 149 color = tinycolor(color).greyscale().toRgbString() 150 } 151 else if (style == "contrast") { 152 let tinyColor = tinycolor(color); 153 tinyColor._r = 255 - tinyColor._r 154 tinyColor._g = 255 - tinyColor._g 155 tinyColor._b = 255 - tinyColor._b 156 157 color = tinyColor.toRgbString(); 158 } 159 160 return color; 161 } 162 163 /** 164 * mergeRangesWithSameColor(): 165 * @@param {any} ranges - the ranges we get. 166 * @@param {boolean} addOpacityToRanges - add the opacity to the ranges. 167 * takes an array of ranges and merges adjacent ranges with the same color, 168 * returning an array of merged ranges. 169 */ 170 function mergeRangesWithSameColor(ranges, addOpacityToRanges = false) { 171 const mergedRanges = []; 172 173 let currentRange = ranges[0]; 174 175 for (let i = 1; i < ranges.length; i++) { 176 const nextRange = ranges[i]; 177 178 //currentRange.to === nextRange.from: that check means that they are one after one. 179 //currentRange.color === nextRange.color: that check means that this is the same color and we need to merge them. 180 if (currentRange.to === nextRange.from && currentRange.color === nextRange.color) { 181 // Merge the two ranges 182 currentRange.to = nextRange.to; 183 } else { 184 // Add the current range to the mergedRanges array 185 let range = { from: currentRange.from, to: currentRange.to, color: currentRange.color }; 186 if (addOpacityToRanges) { 187 range.opacity = currentRange.opacity; 188 } 189 mergedRanges.push(range); 190 // Update currentRange to the nextRange 191 currentRange = nextRange; 192 } 193 } 194 195 // Add the last range to the mergedRanges array 196 let lastRange = { from: currentRange.from, to: currentRange.to, color: currentRange.color }; 197 if (addOpacityToRanges) { 198 lastRange.opacity = currentRange.opacity; 199 } 200 mergedRanges.push(lastRange); 201 202 return mergedRanges; 203 } 204 // make helper class immutable 205 JsUtils.deepFreeze(this); 206 207 //define helper as a global variable 208 JsUtils.defineGlobalConstant(this, "IndexUtils"); 209}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.