server/src/assets/js/mtdc-tracking-worker.js
changeset 90 00c9bb0f6f37
child 96 12dab8a7797c
equal deleted inserted replaced
89:4196849b15d3 90:00c9bb0f6f37
       
     1 /* globals operative _ Cookies $ */
       
     2 /* eslint no-console: 0 */
       
     3 
       
     4 
       
     5 (function(root) {
       
     6     'use strict';
       
     7 
       
     8     if (typeof root.Mtdc !== 'object') {
       
     9         root.Mtdc = {};
       
    10     }
       
    11 
       
    12     var Mtdc = root.Mtdc;
       
    13 
       
    14 
       
    15     Mtdc.TrackingWorker = function(renkan, trackingUrl, trackingCloseUrl, registration, debounceDelay = 1000) {
       
    16 
       
    17         function _sendTrackingInfo() {
       
    18             var trackingMessages = this.trackingMessages;
       
    19             this.trackingMessages = [];
       
    20             if(trackingMessages.length === 0) {
       
    21                 return;
       
    22             }
       
    23             $.ajax({
       
    24                 method: 'POST',
       
    25                 url: this.trackingUrl,
       
    26                 headers: {
       
    27                     'X-CSRFToken': this.csrftoken
       
    28                 },
       
    29                 data: JSON.stringify(trackingMessages),
       
    30                 contentType: 'application/json'
       
    31             }).fail(function(){
       
    32                 console.log('send tracking data failed', trackingMessages);
       
    33             }); // this is fire and forget. Nothong to do on done
       
    34         }
       
    35 
       
    36         var trackingWorker = {
       
    37             trackingMessages: [],
       
    38             currentUser: renkan.current_user,
       
    39             renkan: renkan,
       
    40             trackingUrl: trackingUrl,
       
    41             trackingCloseUrl: trackingCloseUrl,
       
    42             getUUID4 : function() {
       
    43                 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
       
    44                     var r = Math.random() * 16 | 0,
       
    45                         v = c === 'x' ? r : (r & 0x3 | 0x8);
       
    46                     return v.toString(16);
       
    47                 });
       
    48             },
       
    49             _init: function() {
       
    50                 this.sendTrackingInfo = _.debounce(_.bind(_sendTrackingInfo, this), debounceDelay);
       
    51                 this.csrftoken = Cookies.get('csrftoken');
       
    52                 this.registration = registration ? registration : this.getUUID4();
       
    53                 return this;
       
    54             },
       
    55             _pushTrackingMessage: function(msg) {
       
    56                 this.trackingMessages.push(msg);
       
    57                 this.sendTrackingInfo();
       
    58             },
       
    59             flushTrackingInfo() {
       
    60                 //this.sendTrackingInfo.flush();
       
    61             },
       
    62             _getBaseMsg: function(verb) {
       
    63                 var timestamp = new Date(),
       
    64                     verbNode = {
       
    65                         add: {
       
    66                             id: 'http://activitystrea.ms/schema/1.0/create',
       
    67                             display: { 'fr-FR': 'a créé' }
       
    68                         },
       
    69                         update: {
       
    70                             id: 'http://activitystrea.ms/schema/1.0/update',
       
    71                             display: { 'fr-FR': 'a modifié' }
       
    72                         },
       
    73                         delete: {
       
    74                             id: 'http://activitystrea.ms/schema/1.0/delete',
       
    75                             display: { 'fr-FR': 'a supprimé' }
       
    76                         },
       
    77                         move: {
       
    78                             id: 'http://activitystrea.ms/schema/1.0/move',
       
    79                             display: { 'fr-FR': 'a déplacé'}
       
    80                         },
       
    81                         close: {
       
    82                             id: 'http://activitystrea.ms/schema/1.0/close',
       
    83                             display: { 'fr-FR': 'a fermé'}
       
    84                         }
       
    85                     }[verb];
       
    86                 return {
       
    87                     actor: {
       
    88                         objectType: 'Agent',
       
    89                         name: this.currentUser,
       
    90                         account: {
       
    91                             homePage: 'https://www.metaeducation.fr/Utilisateurs/',
       
    92                             name: this.currentUser
       
    93                         }
       
    94                     },
       
    95                     verb: verbNode,
       
    96                     object: {
       
    97                         objectType: 'Activity'
       
    98                     },
       
    99                     context: {
       
   100                         registration: this.registration,
       
   101                         extensions: {
       
   102                             'http://liris.renkantracking.org/application': 'Outil carte mentale'
       
   103                         }
       
   104                     },
       
   105                     timestamp: timestamp.toISOString()
       
   106                 };
       
   107             },
       
   108             _getObjectUrn: function(objectType, objectId) {
       
   109                 return 'urn:mtdc:renkan:'+ objectType + ':' + objectId;
       
   110             },
       
   111             _sendNodeMsg: function(verb, nodeData, changedData, previousData) {
       
   112                 this._sendObjectMsg('node', 'http://www.w3.org/ns/activitystreams#Node', verb, nodeData, changedData, previousData);
       
   113             },
       
   114             _sendEdgeMsg: function(verb, nodeData, changedData, previousData) {
       
   115                 this._sendObjectMsg('edge', 'http://www.w3.org/ns/activitystreams#Edge', verb, nodeData, changedData, previousData);
       
   116             },
       
   117             _sendViewMsg: function(verb, viewData, changedData, previousData) {
       
   118                 this._sendObjectMsg('view', 'http://www.w3.org/ns/activitystreams#View', verb, viewData, changedData, previousData);
       
   119             },
       
   120             _sendObjectMsg: function(objectType, objectTypeUrl, verb, objData, changedData, previousData) {
       
   121 
       
   122                 var msg = this._getBaseMsg(verb);
       
   123 
       
   124                 msg.object = _.merge(msg.object, {
       
   125                     id: this._getObjectUrn(objectType, objData._id), //TODO full url ???
       
   126                     definition: {
       
   127                         name: {
       
   128                             'fr-FR': objData.title
       
   129                         },
       
   130                         description: {
       
   131                             'fr-FR': objData.description
       
   132                         },
       
   133                         type: objectTypeUrl,
       
   134                         extensions: {
       
   135                             'http://www.w3.org/ns/activitystreams#Data': objData,
       
   136                             'http://www.w3.org/ns/activitystreams#DataChanged': changedData, //this part are not sent if undefined
       
   137                             'http://www.w3.org/ns/activitystreams#DataPrevious': previousData //this part are not sent if undefined
       
   138                         }
       
   139                     }
       
   140                 });
       
   141                 msg.context = _.merge(msg.context, {
       
   142                     extensions: {
       
   143                         'http://liris.renkantracking.org/fromCreate': objData.origin
       
   144                     }
       
   145                 });
       
   146 
       
   147                 this._pushTrackingMessage(msg);
       
   148 
       
   149             },
       
   150             addNode: function(nodeData) {
       
   151                 this._sendNodeMsg('add', nodeData);
       
   152             },
       
   153             deleteNode: function(nodeData) {
       
   154                 this._sendNodeMsg('delete', nodeData);
       
   155             },
       
   156             updateNode: function(nodeData, changedData, previousData) {
       
   157                 if('position' in changedData) {
       
   158                     this._sendNodeMsg('move', nodeData, { position: changedData['position']}, { position: previousData['position']});
       
   159                     delete changedData['position'];
       
   160                     delete previousData['position'];
       
   161                 }
       
   162                 if(_.isEmpty(changedData)) {
       
   163                     return;
       
   164                 }
       
   165                 this._sendNodeMsg('update', nodeData, changedData, previousData);
       
   166             },
       
   167             addEdge: function(edgeData) {
       
   168                 this._sendEdgeMsg('add', edgeData);
       
   169             },
       
   170             deleteEdge: function(edgeData) {
       
   171                 this._sendEdgeMsg('delete', edgeData);
       
   172             },
       
   173             updateEdge: function(edgeData, changedData, previousData) {
       
   174                 this._sendEdgeMsg('update', edgeData, changedData, previousData);
       
   175             },
       
   176             addView: function(edgeData) {
       
   177                 this._sendViewMsg('add', edgeData);
       
   178             },
       
   179             deleteView: function(edgeData) {
       
   180                 this._sendViewMsg('delete', edgeData);
       
   181             },
       
   182             updateView: function(viewData, changedData, previousData) {
       
   183                 this._sendViewMsg('update', viewData, changedData, previousData);
       
   184             },
       
   185             closeProject: function() {
       
   186                 this.flushTrackingInfo();
       
   187                 $.ajax({
       
   188                     method: 'POST',
       
   189                     url: this.trackingCloseUrl,
       
   190                     async: false,
       
   191                     headers: {
       
   192                         'X-CSRFToken': this.csrftoken
       
   193                     },
       
   194                     data: {
       
   195                         'renkan_guid': this.renkan.project.get('id'),
       
   196                         'registration': this.registration
       
   197                     }
       
   198 
       
   199                 }).fail(function(){
       
   200                     console.log('send tracking data failed');
       
   201                 }); // this is fire and forget. Nothong tpo do on failed
       
   202             }
       
   203 
       
   204         };
       
   205 
       
   206         return trackingWorker._init();
       
   207     };
       
   208 
       
   209 
       
   210 })(window);