|
598
|
1 |
IriSP.Widgets.Tagcloud = function(player, config) { |
|
|
2 |
IriSP.Widgets.Widget.call(this, player, config); |
|
|
3 |
this.stopwords = IriSP._.uniq([].concat(this.custom_stopwords).concat(this.stopword_lists[this.stopword_language])); |
|
|
4 |
} |
|
|
5 |
|
|
|
6 |
IriSP.Widgets.Tagcloud.prototype = new IriSP.Widgets.Widget(); |
|
|
7 |
|
|
|
8 |
IriSP.Widgets.Tagcloud.prototype.template = |
|
|
9 |
'<div class="Ldt-Tagcloud-Container"><ul class="Ldt-Tagcloud-List">' |
|
623
|
10 |
+ '{{#words}}<li class="Ldt-Tagcloud-item Ldt-TraceMe" trace-info="tag:{{word}}" content="{{word}}" style="font-size: {{size}}px">{{word}}</li>{{/words}}' |
|
598
|
11 |
+ '</ul></div>'; |
|
|
12 |
|
|
|
13 |
IriSP.Widgets.Tagcloud.prototype.defaults = { |
|
|
14 |
include_titles: true, |
|
|
15 |
include_descriptions: true, |
|
|
16 |
tag_count: 30, |
|
|
17 |
stopword_language: "fr", |
|
|
18 |
custom_stopwords: [], |
|
|
19 |
exclude_pattern: false, |
|
|
20 |
annotation_type: false, |
|
|
21 |
min_font_size: 10, |
|
|
22 |
max_font_size: 26 |
|
|
23 |
} |
|
|
24 |
|
|
|
25 |
IriSP.Widgets.Tagcloud.prototype.stopword_lists = { |
|
|
26 |
"fr" : [ |
|
|
27 |
'aussi', 'avec', 'aux', 'bien', 'car', 'cette', 'comme', 'dans', 'des', 'donc', 'dont', 'elle', 'encore', 'entre', 'est', |
|
|
28 |
'être', 'eux', 'faire', 'fait', 'http', 'ici', 'ils', 'les', 'leur', 'leurs', 'mais', 'mes', 'même', 'mon', 'notre', |
|
|
29 |
'non', 'nos', 'nous', 'ont', 'par', 'pas', 'peu', 'peut', 'plus', 'pour', 'quand', 'que', 'qui', 'quoi', 'sans', |
|
|
30 |
'ses' ,'son', 'sont', 'sur', 'tes', 'très', 'the', 'ton', 'tous', 'tout', 'une', 'votre', 'vos', 'vous' |
|
|
31 |
], |
|
|
32 |
"en" : [ |
|
|
33 |
'about', 'again', 'are', 'and', 'because', 'being', 'but', 'can', 'done', 'have', 'for', 'from', |
|
|
34 |
'get', 'here', 'http', 'like', 'more', 'one', 'our', 'she', 'that', 'the', 'their', 'then', 'there', |
|
|
35 |
'they', 'this', 'very', 'what', 'when', 'where', 'who', 'why', 'will', 'with', 'www', 'you', 'your' |
|
|
36 |
] |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
IriSP.Widgets.Tagcloud.prototype.draw = function() { |
|
|
40 |
|
|
|
41 |
var _urlRegExp = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g, |
|
|
42 |
_regexpword = /[^\s\.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g, |
|
|
43 |
_words = {}, |
|
|
44 |
_this = this; |
|
|
45 |
this.getWidgetAnnotations().forEach(function(_annotation) { |
|
|
46 |
var _txt = (_this.include_titles ? _annotation.title : '') + ' ' + (_this.include_descriptions ? _annotation.description : ''); |
|
|
47 |
IriSP._(_txt.toLowerCase().replace(_urlRegExp, '').match(_regexpword)).each(function(_word) { |
|
|
48 |
if (IriSP._(_this.stopwords).indexOf(_word) == -1 && (!_this.exclude_pattern || !_this.exclude_pattern.test(_word))) { |
|
|
49 |
_words[_word] = 1 + (_words[_word] || 0); |
|
|
50 |
} |
|
|
51 |
}) |
|
|
52 |
}); |
|
|
53 |
_words = IriSP._(_words) |
|
|
54 |
.chain() |
|
|
55 |
.map(function(_v, _k) { |
|
|
56 |
return { |
|
|
57 |
"word" : _k, |
|
|
58 |
"count" : _v |
|
|
59 |
} |
|
|
60 |
}) |
|
|
61 |
.filter(function(_v) { |
|
|
62 |
return _v.count > 2; |
|
|
63 |
}) |
|
|
64 |
.sortBy(function(_v) { |
|
|
65 |
return - _v.count; |
|
|
66 |
}) |
|
|
67 |
.first(this.tag_count) |
|
|
68 |
.value(); |
|
623
|
69 |
if (!_words.length) { |
|
|
70 |
return; |
|
|
71 |
} |
|
598
|
72 |
var _max = _words[0].count, |
|
|
73 |
_min = Math.min(_words[_words.length - 1].count, _max - 1), |
|
|
74 |
_scale = (this.max_font_size - this.min_font_size) / Math.sqrt(_max - _min); |
|
|
75 |
IriSP._(_words).each(function(_word) { |
|
|
76 |
_word.size = Math.floor( _this.min_font_size + _scale * Math.sqrt(_word.count - _min) ); |
|
|
77 |
}); |
|
|
78 |
this.words = _words; |
|
|
79 |
this.renderTemplate(); |
|
|
80 |
this.$words = this.$.find(".Ldt-Tagcloud-item"); |
|
|
81 |
this.$words.click(function() { |
|
|
82 |
var _txt = IriSP.jQuery(this).attr("content"); |
|
|
83 |
_this.player.popcorn.trigger("IriSP.search.triggeredSearch", _txt); |
|
|
84 |
}); |
|
|
85 |
this.bindPopcorn("IriSP.search", "onSearch"); |
|
|
86 |
this.bindPopcorn("IriSP.search.closed", "onSearch"); |
|
|
87 |
this.bindPopcorn("IriSP.search.cleared", "onSearch"); |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
IriSP.Widgets.Tagcloud.prototype.onSearch = function(searchString) { |
|
|
91 |
searchString = typeof searchString !== "undefined" ? searchString : ''; |
|
|
92 |
if (searchString) { |
|
|
93 |
var _rgxp = IriSP.Model.regexpFromTextOrArray(searchString); |
|
|
94 |
} |
|
|
95 |
this.$words.each(function() { |
|
|
96 |
var _el = IriSP.jQuery(this), |
|
|
97 |
_txt = _el.attr("content"); |
|
|
98 |
if (searchString) { |
|
|
99 |
_el.html(_txt.replace(_rgxp, '<span class="Ldt-Tagcloud-active">$1</span>')); |
|
|
100 |
} else { |
|
|
101 |
_el.html(_txt); |
|
|
102 |
} |
|
|
103 |
}); |
|
|
104 |
} |