src/widgets/Trace.js
changeset 1072 ac1eacb3aa33
parent 1068 7623f9af9272
--- a/src/widgets/Trace.js	Sun Nov 12 22:07:33 2017 +0100
+++ b/src/widgets/Trace.js	Wed Sep 04 17:32:50 2024 +0200
@@ -1,97 +1,121 @@
-IriSP.Widgets.Trace = function(player, config) {
-  IriSP.Widgets.Widget.call(this, player, config);
-    
-};
+import _ from "lodash";
+import jQuery from "jquery";
 
-IriSP.Widgets.Trace.prototype = new IriSP.Widgets.Widget();
+const Trace = function (ns) {
+  return class extends ns.Widgets.Widget {
+    constructor(player, config) {
+      super(player, config);
+    }
 
-IriSP.Widgets.Trace.prototype.defaults = {
-    js_console : false,
-    url: "http://traces.advene.org:5000/",
-    requestmode: 'GET',
-    syncmode: "sync",
-    default_subject: "IRI",
-    tracer: null,
-    extend: false
-};
+    static defaults = {
+      js_console: false,
+      url: "http://traces.advene.org:5000/",
+      requestmode: "GET",
+      syncmode: "sync",
+      default_subject: "IRI",
+      tracer: null,
+      extend: false,
+    };
 
-IriSP.Widgets.Trace.prototype.draw = function() {
-  if (typeof window.tracemanager === "undefined") {
-      console.log("Tracemanager not found");
-      return;
-  }
-  var _this = this,
-    _medialisteners = {
-        "play" : 0,
-        "pause" : 0,
-        "volumechange" : 0,
-        "seeked" : 0,
-        "play" : 0,
-        "pause" : 0,
-        "timeupdate" : 10000
-    },
-    _annlisteners = {
-        search: 0,
-        "search-cleared": 0
-    };
-    IriSP._(_medialisteners).each(function(_ms, _listener) {
-        var _f = function(_arg) {
-            _this.eventHandler(_listener, _arg);
+    draw() {
+      if (typeof window.tracemanager === "undefined") {
+        console.log("Tracemanager not found");
+        return;
+      }
+      var _this = this,
+        _medialisteners = {
+          play: 0,
+          pause: 0,
+          volumechange: 0,
+          seeked: 0,
+          play: 0,
+          pause: 0,
+          timeupdate: 10000,
+        },
+        _annlisteners = {
+          search: 0,
+          "search-cleared": 0,
+        };
+      _(_medialisteners).each(function (_ms, _listener) {
+        var _f = function (_arg) {
+          _this.eventHandler(_listener, _arg);
         };
         if (_ms) {
-            _f = IriSP._.throttle(_f, _ms);
+          _f = _.throttle(_f, _ms);
         }
         _this.media.on(_listener, _f);
-    });
-    var _annotations = this.source.getAnnotations();
-    IriSP._(_annlisteners).each(function(_ms, _listener) {
-        var _f = function(_arg) {
-            _this.eventHandler(_listener, _arg);
+      });
+      var _annotations = this.source.getAnnotations();
+      _(_annlisteners).each(function (_ms, _listener) {
+        var _f = function (_arg) {
+          _this.eventHandler(_listener, _arg);
         };
         if (_ms) {
-            _f = IriSP._.throttle(_f, _ms);
+          _f = _.throttle(_f, _ms);
         }
         _annotations.on(_listener, _f);
-    });
-    
-    if (!this.tracer) {
-    
+      });
+
+      if (!this.tracer) {
         this.tracer = window.tracemanager.init_trace("test", {
-            url: this.url,
-            requestmode: this.requestmode,
-            syncmode: this.syncmode,
-            default_subject: this.default_subject
+          url: this.url,
+          requestmode: this.requestmode,
+          syncmode: this.syncmode,
+          default_subject: this.default_subject,
         });
-    
-    }
-    
-    
-    
-    this.tracer.trace("TraceWidgetInit", {});
+      }
+
+      this.tracer.trace("TraceWidgetInit", {});
 
-    // Configure annotation creation/update/delete/publish tracing
-    _this.player.on("Annotation.create", function (a) {
-        _this.tracer.trace("AnnotationCreated", { id: a.id, annotation_begin: a.begin.milliseconds, annotation_end: a.end.milliseconds, annotation_media: a.media.id, content_length: a.title.length, content_words: a.title.split(/\s+/).length });
-    });
-    _this.player.on("Annotation.delete", function (aid) {
+      // Configure annotation creation/update/delete/publish tracing
+      _this.player.on("Annotation.create", function (a) {
+        _this.tracer.trace("AnnotationCreated", {
+          id: a.id,
+          annotation_begin: a.begin.milliseconds,
+          annotation_end: a.end.milliseconds,
+          annotation_media: a.media.id,
+          content_length: a.title.length,
+          content_words: a.title.split(/\s+/).length,
+        });
+      });
+      _this.player.on("Annotation.delete", function (aid) {
         _this.tracer.trace("AnnotationDeleted", { id: aid });
-    });
-    _this.player.on("Annotation.update", function (a) {
-        _this.tracer.trace("AnnotationUpdated", { id: a.id, annotation_begin: a.begin.milliseconds, annotation_end: a.end.milliseconds, annotation_media: a.media.id, content_length: a.title.length, content_words: a.title.split(/\s+/).length });
-    });
-    _this.player.on("Annotation.publish", function (a) {
-        _this.tracer.trace("AnnotationPublished", { id: a.id, annotation_begin: a.begin.milliseconds, annotation_end: a.end.milliseconds, annotation_media: a.media.id, content_length: a.title.length, content_words: a.title.split(/\s+/).length });
-    });
+      });
+      _this.player.on("Annotation.update", function (a) {
+        _this.tracer.trace("AnnotationUpdated", {
+          id: a.id,
+          annotation_begin: a.begin.milliseconds,
+          annotation_end: a.end.milliseconds,
+          annotation_media: a.media.id,
+          content_length: a.title.length,
+          content_words: a.title.split(/\s+/).length,
+        });
+      });
+      _this.player.on("Annotation.publish", function (a) {
+        _this.tracer.trace("AnnotationPublished", {
+          id: a.id,
+          annotation_begin: a.begin.milliseconds,
+          annotation_end: a.end.milliseconds,
+          annotation_media: a.media.id,
+          content_length: a.title.length,
+          content_words: a.title.split(/\s+/).length,
+        });
+      });
 
-    _this.player.trigger("trace-ready");
-    this.mouseLocation = '';
-    IriSP.jQuery(".Ldt-Widget").on("mousedown mouseenter mouseleave", ".Ldt-TraceMe", function(_e) {
-        var _target = IriSP.jQuery(this);
-        
-        var _widget = _target.attr("widget-type") || _target.parents(".Ldt-Widget").attr("widget-type"),
+      _this.player.trigger("trace-ready");
+      this.mouseLocation = "";
+      jQuery(".Ldt-Widget").on(
+        "mousedown mouseenter mouseleave",
+        ".Ldt-TraceMe",
+        function (_e) {
+          var _target = ns.jQuery(this);
+
+          var _widget =
+              _target.attr("widget-type") ||
+              _target.parents(".Ldt-Widget").attr("widget-type"),
             _data = {
-                "type": _e.type,
-                "widget": _widget
+              type: _e.type,
+              widget: _widget,
             },
             _targetEl = _target[0],
             _class = _targetEl.className,
@@ -99,47 +123,66 @@
             _id = _targetEl.id,
             _value = _target.val(),
             _traceInfo = _target.attr("trace-info");
-        _data.target = _name + (_id && _id.length ? '#' + IriSP.jqEscape(_id) : '') + (_class && _class.length ? ('.' + IriSP.jqEscape(_class).replace(/\s/g,'.')).replace(/\.Ldt-(Widget|TraceMe)/g,'') : '');
-        if (typeof _traceInfo == "string" && _traceInfo) {
+          _data.target =
+            _name +
+            (_id && _id.length ? "#" + ns.jqEscape(_id) : "") +
+            (_class && _class.length
+              ? ("." + ns.jqEscape(_class).replace(/\s/g, ".")).replace(
+                  /\.Ldt-(Widget|TraceMe)/g,
+                  ""
+                )
+              : "");
+          if (typeof _traceInfo == "string" && _traceInfo) {
             _data.traceInfo = _traceInfo;
+          }
+          if (typeof _value == "string" && _value.length) {
+            _data.value = _value;
+          }
+          _this.eventHandler("UIEvent", _data);
         }
-        if (typeof _value == "string" && _value.length) {
-            _data.value = _value;
-        }
-        _this.eventHandler('UIEvent', _data);
-    });
+      );
+    }
+
+    eventHandler(_listener, _arg) {
+      var _traceName = "Mdp_";
+      if (typeof _arg == "string" || typeof _arg == "number") {
+        _arg = { value: _arg };
+      }
+      if (typeof _arg == "undefined") {
+        _arg = {};
+      }
+      switch (_listener) {
+        case "UIEvent":
+          _traceName += _arg.widget + "_" + _arg.type;
+          delete _arg.widget;
+          delete _arg.type;
+          break;
+        case "play":
+        case "pause":
+          _arg.milliseconds = this.media.getCurrentTime().milliseconds;
+        case "timeupdate":
+        case "seeked":
+        case "volumechange":
+          _traceName += "media_" + _listener;
+          break;
+        default:
+          _traceName += _listener.replace(".", "_");
+      }
+      if (typeof this.extend === "object" && this.extend) {
+        _(_arg).extend(this.extend);
+      }
+      this.tracer.trace(_traceName, _arg);
+      if (
+        this.js_console &&
+        typeof window.console !== "undefined" &&
+        typeof console.log !== "undefined"
+      ) {
+        console.log(
+          "tracer.trace('" + _traceName + "', " + JSON.stringify(_arg) + ");"
+        );
+      }
+    }
+  };
 };
 
-IriSP.Widgets.Trace.prototype.eventHandler = function(_listener, _arg) {
-    var _traceName = 'Mdp_';
-    if (typeof _arg == "string" || typeof _arg == "number") {
-        _arg = { "value" : _arg };
-    }
-    if (typeof _arg == "undefined") {
-        _arg = {};
-    }
-    switch(_listener) {
-        case 'UIEvent':
-            _traceName += _arg.widget + '_' + _arg.type;
-            delete _arg.widget;
-            delete _arg.type;
-        break;
-        case 'play':
-        case 'pause':
-            _arg.milliseconds = this.media.getCurrentTime().milliseconds;
-        case 'timeupdate':
-        case 'seeked':
-        case 'volumechange':
-            _traceName += 'media_' + _listener;
-        break;
-        default:
-            _traceName += _listener.replace('.','_');
-    }
-    if (typeof this.extend === "object" && this.extend) {
-        IriSP._(_arg).extend(this.extend);
-    }
-    this.tracer.trace(_traceName, _arg);
-    if (this.js_console && typeof window.console !== "undefined" && typeof console.log !== "undefined") {
-        console.log("tracer.trace('" + _traceName + "', " + JSON.stringify(_arg) + ");");
-    }
-};
+export { Trace };