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