').append(a);
outer_d.append(d);
}
var work_area = $('#work_area #management_window').empty();
outer_d.appendTo(work_area);
choose_device_event_handler();
} else {
var device_name = device_array[0][0];
$('.choosed').attr('style', 'cursor:pointer;color:blue');
$('.choosed').val(device_name);
var do_id = $('.choosed').parent().parent().find('.setting_obj').attr('name');
var return_info = ajax_bind_device(do_id, device_name, device_array[0][2]);
$('.choosed').attr('name', return_info['d_id']);
ajax_restart_project();
// 顯示 unmount 在 right subwindow
$('#work_area #management_window').empty();
var outer_d = $('
').attr('id', 'dname-list');
var work_area = $('#work_area #management_window').empty();
outer_d.appendTo(work_area);
choose_device_event_handler();
}
});
}
function check_duplicate_and_circle_connection(circle_node, feature_node) {
if ($(feature_node.parents().filter('.device-obj')).attr('name')[0] == 'i') var df_type = 'input';
else var df_type = 'output';
var na_id = $(circle_node.parents().get(1)).attr('name');
var na_idx = circle_node.attr('name');
var df_id = feature_node.attr('name');
check = ajax_check_duplicate_circle_connection(na_id, df_id, df_type);
if (check['is_duplicate'] == '0') {
// deaw line and save conenction
draw_line_between_feature_circle(feature_node, circle_node);
ajax_save_circle_connect_setting(na_idx, na_id, df_id);
// make df module in workspace window
var odfo_id = null;
if (df_type == 'output') {
odfo_id = df_id;
} else {
odfo_id = '0';
}
construct_stage1_setting_block(na_id, odfo_id);
}
else{
if (check['remove_circle'] == 1) {
var c_node = $('div[name='+check['na_id']+'].map-block img')
c_node.addClass('hidden-flag');
var c_label = $(c_node.parents().get(1)).find('label');
c_label.addClass('hidden-flag');
}
if (df_type == 'output') {
construct_stage1_setting_block(na_id, df_id);
}
}
ajax_restart_project();
}
function connect_feature_circle_event_handler() {
$('.map-img-div img').unbind('click');
$('.map-img-div img').on('click', function(e) {
if ($('img.feature-circle-clicked').length != 0) {
$('img.feature-circle-clicked').removeClass('feature-circle-clicked');
} else if ($('div.feature-circle-clicked').length == 0) {
$(this).addClass('feature-circle-clicked');
} else {
var feature_node = $($('div.feature-circle-clicked').get(0));
var circle_node = $(this);
check_duplicate_and_circle_connection(circle_node, feature_node);
feature_node.removeClass('feature-circle-clicked');
feature_node.removeClass('feature-clicked');
}
var na_id = $(this).parent().parent().attr('name');
construct_stage1_setting_block(na_id, '0');
});
}
function send_connection_data_to_server(circle_node, start_node, end_node) {
connection_info = [];
var join_name = $(circle_node.parents().get(1)).find('label').text();
var join_idx = $(circle_node.parents().get(1)).find('label').attr('name');
var idfo_id = $(start_node.get(0)).attr('name'); // start_feature_id
var odfo_id = $(end_node.get(0)).attr('name'); // end_feature_id
connection_info.push(join_name);
connection_info.push(join_idx);
connection_info.push(idfo_id);
connection_info.push(odfo_id);
var id_dict = ajax_save_connection_line(connection_info, p_id);
// set na_id to circle_node
$(circle_node.parents().get(1)).attr('name', id_dict['na_id']);
construct_stage1_setting_block(id_dict['na_id'], id_dict['odfo_id']);
}
function plot_single_device_feature_module(device_feature) {
$('#management_window').empty();
$('
',{ 'id':'module_mapping_setting_outer_block'}).appendTo('#management_window');
var dfo_id = $($(device_feature).get(0)).attr('name');
var fn_info = ajax_get_stage1_df_info(dfo_id);
// 2. 檢查是 idf 還是 odf,取得 device feature id
if ($($(device_feature).parents().get(1)).attr('name')[0] == 'i') {
// 是 idf name="in_device"
var df_outer_block = make_single_idf_block_html(fn_info);
} else {
// 是 odf
var df_outer_block = make_single_odf_block_html(fn_info);
}
df_outer_block.appendTo('#module_mapping_setting_outer_block');
bind_rename_df_alias();
save_idf_default_event_handler();
save_odf_default_event_handler();
get_mf_function_list();
}
function bind_rename_df_alias() {
$('.div-odf-first-tab-title, .div-idf-tab-title-left').unbind('click');
$('.div-odf-first-tab-title, .div-idf-tab-title-left').bind('click', function () {
$(this).unbind('click');
$(this).find('.label-odf, .label-idf').attr('hidden', true);
let alias_name = $(this).find('.label-odf, .label-idf').text();
let textarea = $('
', {'id':'tb-alias-name', 'value': alias_name, 'type':'text', 'width': '100%'});
textarea.appendTo($(this));
$(textarea).select();
$(textarea).focusout(function(){
save_alias_name();
bind_rename_df_alias();
});
$(textarea).keyup(function (e) {
if (e.keyCode == 13) {
save_alias_name();
}
});
});
}
function save_alias_name() {
let textarea = $('.div-odf-first-tab-title, .div-idf-tab-title-left').find('#tb-alias-name');
let label = $('.div-odf-first-tab-title, .div-idf-tab-title-left').find('.label-odf, .label-idf')
let dfo_id = label.attr('name');
let dfo_name = label.text();
let new_name = textarea.val().replace(/^\s+|\s+$/g, ''); //trim() need be a base function
label.attr('hidden', false);
textarea.remove();
if (new_name && new_name != dfo_name) {
label.text(new_name);
$('
', { 'class':'glyphicon glyphicon-pencil'}).appendTo(label);
}
}
function connect_feature_and_feature(device_feature) {
var start_node = $($('div.feature-clicked').get(0));
var end_node = device_feature;
// if click different column
if (start_node.parent().parent().attr('name') != $(device_feature).parent().parent().attr('name')) { // name="in_device" vs name="out_device"
// check these two feature connection is duplicate
if ($($(end_node).parents().get(1)).attr('name')[0] == 'i') {
var idf_id = $($(device_feature).get(0)).attr('name');
var odfo_id = $(start_node.get(0)).attr('name');
} else {
var odfo_id = $($(end_node).get(0)).attr('name');
var idf_id = $(start_node.get(0)).attr('name');
}
var feature_id_list = [idf_id, odfo_id];
var check = ajax_check_duplicate_connection(feature_id_list);
if (check['is_duplicate'] == '0') {
// 沒有重複,畫線
var this_node = $(end_node);
var circle_node = draw_line_between_two_feature(this_node, start_node);
f_info = send_connection_data_to_server(circle_node, start_node, this_node);
circle_node.removeClass('hidden-flag');
var circle_label = $(circle_node.parents().get(1)).find('label');
circle_label.removeClass('hidden-flag');
ajax_restart_project();
}
} // end if click different column
start_node.removeClass('feature-clicked');
start_node.removeClass('feature-circle-clicked');
}
function connect_circle_and_feature(device_feature) {
var circle_node = $('img.feature-circle-clicked');
var feature_node = $(device_feature);
check_duplicate_and_circle_connection(circle_node, feature_node);
circle_node.removeClass('feature-circle-clicked');
}
function save_connection_line() {
var is_odf = $($(this).parents().get(2)).attr('id') == 'out_device_column';
var df_id = $(this).attr('name');
var na_id = '0';
if (is_odf) {
na_id = ajax_check_dfo_is_connected(df_id);
}
// 1. 檢查 circle 有沒有被點
if ($('img.feature-circle-clicked').length != 0) {
// 有被點
// 檢查是不是 odf
if (is_odf) {
// 檢查有沒有 dup line
if (ajax_check_duplicate_circle_connection(na_id, df_id, 'output')['is_duplicate'] == '1') {
// 有 dup line,變紅線、畫 workspace window
construct_stage1_setting_block(na_id, df_id);
} else {
// 沒有 dup line,連線
connect_circle_and_feature(this);
}
} else{
connect_circle_and_feature(this);
}
// 2. 檢查另一邊的 feature 有沒有被點
} else if ($('div.feature-clicked').length != 0) {
// 有被點
// 畫線 feature with feature
connect_feature_and_feature(this)
} else {
// 沒有被點
// 變藍色
$(this).addClass('feature-clicked');
$(this).addClass('feature-circle-clicked');
plot_single_device_feature_module(this);
}
}
function connect_feature_event_handler() {
$('.feature_obj').unbind('click');
$('.feature_obj').on('click', save_connection_line);
}
function redraw_connect_line() {
var wid = $('.div-catagory').width() + "px";
$('.div-catagory').css('height', wid);
$('.setting_obj').css('height', wid);
$('.div-device').css('height', wid);
$('.label-device').css('line-height', wid/2);
$('.label-feature').css('line-height', wid);
$('.div-obstacle').css('height', wid);
$('.img-input-feature').css('height', wid);
$('.img-input-feature').css('width', wid);
$('.img-output-feature').css('height', wid);
$('.img-output-feature').css('width', wid);
$('.feature_obj').css('height', wid);
var work_area_padding = $('#work_area').outerHeight() - $('#management_window').outerHeight();
var in_device_column_height = 0;
var out_device_column_height = 0;
var work_area_height = 0;
var window_height = window.innerHeight-$('nav').height() - 2;
if ($('#in_device_column').find('.div-outer').length > 0) {
in_device_column_height = $('#in_device_column').find('.div-outer').last().offset().top + $('#in_device_column').find('.div-outer').last().height();
}
if ($('#out_device_column').find('.div-outer').length > 0) {
out_device_column_height = $('#out_device_column').find('.div-outer').last().offset().top + $('#out_device_column').find('.div-outer').last().height();
}
if ($('#management_window').find('div').length > 0) {
work_area_height = $('#in_device_column').offset().top + $('#management_window').find('div').first().height();
}
var general_height = Math.max(in_device_column_height, out_device_column_height, work_area_height, window_height, 915);
$('#graphical_layout_window').css('height', general_height);
$('#work_area').css('height', general_height);
var canvas = $('#background');
var ctx = document.getElementById("background").getContext("2d");
ctx.canvas.height = general_height;
ctx.clearRect(0 , 0 , canvas.width(), canvas.height());
//draw lines
var connection_pair = ajax_reload_connect_line(p_id);
$.each(connection_pair, function (index, value ) {
var feature_node = $('.feature_obj[name='+value[1]+']');
var circle_node = $('.map-block[name='+value[0]+']').find('img');
var color = value[2];
draw_line_between_feature_circle(feature_node, circle_node, color);
});
if ($('.div-idf-monitor-outer').length == 0 && $('.div-odf-monitor-outer').length == 0) {
var outer_height = $('.div-idf-monitor-outer .div-add-function-outer').height() - 70;
$('.div-idf-monitor-outer .div-intro-block').css('height', outer_height);
var outer_height = $('.div-join-monitor-outer .div-add-function-outer').height() - 70;
$('.div-join-monitor-outer .div-intro-block').css('height', outer_height+30);
var outer_height = $('.div-odf-monitor-outer .div-add-function-outer').height() - 70;
$('.div-odf-monitor-outer .div-intro-block').css('height', outer_height);
}
}
function bind_move_in_df_function_list() {
$('#add_function').unbind('click');
$('#add_function').on('click', function(e) {
if ($('.other_function_select option:selected').length != 0) {
var fn_info = {'fn_id':'', 'dfo_id':'' };
fn_info['dfo_id'] = parseInt($('.div-program-title > label').attr('name'));
fn_info['fn_id'] = parseInt($('.other_function_select option:selected').attr('name'));
var fn_list = ajax_move_in_df_function_list(fn_info);
// positive_list = { 'other_list': [['x1', 1],...], 'quick_list':[['x2', 3], ...]}
var other_function_select = $('.other_function_select');
other_function_select.empty();
for (var i=0; i', { 'text': fn_list['other_list'][i][0], // function name
'name':fn_list['other_list'][i][1] }).appendTo(other_function_select); // function id
}
var exist_function_select = $('.exist_function_select');
update_positive_list_of_function_management(exist_function_select, fn_list);
}
});
}
function bind_move_out_df_function_list() {
$('#remove_function').unbind('click');
$('#remove_function').on('click', function(e) {
if ($('.exist_function_select option:selected').length != 0) {
// check if function can be remove
var fn_name = $('.exist_function_select option:selected').text();
if ($('.exist_function_select option:selected').length != 0 ) {
var fn_name = $('.exist_function_select option:selected').text();
if (ajax_check_function_is_used(fn_name) == '1') {
alert('This function has been used, it can not be removed');
return;
} else if (fn_name == 'add new function') {
return;
}
}
else {
return;
}
var fn_info = {'fn_id':'', 'dfo_id':'' };
fn_info['dfo_id'] = parseInt($('.div-program-title > label').attr('name'));
fn_info['fn_id'] = parseInt($('.exist_function_select option:selected').attr('name'));
var fn_list = ajax_move_out_df_function_list(fn_info);
// positive_list = { 'other_list': [['x1', 1],...], 'quick_list':[['x2', 3], ...]}
var other_function_select = $('.other_function_select');
other_function_select.empty();
for (var i=0; i', { 'text': fn_list['other_list'][i][0], // function name
'name':fn_list['other_list'][i][1] }).appendTo(other_function_select); // function id
}
var exist_function_select = $('.exist_function_select');
update_positive_list_of_function_management(exist_function_select, fn_list);
}
});
}
function get_function_info(df_id, df_type, dfo_default_func) {
var fn_info = ajax_get_dfo_function_list(df_id);
var add_function_block = make_add_function_block_html(fn_info, df_id, df_type);
$('#module_mapping_setting_outer_block').before(add_function_block);
bind_move_in_df_function_list();
bind_move_out_df_function_list();
var function_content = "def run(*args):\n \n return";
var function_name = "";
var div_add_function_outer = $(".div-add-function-outer");
var edit_function_block = make_edit_function_block_html(function_name, function_content);
edit_function_block.appendTo(div_add_function_outer);
save_function_info_event_handler(df_id, df_type);
close_add_function_event_handler(df_type, dfo_default_func);
// 選取 funtion 時更換 function name 跟 textarea 裡 function 的內容,
$(".exist_function_select").unbind('change');
$(".exist_function_select").change(function() {
selected_fn_name = $(this).val();
if (selected_fn_name != undefined) {
var fn_id = '0';
if ($('.exist_function_select option:selected').val() == 'add new function') {
fn_id = 'add_function';
} else {
fn_id = $('.exist_function_select option:selected').attr('name');
}
fn_info = ajax_get_function_info(fn_id);
function_content = fn_info['code'];
function_version = fn_info['version'];
var argu_info = ajax_check_function_is_switch(fn_id);
if (argu_info['is_switch'] == '1') {
$('#is_switch').prop('checked', true);
$('.input-argument-block').removeClass('disable-flag');
$('.input-argument-block').text(argu_info['non_df_argument']);
}else {
$('#is_switch').prop('checked', false);
$('.input-argument-block').addClass('disable-flag');
}
window['input-program-block'] = function_content;
if (window.myCodeMirror )
window.myCodeMirror.getDoc().setValue(function_content);
if ($('.exist_function_select option:selected').val() == 'add new function') {
$('.function_name').val('');
$('.function_name').attr('name', 'add_function');
} else {
$('.function_name').val(selected_fn_name);
$('.function_name').attr('name', fn_id);
}
s = $('.select-dmc-df');
s.empty();
for (var i=0; i< function_version.length; i++) {
$('