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