src/widgets/Tagcloud.js
branchnew-model
changeset 908 f56199193fad
parent 906 4b6e154ae8de
child 957 4da0a5740b6c
child 1019 3ab36f402b0c
equal deleted inserted replaced
906:4b6e154ae8de 908:f56199193fad
    11     + '</ul></div>';
    11     + '</ul></div>';
    12 
    12 
    13 IriSP.Widgets.Tagcloud.prototype.defaults = {
    13 IriSP.Widgets.Tagcloud.prototype.defaults = {
    14     include_titles: true,
    14     include_titles: true,
    15     include_descriptions: true,
    15     include_descriptions: true,
       
    16     include_tag_texts: true,
    16     tag_count: 30,
    17     tag_count: 30,
    17     stopword_language: "fr",
    18     stopword_language: "fr",
    18     custom_stopwords: [],
    19     custom_stopwords: [],
    19     exclude_pattern: false,
    20     exclude_pattern: false,
    20     annotation_type: false,
    21     annotation_type: false,
       
    22     segment_annotation_type: false,
    21     min_font_size: 10,
    23     min_font_size: 10,
    22     max_font_size: 26
    24     max_font_size: 26
    23 }
    25 }
    24 
    26 
    25 IriSP.Widgets.Tagcloud.prototype.stopword_lists = {
    27 IriSP.Widgets.Tagcloud.prototype.stopword_lists = {
    35         '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'
    36     ]
    38     ]
    37 }
    39 }
    38 
    40 
    39 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");
    40     
    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() {
    41     var _urlRegExp = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g,
    68     var _urlRegExp = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g,
    42         _regexpword = /[^\s\.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g,
    69         _regexpword = /[^\s\.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g,
    43         _words = {},
    70         _words = {},
    44         _this = this;
    71         _this = this,
    45     this.getWidgetAnnotations().forEach(function(_annotation) {
    72         _annotations = this.getWidgetAnnotations();
    46        var _txt = (_this.include_titles ? _annotation.title : '') + ' ' + (_this.include_descriptions ? _annotation.description : '');
    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() : '');
    47        IriSP._(_txt.toLowerCase().replace(_urlRegExp, '').match(_regexpword)).each(function(_word) {
    87        IriSP._(_txt.toLowerCase().replace(_urlRegExp, '').match(_regexpword)).each(function(_word) {
    48            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))) {
    49                _words[_word] = 1 + (_words[_word] || 0);
    89                _words[_word] = 1 + (_words[_word] || 0);
    50            }
    90            }
    51        })
    91        })
    64         .sortBy(function(_v) {
   104         .sortBy(function(_v) {
    65             return - _v.count;
   105             return - _v.count;
    66         })
   106         })
    67         .first(this.tag_count)
   107         .first(this.tag_count)
    68         .value();
   108         .value();
    69     if (!_words.length) {
   109     if (_words.length) {
    70         return;
   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             });
    71     }
   116     }
    72     var _max = _words[0].count,
   117     this.$.html(Mustache.to_html(this.template,  {words: _words }));
    73         _min = Math.min(_words[_words.length - 1].count, _max - 1),
   118     this.$.find(".Ldt-Tagcloud-item").click(function() {
    74         _scale = (this.max_font_size - this.min_font_size) / Math.sqrt(_max - _min);
       
    75     IriSP._(_words).each(function(_word) {
       
    76             _word.size = Math.floor( _this.min_font_size + _scale * Math.sqrt(_word.count - _min) );
       
    77         });
       
    78     this.words = _words;
       
    79     this.renderTemplate();
       
    80     this.$words = this.$.find(".Ldt-Tagcloud-item");
       
    81     this.$words.click(function() {
       
    82         var _txt = IriSP.jQuery(this).attr("content");
   119         var _txt = IriSP.jQuery(this).attr("content");
    83         _this.player.popcorn.trigger("IriSP.search.triggeredSearch", _txt);
   120         _this.player.popcorn.trigger("IriSP.search.triggeredSearch", _txt);
    84     });
   121     });
    85     this.bindPopcorn("IriSP.search", "onSearch");
   122     
    86     this.bindPopcorn("IriSP.search.closed", "onSearch");
       
    87     this.bindPopcorn("IriSP.search.cleared", "onSearch");
       
    88 }
   123 }
    89 
   124 
    90 IriSP.Widgets.Tagcloud.prototype.onSearch = function(searchString) {
   125 IriSP.Widgets.Tagcloud.prototype.onSearch = function(searchString) {
    91     searchString = typeof searchString !== "undefined" ? searchString : '';
   126     searchString = typeof searchString !== "undefined" ? searchString : '';
    92     if (searchString) {
   127     if (searchString) {
    93         var _rgxp = IriSP.Model.regexpFromTextOrArray(searchString);
   128         var _rgxp = IriSP.Model.regexpFromTextOrArray(searchString);
    94     }
   129     }
    95     this.$words.each(function() {
   130     this.$.find(".Ldt-Tagcloud-item").each(function() {
    96         var _el = IriSP.jQuery(this),
   131         var _el = IriSP.jQuery(this),
    97             _txt = _el.attr("content");
   132             _txt = _el.attr("content");
    98         if (searchString) {
   133         if (searchString) {
    99             _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>'));
   100         } else {
   135         } else {