src/widgets/Tagcloud.js
changeset 944 8a6c9e3d0158
parent 908 f56199193fad
child 957 4da0a5740b6c
child 1019 3ab36f402b0c
equal deleted inserted replaced
907:27b248a13355 944:8a6c9e3d0158
       
     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">'
       
    10     + '{{#words}}<li class="Ldt-Tagcloud-item Ldt-TraceMe" trace-info="tag:{{word}}" content="{{word}}" style="font-size: {{size}}px">{{word}}</li>{{/words}}'
       
    11     + '</ul></div>';
       
    12 
       
    13 IriSP.Widgets.Tagcloud.prototype.defaults = {
       
    14     include_titles: true,
       
    15     include_descriptions: true,
       
    16     include_tag_texts: true,
       
    17     tag_count: 30,
       
    18     stopword_language: "fr",
       
    19     custom_stopwords: [],
       
    20     exclude_pattern: false,
       
    21     annotation_type: false,
       
    22     segment_annotation_type: false,
       
    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() {
       
    42     this.bindPopcorn("IriSP.search", "onSearch");
       
    43     this.bindPopcorn("IriSP.search.closed", "onSearch");
       
    44     this.bindPopcorn("IriSP.search.cleared", "onSearch");
       
    45     
       
    46     if (this.segment_annotation_type) {
       
    47         this.bindPopcorn("timeupdate","onTimeupdate");
       
    48     } else {
       
    49         this.redraw();
       
    50     }
       
    51 }
       
    52 
       
    53 IriSP.Widgets.Tagcloud.prototype.onTimeupdate = function() {
       
    54     var _time = Math.floor(this.player.popcorn.currentTime() * 1000),
       
    55         _list = this.source.getAnnotationsByTypeTitle(this.segment_annotation_type).filter(function(_annotation) {
       
    56             return _annotation.begin <= _time && _annotation.end > _time;
       
    57         });
       
    58     if (_list.length) {
       
    59         if (_list[0].begin !== this.begin_time || _list[0].end !== this.end_time) {
       
    60             this.begin_time = _list[0].begin;
       
    61             this.end_time = _list[0].end;
       
    62             this.redraw();
       
    63         }
       
    64     }
       
    65 }
       
    66 
       
    67 IriSP.Widgets.Tagcloud.prototype.redraw = function() {
       
    68     var _urlRegExp = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g,
       
    69         _regexpword = /[^\s\.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g,
       
    70         _words = {},
       
    71         _this = this,
       
    72         _annotations = this.getWidgetAnnotations();
       
    73         
       
    74     if (typeof this.begin_time !== "undefined" && typeof this.end_time !== "undefined") {
       
    75         _annotations = _annotations.filter(function(_annotation) {
       
    76             return _annotation.begin >= _this.begin_time && _annotation.end <= _this.end_time;
       
    77         })
       
    78     }
       
    79     
       
    80     _annotations.forEach(function(_annotation) {
       
    81        var _txt =
       
    82             (_this.include_titles ? _annotation.title : '')
       
    83             + ' '
       
    84             + (_this.include_descriptions ? _annotation.description : '')
       
    85             + ' '
       
    86             + (_this.include_tag_texts ? _annotation.getTagTexts() : '');
       
    87        IriSP._(_txt.toLowerCase().replace(_urlRegExp, '').match(_regexpword)).each(function(_word) {
       
    88            if (IriSP._(_this.stopwords).indexOf(_word) == -1 && (!_this.exclude_pattern || !_this.exclude_pattern.test(_word))) {
       
    89                _words[_word] = 1 + (_words[_word] || 0);
       
    90            }
       
    91        })
       
    92     });
       
    93     _words = IriSP._(_words)
       
    94         .chain()
       
    95         .map(function(_v, _k) {
       
    96             return {
       
    97                 "word" : _k,
       
    98                 "count" : _v
       
    99             }
       
   100         })
       
   101         .filter(function(_v) {
       
   102             return _v.count > 2;
       
   103         })
       
   104         .sortBy(function(_v) {
       
   105             return - _v.count;
       
   106         })
       
   107         .first(this.tag_count)
       
   108         .value();
       
   109     if (_words.length) {
       
   110         var _max = _words[0].count,
       
   111             _min = Math.min(_words[_words.length - 1].count, _max - 1),
       
   112             _scale = (this.max_font_size - this.min_font_size) / Math.sqrt(_max - _min);
       
   113         IriSP._(_words).each(function(_word) {
       
   114                 _word.size = Math.floor( _this.min_font_size + _scale * Math.sqrt(_word.count - _min) );
       
   115             });
       
   116     }
       
   117     this.$.html(Mustache.to_html(this.template,  {words: _words }));
       
   118     this.$.find(".Ldt-Tagcloud-item").click(function() {
       
   119         var _txt = IriSP.jQuery(this).attr("content");
       
   120         _this.player.popcorn.trigger("IriSP.search.triggeredSearch", _txt);
       
   121     });
       
   122     
       
   123 }
       
   124 
       
   125 IriSP.Widgets.Tagcloud.prototype.onSearch = function(searchString) {
       
   126     searchString = typeof searchString !== "undefined" ? searchString : '';
       
   127     if (searchString) {
       
   128         var _rgxp = IriSP.Model.regexpFromTextOrArray(searchString);
       
   129     }
       
   130     this.$.find(".Ldt-Tagcloud-item").each(function() {
       
   131         var _el = IriSP.jQuery(this),
       
   132             _txt = _el.attr("content");
       
   133         if (searchString) {
       
   134             _el.html(_txt.replace(_rgxp, '<span class="Ldt-Tagcloud-active">$1</span>'));
       
   135         } else {
       
   136             _el.html(_txt);
       
   137         }
       
   138     });
       
   139 }