diff --git a/.talismanrc b/.talismanrc new file mode 100644 index 0000000..bf127b7 --- /dev/null +++ b/.talismanrc @@ -0,0 +1,4 @@ +fileignoreconfig: +- filename: Search Bar/yarn.lock + checksum: 3aa2a354bcdab7bc6969ff6cbc8ba618498bb208105009d18f57ad441ac2d061 +version: "" \ No newline at end of file diff --git a/Search Bar/.DS_Store b/Search Bar/.DS_Store new file mode 100644 index 0000000..fcf7368 Binary files /dev/null and b/Search Bar/.DS_Store differ diff --git a/Search Bar/.gitignore b/Search Bar/.gitignore new file mode 100644 index 0000000..099ae14 --- /dev/null +++ b/Search Bar/.gitignore @@ -0,0 +1,3 @@ +node_modules +package-lock.json + diff --git a/Search Bar/.idea/.gitignore b/Search Bar/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/Search Bar/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/Search Bar/.idea/misc.xml b/Search Bar/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/Search Bar/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Search Bar/.idea/modules.xml b/Search Bar/.idea/modules.xml new file mode 100644 index 0000000..435e204 --- /dev/null +++ b/Search Bar/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Search Bar/.idea/serp.iml b/Search Bar/.idea/serp.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/Search Bar/.idea/serp.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Search Bar/.idea/vcs.xml b/Search Bar/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/Search Bar/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Search Bar/bundle.js b/Search Bar/bundle.js new file mode 100644 index 0000000..ae09e0a --- /dev/null +++ b/Search Bar/bundle.js @@ -0,0 +1,322 @@ +(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i 2 && arguments[2] !== undefined ? arguments[2] : {}, + comparator = _ref.comparator, + _ref$splitRegex = _ref.splitRegex, + splitRegex = _ref$splitRegex === undefined ? /\s+/ : _ref$splitRegex; + + var data = items; + var trie = {}; + + var compareFunction = comparator ? function (id1, id2) { + return comparator(items[id1], items[id2]); + } : null; + + var addWord = function addWord(word, id, wordIndex) { + var wordLength = word.length; + var node = trie; + + for (var i = 0; i < wordLength; i++) { + var letter = word[i]; + + if (!node[letter]) { + node[letter] = { + ids: [] + }; + } + + if (!node[letter].ids[wordIndex]) { + node[letter].ids[wordIndex] = []; + } + + node[letter].ids[wordIndex].push(id); + + if (compareFunction) { + node[letter].ids[wordIndex].sort(compareFunction); + } + + node = node[letter]; + } + }; + + var addPhrase = function addPhrase(phrase, id) { + var words = phrase.trim().toLowerCase().split(splitRegex); + var wordsCount = words.length; + + for (var i = 0; i < wordsCount; i++) { + addWord(words[i], id, i); + } + }; + + var getWordIndices = function getWordIndices(word) { + var wordLength = word.length; + var node = trie; + + for (var i = 0; i < wordLength; i++) { + if (node[word[i]]) { + node = node[word[i]]; + } else { + return []; + } + } + + var ids = node.ids; + var length = ids.length; + var result = []; + + for (var _i = 0; _i < length; _i++) { + if (ids[_i]) { + result = (0, _concatAndRemoveDups2.default)(result, ids[_i]); + } + } + + return result; + }; + + var getPhraseIndices = function getPhraseIndices(phrase, _ref2) { + var limit = _ref2.limit, + _ref2$splitRegex = _ref2.splitRegex, + splitRegex = _ref2$splitRegex === undefined ? /\s+/ : _ref2$splitRegex; + + var words = phrase.toLowerCase().split(splitRegex).filter(Boolean); + + if (words.length === 0) { + return []; + } + + var wordsCount = words.length; + var indicesArray = []; + + for (var i = 0; i < wordsCount; i++) { + indicesArray[indicesArray.length] = getWordIndices(words[i]); + } + + return (0, _intersectionWithLimit2.default)(indicesArray, limit); + }; + + var getMatches = function getMatches(query) { + var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + + var indices = getPhraseIndices(query, options); + var indicesCount = indices.length; + var result = []; + + for (var i = 0; i < indicesCount; i++) { + result[result.length] = data[indices[i]]; + } + + return result; + }; + + var itemsCount = items.length; + + for (var i = 0; i < itemsCount; i++) { + addPhrase(items[i][textKey], i); + } + + return { + getMatches: getMatches + }; +}; +},{"../concat-and-remove-dups/concat-and-remove-dups":2,"../intersection-with-limit/intersection-with-limit":5}],4:[function(require,module,exports){ +'use strict'; + +var _createTrie = require('./create-trie/create-trie'); + +var _createTrie2 = _interopRequireDefault(_createTrie); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +module.exports = _createTrie2.default; +},{"./create-trie/create-trie":3}],5:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +exports.default = function (arrays, limit) { + var arraysCount = arrays.length; + var firstArray = arrays[0]; + var firstArrayCount = firstArray.length; + + limit = limit || firstArrayCount; + + var result = [], + candidate = void 0, + found = void 0; + + for (var i = 0; i < firstArrayCount && result.length < limit; i++) { + candidate = firstArray[i]; + found = true; + + for (var k = 1; k < arraysCount; k++) { + if (arrays[k].indexOf(candidate) === -1) { + found = false; + break; + } + } + + if (found) { + result[result.length] = candidate; + } + } + + return result; +}; +},{}],6:[function(require,module,exports){ +'use strict'; +module.exports = (function() +{ + function _min(d0, d1, d2, bx, ay) + { + return d0 < d1 || d2 < d1 + ? d0 > d2 + ? d2 + 1 + : d0 + 1 + : bx === ay + ? d1 + : d1 + 1; + } + + return function(a, b) + { + if (a === b) { + return 0; + } + + if (a.length > b.length) { + var tmp = a; + a = b; + b = tmp; + } + + var la = a.length; + var lb = b.length; + + while (la > 0 && (a.charCodeAt(la - 1) === b.charCodeAt(lb - 1))) { + la--; + lb--; + } + + var offset = 0; + + while (offset < la && (a.charCodeAt(offset) === b.charCodeAt(offset))) { + offset++; + } + + la -= offset; + lb -= offset; + + if (la === 0 || lb < 3) { + return lb; + } + + var x = 0; + var y; + var d0; + var d1; + var d2; + var d3; + var dd; + var dy; + var ay; + var bx0; + var bx1; + var bx2; + var bx3; + + var vector = []; + + for (y = 0; y < la; y++) { + vector.push(y + 1); + vector.push(a.charCodeAt(offset + y)); + } + + var len = vector.length - 1; + + for (; x < lb - 3;) { + bx0 = b.charCodeAt(offset + (d0 = x)); + bx1 = b.charCodeAt(offset + (d1 = x + 1)); + bx2 = b.charCodeAt(offset + (d2 = x + 2)); + bx3 = b.charCodeAt(offset + (d3 = x + 3)); + dd = (x += 4); + for (y = 0; y < len; y += 2) { + dy = vector[y]; + ay = vector[y + 1]; + d0 = _min(dy, d0, d1, bx0, ay); + d1 = _min(d0, d1, d2, bx1, ay); + d2 = _min(d1, d2, d3, bx2, ay); + dd = _min(d2, d3, dd, bx3, ay); + vector[y] = dd; + d3 = d2; + d2 = d1; + d1 = d0; + d0 = dy; + } + } + + for (; x < lb;) { + bx0 = b.charCodeAt(offset + (d0 = x)); + dd = ++x; + for (y = 0; y < len; y += 2) { + dy = vector[y]; + vector[y] = dd = _min(dy, d0, dd, bx0, vector[y + 1]); + d0 = dy; + } + } + + return dd; + }; +})(); + + +},{}]},{},[1]); diff --git a/Search Bar/index.css b/Search Bar/index.css new file mode 100644 index 0000000..2b5f57b --- /dev/null +++ b/Search Bar/index.css @@ -0,0 +1,215 @@ + +body{ + margin: 0; + padding: 0; +} +html{ + margin: 0; + padding: 0; +} +.ibox-content { + background-color: #FFFFFF; + color: inherit; + padding: 15px 20px 20px 20px; + border-color: #E7EAEC; + border-image: none; + border-style: solid solid none; + border-width: 1px 0px; +} + +#searchHeader { + overflow-y: visible; +} +@media (max-width: 576px) { + #searchHeader { + max-height: 100px; + } +} +#brandingTitle { + font-size: 24px; + font-family: sans-serif; + color: black; + font-weight: bold; +} +#branding { + padding: 0px; + display: inline-block; + text-align: center; + margin: 10px auto; +} +#searchBarArea { + display: inline-block; + background-color: #fefefe; + z-index: 2; + height: 50px; +} +#searchBar { + position: absolute; + left: 0; + right: 0; + margin-left: auto; + margin-right: auto; + background-color: #fefefe; + border: 1px solid #eee; + padding: 0px 18px; + border-radius: 25px; + box-shadow: 1px 1px 4px #ccc; +} +#searchBar:hover { + border: 1px solid #eee; + box-shadow: 1px 2px 4px #bbb; +} +#searchBar:focus { + border: 1px solid #eee; + outline: none; + box-shadow: 1px 2px 4px #bbb; +} +#searchBox { + background-color: inherit; + border: none!important; + box-shadow: none!important; +} +#searchBox:focus { + outline: none; + border: none!important; + box-shadow: none!important; +} +#searchSuggestions { + max-height: 150px; + overflow-y: scroll; + color: #666; + padding-bottom: 10px; +} +/* +#searchForm:focus-within + #searchList > #searchSuggestions { + display: block; +} */ +div.searchSuggestionSeparator { + border-top: 1px solid #ddd; +} +/* +#searchForm:focus-within + #searchList > div.searchSuggestionSeparator { + display: block; +} */ + +.displayBlock { + display: block!important; +} + +#searchSuggestions > div { + width: 100%; + padding: 5px 5px; + font-size: 16px; +} +#searchSuggestions > div:hover { + background-color: #eee; +} +#content { + float: left; + max-height: 750px; + overflow-y: scroll; +} +#contentMeta { + margin: 20px 0 40px 0; + font-size: 16px; + color: #666; +} +.searchResult h3 { + margin-bottom: 0; + color: #1E0FBE; +} + +.searchResult .searchLink { + color: #006621; + float: left; +} + +.searchResult p { + font-size: 12px; + margin-top: 5px; +} + +.hrLine { + border-top: 1px solid #E7EAEC; + color: #ffffff; + background-color: #ffffff; + height: 1px; + margin: 20px 0; +} +h2 { + font-size: 24px; + font-weight: 100; +} +#search, #searchContent { + border-left: 1px solid #E7EAEC; + float: left; +} + +#contentRepo { + border: 1px solid #E7EAEC; + float: none; + margin: 20px auto; +} +#contentRepo p { + text-align: center; +} +.entryHeader { + padding: 10px 5px; + color: #333; + font: 24px bolder; + text-align: center; +} +.entryList { + height: 300px; + overflow-y: scroll; +} +.entry { + margin: 4px; + margin-bottom: 30px; + border-bottom: 2px solid #eee; +} +td { + white-space: normal !important; + word-wrap: break-word; + max-width: 200px; +} +#contentEntryForm { + overflow-y: scroll; + float: none; + margin: 20px auto; +} + +#contentEntryForm p { + text-align: center; +} + +.form-field { + margin-bottom: 10px; +} +.borderless td, .borderless th { + border: none!important; +} + +.suggestion { + white-space: nowrap; + overflow: hidden; + width: 100%; + text-align: left; + text-overflow: ellipsis; + background-color: #fefefe; + padding: 5px 5px; + font-size: 16px; + border: None; + color: #666; +} +.suggestion:focus { + outline:0; + background-color: #ddd; +} +.suggestion:hover { + outline:0; + background-color: #eee; +} +.match { + background-color: #ffe6ae; +} \ No newline at end of file diff --git a/Search Bar/index.html b/Search Bar/index.html new file mode 100644 index 0000000..9a15d68 --- /dev/null +++ b/Search Bar/index.html @@ -0,0 +1,85 @@ + + + + + + Search Bar + + + + + + + + + + +
+
+ +
+
+
+
+
+
All Entries
+

(List of all searchable entries)

+
+ +
+
+
+

(Fill this form to add into the list of searchable entries)

+
+
+ +
+ +
+ +
+ +
+ +
+ + +
+
+
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/Search Bar/index.js b/Search Bar/index.js new file mode 100644 index 0000000..19c1dcf --- /dev/null +++ b/Search Bar/index.js @@ -0,0 +1,236 @@ + +var defaultEntries = [ + {"title":"Thor", + "link":"https://comicvine.gamespot.com/thor/4005-2268/", + "desc":"Thor Odinson is the All-father of Asgard /God of Thunder, offspring of All-Father Odin & the Elder Earth-Goddess Gaea. Combining the powers of both realms makes him an elder-god hybrid and a being of limitless potential. Armed with his enchanted Uru hammer Mjolnir which helps him to channel his godly energies. The mightiest and the most beloved warrior in all of Asgard, a staunch ally for good and one of the most powerful beings in the multiverse/omniverse. Thor is also a founding member of the Avengers.", + "indexed":"Thor Thor Odinson is the All father of Asgard God of Thunder offspring of All Father Odin the Elder Earth Goddess Gaea Combining the powers of both realms makes him an elder god hybrid and a being of limitless potential Armed with his enchanted Uru hammer Mjolnir which helps him to channel his godly energies The mightiest and the most beloved warrior in all of Asgard a staunch ally for good and one of the most powerful beings in the multiverse omniverse Thor is also a founding member of the Avengers"}, + {"title":"Iron Man", + "link":"https://comicvine.gamespot.com/iron-man/4005-1455/", + "desc":"Tony Stark was the arrogant son of wealthy, weapon manufacturer Howard Stark. Tony cared only about himself, but he would have a change of heart after he was kidnapped by terrorists and gravely injured. Pressured to create a weapon of mass destruction, Stark instead created a suit of armor powerful enough for him to escape. Tony uses his vast resources and intellect to make the world a better place as The Invincible Iron Man. Stark's super hero identity led him to become a founding member of the Avengers.", + "indexed":"Iron Man Tony Stark was the arrogant son of wealthy weapon manufacturer Howard Stark Tony cared only about himself but he would have a change of heart after he was kidnapped by terrorists and gravely injured Pressured to create a weapon of mass destruction Stark instead created a suit of armor powerful enough for him to escape Tony uses his vast resources and intellect to make the world a better place as The Invincible Iron Man Stark s super hero identity led him to become a founding member of the Avengers"}, + {"title":"SpiderMan", + "link":"https://comicvine.gamespot.com/spider-man/4005-1443/", + "desc":"Peter Parker was bitten by a radioactive spider as a teenager, granting him spider-like powers. After the death of his Uncle Ben, Peter learned that \"with great power, comes great responsibility\". Swearing to always protect the innocent from harm, Peter Parker became Spider-Man.", + "indexed":"SpiderMan Peter Parker was bitten by a radioactive spider as a teenager granting him spider like powers After the death of his Uncle Ben Peter learned that with great power comes great responsibility Swearing to always protect the innocent from harm Peter Parker became Spider Man"}, + {"title":"Hulk", + "link":"https://comicvine.gamespot.com/hulk/4005-2267/", + "desc":"After being bombarded with a massive dose of gamma radiation while saving a young man's life during an experimental bomb testing, Dr. Robert Bruce Banner was transformed into the Incredible Hulk: a green behemoth who is the living personification of rage and pure physical strength.", + "indexed":"Hulk After being bombarded with a massive dose of gamma radiation while saving a young man s life during an experimental bomb testing Dr Robert Bruce Banner was transformed into the Incredible Hulk a green behemoth who is the living personification of rage and pure physical strength"}, + {"title":"Captain America", + "link":"https://comicvine.gamespot.com/captain-america/4005-1442/", + "desc":"During World War II, Steve Rogers volunteered to receive the experimental Super-Soldier Serum. Enhanced to the pinnacle of human physical potential and armed with an unbreakable shield, he became Captain America. After a failed mission left him encased in ice for decades, he was found and revived by the Avengers, later joining their ranks and eventually becoming the team's leader.", + "indexed":"Captain America During World War II Steve Rogers volunteered to receive the experimental Super Soldier Serum Enhanced to the pinnacle of human physical potential and armed with an unbreakable shield he became Captain America After a failed mission left him encased in ice for decades he was found and revived by the Avengers later joining their ranks and eventually becoming the team s leader"}, + {"title":"Deadpool", + "link":"https://comicvine.gamespot.com/deadpool/4005-7606/", + "desc":"Wade Wilson is a former test subject of the Weapon X program, where he received his regenerative healing factor through the scientific experiments conducted upon him. Deadpool's powers and personality traits combine to make a wild, mentally unstable, and unpredictable mercenary. Deadpool has been a member of X-Force and the Thunderbolts, and a self-professed member of the X-Men and the Avengers. The \"Merc with a Mouth\" is infamous for breaking the fourth wall.", + "indexed":"Deadpool Wade Wilson is a former test subject of the Weapon X program where he received his regenerative healing factor through the scientific experiments conducted upon him Deadpool s powers and personality traits combine to make a wild mentally unstable and unpredictable mercenary Deadpool has been a member of X Force and the Thunderbolts and a self professed member of the X Men and the Avengers The Merc with a Mouth is infamous for breaking the fourth wall"}, + {"title":"Nick Fury", + "link":"https://comicvine.gamespot.com/nick-fury/4005-3202/", + "desc":"Nicholas Joseph Fury served in World War II as the leader of the Howling Commandos. He later became an agent, and eventually director, of S.H.I.E.L.D. Fury is one of the greatest strategic minds in the world, a born leader and a master of espionage. He currently serves as a replacement Watcher.", + "indexed":"Nick Fury Nicholas Joseph Fury served in World War II as the leader of the Howling Commandos He later became an agent and eventually director of S H I E L D Fury is one of the greatest strategic minds in the world a born leader and a master of espionage He currently serves as a replacement Watcher"}, + {"title":"Doctor Strange", + "link":"https://comicvine.gamespot.com/doctor-strange/4005-1456/", + "desc":"Dr. Stephen Strange was once a gifted but egotistical surgeon who sought out the Ancient One to heal his hands after they were wounded in a car accident. Instead, the Ancient One trained him to become Master of the Mystic Arts and the Sorcerer Supreme of Earth.", + "indexed":"Doctor Strange Dr Stephen Strange was once a gifted but egotistical surgeon who sought out the Ancient One to heal his hands after they were wounded in a car accident Instead the Ancient One trained him to become Master of the Mystic Arts and the Sorcerer Supreme of Earth"}, + {"title":"Black Panther", + "link":"https://comicvine.gamespot.com/black-panther/4005-1477/", + "desc":"T'Challa is the Black Panther, king of Wakanda, one of the most technologically advanced nations on Earth. He is among the top intellects and martial artists of the world, a veteran Avenger, and a member of the Illuminati. Using his powers and abilities, he has pledged his fortune, powers, and life to the service of all mankind.", + "indexed":"Black Panther T Challa is the Black Panther king of Wakanda one of the most technologically advanced nations on Earth He is among the top intellects and martial artists of the world a veteran Avenger and a member of the Illuminati Using his powers and abilities he has pledged his fortune powers and life to the service of all mankind"}]; +var entries = JSON.parse(localStorage.getItem('entries')) || defaultEntries; +var pastSearches = JSON.parse(localStorage.getItem('pastSearches')) || []; +var knownWords = JSON.parse(localStorage.getItem('knownWords')) || ['thor', 'odinson', 'is', 'the', 'all', 'father', 'of', 'asgard', 'god', 'thunder', 'offspring', 'odin', 'elder', 'earth', 'goddess', 'gaea', 'combining', 'powers', 'both', 'realms', 'makes', 'him', 'an', 'hybrid', 'and', 'being', 'limitless', 'potential', 'armed', 'with', 'his', 'enchanted', 'uru', 'hammer', 'mjolnir', 'which', 'helps', 'to', 'channel', 'godly', 'energies', 'mightiest', 'most', 'beloved', 'warrior', 'in', 'staunch', 'ally', 'for', 'good', 'one', 'powerful', 'beings', 'multiverse', 'omniverse', 'also', 'founding', 'member', 'avengers', 'iron', 'man', 'tony', 'stark', 'was', 'arrogant', 'son', 'wealthy', 'weapon', 'manufacturer', 'howard', 'cared', 'only', 'about', 'himself', 'but', 'he', 'would', 'have', 'change', 'heart', 'after', 'kidnapped', 'by', 'terrorists', 'gravely', 'injured', 'pressured', 'create', 'mass', 'destruction', 'instead', 'created', 'suit', 'armor', 'enough', 'escape', 'uses', 'vast', 'resources', 'intellect', 'make', 'world', 'better', 'place', 'as', 'invincible', 'super', 'hero', 'identity', 'led', 'become', 'spiderman', 'peter', 'parker', 'bitten', 'radioactive', 'spider', 'teenager', 'granting', 'like', 'death', 'uncle', 'ben', 'learned', 'that', 'great', 'power', 'comes', 'responsibility', 'swearing', 'always', 'protect', 'innocent', 'from', 'harm', 'became', 'hulk', 'bombarded', 'massive', 'dose', 'gamma', 'radiation', 'while', 'saving', 'young', 'life', 'during', 'experimental', 'bomb', 'testing', 'dr', 'robert', 'bruce', 'banner', 'transformed', 'into', 'incredible', 'green', 'behemoth', 'who', 'living', 'personification', 'rage', 'pure', 'physical', 'strength', 'captain', 'america', 'war', 'ii', 'steve', 'rogers', 'volunteered', 'receive', 'soldier', 'serum', 'enhanced', 'pinnacle', 'human', 'unbreakable', 'shield', 'failed', 'mission', 'left', 'encased', 'ice', 'decades', 'found', 'revived', 'later', 'joining', 'their', 'ranks', 'eventually', 'becoming', 'team', 'leader', 'deadpool', 'wade', 'wilson', 'former', 'test', 'subject', 'program', 'where', 'received', 'regenerative', 'healing', 'factor', 'through', 'scientific', 'experiments', 'conducted', 'upon', 'personality', 'traits', 'combine', 'wild', 'mentally', 'unstable', 'unpredictable', 'mercenary', 'has', 'been', 'force', 'thunderbolts', 'self', 'professed', 'men', 'merc', 'mouth', 'infamous', 'breaking', 'fourth', 'wall', 'nick', 'fury', 'nicholas', 'joseph', 'served', 'howling', 'commandos', 'agent', 'director', 'greatest', 'strategic', 'minds', 'born', 'master', 'espionage', 'currently', 'serves', 'replacement', 'watcher', 'doctor', 'strange', 'stephen', 'once', 'gifted', 'egotistical', 'surgeon', 'sought', 'out', 'ancient', 'heal', 'hands', 'they', 'were', 'wounded', 'car', 'accident', 'trained', 'mystic', 'arts', 'sorcerer', 'supreme', 'black', 'panther', 'challa', 'king', 'wakanda', 'technologically', 'advanced', 'nations', 'on', 'among', 'top', 'intellects', 'martial', 'artists', 'veteran', 'avenger', 'illuminati', 'using', 'abilities', 'pledged', 'fortune', 'service', 'mankind']; +var trie = createTrie(entries, 'indexed'); + +function onlyUnique(value, index, self) { + return self.indexOf(value) === index; +} + +function renderResults(results, query) { + var queryWords = query.toLowerCase().split(' ').filter(function(e){return e}); + $('#mainContent').html(''); + + results.forEach(result => { + + var resultTitle = result.title; + var resultDesc = result.desc; + + for (const qWordIndex in queryWords) { + var qWord = queryWords[qWordIndex]; + var qWordRegex = new RegExp("(^|[^a-zA-Z0-9])(" + qWord + ")", "gi"); + + resultTitle = resultTitle.replaceAll(qWordRegex, '$1$2'); + resultDesc = resultDesc.replaceAll(qWordRegex, '$1$2'); + } + + var resultHtml = `
+

${resultTitle}

+ ${result.link}
+

${resultDesc}

+
+
` + $('#mainContent').append(resultHtml); + }); + return false; +} + +function renderEntries(entries) { + entries.forEach(entry => { + var entryHtml = `
+ + + + + + +
Title${entry.title}
Link${entry.link}
Desc${entry.desc}
+
` + $('#entryList').append(entryHtml); + }); + return false; +} + +function renderSearchSuggestions(suggestions) { + var suggestionHtmlList = []; + suggestions.forEach(suggestion => { + var suggestionHtml = `` + suggestionHtmlList.push(suggestionHtml); + }); + var suggestionsHtml = ''; + if (suggestionHtmlList.length > 0 ) { + suggestionsHtml = `
${suggestionHtmlList.join('')}
`; + } + $('#searchList').html(suggestionsHtml); + return false; +} + +function renderFoundSuggestions() { + query = $('#searchBox').val(); + + if (query.length > 2) { + var suggestions = trie.getMatches(query, { limit: 12}); + var queryRegex = new RegExp("^(" + query + ")|([ ]" + query + ")", "i"); + var suggestionList = [query]; + suggestions.forEach(suggestion => { + suggestionList.push(suggestion.indexed.slice(queryRegex.exec(suggestion.indexed).index)); + }); + suggestionList = suggestionList.filter(onlyUnique).slice(0, 6); + renderSearchSuggestions(suggestionList); + } + else if (query.length > 0) { + renderSearchSuggestions([query]); + } + else { + renderSearchSuggestions(pastSearches); + } + return false; +} + + +function searchEntries() { + hideSearchList(); + query = $('#searchBox').val(); + if(query) { + pastSearches.unshift(query); + localStorage.setItem('pastSearches', JSON.stringify(pastSearches)); + } + else { + return false; + } + + var searchResults = trie.getMatches(query); + renderResults(searchResults, query); + + + if (searchResults.length == 0) { + var queryWords = query.toLowerCase().replace(/[^\w\s]/gi, ' ').trim().split(' ').filter(function(e){return e}); + var correctedQueryWords = []; + for (const qWordIndex in queryWords) { + var qWord = queryWords[qWordIndex]; + var minDistance = 999; + var minWord = null; + for (const wordIndex in knownWords) { + var knownWord = knownWords[wordIndex]; + var distance = levenshtein(qWord, knownWord); + if (distance <= 1) { + minDistance = distance; + minWord = knownWord; + break; + } + else { + if (minDistance > distance || (minDistance == distance && (qWord[0] === knownWord[0] || qWord.length === knownWord.length))) { + minDistance = distance; + minWord = knownWord; + } + } + } + if (minWord !== null && minDistance <=2) { + correctedQueryWords.push(minWord); + } + } + + if (queryWords.length !== correctedQueryWords.length) { + $('#contentMeta').html(`No results for search "${query}"`); + } + else { + $('#contentMeta').html(`Did you mean ${correctedQueryWords.join(' ')}?`); + } + } + else { + $('#contentMeta').html(`Showing Results for "${query}"`); + } + return false; +} + +function hideSearchList() { + $('#searchList').attr('class', ''); +} + +function showSearchList(event) { + event.stopPropagation(); + $('#searchList').addClass('displayBlock'); +} + +function myFunc(elem) { + var selectedSuggestion = $(elem).text(); + $('#searchBox').val(selectedSuggestion); + searchEntries(); + return false; +} + +$(document).ready(function(){ + $('#contentEntryForm').children( "form" ).submit(event => { + var newEntry = { + "title": $('#entryTitle').val(), + "link": $('#entryLink').val(), + "desc": $('#entryDescription').val(), + "indexed": (`${$('#entryTitle').val()} ${$('#entryDescription').val()}`).replace(/[^\w\s]/gi, ' ').trim() + }; + entries.push(newEntry); + var newWords = newEntry.indexed.toLowerCase().split(' '); + newWords = newWords.filter(onlyUnique); + var index = newWords.indexOf(''); + if (index > -1) { + newWords.splice(index, 1); + } + for (const wordIndex in newWords) { + var word = newWords[wordIndex].toLowerCase(); + if (word.length > 1) { + if (knownWords.indexOf(word) == -1) { + knownWords.unshift(word); + } + } + } + localStorage.setItem('entries', JSON.stringify(entries)); + localStorage.setItem('knownWords', JSON.stringify(knownWords)); + trie = createTrie(entries, 'indexed'); + renderEntries(entries); + $('#contentEntryForm').children( "form" )[0].reset(); + }); + $('#searchBox').on('input', renderFoundSuggestions); + $('#searchForm').submit(searchEntries); + $('#searchBox').click(showSearchList); + $(document).on('click', function(){ + hideSearchList(); + }); + renderEntries(entries); +}); diff --git a/Search Bar/main.js b/Search Bar/main.js new file mode 100644 index 0000000..d2256ac --- /dev/null +++ b/Search Bar/main.js @@ -0,0 +1,5 @@ +const createTrie = require('autosuggest-trie'); +const levenshtein = require('js-levenshtein'); + +window.createTrie = createTrie; +window.levenshtein = levenshtein; \ No newline at end of file diff --git a/Search Bar/package.json b/Search Bar/package.json new file mode 100644 index 0000000..1beefb3 --- /dev/null +++ b/Search Bar/package.json @@ -0,0 +1,7 @@ +{ + "type": "module", + "dependencies": { + "autosuggest-trie": "^2.1.1", + "js-levenshtein": "^1.1.6" + } +} diff --git a/Search Bar/yarn.lock b/Search Bar/yarn.lock new file mode 100644 index 0000000..df6d8d9 --- /dev/null +++ b/Search Bar/yarn.lock @@ -0,0 +1,13 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"autosuggest-trie@^2.1.1": + "integrity" "sha1-+SxIB87vYy3vRCTML3kKDM3Br3s=" + "resolved" "https://registry.npmjs.org/autosuggest-trie/-/autosuggest-trie-2.1.1.tgz" + "version" "2.1.1" + +"js-levenshtein@^1.1.6": + "integrity" "sha512-X2BB11YZtrRqY4EnQcLX5Rh373zbK4alC1FW7D7MBhL2gtcC17cTnr6DmfHZeS0s2rTHjUTMMHfG7gO8SSdw+g==" + "resolved" "https://registry.npmjs.org/js-levenshtein/-/js-levenshtein-1.1.6.tgz" + "version" "1.1.6"