var ws;
var running = false;
var load;
var graphrealization;
var subscription;
var subscription_state = 'less';
var save = {};
save['state']= undefined;
save['dsl'] = undefined;
save['endpoints'] = undefined;
save['dataelements'] = undefined;
save['details'] = undefined;
var node_state = {};
var sub_more = 'topic' + '=' + 'running' + '&' +// {{{
'events' + '=' + 'activity_calling,activity_manipulating,activity_failed,activity_done' + '&' +
'topic' + '=' + 'running' + '&' +
'votes' + '=' + 'syncing_after' + '&' +
'topic' + '=' + 'properties/description' + '&' +
'events' + '=' + 'change,error' + '&' +
'topic' + '=' + 'properties/position' + '&' +
'events' + '=' + 'change' + '&' +
'topic' + '=' + 'properties/state' + '&' +
'events' + '=' + 'change' + '&' +
'topic' + '=' + 'properties/dataelements' + '&' +
'events' + '=' + 'change' + '&' +
'topic' + '=' + 'properties/endpoints' + '&' +
'events' + '=' + 'change' + '&' +
'topic' + '=' + 'properties/handlers' + '&' +
'events' + '=' + 'change';// }}}
var sub_less = 'topic' + '=' + 'running' + '&' +// {{{
'events' + '=' + 'activity_calling,activity_manipulating,activity_failed,activity_done' + '&' +
'topic' + '=' + 'properties/position' + '&' +
'events' + '=' + 'change' + '&' +
'topic' + '=' + 'properties/description' + '&' +
'events' + '=' + 'change,error' + '&' +
'topic' + '=' + 'properties/state' + '&' +
'events' + '=' + 'change' + '&' +
'topic' + '=' + 'properties/dataelements' + '&' +
'events' + '=' + 'change' + '&' +
'topic' + '=' + 'properties/endpoints' + '&' +
'events' + '=' + 'change' + '&' +
'topic' + '=' + 'properties/handlers' + '&' +
'events' + '=' + 'change';// }}}
$(document).ready(function() {// {{{
$("input[name=base-url]").val(location.protocol + "//" + location.host + ":9298/");
$("button[name=base]").click(create_instance);
$("button[name=instance]").click(monitor_instance);
$("button[name=loadtestset]").click(load_testset);
$("button[name=loadtestsetfile]").click(load_testsetfile);
$("button[name=savetestset]").click(function(){ save_testset(); });
$("button[name=savesvg]").click(function(){ save_svg(); });
$("input[name=votecontinue]").click(check_subscription);
$.ajax({
url: "testsets/index.xml",
dataType: 'xml',
success: function(res){
$('testset',res).each(function(){
var ts = $(this).text();
$('select[name=testset-names]').append(
$("").attr("value",ts).text(ts)
);
});
}
});
var q = $.parseQuery();
if (q.monitor) {
$("input[name=instance-url]").val(q.monitor);
ui_toggle_vis_tab($("#instance td.switch"));
monitor_instance();
}
if (q.load) {
load = q.load;
ui_toggle_vis_tab($("#instance td.switch"));
create_instance();
}
});// }}}
function check_subscription() { // {{{
var url = $("input[name=current-instance]").val();
var num = 0;
if ($("input[name=votecontinue]").is(':checked')) num += 1;
if (num > 0 && subscription_state == 'less') {
$.ajax({
type: "PUT",
url: url + "/notifications/subscriptions/" + subscription,
data: (
'message-uid' + '=' + 'xxx' + '&' +
sub_more + '&' +
'fingerprint-with-producer-secret' + '=' + 'xxx'
)
});
subscription_state = 'more';
}
if (num == 0 && subscription_state == 'more') {
$.ajax({
type: "PUT",
url: url + "/notifications/subscriptions/" + subscription,
data: (
'message-uid' + '=' + 'xxx' + '&' +
sub_less + '&' +
'fingerprint-with-producer-secret' + '=' + 'xxx'
)
});
subscription_state = 'less';
format_visual_vote_clear();
}
}// }}}
function create_instance() {// {{{
var info = load ? load : prompt("Instance info?", "Enter info here");
if (info != null) {
if (info.match(/\S/)) {
var base = $("input[name=base-url]").val();
$.ajax({
type: "POST",
url: base,
dataType: "text",
data: "info=" + info,
success: function(res){
$("input[name=instance-url]").val((base + "//" + res + "/").replace(/\/+/g,"/").replace(/:\//,"://"));
if (load) monitor_instance();
},
error: function(a,b,c) {
alert("No CPEE running.");
}
});
} else {
alert("An instance info is necessary!");
}
}
}// }}}
function monitor_instance() {// {{{
var url = $("input[name=instance-url]").val();
$('.tabbehind button').hide();
$('#dat_details').empty();
$.ajax({
type: "GET",
url: url + "/properties/schema/",
success: function(res){
$(".tabbed.hidden").removeClass("hidden");
$(".tabbed .tab.hidden").removeClass("hidden");
// Change url to return to current instance when reloading
$("input[name=current-instance]").val(url);
$("#current-instance").text(url);
$("#current-instance").attr('href',url);
history.replaceState({}, '', '?monitor='+url);
ui_tab_click($("#tabinstance")[0]);
// Change url to return to current instance when reloading (because new subscription is made)
$("input[name=votecontinue]").removeAttr('checked');
subscription_state = 'less';
$.ajax({
type: "POST",
url: url + "/notifications/subscriptions/",
data: sub_less,
success: function(res){
res = res.unserialize();
$.each(res,function(a,b){
if (b[0] == 'key') {
subscription = b[1];
}
});
append_to_log("monitoring", "id", subscription);
var Socket = "MozWebSocket" in window ? MozWebSocket : WebSocket;
if (ws) ws.close();
ws = new Socket(url.replace(/http/,'ws') + "/notifications/subscriptions/" + subscription + "/ws/");
ws.onopen = function() {
append_to_log("monitoring", "opened", "");
};
ws.onmessage = function(e) {
data = e.data.parseXML();
if ($('event > topic',data).length > 0) {
switch($('event > topic',data).text()) {
case 'properties/dataelements':
monitor_instance_dataelements();
break;
case 'properties/description':
monitor_instance_dsl();
break;
case 'properties/endpoints':
monitor_instance_endpoints();
break;
case 'properties/state':
monitor_instance_state_change(JSON.parse($('event > notification',data).text()).state);
break;
case 'properties/position':
monitor_instance_pos_change($('event > notification',data).text());
break;
case 'running':
monitor_instance_running($('event > notification',data).text(),$('event > event',data).text());
break;
}
append_to_log("event", $('event > topic',data).text() + "/" + $('event > event',data).text(), $('event > notification',data).text());
}
if ($('vote > topic',data).length > 0) {
var notification = $('vote > notification',data).text();
append_to_log("vote", $('vote > topic',data).text() + "/" + $('vote > vote',data).text(), notification);
monitor_instance_vote_add(notification);
}
};
ws.onclose = function() {
append_to_log("monitoring", "closed", "server down i assume.");
};
if (load) load_testset();
}
});
monitor_instance_dataelements();
monitor_instance_endpoints();
monitor_instance_dsl();
monitor_instance_state();
},
error: function(a,b,c) {
alert("This ain't no CPEE instance");
}
});
}// }}}
function monitor_instance_dataelements() {// {{{
var url = $("input[name=current-instance]").val();
$.ajax({
type: "GET",
url: url + "/properties/values/dataelements/",
success: function(res){
var values = $("value > *",res);
var temp = {};
values.each(function() {
temp[this.nodeName] = format_text($(this).text());
});
var temp_xml = serialize_hash(temp);
if (temp_xml != save['dataelements']) {
save['dataelements'] = temp_xml;
var ctv = $("#dat_dataelements");
ctv.empty();
$.each(temp,function(a,b){
var node = $("#dat_template_pair tr").clone(true);
$('.pair_name',node).val(a);
$('.pair_value',node).val(b);
ctv.append(node);
});
}
}
});
} // }}}
function monitor_instance_endpoints() {// {{{
var url = $("input[name=current-instance]").val();
$.ajax({
type: "GET",
url: url + "/properties/values/endpoints/",
success: function(res){
var values = $("value > *",res);
var temp = {}
values.each(function(){
temp[this.nodeName] = $(this).text();
});
var temp_xml = serialize_hash(temp);
if (temp_xml != save['endpoints']) {
save['endpoints'] = temp_xml;
var ctv = $("#dat_endpoints");
ctv.empty();
$.each(temp,function(a,b){
var node = $("#dat_template_pair tr").clone(true);
$('.pair_name',node).val(a);
$('.pair_value',node).val(b);
ctv.append(node);
});
ctv.append(temp);
}
}
});
}// }}}
function monitor_instance_dsl() {// {{{
var url = $("input[name=current-instance]").val();
$.ajax({
type: "GET",
dataType: "text",
url: url + "/properties/values/dsl/",
success: function(res){
if (res != save['dsl']) {
save['dsl'] = res;
var ctv = $("#areadsl");
ctv.empty();
res = format_code(res,false,true);
res = res.replace(/activity\s+:([A-Za-z][a-zA-Z0-9_]+)/g,"activity :$1");
res = res.replace(/activity\s+\[:([A-Za-z][a-zA-Z0-9_]+)([^\]]*\])/g,"activity [:$1$2");
ctv.append(res);
$.ajax({
type: "GET",
url: url + "/properties/values/dslx/",
success: function(res){
graphrealization = new WfAdaptor(CPEE);
graphrealization.set_svg_container($('#graphcanvas'));
graphrealization.set_description($(res), true);
graphrealization.notify = function(svgid) {
save_description();
manifestation.events.click(svgid,undefined);
};
monitor_instance_pos();
}
});
}
}
});
}// }}}
function monitor_instance_state() {// {{{
var url = $("input[name=current-instance]").val();
$.ajax({
type: "GET",
url: url + "/properties/values/state/",
dataType: "text",
success: function(res){
monitor_instance_state_change(res);
}
});
}// }}}
function monitor_instance_pos() {// {{{
var url = $("input[name=current-instance]").val();
$.ajax({
type: "GET",
url: url + "/properties/values/positions/",
success: function(res){
var values = $("value > *",res);
format_visual_clear();
values.each(function(){
var what = this.nodeName;
format_visual_add(what,"passive");
});
}
});
}// }}}
function monitor_instance_running(notification,event) {// {{{
if (save['state'] == "stopping") return;
var parts = JSON.parse(notification);
if (event == "activity_calling")
format_visual_add(parts.activity,"active")
if (event == "activity_done")
format_visual_remove(parts.activity,"active")
} // }}}
function monitor_instance_state_change(notification) { //{{{
if (notification == "ready" || notification == "stopped" || notification == "running") {
$("#state button").removeAttr('disabled');
}
if (notification != save['state']) {
save['state'] = notification;
var ctv = $("#state");
ctv.empty();
if (notification == "stopped") {
format_visual_clear();
monitor_instance_pos();
}
if (notification == "running") {
format_visual_clear();
}
var but = "";
if (notification == "ready" || notification == "stopped") {
but = " ⇒ / ";
}
if (notification == "running") {
but = " ⇒ ";
}
if (notification == "finished") {
$('.tabbehind button').hide();
} else {
$('#parameters .tabbehind button').show();
}
ctv.append(notification + but);
}
} //}}}
function monitor_instance_pos_change(notification) {// {{{
var parts = JSON.parse(notification);
if (parts['unmark']) {
$.each(parts['unmark'],function(a,b){
format_visual_remove(b,"passive")
});
}
if (parts['at']) {
$.each(parts['at'],function(a,b){
format_visual_add(b,"passive")
});
}
} // }}}
function monitor_instance_vote_add(notification) {// {{{
var parts = JSON.parse(notification);
var ctv = $("#votes");
astr = '';
if ($("input[name=votecontinue]").is(':checked'))
astr += "";
ctv.append(astr);
format_visual_add(parts.activity,"vote")
}// }}}
function monitor_instance_vote_remove(activity,callback,value) {//{{{
var url = $("input[name=current-instance]").val();
$.ajax({
type: "PUT",
url: url + "/callbacks/" + callback,
data: ({'continue': value}),
error: report_failure
});
format_visual_remove(activity,"vote");
$('#vote_to_continue-' + activity + '-' + callback).remove();
}//}}}
function start_instance() {// {{{
var url = $("input[name=current-instance]").val();
$.ajax({
type: "PUT",
url: url + "/properties/values/state",
data: ({value: "running"}),
error: report_failure
});
}// }}}
function sim_instance() {// {{{
var url = $("input[name=current-instance]").val();
$.ajax({
type: "PUT",
url: url + "/properties/values/state",
data: ({value: "simulating"}),
error: report_failure
});
}// }}}
function stop_instance() {// {{{
var url = $("input[name=current-instance]").val();
$.ajax({
type: "PUT",
url: url + "/properties/values/state",
data: ({value: "stopping"}),
error: report_failure
});
}// }}}
function save_testset() {// {{{
var base = $("input[name=current-instance]").val();
var testset = $X('