| author | veltr |
| Mon, 05 Nov 2012 19:02:48 +0100 | |
| branch | platform-restapi |
| changeset 976 | 4b9ec475026a |
| parent 965 | eadb7290c325 |
| child 983 | 97fef7a4b189 |
| permissions | -rw-r--r-- |
| 880 | 1 |
IriSP.Widgets.Tagcloud = function(player, config) { |
2 |
IriSP.Widgets.Widget.call(this, player, config); |
|
| 882 | 3 |
this.stopwords = IriSP._.uniq([].concat(this.custom_stopwords).concat(this.stopword_lists[this.stopword_language])); |
| 880 | 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">' |
|
| 906 | 10 |
+ '{{#words}}<li class="Ldt-Tagcloud-item Ldt-TraceMe" trace-info="tag:{{word}}" content="{{word}}" style="font-size: {{size}}px">{{word}}</li>{{/words}}' |
| 880 | 11 |
+ '</ul></div>'; |
12 |
||
13 |
IriSP.Widgets.Tagcloud.prototype.defaults = { |
|
14 |
include_titles: true, |
|
15 |
include_descriptions: true, |
|
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
16 |
include_tag_texts: true, |
| 880 | 17 |
tag_count: 30, |
18 |
stopword_language: "fr", |
|
19 |
custom_stopwords: [], |
|
20 |
exclude_pattern: false, |
|
21 |
annotation_type: false, |
|
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
22 |
segment_annotation_type: false, |
| 880 | 23 |
min_font_size: 10, |
24 |
max_font_size: 26 |
|
25 |
} |
|
26 |
||
27 |
IriSP.Widgets.Tagcloud.prototype.stopword_lists = { |
|
28 |
"fr" : [ |
|
29 |
'aussi', 'avec', 'aux', 'bien', 'car', 'cette', 'comme', 'dans', 'des', 'donc', 'dont', 'elle', 'encore', 'entre', 'est', |
|
30 |
'être', 'eux', 'faire', 'fait', 'http', 'ici', 'ils', 'les', 'leur', 'leurs', 'mais', 'mes', 'même', 'mon', 'notre', |
|
31 |
'non', 'nos', 'nous', 'ont', 'par', 'pas', 'peu', 'peut', 'plus', 'pour', 'quand', 'que', 'qui', 'quoi', 'sans', |
|
32 |
'ses' ,'son', 'sont', 'sur', 'tes', 'très', 'the', 'ton', 'tous', 'tout', 'une', 'votre', 'vos', 'vous' |
|
33 |
], |
|
34 |
"en" : [ |
|
35 |
'about', 'again', 'are', 'and', 'because', 'being', 'but', 'can', 'done', 'have', 'for', 'from', |
|
36 |
'get', 'here', 'http', 'like', 'more', 'one', 'our', 'she', 'that', 'the', 'their', 'then', 'there', |
|
37 |
'they', 'this', 'very', 'what', 'when', 'where', 'who', 'why', 'will', 'with', 'www', 'you', 'your' |
|
38 |
] |
|
39 |
} |
|
40 |
||
41 |
IriSP.Widgets.Tagcloud.prototype.draw = function() { |
|
| 957 | 42 |
this.onMdpEvent("search", "onSearch"); |
43 |
this.onMdpEvent("search.closed", "onSearch"); |
|
44 |
this.onMdpEvent("search.cleared", "onSearch"); |
|
| 880 | 45 |
|
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
46 |
if (this.segment_annotation_type) { |
| 965 | 47 |
var _this = this; |
48 |
this.source.getAnnotationsByTypeTitle(this.segment_annotation_type).forEach(function(_a) { |
|
49 |
_a.on("enter", function() { |
|
50 |
_this.redraw(_a.begin, _a.end); |
|
51 |
}) |
|
52 |
}); |
|
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
53 |
} else { |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
54 |
this.redraw(); |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
55 |
} |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
56 |
} |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
57 |
|
| 965 | 58 |
IriSP.Widgets.Tagcloud.prototype.redraw = function(_from, _to) { |
| 880 | 59 |
var _urlRegExp = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g, |
60 |
_regexpword = /[^\s\.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g, |
|
61 |
_words = {}, |
|
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
62 |
_this = this, |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
63 |
_annotations = this.getWidgetAnnotations(); |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
64 |
|
| 965 | 65 |
if (typeof _from !== "undefined" && typeof _to !== "undefined") { |
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
66 |
_annotations = _annotations.filter(function(_annotation) { |
| 965 | 67 |
return _annotation.begin >= _from && _annotation.end <= _to; |
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
68 |
}) |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
69 |
} |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
70 |
|
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
71 |
_annotations.forEach(function(_annotation) { |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
72 |
var _txt = |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
73 |
(_this.include_titles ? _annotation.title : '') |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
74 |
+ ' ' |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
75 |
+ (_this.include_descriptions ? _annotation.description : '') |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
76 |
+ ' ' |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
77 |
+ (_this.include_tag_texts ? _annotation.getTagTexts() : ''); |
| 880 | 78 |
IriSP._(_txt.toLowerCase().replace(_urlRegExp, '').match(_regexpword)).each(function(_word) { |
79 |
if (IriSP._(_this.stopwords).indexOf(_word) == -1 && (!_this.exclude_pattern || !_this.exclude_pattern.test(_word))) { |
|
80 |
_words[_word] = 1 + (_words[_word] || 0); |
|
81 |
} |
|
82 |
}) |
|
83 |
}); |
|
84 |
_words = IriSP._(_words) |
|
85 |
.chain() |
|
86 |
.map(function(_v, _k) { |
|
87 |
return { |
|
88 |
"word" : _k, |
|
89 |
"count" : _v |
|
90 |
} |
|
91 |
}) |
|
92 |
.filter(function(_v) { |
|
93 |
return _v.count > 2; |
|
94 |
}) |
|
95 |
.sortBy(function(_v) { |
|
96 |
return - _v.count; |
|
97 |
}) |
|
98 |
.first(this.tag_count) |
|
99 |
.value(); |
|
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
100 |
if (_words.length) { |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
101 |
var _max = _words[0].count, |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
102 |
_min = Math.min(_words[_words.length - 1].count, _max - 1), |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
103 |
_scale = (this.max_font_size - this.min_font_size) / Math.sqrt(_max - _min); |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
104 |
IriSP._(_words).each(function(_word) { |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
105 |
_word.size = Math.floor( _this.min_font_size + _scale * Math.sqrt(_word.count - _min) ); |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
106 |
}); |
| 883 | 107 |
} |
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
108 |
this.$.html(Mustache.to_html(this.template, {words: _words })); |
|
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
109 |
this.$.find(".Ldt-Tagcloud-item").click(function() { |
| 880 | 110 |
var _txt = IriSP.jQuery(this).attr("content"); |
| 957 | 111 |
_this.player.trigger("search.triggeredSearch", _txt); |
| 880 | 112 |
}); |
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
113 |
|
| 880 | 114 |
} |
115 |
||
116 |
IriSP.Widgets.Tagcloud.prototype.onSearch = function(searchString) { |
|
117 |
searchString = typeof searchString !== "undefined" ? searchString : ''; |
|
118 |
if (searchString) { |
|
119 |
var _rgxp = IriSP.Model.regexpFromTextOrArray(searchString); |
|
120 |
} |
|
|
908
f56199193fad
CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents:
906
diff
changeset
|
121 |
this.$.find(".Ldt-Tagcloud-item").each(function() { |
| 880 | 122 |
var _el = IriSP.jQuery(this), |
123 |
_txt = _el.attr("content"); |
|
124 |
if (searchString) { |
|
125 |
_el.html(_txt.replace(_rgxp, '<span class="Ldt-Tagcloud-active">$1</span>')); |
|
126 |
} else { |
|
127 |
_el.html(_txt); |
|
128 |
} |
|
129 |
}); |
|
130 |
} |