src/widgets/Tagcloud.js
branchnew-model
changeset 1020 198c2b79f5e1
parent 1019 3ab36f402b0c
--- a/src/widgets/Tagcloud.js	Thu Jan 02 16:40:25 2014 +0100
+++ b/src/widgets/Tagcloud.js	Thu Jan 02 16:49:20 2014 +0100
@@ -1,7 +1,7 @@
 IriSP.Widgets.Tagcloud = function(player, config) {
     IriSP.Widgets.Widget.call(this, player, config);
     this.stopwords = IriSP._.uniq([].concat(this.custom_stopwords).concat(this.stopword_lists[this.stopword_language]));
-};
+}
 
 IriSP.Widgets.Tagcloud.prototype = new IriSP.Widgets.Widget();
 
@@ -22,7 +22,7 @@
     segment_annotation_type: false,
     min_font_size: 10,
     max_font_size: 26
-};
+}
 
 IriSP.Widgets.Tagcloud.prototype.stopword_lists = {
     "fr" : [
@@ -36,33 +36,45 @@
         'get', 'here', 'http', 'like', 'more', 'one', 'our', 'she', 'that', 'the', 'their', 'then', 'there',
         'they', 'this', 'very', 'what', 'when', 'where', 'who', 'why', 'will', 'with', 'www', 'you', 'your'
     ]
-};
+}
 
 IriSP.Widgets.Tagcloud.prototype.draw = function() {
+    this.bindPopcorn("IriSP.search", "onSearch");
+    this.bindPopcorn("IriSP.search.closed", "onSearch");
+    this.bindPopcorn("IriSP.search.cleared", "onSearch");
     
     if (this.segment_annotation_type) {
-        var _this = this;
-        this.source.getAnnotationsByTypeTitle(this.segment_annotation_type).forEach(function(_a) {
-            _a.on("enter", function() {
-                _this.redraw(_a.begin, _a.end);
-            });
-        });
+        this.bindPopcorn("timeupdate","onTimeupdate");
     } else {
         this.redraw();
     }
-};
+}
 
-IriSP.Widgets.Tagcloud.prototype.redraw = function(_from, _to) {
+IriSP.Widgets.Tagcloud.prototype.onTimeupdate = function() {
+    var _time = Math.floor(this.player.popcorn.currentTime() * 1000),
+        _list = this.source.getAnnotationsByTypeTitle(this.segment_annotation_type).filter(function(_annotation) {
+            return _annotation.begin <= _time && _annotation.end > _time;
+        });
+    if (_list.length) {
+        if (_list[0].begin !== this.begin_time || _list[0].end !== this.end_time) {
+            this.begin_time = _list[0].begin;
+            this.end_time = _list[0].end;
+            this.redraw();
+        }
+    }
+}
+
+IriSP.Widgets.Tagcloud.prototype.redraw = function() {
     var _urlRegExp = /https?:\/\/[0-9a-zA-Z\.%\/-_]+/g,
         _regexpword = /[^\s\.&;,'"!\?\d\(\)\+\[\]\\\…\-«»:\/]{3,}/g,
         _words = {},
         _this = this,
         _annotations = this.getWidgetAnnotations();
         
-    if (typeof _from !== "undefined" && typeof _to !== "undefined") {
+    if (typeof this.begin_time !== "undefined" && typeof this.end_time !== "undefined") {
         _annotations = _annotations.filter(function(_annotation) {
-            return _annotation.begin >= _from && _annotation.end <= _to;
-        });
+            return _annotation.begin >= _this.begin_time && _annotation.end <= _this.end_time;
+        })
     }
     
     _annotations.forEach(function(_annotation) {
@@ -76,7 +88,7 @@
            if (IriSP._(_this.stopwords).indexOf(_word) == -1 && (!_this.exclude_pattern || !_this.exclude_pattern.test(_word))) {
                _words[_word] = 1 + (_words[_word] || 0);
            }
-       });
+       })
     });
     _words = IriSP._(_words)
         .chain()
@@ -84,7 +96,7 @@
             return {
                 "word" : _k,
                 "count" : _v
-            };
+            }
         })
         .filter(function(_v) {
             return _v.count > 2;
@@ -105,11 +117,10 @@
     this.$.html(Mustache.to_html(this.template,  {words: _words }));
     this.$.find(".Ldt-Tagcloud-item").click(function() {
         var _txt = IriSP.jQuery(this).attr("content");
-        _this.source.getAnnotations().search(_txt);
+        _this.player.popcorn.trigger("IriSP.search.triggeredSearch", _txt);
     });
-    this.source.getAnnotations().on("search", this.functionWrapper("onSearch"));
-    this.source.getAnnotations().on("search-cleared", this.functionWrapper("onSearch"));
-};
+    
+}
 
 IriSP.Widgets.Tagcloud.prototype.onSearch = function(searchString) {
     searchString = typeof searchString !== "undefined" ? searchString : '';
@@ -125,4 +136,4 @@
             _el.html(_txt);
         }
     });
-};
+}