diff --git a/data/search-index.js b/data/search-index.js index f2888f16c..e3fa05773 100644 --- a/data/search-index.js +++ b/data/search-index.js @@ -42,6 +42,7 @@ // }); exports.BattleSearchIndex = [ +"abomasnow", "abra", "absol", "absorb", @@ -1747,6 +1748,7 @@ exports.BattleSearchIndex = [ exports.BattleSearchIndexType = [ "pokemon", "pokemon", +"pokemon", "move", "item", "pokemon", @@ -3451,6 +3453,7 @@ exports.BattleSearchIndexOffset = [ "", "", "", +"", "0000001111", "", "", diff --git a/js/search.js b/js/search.js index 2408579df..e7343a0b8 100644 --- a/js/search.js +++ b/js/search.js @@ -24,32 +24,36 @@ // Search functions // - Search.prototype.q = ''; + Search.prototype.q = null; var typeTable = { pokemon: 0, - move: 1, - item: 2, - ability: 3 + type: 1, + move: 2, + item: 3, + ability: 4, + category: 5 }; var typeName = { pokemon: 'Pokemon', + type: 'Type', move: 'Moves', item: 'Items', - ability: 'Abilities' + ability: 'Abilities', + category: 'Category' }; Search.prototype.find = function(query) { query = toId(query); if (query === this.q) { - return; + return false; } this.q = query; if (!query) { - this.$el.html(''); - return; + this.el.innerHTML = ''; + return true; } - var i = this.getClosest(query); + var i = Search.getClosest(query); if (!BattleSearchIndex[i]) i--; var bufs = ['','','','']; @@ -59,19 +63,21 @@ var type = BattleSearchIndexType[i+j]; var matchLength = query.length; + if (!id) break; if (id.substr(0,query.length) !== query) { if (j) break; matchLength = 0; } if (!bufs[typeTable[type]]) bufs[typeTable[type]] = '
  • '+typeName[type]+'

  • '; - bufs[typeTable[type]] += this.renderRow(id, type, 0, matchLength + (BattleSearchIndexOffset[i+j][matchLength-1]||'0').charCodeAt(0)-48); + bufs[typeTable[type]] += Search.renderRow(id, type, 0, matchLength + (BattleSearchIndexOffset[i+j][matchLength-1]||'0').charCodeAt(0)-48); } - this.$el.html(''); + this.el.innerHTML = ''; + return true; }; - Search.prototype.getClosest = function(query) { + Search.getClosest = function(query) { // binary search through the index! var left = 0; var right = BattleSearchIndex.length - 1; @@ -91,48 +97,39 @@ // // Rendering functions + // + // These are all static! // - Search.prototype.renderRow = function(id, type, matchStart, matchLength, errorMessage) { + Search.urlRoot = ''; + + Search.renderRow = function(id, type, matchStart, matchLength, errorMessage) { switch (type) { case 'pokemon': - var pokemon = Tools.getTemplate(id); - return this.renderPokemonRow(pokemon, matchStart, matchLength, errorMessage); + var pokemon = BattlePokedex[id]; + return Search.renderPokemonRow(pokemon, matchStart, matchLength, errorMessage); case 'move': - var move = Tools.getMove(id); - return this.renderMoveRow(move, matchStart, matchLength, errorMessage); + var move = BattleMovedex[id]; + return Search.renderMoveRow(move, matchStart, matchLength, errorMessage); case 'item': - var item = Tools.getItem(id); - return this.renderItemRow(item, matchStart, matchLength, errorMessage); + var item = BattleItems[id]; + return Search.renderItemRow(item, matchStart, matchLength, errorMessage); case 'ability': - var ability = Tools.getAbility(id); - return this.renderAbilityRow(ability, matchStart, matchLength, errorMessage); + var ability = BattleAbilities[id]; + return Search.renderAbilityRow(ability, matchStart, matchLength, errorMessage); + case 'type': + var type = {name: id[0].toUpperCase()+id.substr(1)}; + return Search.renderTypeRow(type, matchStart, matchLength, errorMessage); + case 'category': + var category = {name: id[0].toUpperCase()+id.substr(1)}; + return Search.renderCategoryRow(category, matchStart, matchLength, errorMessage); } return 'Error: not found'; }; - Search.prototype.renderRowByIdLength = function(id, type, idMatchLength, errorMessage) { - switch (type) { - case 'pokemon': - var pokemon = Tools.getTemplate(id); - idMatchLength += pokemon.species.substr(0, idMatchLength).replace(/[a-zA-Z0-9]+/,'').length; - return this.renderPokemonRow(pokemon, 0, idMatchLength, errorMessage); - case 'move': - var move = Tools.getMove(id); - idMatchLength += move.name.substr(0, idMatchLength).replace(/[a-zA-Z0-9]+/,'').length; - return this.renderMoveRow(move, 0, idMatchLength, errorMessage); - case 'item': - var item = Tools.getItem(id); - idMatchLength += item.name.substr(0, idMatchLength).replace(/[a-zA-Z0-9]+/,'').length; - return this.renderItemRow(item, 0, idMatchLength, errorMessage); - case 'ability': - var ability = Tools.getAbility(id); - idMatchLength += ability.name.substr(0, idMatchLength).replace(/[a-zA-Z0-9]+/,'').length; - return this.renderAbilityRow(ability, 0, idMatchLength, errorMessage); - } - return 'Error: not found'; - }; - Search.prototype.renderPokemonRow = function(pokemon, matchStart, matchLength, errorMessage) { - var buf = '
  • '; + Search.renderPokemonRow = function(pokemon, matchStart, matchLength, errorMessage) { + var attrs = ''; + if (Search.urlRoot) attrs = ' href="'+Search.urlRoot+'pokemon/'+toId(pokemon.species)+'" data-target="push"'; + var buf = '
  • '; // number buf += ''+(pokemon.num)+' '; @@ -213,8 +210,10 @@ return buf; }; - Search.prototype.renderItemRow = function(item, matchStart, matchLength, errorMessage) { - var buf = '
  • '; + Search.renderItemRow = function(item, matchStart, matchLength, errorMessage) { + var attrs = ''; + if (Search.urlRoot) attrs = ' href="'+Search.urlRoot+'item/'+toId(item.name)+'" data-target="push"'; + var buf = '
  • '; // icon buf += ''; @@ -242,8 +241,10 @@ return buf; }; - Search.prototype.renderAbilityRow = function(ability, matchStart, matchLength, errorMessage) { - var buf = '
  • '; + Search.renderAbilityRow = function(ability, matchStart, matchLength, errorMessage) { + var attrs = ''; + if (Search.urlRoot) attrs = ' href="'+Search.urlRoot+'ability/'+toId(ability.name)+'" data-target="push"'; + var buf = '
  • '; // name var name = ability.name; @@ -258,15 +259,17 @@ buf += '
  • '; return buf; } - + buf += ''+Tools.escapeHTML(ability.shortDesc || ability.desc)+' '; - + buf += ''; - + return buf; }; - Search.prototype.renderMoveRow = function(move, matchStart, matchLength, errorMessage) { - var buf = '
  • '; + Search.renderMoveRow = function(move, matchStart, matchLength, errorMessage) { + var attrs = ''; + if (Search.urlRoot) attrs = ' href="'+Search.urlRoot+'move/'+toId(move.name)+'" data-target="push"'; + var buf = '
  • '; // name var name = move.name; @@ -312,6 +315,87 @@ return buf; }; + Search.renderAbilityRow = function(ability, matchStart, matchLength, errorMessage) { + var attrs = ''; + if (Search.urlRoot) attrs = ' href="'+Search.urlRoot+'ability/'+toId(ability.name)+'" data-target="push"'; + var buf = '
  • '; + + // name + var name = ability.name; + if (matchLength) { + name = name.substr(0, matchStart)+''+name.substr(matchStart, matchLength)+''+name.substr(matchStart+matchLength); + } + buf += ''+name+' '; + + // error + if (errorMessage) { + buf += ''+errorMessage+' '; + buf += '
  • '; + return buf; + } + + buf += ''+Tools.escapeHTML(ability.shortDesc || ability.desc)+' '; + + buf += ''; + + return buf; + }; + Search.renderTypeRow = function(type, matchStart, matchLength, errorMessage) { + var attrs = ''; + if (Search.urlRoot) attrs = ' href="'+Search.urlRoot+'type/'+toId(type.name)+'" data-target="push"'; + var buf = '
  • '; + + // name + var name = type.name; + if (matchLength) { + name = name.substr(0, matchStart)+''+name.substr(matchStart, matchLength)+''+name.substr(matchStart+matchLength); + } + buf += ''+name+' '; + + // error + if (errorMessage) { + buf += ''+errorMessage+' '; + buf += '
  • '; + return buf; + } + + // type + buf += ''; + buf += Tools.getTypeIcon(type.name); + buf += ' '; + + buf += ''; + + return buf; + }; + Search.renderCategoryRow = function(category, matchStart, matchLength, errorMessage) { + var attrs = ''; + if (Search.urlRoot) attrs = ' href="'+Search.urlRoot+'category/'+toId(category.name)+'" data-target="push"'; + var buf = '
  • '; + + // name + var name = category.name; + if (matchLength) { + name = name.substr(0, matchStart)+''+name.substr(matchStart, matchLength)+''+name.substr(matchStart+matchLength); + } + buf += ''+name+' '; + + // error + if (errorMessage) { + buf += ''+errorMessage+' '; + buf += '
  • '; + return buf; + } + + // category + buf += ''; + buf += ''+category.name+''; + buf += ' '; + + buf += ''; + + return buf; + }; exports.BattleSearch = Search;