| author | veltr |
| Fri, 21 Dec 2012 16:22:08 +0100 | |
| changeset 44 | 869410bab434 |
| parent 42 | 48d825187d67 |
| child 52 | e0f6f3c31150 |
| permissions | -rw-r--r-- |
| 34 | 1 |
Rkns.Wikipedia = { |
2 |
} |
|
3 |
||
4 |
Rkns.Wikipedia.Search = function(_renkan, _opts) { |
|
5 |
this.renkan = _renkan; |
|
6 |
this.lang = _opts.lang || "en"; |
|
7 |
} |
|
| 24 | 8 |
|
| 34 | 9 |
Rkns.Wikipedia.Search.prototype.getBgClass = function() { |
10 |
return "Rk-Wikipedia-Search-Icon Rk-Wikipedia-Lang-" + this.lang; |
|
11 |
} |
|
12 |
||
13 |
Rkns.Wikipedia.Search.prototype.getSearchTitle = function() { |
|
|
36
d249d36ecc37
Add Edge button, French translation and various bugfixes
veltr
parents:
34
diff
changeset
|
14 |
return ( this.renkan.l10n["wiki_" + this.lang] || this.renkan.l10n.wiki_) ; |
| 34 | 15 |
} |
16 |
||
17 |
Rkns.Wikipedia.Search.prototype.search = function(_q) { |
|
18 |
this.renkan.tabs.push( |
|
19 |
new Rkns.Wikipedia.Bin(this.renkan, { |
|
20 |
lang: this.lang, |
|
21 |
search: _q |
|
22 |
}) |
|
23 |
); |
|
24 |
} |
|
25 |
||
26 |
Rkns.Wikipedia.Bin = Rkns.Utils.inherit(Rkns._BaseBin); |
|
27 |
||
28 |
Rkns.Wikipedia.Bin.prototype.resultTemplate = Rkns._.template( |
|
| 44 | 29 |
'<li class="Rk-Wikipedia-Result Rk-Bin-Item" data-uri="<%-url%>" ' |
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="<%-url%>" target="_blank"><%=htitle%></a></h4>' |
|
32 |
+ '<p class="Rk-Wikipedia-Snippet"><%=hdescription%></p></li>' |
|
| 24 | 33 |
); |
34 |
||
| 34 | 35 |
Rkns.Wikipedia.Bin.prototype._init = function(_renkan, _opts) { |
| 24 | 36 |
this.search = _opts.search; |
37 |
this.lang = _opts.lang || "en"; |
|
| 34 | 38 |
this.title_icon_$.addClass('Rk-Wikipedia-Title-Icon Rk-Wikipedia-Lang-' + this.lang); |
39 |
this.title_$.html(this.search).addClass("Rk-Wikipedia-Title"); |
|
| 42 | 40 |
this.refresh(); |
41 |
} |
|
42 |
||
| 44 | 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 |
|
|
| 42 | 83 |
Rkns.Wikipedia.Bin.prototype.refresh = function() { |
| 24 | 84 |
var _this = this; |
| 42 | 85 |
Rkns.$.ajax({ |
86 |
url: "http://" + _this.lang + ".wikipedia.org/w/api.php?action=query&list=search&srsearch=" + encodeURIComponent(this.search) + "&format=json", |
|
87 |
dataType: "jsonp", |
|
88 |
success: function(_data) { |
|
| 44 | 89 |
_this.data = _data; |
90 |
_this.render(); |
|
| 24 | 91 |
} |
| 42 | 92 |
}); |
| 24 | 93 |
} |