web/res/metadataplayer/Tagcloud.js
changeset 598 d366aa22bd79
child 623 5b7d7ab6baff
equal deleted inserted replaced
597:07ab28bca482 598:d366aa22bd79
       
     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" 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     tag_count: 30,
       
    17     stopword_language: "fr",
       
    18     custom_stopwords: [],
       
    19     exclude_pattern: false,
       
    20     annotation_type: false,
       
    21     min_font_size: 10,
       
    22     max_font_size: 26
       
    23 }
       
    24 
       
    25 IriSP.Widgets.Tagcloud.prototype.stopword_lists = {
       
    26     "fr" : [
       
    27         'aussi', 'avec', 'aux', 'bien', 'car', 'cette', 'comme', 'dans', 'des', 'donc', 'dont', 'elle', 'encore', 'entre', 'est',
       
    28         'être', 'eux', 'faire', 'fait', 'http', 'ici', 'ils', 'les', 'leur', 'leurs', 'mais', 'mes', 'même', 'mon', 'notre',
       
    29         'non', 'nos', 'nous', 'ont', 'par', 'pas', 'peu', 'peut', 'plus', 'pour', 'quand', 'que', 'qui', 'quoi', 'sans',
       
    30         'ses' ,'son', 'sont', 'sur', 'tes', 'très', 'the', 'ton', 'tous', 'tout', 'une', 'votre', 'vos', 'vous'
       
    31     ],
       
    32     "en" : [
       
    33         'about', 'again', 'are', 'and', 'because', 'being', 'but', 'can', 'done', 'have', 'for', 'from',
       
    34         'get', 'here', 'http', 'like', 'more', 'one', 'our', 'she', 'that', 'the', 'their', 'then', 'there',
       
    35         'they', 'this', 'very', 'what', 'when', 'where', 'who', 'why', 'will', 'with', 'www', 'you', 'your'
       
    36     ]
       
    37 }
       
    38 
       
    39 IriSP.Widgets.Tagcloud.prototype.draw = function() {
       
    40     
       
    41     var _urlRegExp = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g,
       
    42         _regexpword = /[^\s\.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g,
       
    43         _words = {},
       
    44         _this = this;
       
    45     this.getWidgetAnnotations().forEach(function(_annotation) {
       
    46        var _txt = (_this.include_titles ? _annotation.title : '') + ' ' + (_this.include_descriptions ? _annotation.description : '');
       
    47        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))) {
       
    49                _words[_word] = 1 + (_words[_word] || 0);
       
    50            }
       
    51        })
       
    52     });
       
    53     _words = IriSP._(_words)
       
    54         .chain()
       
    55         .map(function(_v, _k) {
       
    56             return {
       
    57                 "word" : _k,
       
    58                 "count" : _v
       
    59             }
       
    60         })
       
    61         .filter(function(_v) {
       
    62             return _v.count > 2;
       
    63         })
       
    64         .sortBy(function(_v) {
       
    65             return - _v.count;
       
    66         })
       
    67         .first(this.tag_count)
       
    68         .value();
       
    69     var _max = _words[0].count,
       
    70         _min = Math.min(_words[_words.length - 1].count, _max - 1),
       
    71         _scale = (this.max_font_size - this.min_font_size) / Math.sqrt(_max - _min);
       
    72     IriSP._(_words).each(function(_word) {
       
    73             _word.size = Math.floor( _this.min_font_size + _scale * Math.sqrt(_word.count - _min) );
       
    74         });
       
    75     this.words = _words;
       
    76     this.renderTemplate();
       
    77     this.$words = this.$.find(".Ldt-Tagcloud-item");
       
    78     this.$words.click(function() {
       
    79         var _txt = IriSP.jQuery(this).attr("content");
       
    80         _this.player.popcorn.trigger("IriSP.search.triggeredSearch", _txt);
       
    81     });
       
    82     this.bindPopcorn("IriSP.search", "onSearch");
       
    83     this.bindPopcorn("IriSP.search.closed", "onSearch");
       
    84     this.bindPopcorn("IriSP.search.cleared", "onSearch");
       
    85 }
       
    86 
       
    87 IriSP.Widgets.Tagcloud.prototype.onSearch = function(searchString) {
       
    88     searchString = typeof searchString !== "undefined" ? searchString : '';
       
    89     if (searchString) {
       
    90         var _rgxp = IriSP.Model.regexpFromTextOrArray(searchString);
       
    91     }
       
    92     this.$words.each(function() {
       
    93         var _el = IriSP.jQuery(this),
       
    94             _txt = _el.attr("content");
       
    95         if (searchString) {
       
    96             _el.html(_txt.replace(_rgxp, '<span class="Ldt-Tagcloud-active">$1</span>'));
       
    97         } else {
       
    98             _el.html(_txt);
       
    99         }
       
   100     });
       
   101 }