diff -r 01fb9167ad75 -r d0da34fda4b4 client/js/wikipedia-bin.js --- a/client/js/wikipedia-bin.js Mon Dec 31 12:19:10 2012 +0100 +++ b/client/js/wikipedia-bin.js Mon Dec 31 13:37:31 2012 +0100 @@ -26,10 +26,10 @@ Rkns.Wikipedia.Bin = Rkns.Utils.inherit(Rkns._BaseBin); Rkns.Wikipedia.Bin.prototype.resultTemplate = Rkns._.template( - '
  • ' - + '

    <%=highlightedtitle%>

    ' - + '

    <%=result.snippet%>

  • ' + '
  • ' + + '

    <%=htitle%>

    ' + + '

    <%=hdescription%>

  • ' ); Rkns.Wikipedia.Bin.prototype._init = function(_renkan, _opts) { @@ -40,23 +40,54 @@ this.refresh(); } +Rkns.Wikipedia.Bin.prototype.render = function(searchstr) { + var _rgxp = new RegExp('('+(searchstr || this.search).replace(/(\W)/g,'\\$1')+')','gi'); + if (searchstr) { + var rxtest = new RegExp(searchstr.replace(/(\W)/g,'\\$1'),'i'); + } + function highlight(_text) { + return _text.replace(_rgxp, "$1"); + } + var _html = "", + _this = this; + Rkns._(this.data.query.search).each(function(_result) { + var title = _result.title, + url = "http://" + _this.lang + ".wikipedia.org/wiki/" + encodeURI(title.replace(/ /g,"_")), + description = Rkns.$('
    ').html(_result.snippet).text(); + if (searchstr && !rxtest.test(title) && !rxtest.test(description)) { + return; + } + count++; + _html += _this.resultTemplate({ + url: url, + title: title, + htitle: highlight(title), + description: description, + hdescription: highlight(description) + }); + }); + _this.main_$.html(_html); + if (searchstr && count) { + this.count_$.text(count).show(); + } else { + this.count_$.hide(); + } + if (searchstr && !count) { + this.$.hide(); + } else { + this.$.show(); + } + _renkan.resizeBins(); +} + Rkns.Wikipedia.Bin.prototype.refresh = function() { var _this = this; Rkns.$.ajax({ url: "http://" + _this.lang + ".wikipedia.org/w/api.php?action=query&list=search&srsearch=" + encodeURIComponent(this.search) + "&format=json", dataType: "jsonp", success: function(_data) { - var _rgxp = new RegExp('('+_this.search.replace(/(\W)/g,'\\$1')+')','gi'), - _html = Rkns._(_data.query.search).map(function(_result) { - return _this.resultTemplate({ - result: _result, - wpurl: "http://" + _this.lang + ".wikipedia.org/wiki/" + encodeURI(_result.title.replace(/ /g,"_")), - highlightedtitle: _result.title.replace(_rgxp, "$1"), - wpdesc: Rkns.$('
    ').html(_result.snippet).text() - }); - }).join(""); - _this.main_$.html(_html); - _renkan.resizeBins(); + _this.data = _data; + _this.render(); } }); } \ No newline at end of file