--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/assets/js/mtdc-tracking-worker.js Mon Jul 25 12:13:09 2016 +0200
@@ -0,0 +1,210 @@
+/* globals operative _ Cookies $ */
+/* eslint no-console: 0 */
+
+
+(function(root) {
+ 'use strict';
+
+ if (typeof root.Mtdc !== 'object') {
+ root.Mtdc = {};
+ }
+
+ var Mtdc = root.Mtdc;
+
+
+ Mtdc.TrackingWorker = function(renkan, trackingUrl, trackingCloseUrl, registration, debounceDelay = 1000) {
+
+ function _sendTrackingInfo() {
+ var trackingMessages = this.trackingMessages;
+ this.trackingMessages = [];
+ if(trackingMessages.length === 0) {
+ return;
+ }
+ $.ajax({
+ method: 'POST',
+ url: this.trackingUrl,
+ headers: {
+ 'X-CSRFToken': this.csrftoken
+ },
+ data: JSON.stringify(trackingMessages),
+ contentType: 'application/json'
+ }).fail(function(){
+ console.log('send tracking data failed', trackingMessages);
+ }); // this is fire and forget. Nothong to do on done
+ }
+
+ var trackingWorker = {
+ trackingMessages: [],
+ currentUser: renkan.current_user,
+ renkan: renkan,
+ trackingUrl: trackingUrl,
+ trackingCloseUrl: trackingCloseUrl,
+ getUUID4 : function() {
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
+ var r = Math.random() * 16 | 0,
+ v = c === 'x' ? r : (r & 0x3 | 0x8);
+ return v.toString(16);
+ });
+ },
+ _init: function() {
+ this.sendTrackingInfo = _.debounce(_.bind(_sendTrackingInfo, this), debounceDelay);
+ this.csrftoken = Cookies.get('csrftoken');
+ this.registration = registration ? registration : this.getUUID4();
+ return this;
+ },
+ _pushTrackingMessage: function(msg) {
+ this.trackingMessages.push(msg);
+ this.sendTrackingInfo();
+ },
+ flushTrackingInfo() {
+ //this.sendTrackingInfo.flush();
+ },
+ _getBaseMsg: function(verb) {
+ var timestamp = new Date(),
+ verbNode = {
+ add: {
+ id: 'http://activitystrea.ms/schema/1.0/create',
+ display: { 'fr-FR': 'a créé' }
+ },
+ update: {
+ id: 'http://activitystrea.ms/schema/1.0/update',
+ display: { 'fr-FR': 'a modifié' }
+ },
+ delete: {
+ id: 'http://activitystrea.ms/schema/1.0/delete',
+ display: { 'fr-FR': 'a supprimé' }
+ },
+ move: {
+ id: 'http://activitystrea.ms/schema/1.0/move',
+ display: { 'fr-FR': 'a déplacé'}
+ },
+ close: {
+ id: 'http://activitystrea.ms/schema/1.0/close',
+ display: { 'fr-FR': 'a fermé'}
+ }
+ }[verb];
+ return {
+ actor: {
+ objectType: 'Agent',
+ name: this.currentUser,
+ account: {
+ homePage: 'https://www.metaeducation.fr/Utilisateurs/',
+ name: this.currentUser
+ }
+ },
+ verb: verbNode,
+ object: {
+ objectType: 'Activity'
+ },
+ context: {
+ registration: this.registration,
+ extensions: {
+ 'http://liris.renkantracking.org/application': 'Outil carte mentale'
+ }
+ },
+ timestamp: timestamp.toISOString()
+ };
+ },
+ _getObjectUrn: function(objectType, objectId) {
+ return 'urn:mtdc:renkan:'+ objectType + ':' + objectId;
+ },
+ _sendNodeMsg: function(verb, nodeData, changedData, previousData) {
+ this._sendObjectMsg('node', 'http://www.w3.org/ns/activitystreams#Node', verb, nodeData, changedData, previousData);
+ },
+ _sendEdgeMsg: function(verb, nodeData, changedData, previousData) {
+ this._sendObjectMsg('edge', 'http://www.w3.org/ns/activitystreams#Edge', verb, nodeData, changedData, previousData);
+ },
+ _sendViewMsg: function(verb, viewData, changedData, previousData) {
+ this._sendObjectMsg('view', 'http://www.w3.org/ns/activitystreams#View', verb, viewData, changedData, previousData);
+ },
+ _sendObjectMsg: function(objectType, objectTypeUrl, verb, objData, changedData, previousData) {
+
+ var msg = this._getBaseMsg(verb);
+
+ msg.object = _.merge(msg.object, {
+ id: this._getObjectUrn(objectType, objData._id), //TODO full url ???
+ definition: {
+ name: {
+ 'fr-FR': objData.title
+ },
+ description: {
+ 'fr-FR': objData.description
+ },
+ type: objectTypeUrl,
+ extensions: {
+ 'http://www.w3.org/ns/activitystreams#Data': objData,
+ 'http://www.w3.org/ns/activitystreams#DataChanged': changedData, //this part are not sent if undefined
+ 'http://www.w3.org/ns/activitystreams#DataPrevious': previousData //this part are not sent if undefined
+ }
+ }
+ });
+ msg.context = _.merge(msg.context, {
+ extensions: {
+ 'http://liris.renkantracking.org/fromCreate': objData.origin
+ }
+ });
+
+ this._pushTrackingMessage(msg);
+
+ },
+ addNode: function(nodeData) {
+ this._sendNodeMsg('add', nodeData);
+ },
+ deleteNode: function(nodeData) {
+ this._sendNodeMsg('delete', nodeData);
+ },
+ updateNode: function(nodeData, changedData, previousData) {
+ if('position' in changedData) {
+ this._sendNodeMsg('move', nodeData, { position: changedData['position']}, { position: previousData['position']});
+ delete changedData['position'];
+ delete previousData['position'];
+ }
+ if(_.isEmpty(changedData)) {
+ return;
+ }
+ this._sendNodeMsg('update', nodeData, changedData, previousData);
+ },
+ addEdge: function(edgeData) {
+ this._sendEdgeMsg('add', edgeData);
+ },
+ deleteEdge: function(edgeData) {
+ this._sendEdgeMsg('delete', edgeData);
+ },
+ updateEdge: function(edgeData, changedData, previousData) {
+ this._sendEdgeMsg('update', edgeData, changedData, previousData);
+ },
+ addView: function(edgeData) {
+ this._sendViewMsg('add', edgeData);
+ },
+ deleteView: function(edgeData) {
+ this._sendViewMsg('delete', edgeData);
+ },
+ updateView: function(viewData, changedData, previousData) {
+ this._sendViewMsg('update', viewData, changedData, previousData);
+ },
+ closeProject: function() {
+ this.flushTrackingInfo();
+ $.ajax({
+ method: 'POST',
+ url: this.trackingCloseUrl,
+ async: false,
+ headers: {
+ 'X-CSRFToken': this.csrftoken
+ },
+ data: {
+ 'renkan_guid': this.renkan.project.get('id'),
+ 'registration': this.registration
+ }
+
+ }).fail(function(){
+ console.log('send tracking data failed');
+ }); // this is fire and forget. Nothong tpo do on failed
+ }
+
+ };
+
+ return trackingWorker._init();
+ };
+
+
+})(window);