0% found this document useful (0 votes)
1K views

functions

Uploaded by

roco65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

functions

Uploaded by

roco65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

$( document ).

ready(function() {
$('#code').keyup(function()
{
parse();
});
});

function init()
{
data = new google.visualization.DataTable();
data.addColumn('datetime', 'start');
data.addColumn('datetime', 'end');
data.addColumn('string', 'content');
data.addColumn('string', 'className');
data.addColumn('string', 'group');

// specify options
var options = {
width: "100%",
height: "100%",
top: "0px",
editable: true,
layout: "box",
style: "box",
animateZoom: false,
'zoomMax': 2000 * 60 * 60 * 24 * 365 * 10000, // milliseconds
animate: false,
max: new Date(650),
min: new Date(0),
snapEvents: false,
showCustomTime: true,
customStackOrder: orderEvents
};

// Instantiate our timeline object.


vis = new links.Timeline(document.getElementById('mytimeline'));

google.visualization.events.addListener(vis, 'select', onSelect);

// Draw our timeline with the created data and options

data.addRow([new Date(0), new Date(options.max), "", "", ""]);

vis.draw(data, options);

vis.setCustomTime(new Date( 570))

createColorClasses(100);

parse();

$('#modality').click(function()
{
toggleModality();
});

$('#player .next').click(function()
{
nextCollision();
});

$('#player .prev').click(function()
{
prevCollision();
});
}

function orderEvents(item1, item2)


{

function get_random_color()
{
var letters = 'ABCDEF'.split('');
var color = '#';

for (var i = 0; i < 6; i++ )


color += letters[Math.round(Math.random() * 5)];

return color;
}

function createColorClasses(num)
{
var style = document.createElement('style');
style.type = 'text/css';

for(i=0; i< num; i++)


{
style.innerHTML += ".color"+i+" { background-color: "+get_random_color()
+" !important; }";
}
document.getElementsByTagName('head')[0].appendChild(style);
}

function parse()
{
var codeArr = $("#code").val().split("\n");

var detectSubActions = false;

for( var i=0; i < codeArr.length; i++)


{
var currLine = codeArr[i];
currLine = currLine.replace(/\t/g, " ").replace(/\s+/g," ");

if (currLine.indexOf("#define") > -1)


{
var lineArr = currLine.split(" ");

if(lineArr.length > 2)
{
action = lineArr[1];
time = lineArr[2];

mainAction = action.substring(action.indexOf("_")+1);
if(action != undefined && time != undefined)
{
if(action.toUpperCase().indexOf("START") > -1)
{
myActions[mainAction] = {name:
mainAction,start: parseInt(time), duration: "", subActions: new Object()};
}
else if(action.toUpperCase().indexOf("DURATION") > -
1)
{
myActions[mainAction].duration =
parseInt(time);
}
}
}
}
else
{
if(currLine.indexOf("{") > -1)
{
detectSubActions = true;
numSubAction = 0;
}
else if(currLine.indexOf("}") > -1)
{
detectSubActions = false;
}
else //inside actions
{
if(detectSubActions && currLine.indexOf("//|") > -1)
{
currLine = currLine.replace("//| ", "").replace("//|", "");

timing = currLine.substring(0, currLine.indexOf(" "));


nameSubAction = currLine.substring(currLine.indexOf(" ")+1)

if(timing.indexOf(",") > -1)


{
timingArr = timing.split(",");

startSubAction = timingArr[0];
durationSubAction = timingArr[1];

var subaction = {name: nameSubAction, device:


nameSubAction.substring(0, nameSubAction.indexOf(" ")), start:
parseInt(startSubAction)/*parseInt(startSubAction)+myActions[mainAction].start*/,
duration: parseInt(durationSubAction)};
myActions[mainAction].subActions[numSubAction] = subaction;

if(!isBlackList(mainAction))
{

checkDeviceCollisions(subaction.device.toUpperCase(), {name: mainAction,


start: myActions[mainAction].start + subaction.start, duration:
subaction.duration});
}

numSubAction++;
}
}
}
}
}

drawVisualization();
drawCollisionPlayer();
}

function checkDeviceCollisions(device, action)


{
if(myDevices[device] == undefined)
{
myDevices[device] = {collisions: new Array()};
}

for(var i = 0; i < myDevices[device].collisions.length; ++i)


{
var collision = myDevices[device].collisions[i];

var inBounds1 = action.start >= collision.start && action.start <


collision.start + collision.duration;
var inBounds2 = collision.start >= action.start && collision.start <
action.start + action.duration;

if( isCompatible(action.name, collision.name) && (inBounds1 ||


inBounds2))
{
myActions[collision.name].collision = true;
myActions[action.name].collision = true;

var text = (device + " collision between " + action.name + "[" +


action.start + ":" + action.duration + "] and " + collision.name + "[" +
collision.start + ":" + collision.duration + "]");

myCollisions.push({tick: new Date(inBounds1 ? action.start :


collision.start), action1: action.name, action2: collision.name, text: text});
}
}

myDevices[device].collisions.push(action);
}

function isCompatible(action1, action2)


{
if(action1 == action2)
{
return false;
}
else
{
for (var i in myIncompatibles)
{
if(myIncompatibles[i].indexOf(action1) > -1 &&
myIncompatibles[i].indexOf(action2) > -1)
{
return false;
}
}
}

return true;
}

function isBlackList(actionName)
{
return myBlackList.indexOf(actionName) > -1;
}

// Called when the Visualization API is loaded.


function drawVisualization()
{
data.removeRows(0, data.getNumberOfRows());

var t = new Date();

var numAction = 0;

if($("#modality").html() == "Expanded")
{
if(data.getNumberOfColumns() == 5)
data.removeColumn(4);

for(var index in myActions)


{
action = myActions[index];
data.addRow([new Date( parseInt(action.start)), new
Date( parseInt(action.start) + parseInt(action.duration)), action.name,
action.collision != undefined ? "collision" : "color"+numAction]);

numAction++;
}
}
else
{
if(data.getNumberOfColumns() == 4)
data.addColumn("string", "group");

for(var index in myActions)


{
action = myActions[index];

for(var index2 in action.subActions)


{
action2 = action.subActions[index2];
data.addRow([new Date( parseInt(action2.start)), new
Date( parseInt(action2.start) + parseInt(action2.duration)), action2.name, "",
""+action.name]);
}
numAction++;
}
}

vis.redraw();
}

function drawCollisionPlayer()
{
if(myCollisions.length > 0)
{
$("#player").show();
$("div.timeline-customtime").addClass("collisionTimeline");

nextCollision();
}
}

function nextCollision()
{
if(currentCollision < myCollisions.length - 1)
{
currentCollision++;
updateCollision();
}
}

function prevCollision()
{
if(currentCollision > 0)
{
currentCollision--;
updateCollision();
}
}

function updateCollision()
{
vis.setCustomTime(myCollisions[currentCollision].tick);
$("#player .error").html(myCollisions[currentCollision].text);

$(".collision").removeClass("current");

$(".collision").each(function()
{
if($(this).children().text() == myCollisions[currentCollision].action1
|| $(this).children().text() == myCollisions[currentCollision].action2)
{
$(this).addClass("current");
}
});

if(currentCollision + 1 >= myCollisions.length)


{
$("#player .next").hide();
}
else
{
$("#player .next").show();
}

if(currentCollision <= 0)
{
$("#player .prev").hide();
}
else
{
$("#player .prev").show();
}

$("#player .status").html((currentCollision + 1) + " / " +


myCollisions.length);
}

function onSelect()
{
var sel = vis.getSelection();

if (sel.length)
{
if (sel[0].row != undefined)
{
showSubActions(sel[0].row);

//$("#code").val().indexOf(data.getValue(sel[0].row, 2))
// $("#code").setCursorPosition($
("#code").val().indexOf(data.getValue(sel[0].row, 2)));
//console.log("Go to pos "+ $
("#code").val().indexOf(data.getValue(sel[0].row, 2)))
}
}
}

