view wowhead/results.js @ 3:7ac908bdb3d3

Fixed more bugs in wowhead.
author Atul Varma <varmaa@toolness.com>
date Fri, 20 Mar 2009 00:32:33 -0700
parents abd59cfa1e70
children
line wrap: on
line source

var typeRegexp = /.*\(([A-Za-z]+)\).*/;

function showResults(data) {
  var query = data[0];
  var names = data[1];
  if (!query) {
    $("#results").html("Searches Wowhead.");
    return;
  }
  if (!names.length) {
    $("#results").html("No results found.");
    return;
  }
  var meta = data[7];
  var list = $("<ul></ul>");
  for (var i = 0; i < names.length; i++) {
    var name = names[i];
    var item_meta = meta[i];
    var match = name.match(typeRegexp);
    if (!match) {
      list.append($("<li></li>").text(name));
      continue;
    }
    var type = match[1].toLowerCase();
    if (type == "spell" && item_meta[3])
      name = name.replace("(Spell)", "(" + item_meta[3] + ")");
    var id = item_meta[1];
    var css = "list-style: none;";
    if (typeof(item_meta[2]) == "string") {
      var iconName = item_meta[2].toLowerCase();
      css = ('list-style-image: url(' +
             'http://static.wowhead.com/images/icons/small/' +
             iconName + '.jpg);');
    }
    var linkClass = '';
    if (item_meta[3] && typeof(item_meta[3]) == "number")
      linkClass = 'q' + item_meta[3];
    var link = $('<a class="' + linkClass + '" ' +
                 'href="http://www.wowhead.com/?' +
                 type + '=' + id + '"></a>');
    link.text(name);
    list.append(link);
    link.wrap('<li style="' + css + '"></li>');
  }
  $("#results").html(list);
}

SAMPLE_RESULTS = ["sword", ["Bastard Sword (Item)", "Chromatic Sword (Item)", "Commoner's Sword (Item)", "Crude Bastard Sword (Item)", "Feeble Sword (Item)", "Long Bastard Sword (Item)", "Shadowhide Two-handed Sword (Item)", "Sword of Decay (Item)", "Sword of the Night Sky (Item)", "Well-used Sword (Item)"], [], [], [], [], [], [[3, 1194, "INV_Sword_06", 1], [3, 1604, "INV_Sword_22", 2], [3, 1511, "INV_Sword_04", 0], [3, 1412, "INV_Sword_06", 0], [3, 1413, "INV_Sword_04", 0], [3, 1830, "INV_Sword_06", 0], [3, 1460, "INV_Sword_46", 2], [3, 1727, "INV_Sword_26", 3], [3, 2035, "INV_Sword_20", 2], [3, 1008, "INV_Sword_04", 1]]];

//$(window).ready(function() { showResults(SAMPLE_RESULTS); });