src/widgets/Tagcloud.js
branchnew-model
changeset 1020 198c2b79f5e1
parent 1019 3ab36f402b0c
equal deleted inserted replaced
1019:3ab36f402b0c 1020:198c2b79f5e1
     1 IriSP.Widgets.Tagcloud = function(player, config) {
     1 IriSP.Widgets.Tagcloud = function(player, config) {
     2     IriSP.Widgets.Widget.call(this, 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]));
     3     this.stopwords = IriSP._.uniq([].concat(this.custom_stopwords).concat(this.stopword_lists[this.stopword_language]));
     4 };
     4 }
     5 
     5 
     6 IriSP.Widgets.Tagcloud.prototype = new IriSP.Widgets.Widget();
     6 IriSP.Widgets.Tagcloud.prototype = new IriSP.Widgets.Widget();
     7 
     7 
     8 IriSP.Widgets.Tagcloud.prototype.template =
     8 IriSP.Widgets.Tagcloud.prototype.template =
     9     '<div class="Ldt-Tagcloud-Container"><ul class="Ldt-Tagcloud-List">'
     9     '<div class="Ldt-Tagcloud-Container"><ul class="Ldt-Tagcloud-List">'
    20     exclude_pattern: false,
    20     exclude_pattern: false,
    21     annotation_type: false,
    21     annotation_type: false,
    22     segment_annotation_type: false,
    22     segment_annotation_type: false,
    23     min_font_size: 10,
    23     min_font_size: 10,
    24     max_font_size: 26
    24     max_font_size: 26
    25 };
    25 }
    26 
    26 
    27 IriSP.Widgets.Tagcloud.prototype.stopword_lists = {
    27 IriSP.Widgets.Tagcloud.prototype.stopword_lists = {
    28     "fr" : [
    28     "fr" : [
    29         'aussi', 'avec', 'aux', 'bien', 'car', 'cette', 'comme', 'dans', 'des', 'donc', 'dont', 'elle', 'encore', 'entre', 'est',
    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',
    30         'être', 'eux', 'faire', 'fait', 'http', 'ici', 'ils', 'les', 'leur', 'leurs', 'mais', 'mes', 'même', 'mon', 'notre',
    34     "en" : [
    34     "en" : [
    35         'about', 'again', 'are', 'and', 'because', 'being', 'but', 'can', 'done', 'have', 'for', 'from',
    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',
    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'
    37         'they', 'this', 'very', 'what', 'when', 'where', 'who', 'why', 'will', 'with', 'www', 'you', 'your'
    38     ]
    38     ]
    39 };
    39 }
    40 
    40 
    41 IriSP.Widgets.Tagcloud.prototype.draw = function() {
    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");
    42     
    45     
    43     if (this.segment_annotation_type) {
    46     if (this.segment_annotation_type) {
    44         var _this = this;
    47         this.bindPopcorn("timeupdate","onTimeupdate");
    45         this.source.getAnnotationsByTypeTitle(this.segment_annotation_type).forEach(function(_a) {
       
    46             _a.on("enter", function() {
       
    47                 _this.redraw(_a.begin, _a.end);
       
    48             });
       
    49         });
       
    50     } else {
    48     } else {
    51         this.redraw();
    49         this.redraw();
    52     }
    50     }
    53 };
    51 }
    54 
    52 
    55 IriSP.Widgets.Tagcloud.prototype.redraw = function(_from, _to) {
    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() {
    56     var _urlRegExp = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g,
    68     var _urlRegExp = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g,
    57         _regexpword = /[^\s\.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g,
    69         _regexpword = /[^\s\.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g,
    58         _words = {},
    70         _words = {},
    59         _this = this,
    71         _this = this,
    60         _annotations = this.getWidgetAnnotations();
    72         _annotations = this.getWidgetAnnotations();
    61         
    73         
    62     if (typeof _from !== "undefined" && typeof _to !== "undefined") {
    74     if (typeof this.begin_time !== "undefined" && typeof this.end_time !== "undefined") {
    63         _annotations = _annotations.filter(function(_annotation) {
    75         _annotations = _annotations.filter(function(_annotation) {
    64             return _annotation.begin >= _from && _annotation.end <= _to;
    76             return _annotation.begin >= _this.begin_time && _annotation.end <= _this.end_time;
    65         });
    77         })
    66     }
    78     }
    67     
    79     
    68     _annotations.forEach(function(_annotation) {
    80     _annotations.forEach(function(_annotation) {
    69        var _txt =
    81        var _txt =
    70             (_this.include_titles ? _annotation.title : '')
    82             (_this.include_titles ? _annotation.title : '')
    74             + (_this.include_tag_texts ? _annotation.getTagTexts() : '');
    86             + (_this.include_tag_texts ? _annotation.getTagTexts() : '');
    75        IriSP._(_txt.toLowerCase().replace(_urlRegExp, '').match(_regexpword)).each(function(_word) {
    87        IriSP._(_txt.toLowerCase().replace(_urlRegExp, '').match(_regexpword)).each(function(_word) {
    76            if (IriSP._(_this.stopwords).indexOf(_word) == -1 && (!_this.exclude_pattern || !_this.exclude_pattern.test(_word))) {
    88            if (IriSP._(_this.stopwords).indexOf(_word) == -1 && (!_this.exclude_pattern || !_this.exclude_pattern.test(_word))) {
    77                _words[_word] = 1 + (_words[_word] || 0);
    89                _words[_word] = 1 + (_words[_word] || 0);
    78            }
    90            }
    79        });
    91        })
    80     });
    92     });
    81     _words = IriSP._(_words)
    93     _words = IriSP._(_words)
    82         .chain()
    94         .chain()
    83         .map(function(_v, _k) {
    95         .map(function(_v, _k) {
    84             return {
    96             return {
    85                 "word" : _k,
    97                 "word" : _k,
    86                 "count" : _v
    98                 "count" : _v
    87             };
    99             }
    88         })
   100         })
    89         .filter(function(_v) {
   101         .filter(function(_v) {
    90             return _v.count > 2;
   102             return _v.count > 2;
    91         })
   103         })
    92         .sortBy(function(_v) {
   104         .sortBy(function(_v) {
   103             });
   115             });
   104     }
   116     }
   105     this.$.html(Mustache.to_html(this.template,  {words: _words }));
   117     this.$.html(Mustache.to_html(this.template,  {words: _words }));
   106     this.$.find(".Ldt-Tagcloud-item").click(function() {
   118     this.$.find(".Ldt-Tagcloud-item").click(function() {
   107         var _txt = IriSP.jQuery(this).attr("content");
   119         var _txt = IriSP.jQuery(this).attr("content");
   108         _this.source.getAnnotations().search(_txt);
   120         _this.player.popcorn.trigger("IriSP.search.triggeredSearch", _txt);
   109     });
   121     });
   110     this.source.getAnnotations().on("search", this.functionWrapper("onSearch"));
   122     
   111     this.source.getAnnotations().on("search-cleared", this.functionWrapper("onSearch"));
   123 }
   112 };
       
   113 
   124 
   114 IriSP.Widgets.Tagcloud.prototype.onSearch = function(searchString) {
   125 IriSP.Widgets.Tagcloud.prototype.onSearch = function(searchString) {
   115     searchString = typeof searchString !== "undefined" ? searchString : '';
   126     searchString = typeof searchString !== "undefined" ? searchString : '';
   116     if (searchString) {
   127     if (searchString) {
   117         var _rgxp = IriSP.Model.regexpFromTextOrArray(searchString);
   128         var _rgxp = IriSP.Model.regexpFromTextOrArray(searchString);
   123             _el.html(_txt.replace(_rgxp, '<span class="Ldt-Tagcloud-active">$1</span>'));
   134             _el.html(_txt.replace(_rgxp, '<span class="Ldt-Tagcloud-active">$1</span>'));
   124         } else {
   135         } else {
   125             _el.html(_txt);
   136             _el.html(_txt);
   126         }
   137         }
   127     });
   138     });
   128 };
   139 }