function closeTimeline2()
{
delete data2;
delete vis2;
$("#mytimeline2").hide();
$("#closeTimeline").hide();

vis.setSelection([]);
}

function showSubActions(numRow)
{
var element = $("."+data.getValue(numRow, 3));

if(data2 != undefined)
{
delete data2;
delete vis2;
}

data2 = new google.visualization.DataTable();


data2.addColumn('datetime', 'start');
data2.addColumn('datetime', 'end');
data2.addColumn('string', 'content');
data2.addColumn('string', 'className');

$("#mytimeline2").show();
$("#closeTimeline").show();

$("#closeTimeline").click(function(){
closeTimeline2();
});

// Instantiate our timeline object.


vis2 = new links.Timeline(document.getElementById('mytimeline2'));

index = data.getValue(numRow, 2);

var action = myActions[index];

var maxEnd = 0;

for(var index2 in action.subActions)


{
action2 = action.subActions[index2];
end = parseInt(action2.start) + parseInt(action2.duration);
console.log(action2.start);
data2.addRow([new Date(action2.start), new Date(end), action2.name, ""]);

if(end > maxEnd)


{
maxEnd = end;
}
}

// specify options
var options = {
width: $("#code").width()/*element.width()*/,
height: $("#code").height(),
top: "0px",
editable: true,
layout: "box",
style: "box",
min: new Date(0),
max: new Date(maxEnd+50)
};

vis2.draw(data2, options);

var offset = $("#code").offset();


// offset.top += 12;
// var offset = element.offset();
// offset.top += 30;
// offset.top = $("#code").top();
// offset.left = 0;

var offset2 = $("#code").offset();


offset2.top += 5;
offset2.left += 5;
// var offset2 = $("#code").offset();
// offset2.top += 4;
// offset2.left += element.width() - $("#closeTimeline").width();

$("#closeTimeline").offset(offset2);
$("#mytimeline2").offset(offset);

$("#mytimeline2").width($("#code").width());
$("#mytimeline2").height($("#code").height());
}

$.fn.setCursorPosition = function(pos) {
this.each(function(index, elem) {
if (elem.setSelectionRange) {
elem.setSelectionRange(pos, pos);
} else if (elem.createTextRange) {
var range = elem.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
});
return this;
};

function toggleModality()
{
if($("#modality").html() == "Expanded")
{
$("#modality").html("Grouped");

closeTimeline2();
}
else
{
$("#modality").html("Expanded");
}

drawVisualization();
}

You might also like