Commit 4ca3fc3e authored by Jérémie Garcia's avatar Jérémie Garcia

Session Cleanup

Should be more efficient
Do not use multiplay anymore
parent ceeb6bb3
////////////////////////////////////////////////////////////
/////////////////////// SESSION ///////////////////////////
/*
* This file contains the namesapce Traj.Session to manage storing and retrieving of
* Trajectories. Sessions are stored as JSON files on the local storage.
* Should probably be updated to work in the server side not the client side
*/
Traj.Session = {
lblCurrentSession : {},
currentSession : {},
traj_dict : [],
playedCurves : [],
dict : [], //dictionarry object used to manage the sessions
lblCurrentSession : {}, //name of the current session loaded
currentSession : {}, //list of current curves in the session
saveTrajSession : function(){
var idx = this.currentSession;
this.traj_dict[idx] = Traj.TrajManager.trajectories;
this.playedCurves[idx] = Traj.TrajManager.playedCurves;
localStorage.trajectories = JSON.stringify(this.traj_dict);
localStorage.playedCurves = JSON.stringify(this.playedCurves);
clearTrajSessions : function(){
localStorage.clear();
},
appendTrajSession : function(){
Traj.Session.saveTrajSession(this.traj_dict.length);
alert("Session Saved with ID " + (this.traj_dict.length - 1));
updateCurrentSessionLbl : function() {
this.lblCurrentSession.innerHTML = 'Session ' + Number(this.currentSession+1) + '/' + Number(this.dict.length);
},
/* loadTrajSession : function(idx){
trajectories = this.traj_dict[idx];
Traj.View.repaintAll();
},
*/
loadTrajSession : function(idx){
var newTrajectories = this.traj_dict[idx];
Traj.TrajManager.trajectories = [];
var newCurve = {};
for (var i = 0; i < newTrajectories.length; i++) {
newCurve = new TimedCurve(newTrajectories[i].sourceNumber);
for (var j = 0; j < this.traj_dict[idx][i].X.length; j++) {
newCurve.addTimedPoint(newTrajectories[i].X[j],newTrajectories[i].Y[j],newTrajectories[i].Z[j],newTrajectories[i].t[j]);
makeTrajectoriesArrayFromDictArray : function(array){
var trajectories= [],
newCurve = {};
for (var i = 0; i < array.length; i++) {
newCurve = new TimedCurve(array[i].sourceNumber);
for (var j = 0; j < this.dict[idx][i].X.length; j++) {
newCurve.addTimedPoint(array[i].X[j],array[i].Y[j],array[i].Z[j],array[i].t[j]);
}
newCurve.sourceNumber = newTrajectories[i].sourceNumber;
newCurve.multiPlay = newTrajectories[i].multiPlay;
Traj.TrajManager.trajectories.push(newCurve);
newCurve.sourceNumber = array[i].sourceNumber;
trajectories.push(newCurve);
}
Traj.TrajManager.playedCurves = (this.playedCurves == []) ? [] : this.playedCurves[idx];
Traj.TrajManager.selectCurve(0);
Traj.View.repaintAll();
//Traj.TrajManager.multiPlayChanged();
return trajectories;
},
loadTrajSession : function(idx){
var newTrajectories = this.dict[idx],
trajectories = makeTrajectoriesArrayFromDictArray(newTrajectories);
Traj.TrajManager.setTrajectories(trajectories);
},
loadSelectedSession : function() {
this.loadTrajSession(this.currentSession);
},
loadLastTrajSession : function(){
if(this.traj_dict.length > 0){
......@@ -54,26 +50,25 @@ Traj.Session = {
}
},
clearTrajSessions : function(){
localStorage.clear();
saveTrajSession : function(){
var idx = this.currentSession;
this.dict[idx] = Traj.TrajManager.trajectories;
localStorage.trajectories = JSON.stringify(this.dict);
},
traj_initStorage : function(){
this.traj_dict = (typeof localStorage.trajectories === 'undefined') ? [] : JSON.parse( localStorage.trajectories );
this.playedCurves = (typeof localStorage.playedCurves === 'undefined') ? [[]] : JSON.parse( localStorage.playedCurves );
Traj.Session.loadLastTrajSession();
this.currentSession = this.traj_dict.length - 1;
if (this.currentSession<0) {
this.newSession();
}
this.lblCurrentSession = document.getElementById('lblCurrentSession');
this.updateCurrentSessionLbl();
saveInSelectedSession : function() {
this.dict[this.currentSession] = Traj.TrajManager.trajectories;
this.saveTrajSession();
},
appendTrajSession : function(){
Traj.Session.saveTrajSession(this.dict.length);
alert("Session Saved with ID " + (this.dict.length - 1));
},
selectNextSession : function() {
this.saveInSelectedSession();
this.currentSession = (this.currentSession + 1) % this.traj_dict.length;
this.currentSession = (this.currentSession + 1) % this.dict.length;
this.loadTrajSession(this.currentSession);
this.updateCurrentSessionLbl();
this.saveTrajSession();
......@@ -81,29 +76,18 @@ Traj.Session = {
selectPreviousSession : function() {
this.saveInSelectedSession();
this.currentSession = (this.currentSession - 1 + this.traj_dict.length) % this.traj_dict.length;
this.currentSession = (this.currentSession - 1 + this.dict.length) % this.dict.length;
this.loadTrajSession(this.currentSession);
this.updateCurrentSessionLbl();
this.saveTrajSession();
},
loadSlectedSession : function() {
this.loadTrajSession(this.currentSession);
},
saveInSelectedSession : function() {
this.traj_dict[this.currentSession] = Traj.TrajManager.trajectories;
this.playedCurves[this.currentSession] = Traj.TrajManager.playedCurves;
this.saveTrajSession();
},
deleteSelectedSession : function() {
if (this.traj_dict.length > 1) {
if (this.dict.length > 1) {
var keepCurrentSession = this.currentSession;
this.selectPreviousSession();
this.traj_dict.splice(keepCurrentSession,1);
this.playedCurves.splice(keepCurrentSession,1);
if (this.currentSession > this.traj_dict.length-1) {
this.dict.splice(keepCurrentSession,1);
if (this.currentSession > this.dict.length-1) {
this.currentSession = 0;
}
this.updateCurrentSessionLbl();
......@@ -111,31 +95,43 @@ Traj.Session = {
},
newSession : function() {
if (this.traj_dict.length > 0) {
if (this.dict.length > 0) {
this.saveInSelectedSession();
}
this.traj_dict.push([]);
this.playedCurves.push([]);
this.currentSession = this.traj_dict.length-1;
this.dict.push([]);
this.currentSession = this.dict.length-1;
this.updateCurrentSessionLbl();
this.loadSlectedSession();
this.loadSelectedSession();
this.saveTrajSession();
},
updateCurrentSessionLbl : function() {
this.lblCurrentSession.innerHTML = 'Session ' + Number(this.currentSession+1) + '/' + Number(this.traj_dict.length);
}
initStorage : function(){
//if it exists load from JSON file otherwise create a new one
this.dict = (typeof localStorage.trajectories === 'undefined') ? [] : JSON.parse( localStorage.trajectories );
//if it exists load from JSON file otherwise create a new one
Traj.Session.loadLastTrajSession();
this.currentSession = this.dict.length - 1;
if (this.currentSession<0) {
this.newSession();
}
this.lblCurrentSession = document.getElementById('lblCurrentSession');
this.updateCurrentSessionLbl();
},
};
////////////////////////////////////////////////////////////
/////////////////////// State (undo) ///////////////////////
/*
*STATE FOR UNDO MECHANISM
Needs to be redon with a better way
*/
Traj.State = {
historySessions : [],
newAction : function() { // call it when you want to save the state of the app
// Save the state of the app
newAction : function() {
var newState = JSON.stringify(Traj.TrajManager.trajectories);
this.historySessions.unshift(newState);
var length = this.historySessions.length;
......@@ -144,20 +140,13 @@ Traj.State = {
}
},
undo : function() { // load previous state saved. Problem : if you change the session and then press undo, you get all the curves from the last session...
// load previous state saved.
//Problem : if you change the session and then press undo, you get all the curves from the last session...
undo : function() {
if (this.historySessions.length>0) {
Traj.TrajManager.trajectories = [];
var lastState = JSON.parse(this.historySessions[0]);
var newCurve = {};
for (var i = 0; i < lastState.length; i++) {
newCurve = new TimedCurve(lastState[i].sourceNumber);
for (var j = 0; j < lastState[i].X.length; j++) {
newCurve.addTimedPoint(lastState[i].X[j],lastState[i].Y[j],lastState[i].Z[j],lastState[i].t[j]);
}
Traj.TrajManager.trajectories.push(newCurve);
}
Traj.TrajManager.selectCurve(0);
Traj.View.repaintAll();
var lastState = JSON.parse(this.historySessions[0]),
trajectories = makeTrajectoriesArrayFromDictArray(lastState);
Traj.TrajManager.setTrajectories(trajectories);
this.historySessions.splice(0,1);
}
},
......
......@@ -8,18 +8,12 @@ function TimedCurve(source) {
this.t = [];
this.color = Traj.Utils.getColorSource(source) || Traj.Utils.getColorSource(1);
this.sourceNumber = source || Number(1);
this.multiPlay = true;
// this.xScale = 1;
// this.yScale = 1;
// this.zScale = 1;
}
TimedCurve.prototype.changeSource = function(source) {
this.sourceNumber = Number(source);
this.color = Traj.Utils.getColorSource(source);
var index = Traj.TrajManager.trajectories.indexOf(this);
Traj.TrajManager.multiPlayChanged(index)
}
......@@ -171,8 +165,6 @@ TimedCurve.prototype.getAllTimedPoints = function () {
return allpoints;
}
TimedCurve.prototype.clone = function () {
var clone = jQuery.extend(true, {}, this)
return clone;
......@@ -185,7 +177,6 @@ TimedCurve.prototype.cloneNoPoints = function() {
return clone;
}
TimedCurve.prototype.lengthTime = function() {
var lengt = this.X.length-1;
var time = this.t[lengt];
......
......@@ -12,6 +12,13 @@ Traj.TrajManager = {
idContextMenu : null,
modifiedCurve : null,
setTrajectories : function(traj_array){
this.trajectories = traj_array;
this.selectCurve(0);
Traj.View.repaintAll();
},
processCreateNewCurve : function(x, y, z, t) {
Traj.OSC.sendDraw('start');
Traj.EventManager.hideContextMenu();
......
......@@ -19,8 +19,8 @@ var Traj = {
//Initializations
Traj.View.initCanvas(); //Drawing
Traj.Session.traj_initStorage(); //storage
Traj.EventManager.trajInitEvents(); // Events
Traj.Session.initStorage(); //storage
Traj.EventManager.initEvents(); // Events
//called before exiting the window to save the current trajectories in the current session
window.onbeforeunload = function (e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment