1var ItemComparator = (function() { 2 // Global Constants 3 var MIN_ITEMS = 2; 4 var MAX_ITEMS = 4; 5 6 // Private Variables 7 var keys = []; 8 var idx = 0; // loop variable 9 10 // Private methods 11 function addToCompare(item_key) { 12 if (! item_key) return; 13 item_key = item_key.toUpperCase(); 14 for (idx = 0; idx < keys.length; idx++) { 15 // Already here? 16 if (keys[idx] == item_key) return; 17 } 18 // New, so add 19 keys.push(item_key); 20 setCookie(); 21 } 22 23 function removeFromCompare(item_key) { 24 if (! item_key) return; 25 item_key = item_key.toUpperCase(); 26 for (idx = 0; idx < keys.length; idx++) { 27 if (keys[idx] == item_key) { 28 // Create new copy of keys without specified item 29 keys = keys.slice(0, idx).concat(keys.slice(idx + 1)); 30 setCookie(); 31 return; 32 }; 33 } 34 } 35 36 function setCookie() { 37 var now = new Date(); 38 var time = now.getTime(); 39 var expireTime = time + (5 * 60 * 1000); 40 now.setTime(expireTime); 41 document.cookie = 'item_keys='+JSON.stringify(keys)+';cookie=ok;expires='+now.toGMTString()+';path=/'; 42 } 43 44 function toggleVisibility(element) { 45 if (! element) return; 46 if (-1 != element.className.indexOf("hidden")) { 47 element.className = element.className.replace("hidden", ""); 48 } else { 49 element.className += " hidden"; 50 } 51 } 52 53 function toggleSpanTags(checkbox) { 54 if (! checkbox) return; 55 // 1) Find sibling <label>s 56 // 2) Toggle each <span> inside <label> 57 var siblings = checkbox.parentNode.childNodes; 58 for (var idx = 0; idx < siblings.length; idx++) { 59 if ("LABEL" != siblings[idx].tagName) continue; 60 var label = siblings[idx]; 61 62 for (var sub_idx = 0; sub_idx < label.childNodes.length; sub_idx++) { 63 var el = label.childNodes[sub_idx]; 64 if ("SPAN" != el.tagName) continue; 65 toggleVisibility(el); 66 } 67 } 68 } 69 70 // return object 71 var comparator = {}; 72 73 // public 74 comparator.compare = function() { 75 if (keys.length < MIN_ITEMS) { 76 alert("You must select at least " + MIN_ITEMS + " items to compare"); 77 return; 78 } 79 if (keys.length > MAX_ITEMS) { 80 alert("You cannot select more than " + MAX_ITEMS + " items to compare"); 81 return; 82 } 83 84 var old_keys = keys; 85 keys = []; 86 setCookie(); 87 document.location = '/_CGI/COMPARE.HTML?PN=' + old_keys.join('|'); 88 } 89 90 comparator.toggleCompare = function(checkbox, item_key) { 91 if (! checkbox || ! item_key) return; 92 if (checkbox.checked) { 93 addToCompare(item_key); 94 } else { 95 removeFromCompare(item_key); 96 } 97 toggleSpanTags(checkbox); 98 } 99 100 function getCompareCookie() { 101 return (document.cookie.match(/^(?:.*;
101)?\s*item_keys\s*=\s*([^;]+)(?:.*)?$/)||[,null])[1] 102 } 103 104 comparator.scanForCheckedCompares = function() { 105 var cookie = getCompareCookie(); 106 if (cookie == null) { 107 //Need anything here? 108 } else { 109 keys = JSON.parse(cookie); 110 } 111 112 var inputs = document.getElementsByTagName("input"); 113 for (var idx = 0; idx < inputs.length; idx++) { 114 var input = inputs[idx]; 115 if ("checkbox" == input.type) { 116 // Assume default state is unchecked 117 if (input.checked) { 118 input.click(); 119 } 120 } 121 } 122 } 123 return comparator; 124})(); 125 126// Expose global functions 127var compare = ItemComparator.compare; 128var toggleCompare = ItemComparator.toggleCompare; 129 130// Scan for already checked items (set by browser in refresh or back button) and set appropriately 131document.addEventListener('DOMContentLoaded', () => { 132 ItemComparator.scanForCheckedCompares(); 133})
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.