client/js/wikipedia-bin.js
changeset 49 d0da34fda4b4
parent 44 869410bab434
child 52 e0f6f3c31150
equal deleted inserted replaced
48:01fb9167ad75 49:d0da34fda4b4
    24 }
    24 }
    25 
    25 
    26 Rkns.Wikipedia.Bin = Rkns.Utils.inherit(Rkns._BaseBin);
    26 Rkns.Wikipedia.Bin = Rkns.Utils.inherit(Rkns._BaseBin);
    27 
    27 
    28 Rkns.Wikipedia.Bin.prototype.resultTemplate = Rkns._.template(
    28 Rkns.Wikipedia.Bin.prototype.resultTemplate = Rkns._.template(
    29     '<li class="Rk-Wikipedia-Result Rk-Bin-Item" data-uri="<%=wpurl%>" '
    29     '<li class="Rk-Wikipedia-Result Rk-Bin-Item" data-uri="<%-url%>" '
    30     + 'data-title="Wikipedia: <%=result.title%>" data-description="<%=wpdesc%>" data-image="img/wikipedia.png">'
    30     + 'data-title="Wikipedia: <%-title%>" data-description="<%-description%>" data-image="img/wikipedia.png">'
    31     + '<img class="Rk-Wikipedia-Icon" src="img/wikipedia.png"></div><h4 class="Rk-Wikipedia-Title"><a href="<%=wpurl%>" target="_blank"><%=highlightedtitle%></a></h4>'
    31     + '<img class="Rk-Wikipedia-Icon" src="img/wikipedia.png"></div><h4 class="Rk-Wikipedia-Title"><a href="<%-url%>" target="_blank"><%=htitle%></a></h4>'
    32     + '<p class="Rk-Wikipedia-Snippet"><%=result.snippet%></p></li>'
    32     + '<p class="Rk-Wikipedia-Snippet"><%=hdescription%></p></li>'
    33 );
    33 );
    34 
    34 
    35 Rkns.Wikipedia.Bin.prototype._init = function(_renkan, _opts) {
    35 Rkns.Wikipedia.Bin.prototype._init = function(_renkan, _opts) {
    36     this.search = _opts.search;
    36     this.search = _opts.search;
    37     this.lang = _opts.lang || "en";
    37     this.lang = _opts.lang || "en";
    38     this.title_icon_$.addClass('Rk-Wikipedia-Title-Icon Rk-Wikipedia-Lang-' + this.lang);
    38     this.title_icon_$.addClass('Rk-Wikipedia-Title-Icon Rk-Wikipedia-Lang-' + this.lang);
    39     this.title_$.html(this.search).addClass("Rk-Wikipedia-Title");
    39     this.title_$.html(this.search).addClass("Rk-Wikipedia-Title");
    40     this.refresh();
    40     this.refresh();
    41 }
    41 }
    42 
    42 
       
    43 Rkns.Wikipedia.Bin.prototype.render = function(searchstr) {
       
    44     var _rgxp = new RegExp('('+(searchstr || this.search).replace(/(\W)/g,'\\$1')+')','gi');
       
    45     if (searchstr) {
       
    46         var rxtest = new RegExp(searchstr.replace(/(\W)/g,'\\$1'),'i');
       
    47     }
       
    48     function highlight(_text) {
       
    49         return _text.replace(_rgxp, "<span class='searchmatch'>$1</span>");
       
    50     }
       
    51     var _html = "",
       
    52         _this = this;
       
    53     Rkns._(this.data.query.search).each(function(_result) {
       
    54         var title = _result.title,
       
    55             url = "http://" + _this.lang + ".wikipedia.org/wiki/" + encodeURI(title.replace(/ /g,"_")),
       
    56             description = Rkns.$('<div>').html(_result.snippet).text();
       
    57         if (searchstr && !rxtest.test(title) && !rxtest.test(description)) {
       
    58             return;
       
    59         }
       
    60         count++;
       
    61         _html += _this.resultTemplate({
       
    62             url: url,
       
    63             title: title,
       
    64             htitle: highlight(title),
       
    65             description: description,
       
    66             hdescription: highlight(description)
       
    67         });
       
    68     });
       
    69     _this.main_$.html(_html);
       
    70     if (searchstr && count) {
       
    71         this.count_$.text(count).show();
       
    72     } else {
       
    73         this.count_$.hide();
       
    74     }
       
    75     if (searchstr && !count) {
       
    76         this.$.hide();
       
    77     } else {
       
    78         this.$.show();
       
    79     }
       
    80     _renkan.resizeBins();
       
    81 }
       
    82     
    43 Rkns.Wikipedia.Bin.prototype.refresh = function() {
    83 Rkns.Wikipedia.Bin.prototype.refresh = function() {
    44     var _this = this;
    84     var _this = this;
    45     Rkns.$.ajax({
    85     Rkns.$.ajax({
    46         url: "http://" + _this.lang + ".wikipedia.org/w/api.php?action=query&list=search&srsearch=" + encodeURIComponent(this.search) + "&format=json",
    86         url: "http://" + _this.lang + ".wikipedia.org/w/api.php?action=query&list=search&srsearch=" + encodeURIComponent(this.search) + "&format=json",
    47         dataType: "jsonp",
    87         dataType: "jsonp",
    48         success: function(_data) {
    88         success: function(_data) {
    49             var _rgxp = new RegExp('('+_this.search.replace(/(\W)/g,'\\$1')+')','gi'),
    89             _this.data = _data;
    50                 _html = Rkns._(_data.query.search).map(function(_result) {
    90             _this.render();
    51                 return _this.resultTemplate({
       
    52                     result: _result,
       
    53                     wpurl: "http://" + _this.lang + ".wikipedia.org/wiki/" + encodeURI(_result.title.replace(/ /g,"_")),
       
    54                     highlightedtitle: _result.title.replace(_rgxp, "<span class='searchmatch'>$1</span>"),
       
    55                     wpdesc: Rkns.$('<div>').html(_result.snippet).text()
       
    56                 });
       
    57             }).join("");
       
    58             _this.main_$.html(_html);
       
    59             _renkan.resizeBins();
       
    60         }
    91         }
    61     });
    92     });
    62 }
    93 }