Add site-wide search function
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
###
|
||||
Crafting Guide - item_selector.coffee
|
||||
|
||||
Copyright (c) 2015 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
BaseModel = require './base_model'
|
||||
ItemSlug = require './item_slug'
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
module.exports = class ItemSelector extends BaseModel
|
||||
|
||||
constructor: (attributes={}, options={})->
|
||||
if not options.modPack? then throw new Error 'options.modPack is required'
|
||||
|
||||
super attributes, options
|
||||
|
||||
@_maxResults = 100
|
||||
@_minHintLength = 3
|
||||
@_modPack = options.modPack
|
||||
@_results = []
|
||||
|
||||
# Property Methods #############################################################################
|
||||
|
||||
getHint: ->
|
||||
return @_hint
|
||||
|
||||
setHint: (newHint)->
|
||||
oldHint = @_hint
|
||||
return if newHint is oldHint
|
||||
|
||||
@_hint = newHint.toLowerCase()
|
||||
|
||||
@trigger Event.change + ':hint', this, oldHint, newHint
|
||||
@_refreshResults()
|
||||
@trigger Event.change, this
|
||||
|
||||
getResults: ->
|
||||
return @_results
|
||||
|
||||
Object.defineProperties @prototype,
|
||||
hint: {get:@prototype.getHint, set:@prototype.setHint}
|
||||
results: {get:@prototype.getResults}
|
||||
|
||||
# Private Methods ##############################################################################
|
||||
|
||||
_isMatch: (name)->
|
||||
hintIndex = 0
|
||||
hintLetter = @_hint[hintIndex]
|
||||
name = name.toLowerCase()
|
||||
|
||||
for nameIndex in [0...name.length] by 1
|
||||
if name.charAt(nameIndex) is hintLetter
|
||||
hintIndex += 1
|
||||
return true if hintIndex is @_hint.length
|
||||
hintLetter = @_hint[hintIndex]
|
||||
|
||||
return false
|
||||
|
||||
_refreshResults: ->
|
||||
oldResults = @_results
|
||||
newResults = {}
|
||||
count = 0
|
||||
|
||||
logger.verbose => "Looking for items which match: #{@_hint}"
|
||||
if @_hint.length >= @_minHintLength
|
||||
@_modPack.eachMod (mod)=>
|
||||
return if count >= @_maxResults
|
||||
|
||||
mod.eachName (name, itemSlug)=>
|
||||
return if count >= @_maxResults
|
||||
|
||||
if @_isMatch name
|
||||
if itemSlug.isQualified
|
||||
newResults[itemSlug] = itemSlug
|
||||
count += 1
|
||||
|
||||
newResults = _.values(newResults)
|
||||
newResults.sort ItemSlug.compare
|
||||
|
||||
@_results = newResults
|
||||
@trigger Event.change + ':results', this, oldResults, newResults
|
||||
@@ -25,20 +25,10 @@ module.exports = class ItemSlug
|
||||
# Class Methods ################################################################################
|
||||
|
||||
@compare: (a, b)->
|
||||
aIsRequired = a.mod in RequiredMods
|
||||
bIsRequired = b.mod in RequiredMods
|
||||
|
||||
if aIsRequired isnt bIsRequired
|
||||
return -1 if aIsRequired
|
||||
return +1 if bIsRequired
|
||||
else if a.isQualified isnt b.isQualified
|
||||
return -1 if a.isQualified
|
||||
return +1 if b.isQualified
|
||||
else if a.mod isnt b.mod
|
||||
return if a.mod < b.mod then -1 else +1
|
||||
else if a.item isnt b.item
|
||||
if a.item isnt b.item
|
||||
return if a.item < b.item then -1 else +1
|
||||
|
||||
if a.mod isnt b.mod
|
||||
return if a.mod < b.mod then -1 else +1
|
||||
return 0
|
||||
|
||||
@equal: (a, b)->
|
||||
|
||||
@@ -53,22 +53,23 @@ module.exports = class ModPack extends BaseModel
|
||||
findItemDisplay: (itemSlug)->
|
||||
if not itemSlug? then throw new Error 'itemSlug is required'
|
||||
|
||||
result = {}
|
||||
result = {slug:itemSlug}
|
||||
item = @findItem itemSlug, includeDisabled:true
|
||||
if item?
|
||||
result.itemName = item.name
|
||||
result.itemSlug = item.slug.item
|
||||
result.modSlug = item.slug.mod
|
||||
result.modVersion = item.modVersion.version
|
||||
result.itemSlug = item.slug.item
|
||||
result.itemName = item.name
|
||||
else
|
||||
result.itemName = @findName itemSlug, includeDisabled:true
|
||||
result.itemSlug = itemSlug.item
|
||||
result.modSlug = @_mods[0].slug
|
||||
result.modVersion = @_mods[0].activeVersion
|
||||
result.itemSlug = itemSlug.item
|
||||
result.itemName = @findName itemSlug, includeDisabled:true
|
||||
|
||||
result.craftingUrl = Url.crafting inventoryText:itemSlug.item
|
||||
result.iconUrl = Url.itemIcon result
|
||||
result.itemUrl = Url.item result
|
||||
result.modName = @getMod(result.modSlug).name
|
||||
return result
|
||||
|
||||
findName: (slug, options={})->
|
||||
|
||||
Reference in New Issue
Block a user