|
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() { |
|
66
|
14 |
var langs = { |
|
|
15 |
"fr": "French", |
|
|
16 |
"en": "English", |
|
|
17 |
"ja": "Japanese" |
|
|
18 |
} |
|
|
19 |
if (langs[this.lang]) { |
|
|
20 |
return this.renkan.translate("Wikipedia in ") + this.renkan.translate(langs[this.lang]); |
|
|
21 |
} else { |
|
|
22 |
return this.renkan.translate("Wikipedia") + " [" + this.lang + "]"; |
|
|
23 |
} |
|
34
|
24 |
} |
|
|
25 |
|
|
|
26 |
Rkns.Wikipedia.Search.prototype.search = function(_q) { |
|
|
27 |
this.renkan.tabs.push( |
|
|
28 |
new Rkns.Wikipedia.Bin(this.renkan, { |
|
|
29 |
lang: this.lang, |
|
|
30 |
search: _q |
|
|
31 |
}) |
|
|
32 |
); |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
Rkns.Wikipedia.Bin = Rkns.Utils.inherit(Rkns._BaseBin); |
|
|
36 |
|
|
|
37 |
Rkns.Wikipedia.Bin.prototype.resultTemplate = Rkns._.template( |
|
68
|
38 |
'<li class="Rk-Wikipedia-Result Rk-Bin-Item" draggable="true" data-uri="<%-url%>" ' |
|
56
|
39 |
+ 'data-title="Wikipedia: <%-title%>" data-description="<%-description%>" data-image="<%-static_url%>img/wikipedia.png">' |
|
|
40 |
+ '<img class="Rk-Wikipedia-Icon" src="<%-static_url%>img/wikipedia.png"></div><h4 class="Rk-Wikipedia-Title"><a href="<%-url%>" target="_blank"><%=htitle%></a></h4>' |
|
44
|
41 |
+ '<p class="Rk-Wikipedia-Snippet"><%=hdescription%></p></li>' |
|
24
|
42 |
); |
|
|
43 |
|
|
34
|
44 |
Rkns.Wikipedia.Bin.prototype._init = function(_renkan, _opts) { |
|
56
|
45 |
this.renkan = _renkan; |
|
24
|
46 |
this.search = _opts.search; |
|
|
47 |
this.lang = _opts.lang || "en"; |
|
34
|
48 |
this.title_icon_$.addClass('Rk-Wikipedia-Title-Icon Rk-Wikipedia-Lang-' + this.lang); |
|
|
49 |
this.title_$.html(this.search).addClass("Rk-Wikipedia-Title"); |
|
42
|
50 |
this.refresh(); |
|
|
51 |
} |
|
|
52 |
|
|
44
|
53 |
Rkns.Wikipedia.Bin.prototype.render = function(searchstr) { |
|
|
54 |
var _rgxp = new RegExp('('+(searchstr || this.search).replace(/(\W)/g,'\\$1')+')','gi'); |
|
|
55 |
if (searchstr) { |
|
|
56 |
var rxtest = new RegExp(searchstr.replace(/(\W)/g,'\\$1'),'i'); |
|
|
57 |
} |
|
|
58 |
function highlight(_text) { |
|
|
59 |
return _text.replace(_rgxp, "<span class='searchmatch'>$1</span>"); |
|
|
60 |
} |
|
|
61 |
var _html = "", |
|
52
|
62 |
_this = this, |
|
|
63 |
count = 0; |
|
44
|
64 |
Rkns._(this.data.query.search).each(function(_result) { |
|
|
65 |
var title = _result.title, |
|
|
66 |
url = "http://" + _this.lang + ".wikipedia.org/wiki/" + encodeURI(title.replace(/ /g,"_")), |
|
|
67 |
description = Rkns.$('<div>').html(_result.snippet).text(); |
|
|
68 |
if (searchstr && !rxtest.test(title) && !rxtest.test(description)) { |
|
|
69 |
return; |
|
|
70 |
} |
|
|
71 |
count++; |
|
|
72 |
_html += _this.resultTemplate({ |
|
|
73 |
url: url, |
|
|
74 |
title: title, |
|
|
75 |
htitle: highlight(title), |
|
|
76 |
description: description, |
|
56
|
77 |
hdescription: highlight(description), |
|
|
78 |
static_url: _this.renkan.static_url |
|
44
|
79 |
}); |
|
|
80 |
}); |
|
|
81 |
_this.main_$.html(_html); |
|
|
82 |
if (searchstr && count) { |
|
|
83 |
this.count_$.text(count).show(); |
|
|
84 |
} else { |
|
|
85 |
this.count_$.hide(); |
|
|
86 |
} |
|
|
87 |
if (searchstr && !count) { |
|
|
88 |
this.$.hide(); |
|
|
89 |
} else { |
|
|
90 |
this.$.show(); |
|
|
91 |
} |
|
56
|
92 |
this.renkan.resizeBins(); |
|
44
|
93 |
} |
|
|
94 |
|
|
42
|
95 |
Rkns.Wikipedia.Bin.prototype.refresh = function() { |
|
24
|
96 |
var _this = this; |
|
42
|
97 |
Rkns.$.ajax({ |
|
|
98 |
url: "http://" + _this.lang + ".wikipedia.org/w/api.php?action=query&list=search&srsearch=" + encodeURIComponent(this.search) + "&format=json", |
|
|
99 |
dataType: "jsonp", |
|
|
100 |
success: function(_data) { |
|
44
|
101 |
_this.data = _data; |
|
|
102 |
_this.render(); |
|
24
|
103 |
} |
|
42
|
104 |
}); |
|
24
|
105 |
} |