|
| 1 | +let DS_SEARCH = (function () { |
| 2 | + const API_TYPES = { |
| 3 | + ESIGNATURE: 'esignature', |
| 4 | + MONITOR: 'monitor', |
| 5 | + CLICK: 'click', |
| 6 | + ROOMS: 'rooms', |
| 7 | + ADMIN: 'admin', |
| 8 | + }; |
| 9 | + |
| 10 | + let processJSONData = function() { |
| 11 | + let json_raw = $("#api_json_data").text() |
| 12 | + , json = json_raw ? JSON.parse(json_raw) : false; |
| 13 | + |
| 14 | + return json; |
| 15 | + } |
| 16 | + |
| 17 | + let processCFR11Value = function () { |
| 18 | + let json_raw = $("#cfr11_data").text(); |
| 19 | + |
| 20 | + return json_raw; |
| 21 | + } |
| 22 | + |
| 23 | + function checkIfExampleMatches(example, matches) { |
| 24 | + const name = example.ExampleName; |
| 25 | + const description = example.ExampleDescription; |
| 26 | + const pathNames = example.LinksToAPIMethod.map(a => a.PathName); |
| 27 | + |
| 28 | + for (let i = 0; i < matches.length; i++) { |
| 29 | + if (name === matches[i].value || description === matches[i].value || pathNames.indexOf(matches[i].value) > -1) { |
| 30 | + return true; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + function clearNonMatchingExamples(examples, matches) { |
| 38 | + for (let i = examples.length - 1; i >= 0; i--) { |
| 39 | + if (!checkIfExampleMatches(examples[i], matches)) { |
| 40 | + examples.splice(i, 1); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + function clearResultsAfterMatching(api, matches) { |
| 46 | + const groups = api.Groups; |
| 47 | + |
| 48 | + for (let i = groups.length - 1; i >= 0; i--) { |
| 49 | + const group = groups[i]; |
| 50 | + clearNonMatchingExamples(group.Examples, matches); |
| 51 | + |
| 52 | + if (group.Examples.length === 0) { |
| 53 | + groups.splice(i, 1); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + let findCodeExamplesByKeywords = function(json, pattern) { |
| 59 | + const options = { |
| 60 | + isCaseSensitive: false, |
| 61 | + minMatchCharLength: pattern.length, |
| 62 | + threshold: -0.0, |
| 63 | + includeMatches: true, |
| 64 | + ignoreLocation: true, |
| 65 | + useExtendedSearch: true, |
| 66 | + keys: [ |
| 67 | + "Groups.Examples.ExampleName", |
| 68 | + "Groups.Examples.ExampleDescription", |
| 69 | + "Groups.Examples.LinksToAPIMethod.PathName" |
| 70 | + ] |
| 71 | + }; |
| 72 | + |
| 73 | + const fuse = new Fuse(json, options); |
| 74 | + |
| 75 | + var searchResults = fuse.search(JSON.stringify(pattern)) |
| 76 | + |
| 77 | + searchResults.forEach(searchResult => clearResultsAfterMatching(searchResult.item, searchResult.matches)); |
| 78 | + |
| 79 | + return searchResults; |
| 80 | + } |
| 81 | + |
| 82 | + let getExamplesByAPIType = function(apiType, codeExamples) { |
| 83 | + let codeExamplesByAPI = codeExamples.find(x => x.Name.toLowerCase() === apiType); |
| 84 | + |
| 85 | + if (codeExamplesByAPI != null) { |
| 86 | + return [codeExamplesByAPI]; |
| 87 | + } else { |
| 88 | + return null; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + let getEnteredAPIType = function(inputValue) { |
| 93 | + const inputLength = inputValue.length; |
| 94 | + |
| 95 | + for (const key in API_TYPES) { |
| 96 | + if (Object.hasOwnProperty.call(API_TYPES, key)) { |
| 97 | + const apiType = API_TYPES[key]; |
| 98 | + const comparedValue = apiType.substr(0, inputLength); |
| 99 | + |
| 100 | + if(inputValue === comparedValue){ |
| 101 | + return apiType; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + function getLinkForApiType(apiName) { |
| 110 | + switch (apiName) { |
| 111 | + case API_TYPES.ADMIN: |
| 112 | + return "aeg"; |
| 113 | + case API_TYPES.CLICK: |
| 114 | + return "ceg"; |
| 115 | + case API_TYPES.ROOMS: |
| 116 | + return "reg"; |
| 117 | + case API_TYPES.MONITOR: |
| 118 | + return "meg"; |
| 119 | + case API_TYPES.ESIGNATURE: |
| 120 | + return "eg" |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + let addCodeExampleToHomepage = function(codeExamples) { |
| 125 | + var cfrPart11 = processCFR11Value(); |
| 126 | + |
| 127 | + codeExamples.forEach( |
| 128 | + element => { |
| 129 | + let linkToCodeExample = getLinkForApiType(element.Name.toLowerCase()); |
| 130 | + |
| 131 | + element.Groups.forEach( |
| 132 | + group => { |
| 133 | + $("#filtered_code_examples").append("<h2>" + group.Name + "</h2>"); |
| 134 | + |
| 135 | + group.Examples.forEach( |
| 136 | + example => { |
| 137 | + if (!example.SkipForLanguages || !example.SkipForLanguages.toLowerCase().includes("php")) |
| 138 | + { |
| 139 | + if (element.Name.toLowerCase() !== API_TYPES.ESIGNATURE.toLowerCase() || |
| 140 | + ((example.CFREnabled == "AllAccounts") || |
| 141 | + ((cfrPart11 == "enabled") && (example.CFREnabled == "CFROnly")) || |
| 142 | + ((cfrPart11 != "enabled") && (example.CFREnabled == "NonCFR")))) |
| 143 | + { |
| 144 | + $("#filtered_code_examples").append( |
| 145 | + "<h4 id=" |
| 146 | + + "example".concat("0".repeat(3 - example.ExampleNumber.toString().length)).concat(example.ExampleNumber) + ">" |
| 147 | + + "<a href = 'index.php?page=" |
| 148 | + + linkToCodeExample.concat("0".repeat(3 - example.ExampleNumber.toString().length)).concat(example.ExampleNumber) |
| 149 | + + "' >" |
| 150 | + + example.ExampleName |
| 151 | + + "</a ></h4 >" |
| 152 | + ); |
| 153 | + |
| 154 | + $("#filtered_code_examples").append("<p>" + example.ExampleDescription + "</p>"); |
| 155 | + |
| 156 | + $("#filtered_code_examples").append("<p>"); |
| 157 | + |
| 158 | + if (example.LinksToAPIMethod.length == 1) |
| 159 | + { |
| 160 | + $("#filtered_code_examples").append(processJSONData().SupportingTexts.APIMethodUsed); |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + $("#filtered_code_examples").append(processJSONData().SupportingTexts.APIMethodUsedPlural); |
| 165 | + } |
| 166 | + |
| 167 | + for (let index = 0; index < example.LinksToAPIMethod.length; index++) { |
| 168 | + $("#filtered_code_examples").append( |
| 169 | + " <a target='_blank' href='" |
| 170 | + + example.LinksToAPIMethod[index].Path |
| 171 | + + "'>" |
| 172 | + + example.LinksToAPIMethod[index].PathName |
| 173 | + + "</a>" |
| 174 | + ); |
| 175 | + |
| 176 | + if (index + 1 === example.LinksToAPIMethod.length) |
| 177 | + { |
| 178 | + $("#filtered_code_examples").append("<span></span>"); |
| 179 | + } |
| 180 | + else if (index + 1 === example.LinksToAPIMethod.length - 1) |
| 181 | + { |
| 182 | + $("#filtered_code_examples").append("<span> and </span>"); |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + $("#filtered_code_examples").append("<span>, </span>"); |
| 187 | + } |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | + $("#filtered_code_examples").append("</p> "); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + ); |
| 196 | + } |
| 197 | + ); |
| 198 | + } |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + let textCouldNotBeFound = function() { |
| 203 | + $("#filtered_code_examples").append(processJSONData().SupportingTexts.SearchFailed); |
| 204 | + } |
| 205 | + |
| 206 | + return { |
| 207 | + processJSONData: processJSONData, |
| 208 | + getEnteredAPIType: getEnteredAPIType, |
| 209 | + getExamplesByAPIType: getExamplesByAPIType, |
| 210 | + findCodeExamplesByKeywords: findCodeExamplesByKeywords, |
| 211 | + textCouldNotBeFound: textCouldNotBeFound, |
| 212 | + addCodeExampleToHomepage: addCodeExampleToHomepage |
| 213 | + } |
| 214 | +})(); |
| 215 | + |
| 216 | +const input = document.getElementById('code_example_search'); |
| 217 | +const log = document.getElementById('values'); |
| 218 | + |
| 219 | +input.addEventListener('input', updateValue); |
| 220 | + |
| 221 | +function updateValue(esearchPattern) { |
| 222 | + document.getElementById('filtered_code_examples').innerHTML = ""; |
| 223 | + |
| 224 | + const inputValue = esearchPattern.target.value.toLowerCase(); |
| 225 | + const json = DS_SEARCH.processJSONData().APIs; |
| 226 | + |
| 227 | + if (inputValue === "") |
| 228 | + { |
| 229 | + DS_SEARCH.addCodeExampleToHomepage(json); |
| 230 | + } |
| 231 | + else |
| 232 | + { |
| 233 | + const apiType = DS_SEARCH.getEnteredAPIType(inputValue); |
| 234 | + |
| 235 | + if (apiType !== null) |
| 236 | + { |
| 237 | + const adminExamples = DS_SEARCH.getExamplesByAPIType(apiType, json); |
| 238 | + DS_SEARCH.addCodeExampleToHomepage(adminExamples); |
| 239 | + } |
| 240 | + else |
| 241 | + { |
| 242 | + const result = DS_SEARCH.findCodeExamplesByKeywords(json, inputValue); |
| 243 | + if (result.length < 1) { |
| 244 | + DS_SEARCH.textCouldNotBeFound(); |
| 245 | + } else { |
| 246 | + result.forEach(x => { |
| 247 | + DS_SEARCH.addCodeExampleToHomepage([x.item]); |
| 248 | + }); |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | +} |
0 commit comments