src/js/widgets/tagCloudWidget.js
branchnew-model
changeset 881 f11b234497f7
parent 880 4c7b33bf2795
child 882 61c384dda19e
equal deleted inserted replaced
880:4c7b33bf2795 881:f11b234497f7
     1 IriSP.TagCloudWidget = function(Popcorn, config, Serializer) {
       
     2   IriSP.Widget.call(this, Popcorn, config, Serializer);
       
     3 }
       
     4 
       
     5 IriSP.TagCloudWidget.prototype = new IriSP.Widget();
       
     6 
       
     7 IriSP.TagCloudWidget.prototype.draw = function() {
       
     8     
       
     9     var _urlRegExp = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g,
       
    10         _stopWords = [
       
    11             'aussi', 'and', 'avec', 'aux', 'bien', 'car', 'cette', 'comme', 'dans', 'donc', 'des', 'elle', 'encore', 'entre', 'est',
       
    12             'être', 'eux', 'faire', 'fait', 'http', 'ici', 'ils', 'les', 'leur', 'leurs', 'mais', 'mes', 'même', 'mon', 'notre',
       
    13             'non', 'nos', 'nous', 'ont', 'par', 'pas', 'peu', 'peut', 'plus', 'pour', 'que', 'qui', 'sans', 'ses' ,'son', 'sont', 'sur',
       
    14             'tes', 'très', 'the', 'ton', 'tous', 'tout', 'une', 'votre', 'vos', 'vous' ],
       
    15         _regexpword = /[^\s\.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g,
       
    16         _words = {},
       
    17         _showTitle = !this._config.excludeTitle,
       
    18         _showDescription = !this._config.excludeDescription,
       
    19         _excludePattern = this._config.excludePattern || null,
       
    20         _tagCount = this._config.tagCount || 30;
       
    21     if (typeof this._config.excludeWords !== "undefined" && this._config.excludeWords.length) {
       
    22         IriSP._(this._config.excludeWords).each(function(_w) {
       
    23             _stopWords.push(_w.toLowerCase());
       
    24         });
       
    25     }
       
    26     
       
    27     IriSP._(this._serializer._data.annotations).each(function(_annotation) {
       
    28        if (_annotation.content && _annotation.content.description) {
       
    29            var _txt = (_showTitle ? _annotation.content.title : '') + ' ' + (_showDescription ? _annotation.content.description : '')
       
    30            IriSP._(_txt.toLowerCase().replace(_urlRegExp, '').match(_regexpword)).each(function(_mot) {
       
    31                if (_stopWords.indexOf(_mot) == -1 && (_excludePattern == null || !_excludePattern.test(_mot))) {
       
    32                    _words[_mot] = 1 + (_words[_mot] || 0);
       
    33                }
       
    34            })
       
    35        } 
       
    36     });
       
    37     
       
    38     _words = IriSP._(_words)
       
    39         .chain()
       
    40         .map(function(_v, _k) {
       
    41             return {
       
    42                 "word" : _k,
       
    43                 "count" : _v
       
    44             }
       
    45         })
       
    46         .filter(function(_v) {
       
    47             return _v.count > 2;
       
    48         })
       
    49         .sortBy(function(_v) {
       
    50             return - _v.count;
       
    51         })
       
    52         .first(_tagCount)
       
    53         .value();
       
    54     var _max = _words[0].count,
       
    55         _min = Math.min(_words[_words.length - 1].count, _max - 1),
       
    56         _scale = 16 / Math.sqrt(_max - _min),
       
    57         _this = this,
       
    58         _html = '<ul>'
       
    59             + IriSP._(_words)
       
    60                 .chain()
       
    61                 .shuffle()
       
    62                 .map(function(_word) {
       
    63                     var _size = 10 + _scale * Math.sqrt(_word.count - _min);
       
    64                     return '<li class="Ldt-TraceMe" style="font-size:'
       
    65                         + _size
       
    66                         + 'px;">'
       
    67                         + _word.word
       
    68                         + '</li>'
       
    69                 })
       
    70                 .value()
       
    71                 .join("")
       
    72             + '</ul>';
       
    73     this.selector
       
    74         .addClass("Ldt-TagCloud")
       
    75         .html(_html);
       
    76     this.selector.find("li").click(function() {
       
    77         var _txt = this.textContent.replace(/(^[\s]+|[\s]+$)/g,'');
       
    78         _this._Popcorn.trigger("IriSP.search.triggeredSearch", _txt);
       
    79     });
       
    80     this._Popcorn.listen("IriSP.search", IriSP.wrap(this, function(searchString) {
       
    81         var _rgxp = new RegExp("(" + searchString.replace(/(\W)/g,'\\$1') + ")","gi");
       
    82         this.selector.find("li").each(function(_i, _e) {
       
    83             _e.innerHTML = searchString.length ?
       
    84                 _e.textContent.replace(_rgxp,'<span class="Ldt-TagCloud-actif Ldt-TraceMe">$1</span>')
       
    85                 : _e.textContent;
       
    86         });
       
    87     }));
       
    88     this._Popcorn.listen("IriSP.search.closed", IriSP.wrap(this, this.endsearch));
       
    89     this._Popcorn.listen("IriSP.search.cleared", IriSP.wrap(this, this.endsearch));
       
    90 }
       
    91 
       
    92 IriSP.TagCloudWidget.prototype.endsearch = function() {
       
    93     this.selector.find("li").each(function(_i, _e) {
       
    94         _e.innerHTML = _e.textContent;
       
    95     });
       
    96 }