src/js/widgets/traceWidget.js
changeset 945 7d9f6fd6f904
parent 944 8a6c9e3d0158
child 947 ec4e9ddf9fba
equal deleted inserted replaced
944:8a6c9e3d0158 945:7d9f6fd6f904
     1 IriSP.TraceWidget = function(Popcorn, config, Serializer) {
       
     2   IriSP.Widget.call(this, Popcorn, config, Serializer);
       
     3   this.lastEvent = "";
       
     4   var _this = this,
       
     5     _listeners = {
       
     6         "IriSP.createAnnotationWidget.addedAnnotation" : 0,
       
     7         "IriSP.search.open" : 0,
       
     8         "IriSP.search.closed" : 0,
       
     9         "IriSP.search" : 0,
       
    10         "IriSP.search.cleared" : 0,
       
    11         "IriSP.search.matchFound" : 0,
       
    12         "IriSP.search.noMatchFound" : 0,
       
    13         "IriSP.search.triggeredSearch" : 0,
       
    14         "IriSP.TraceWidget.MouseEvents" : 0,
       
    15         "play" : 0,
       
    16         "pause" : 0,
       
    17         "volumechange" : 0,
       
    18         "seeked" : 0,
       
    19         "play" : 0,
       
    20         "pause" : 0,
       
    21         "timeupdate" : 2000
       
    22     };
       
    23     IriSP._(_listeners).each(function(_ms, _listener) {
       
    24         var _f = function(_arg) {
       
    25             _this.eventHandler(_listener, _arg);
       
    26         }
       
    27         if (_ms) {
       
    28             _f = IriSP.underscore.throttle(_f, _ms);
       
    29         }
       
    30         _this._Popcorn.listen(_listener, _f);
       
    31     });
       
    32     
       
    33     if (typeof window.tracemanager === "undefined") {
       
    34         $LAB.script(IriSP.getLib("tracemanager")).wait(function() {
       
    35             _this.onTmLoaded();
       
    36         });
       
    37     } else {
       
    38         this.onTmLoaded();
       
    39     }
       
    40     
       
    41 }
       
    42 
       
    43 IriSP.TraceWidget.prototype = new IriSP.Widget();
       
    44 
       
    45 IriSP.TraceWidget.prototype.onTmLoaded = function() {
       
    46     this.tracer = window.tracemanager.init_trace("test", this._config);
       
    47     this.tracer.trace("StartTracing", { "hello": "world" });
       
    48 }
       
    49 
       
    50 IriSP.TraceWidget.prototype.draw = function() {
       
    51     this.mouseLocation = '';
       
    52     var _this = this;
       
    53     IriSP.jQuery(".Ldt-Widget").bind("click mouseover mouseout dragstart dragstop", function(_e) {
       
    54         var _widget = IriSP.jQuery(this).attr("widget-type"),
       
    55             _class = _e.target.className;
       
    56         var _data = {
       
    57             "type": _e.type,
       
    58             "x": _e.clientX,
       
    59             "y": _e.clientY,
       
    60             "widget": _widget
       
    61         }
       
    62         if (typeof _class == "string" && _class.indexOf('Ldt-TraceMe') != -1) {
       
    63             var _name = _e.target.localName,
       
    64                 _id = _e.target.id,
       
    65                 _text = _e.target.textContent.trim(),
       
    66                 _title = _e.target.title,
       
    67                 _value = _e.target.value;
       
    68             _data.target = _name + (_id.length ? '#' + IriSP.jqEscape(_id) : '') + (_class.length ? ('.' + IriSP.jqEscape(_class).replace(/\s/g,'.')).replace(/\.Ldt-(Widget|TraceMe)/g,'') : '');
       
    69             if (typeof _title == "string" && _title.length && _title.length < 140) {
       
    70                 _data.title = _title;
       
    71             }
       
    72             if (typeof _text == "string" && _text.length && _text.length < 140) {
       
    73                 _data.text = _text;
       
    74             }
       
    75             if (typeof _value == "string" && _value.length) {
       
    76                 _data.value = _value;
       
    77             }
       
    78             _this._Popcorn.trigger('IriSP.TraceWidget.MouseEvents', _data);
       
    79         } else {
       
    80             //console.log(_e.type+','+_this.mouseLocation+','+_widget);
       
    81             if (_e.type == "mouseover") {
       
    82                 if (_this.mouseLocation != _widget) {
       
    83                     _this._Popcorn.trigger('IriSP.TraceWidget.MouseEvents', _data);
       
    84                 } else {
       
    85                     if (typeof _this.moTimeout != "undefined") {
       
    86                         clearTimeout(_this.moTimeout);
       
    87                         delete _this.moTimeout;
       
    88                     }
       
    89                 }
       
    90             }
       
    91             if (_e.type == "click") {
       
    92                 _this._Popcorn.trigger('IriSP.TraceWidget.MouseEvents', _data);
       
    93             }
       
    94             if (_e.type == "mouseout") {
       
    95                 if (typeof _this.moTimeout != "undefined") {
       
    96                     clearTimeout(_this.moTimeout);
       
    97                 }
       
    98                 _this.moTimeout = setTimeout(function() {
       
    99                    if (_data.widget != _this.mouseLocation) {
       
   100                        _this._Popcorn.trigger('IriSP.TraceWidget.MouseEvents', _data);
       
   101                    }
       
   102                 },100);
       
   103             }
       
   104         }
       
   105         _this.mouseLocation = _widget;
       
   106     });
       
   107 }
       
   108 
       
   109 IriSP.TraceWidget.prototype.eventHandler = function(_listener, _arg) {
       
   110     var _traceName = 'Mdp_';
       
   111     if (typeof _arg == "string" || typeof _arg == "number") {
       
   112         _arg = { "value" : _arg }
       
   113     }
       
   114     if (typeof _arg == "undefined") {
       
   115         _arg = {}
       
   116     }
       
   117     switch(_listener) {
       
   118         case 'IriSP.TraceWidget.MouseEvents':
       
   119             _traceName += _arg.widget + '_' + _arg.type;
       
   120             delete _arg.widget;
       
   121             delete _arg.type;
       
   122         break;
       
   123         case 'timeupdate':
       
   124         case 'play':
       
   125         case 'pause':
       
   126             _arg.time = this._Popcorn.currentTime() * 1000;
       
   127         case 'seeked':
       
   128         case 'volumechange':
       
   129             _traceName += 'Popcorn_' + _listener;
       
   130         break;
       
   131         default:
       
   132             _traceName += _listener.replace('IriSP.','').replace('.','_');
       
   133     }
       
   134     this.lastEvent = _traceName;
       
   135     this.tracer.trace(_traceName, _arg);
       
   136     if (this._config.js_console) {
       
   137         console.log("tracer.trace('" + _traceName + "', " + JSON.stringify(_arg) + ");");
       
   138     }
       
   139 }