;(function(global, $) { 'use strict'; $.fn.milchart = function(options){ const BAR_PADDING = 10, //barのpadding DRAGGING_TIME = 150, //150ms以上のmousedownで、drag判定。それ以外はclick判定。ダブルクリックが硬いときは値を大きくする CLICK_SPAN = 200, //200ms以下の間隔で連続クリックした場合、doubleclick判定。それ以外はclick判定 AUTO_SCROLLING_MARGIN = 50, //auto_scroll開始位置。左右の端からドラッグした場合。 [xx] px; LEFT_TITLE_WIDTH = 150, //左端のタイトルCELLの幅 TOOLTIP_OFFSET_LEFT = 10, //Tooltipの左方向のオフセットpx TOOLTIP_OFFSET_DOWN = 0, //Tooltipの下方向のオフセットpx CHART_WINDOWS_NAME = '.milchart_window', //チャートが含まれるwindowクラス、or window ID STICKY_TABLE_NAME = '.sticky_table', //sticky_tableのクラス、or window ID TARGET_MODAL_CONTENT = 'modal_content', //変更しなくてよい SUMMARY_NAME = '.summary', //変更しなくてよい DATA_CHART_CLASS = 'data_chart', //変更しなくてよい DATA_CHART_ID = 'data_chart'; //変更しなくてよい var defaults = { gtdata : "", vname_arr : "", ctrl_script : "", }, setting = $.extend(defaults, options), is_drag_new_bar, is_drag_move_bar, is_drag_move_grabpoint, is_drag_move_tooltip, is_drag_move_sortobj, tooltip_offset_left, tooltip_offset_down, scrolling, cell_w, cell_h, vname_w, vname_h, sticky_header, bar_padding, dragging_time, click_span, auto_scrolling_margin, left_title_w, chart_window, sticky_table, target_modal_content, _selected_arr = [], selected_arr = [], timer_id = null, $target = $(this), $target_table = $(this).find('table.chart_table'), $target_td = $(this).find('td'), target_area_width, target_area_height, target_area_left, target_area_top, target_table_w, target_table_h, move_block_count, left_max_date, right_max_date, summary_name, data_chart_class, data_chart_id, fp, /*============================================================================================* * * ■ server * * ・ctrl * ・ * *============================================================================================*/ server = { ctrl : function(ctrl_type, data) { console.log(ctrl_type); console.log(data); //validation if ( !( ctrl_type === 'get_tooltip_content' || ctrl_type === 'get_modal_content' || ctrl_type === 'change_order' || ctrl_type === 'create' || ctrl_type === 'update' || ctrl_type === 'delete' ) ) { return false; } //formから送信時 if (data.modal === 'true') { data.plan_class = data.class; data.plan_title = data.title; data.plan_id = data.id; } var send_data = {}; send_data['ctrl_type'] = ctrl_type; $.each(data,function(key,value){ if (key === 'plan_id') { key = 'id'; } send_data[key] = value; }); $.each(send_data,function(key,value){ if (typeof value == 'undefined') { send_data[key] = ''; } }); var vname_value = util.get_url_param('vname'), add_get_param = ''; //GETパラメータのvnameのvalueをチェック if ( vname_value !== '' ) { if (!util.sanitize_url_param(setting.vname_arr, vname_value) ) { return false; } else { add_get_param = '?vname=' + vname_value; } } else { add_get_param = ''; } var ctrl_script = setting.ctrl_script + add_get_param; $.ajax({ type: "POST", url : ctrl_script, data: send_data, }).done(function (result) { if (ctrl_type === 'get_tooltip_content') { $('.tooltip[data-plan_id="' + send_data.id + '"]').append(result); } else if (ctrl_type === 'get_modal_content') { $('#' + target_modal_content).html(result); //buttonイベントとcloseイベント追加 add_event.set_event_overlay(); } else if (ctrl_type === 'change_order') { //reload location.reload(); } else { if (result !== 'false') { /*----------------------------------* * createとupdateは共通の処理 *----------------------------------*/ if (ctrl_type === 'create' || ctrl_type === 'update') { /*----------------------------------* * selected_arrを作成 *----------------------------------*/ selected_arr = util.get_consecutive_date_arr(data.start_date,data.end_date); /*----------------------------------* * barをselected_arrから新規作成 *----------------------------------*/ add_dom.add_bar(data.bar_id, selected_arr, data.vid, data.plan_id, data.plan_class, data.plan_title); /*----------------------------------* * grabpointを追加 *----------------------------------*/ add_dom.add_grabpoint(data.bar_id); if (ctrl_type === 'create') { $('.bar[data-bar_id="' + data.bar_id + '"]').attr('data-plan_id',result); $('.delete_point[data-bar_id="' + data.bar_id + '"]').remove(); } /*----------------------------------* * モーダルから更新でalert表示 *----------------------------------*/ if (typeof data.modal != 'undefined') { alert("データを更新しました。"); } } else /*----------------------------------* * delete *----------------------------------*/ if (ctrl_type === 'delete') { $('.bar[data-bar_id="' + data.bar_id + '"]').remove(); alert("データを削除しました。"); } $('.overlay').remove(); setTimeout(function() { $.each(fp,function(k,v){ fp[k].destroy();//flatpickr削除 }); }, 1000); } else { if (data.plan_id !== '') { alert("データ更新に失敗しました。"); } } } }).fail(function () { }).always(function () { }); }, }, /*============================================================================================* * * ■ add_event * * ・init * ・set_event_grabpoint * ・set_event_create_bar * ・set_event_move_bar * ・set_event_auto_scroll * ・set_event_delete_point * ・set_event_overlay * ・set_event_modal_calendar * ・set_event_move_tooltip * ・ * *============================================================================================*/ add_event = { /*--------------------------* * init *--------------------------*/ init : function() { add_event.reset(); is_drag_new_bar = false, is_drag_move_bar = false, is_drag_move_grabpoint = false, is_drag_move_tooltip = false, is_drag_move_sortobj = false, scrolling = false, move_block_count = 0, bar_padding = BAR_PADDING, dragging_time = DRAGGING_TIME, click_span = CLICK_SPAN, auto_scrolling_margin = AUTO_SCROLLING_MARGIN, left_title_w = LEFT_TITLE_WIDTH, chart_window = CHART_WINDOWS_NAME, sticky_table = STICKY_TABLE_NAME, target_modal_content = TARGET_MODAL_CONTENT, summary_name = SUMMARY_NAME, data_chart_class = DATA_CHART_CLASS, data_chart_id = DATA_CHART_ID, tooltip_offset_left = TOOLTIP_OFFSET_LEFT, tooltip_offset_down = TOOLTIP_OFFSET_DOWN; target_table_w = $target_table.outerWidth(); target_table_h = $target_table.outerHeight(); $(".viewport").css("width",target_table_w + 'px'); $(".viewport").css("height",target_table_h + 'px'); add_dom.add_initial_data(setting.gtdata);//DBのデータをチャート上へ呼び出し add_event.set_event_create_bar(); add_event.set_event_move_bar(); add_event.set_event_grabpoint(); add_event.set_event_move_vname(); add_event.set_event_auto_scroll(); add_event.set_event_key_control(); util.scroll_window(); util.set_change_vname(); }, /*--------------------------* * reset * windowサイズを変更した場合 *--------------------------*/ reset : function() { cell_w = $target_table.find('td').first().outerWidth(), cell_h = $target_table.find('td').first().outerHeight(), vname_w = $('.data_vid th').first().outerWidth(), vname_h = $('.data_vid th').first().outerHeight(), sticky_header = $(sticky_table).find('thead').outerHeight(), target_area_width = $target.outerWidth(), target_area_height = $target.outerHeight(), //ブラウザスクロールの影響は受けないが、インラインフレームのスクロール量の影響を受ける target_area_left = $target.offset().left, target_area_top = $target.offset().top, left_max_date = $($target_table.find('td')[0]).attr('data-date'), right_max_date = $target_table.find('td:last').attr('data-date'); }, /*----------------------------------------* * * set_event_grabpoint * * grabpointをクリックした際のイベント * * *----------------------------------------*/ set_event_grabpoint : function() { var clicks = 0, bar_id = '', clickdowntime = 0, click_pos_x = 0, click_pos_y = 0, move_x = 0, start_grabpoint_x = 0, start_grabpoint_y = 0, start_bar_x = 0, start_bar_y = 0, grabpoint_size_offset = 0, grabpoint_horizontal = '', move_grabpoint_block_count = 0, bar_w = 0, bar_h = 0, bar_left_position = '', bar_right_position = '', $bar_obj = {}, vid = '', start_date = '', end_date = '', plan_title = '', plan_id = '', plan_class = '', selected_arr = []; $(document).on({ 'mousedown' : function(e) { /*-------------------------------* * grabpointの中心から端までの距離 *-------------------------------*/ grabpoint_size_offset = $(this).outerWidth() / 2; /*-------------------------------* * 動かそうとしているbarのID *-------------------------------*/ bar_id = $(this).attr('data-bar_id'); /*-------------------------------* * barのオブジェクト *-------------------------------*/ $bar_obj = $('.bar[data-bar_id=' + bar_id + ']'); /*-------------------------------* * barのvid *-------------------------------*/ vid = $bar_obj.attr('data-vid'); /*-------------------------------* * barのtitle *-------------------------------*/ plan_title = $bar_obj.attr('title'); /*-------------------------------* * barのpid *-------------------------------*/ plan_id = $bar_obj.attr('data-plan_id'); /*-------------------------------* * barのclass *-------------------------------*/ plan_class = $bar_obj.attr('data-plan_class'); /*-------------------------------* * barのstart_date *-------------------------------*/ start_date = $bar_obj.attr('data-start_date'); /*-------------------------------* * barのend_date *-------------------------------*/ end_date = $bar_obj.attr('data-end_date'); /*-------------------------------* * クリック開始時間 *-------------------------------*/ clickdowntime = new Date().getTime(); /*-------------------------------* * grabpointドラッグフラグを立てる *-------------------------------*/ is_drag_move_grabpoint = true; /*-------------------------------* * barの X,Y 起点位置 *-------------------------------*/ start_bar_x = $bar_obj.position().left; start_bar_y = $bar_obj.position().top; /*-------------------------------* * grabpointの X,Y 起点位置 *-------------------------------*/ start_grabpoint_x = $(this).position().left; start_grabpoint_y = $(this).position().top; /*-------------------------------* * grabpointの horizontal *-------------------------------*/ grabpoint_horizontal = $(this).attr("data-horizontal"); /*-------------------------------* * クリックしたカーソルの位置を保存 *-------------------------------*/ click_pos_x = e.clientX; click_pos_y = e.clientY; /*------------------------------------------* * 左右へ移動したcellのブロック数(距離) *------------------------------------------*/ move_grabpoint_block_count = 0; /*-------------------------------* * barの幅と高さ *-------------------------------*/ bar_w = $bar_obj.outerWidth(); bar_h = $bar_obj.outerHeight(); /*----------------------------------* * z-index再設定 *----------------------------------*/ $bar_obj.css('z-index','20'); }, 'mouseup' : function(e) { /*----------------------------------* * キャンセル処理 *----------------------------------*/ if (!is_drag_move_grabpoint) { return ; } /*----------------------------------* * grabpoint移動フラグを下げる *----------------------------------*/ is_drag_move_grabpoint = false; /*----------------------------------* * クリックアップ時刻を計測 *----------------------------------*/ var clickuptime = new Date().getTime(); // ★click up if (clickuptime - clickdowntime < dragging_time) { clicks++; if (clicks == 1) { setTimeout(function(){ //single if(clicks == 1) { //double } else { /*----------------------------------* * ほかの行の選択テキストを解除 *----------------------------------*/ window.getSelection().removeAllRanges(); } clicks = 0; }, click_span, $(this)); } // ★drag up } else { /*----------------------------------* * barとgrabpointを移動 *----------------------------------*/ var date_type = false; if (grabpoint_horizontal === 'right') { end_date = util.get_count_up_date(end_date,move_grabpoint_block_count,date_type); } else if ( grabpoint_horizontal === 'left') { start_date = util.get_count_up_date(start_date,move_grabpoint_block_count,date_type); } /*----------------------------------* * 新しい配列を作成 *----------------------------------*/ selected_arr = util.get_consecutive_date_arr(start_date, end_date); /*----------------------------------* * barをselected_arrから新規作成 *----------------------------------*/ add_dom.add_bar(bar_id,selected_arr,vid,plan_id,plan_class,plan_title); /*----------------------------------* * grabpointを追加 *----------------------------------*/ add_dom.add_grabpoint(bar_id); /*----------------------------------* * z-index再設定 *----------------------------------*/ $bar_obj.css('z-index','10');//戻す /*----------------------------------* * データベースに保存 *----------------------------------*/ var ctrl_type = 'update', data = { "bar_id" : bar_id, "vid" : vid, "plan_id" : plan_id, "plan_class" : plan_class, "plan_title" : plan_title, "start_date" : selected_arr[0], "end_date" : selected_arr.slice(-1)[0], }; server.ctrl(ctrl_type, data); /*----------------------------------* * 配列をリセット *----------------------------------*/ selected_arr = []; } }, 'mousemove' : function(e) { if (is_drag_move_grabpoint) { /*------------------------------------------* * 左右へ移動したgrabpointのpx(距離) *------------------------------------------*/ move_x = e.clientX - click_pos_x; /*----------------------------------* * grabpointを移動 *----------------------------------*/ $(this).css('left', start_grabpoint_x + move_x + $target.scrollLeft() + 'px'); /*----------------------------------* * barを伸縮 *----------------------------------*/ var new_barwidth; if ( grabpoint_horizontal === 'right') { new_barwidth = bar_w + move_x; } else if ( grabpoint_horizontal === 'left') { new_barwidth = bar_w - move_x; $bar_obj.css( 'left', (start_bar_x + move_x) + $target.scrollLeft() + 'px') } $bar_obj.css( 'width', new_barwidth + 'px') /*------------------------------------------* * 左右へ移動したcellのブロック数(距離) *------------------------------------------*/ move_grabpoint_block_count = Math.round( move_x / (cell_w) ); /*------------------------------------------* * delete_pointの移動 *------------------------------------------*/ add_dom.add_delete_point(bar_id); } }, 'mouseleave': function(e) { if (is_drag_move_grabpoint) { /*----------------------------------* * grabpoint移動フラグを下げる *----------------------------------*/ is_drag_move_grabpoint = false; /*----------------------------------* * barとgrabpointを移動 *----------------------------------*/ var date_type = false; if (grabpoint_horizontal === 'right') { end_date = util.get_count_up_date(end_date,move_grabpoint_block_count,date_type); } else if ( grabpoint_horizontal === 'left') { start_date = util.get_count_up_date(start_date,move_grabpoint_block_count,date_type); } /*----------------------------------* * 新しい配列を作成 *----------------------------------*/ selected_arr = util.get_consecutive_date_arr(start_date, end_date); /*----------------------------------* * barをselected_arrから新規作成 *----------------------------------*/ add_dom.add_bar(bar_id,selected_arr,vid,plan_id,plan_class,plan_title); /*----------------------------------* * grabpointを追加 *----------------------------------*/ add_dom.add_grabpoint(bar_id); /*----------------------------------* * z-index再設定 *----------------------------------*/ $bar_obj.css('z-index','10');//戻す /*----------------------------------* * データベースに保存 *----------------------------------*/ var ctrl_type = 'update', data = { "bar_id" : bar_id, "vid" : vid, "plan_id" : plan_id, "plan_class" : plan_class, "plan_title" : plan_title, "start_date" : selected_arr[0], "end_date" : selected_arr.slice(-1)[0], }; server.ctrl(ctrl_type, data); /*----------------------------------* * 配列をリセット *----------------------------------*/ selected_arr = []; /*----------------------------------* * ほかの行の選択テキストを解除 *----------------------------------*/ window.getSelection().removeAllRanges(); } }, },".grabpoint"); }, /*----------------------------------------* * * set_event_create_bar * * 新しくbarを作る場合 * * *----------------------------------------*/ set_event_create_bar : function() { var vid = ''; $target_td.on({ 'mousedown' : function(e) { /*----------------------------------* * フラグ *----------------------------------*/ if (is_drag_new_bar) { util.reset_create_bar(); } /*----------------------------------* * vid取得 *----------------------------------*/ vid = $(this).attr('data-vid'); /*----------------------------------* * cellデータ取得 *----------------------------------*/ var date = $(this).attr('data-date'); /*--------------------------------------------* * date,vidが取得できなかったらキャンセル *--------------------------------------------*/ if ( (typeof date == 'undefined' || date == '') || (typeof vid == 'undefined' || vid == '') ) { util.reset_create_bar(); } /*--------------------------------------------* * padding外をクリックした際はキャンセル *--------------------------------------------*/ if ( !( ( (e.offsetX > (bar_padding / 2)) && (e.offsetX < cell_w - (bar_padding / 2)) ) && ( (e.offsetY > (bar_padding / 2)) && (e.offsetY < cell_h - (bar_padding / 2)) ) ) ) { util.reset_create_bar(); return false; } if (e.which === 1) { is_drag_new_bar = true; } selected_arr = []; }, 'mouseup' : function(e) { if (is_drag_new_bar){ /*----------------------------------* * validation *----------------------------------*/ if (selected_arr.length === 0) { util.reset_create_bar(); return false; } /*----------------------------------* * barをdomに追加 *----------------------------------*/ var new_bar_id = util.get_unique_number(); add_dom.add_bar(new_bar_id, selected_arr, vid); /*----------------------------------* * drag終了 *----------------------------------*/ clearInterval(timer_id); scrolling = false; util.reset_create_bar(); return false; } }, 'mousemove' : function(e) { if (is_drag_new_bar) { /*----------------------------------* * cellデータ取得 *----------------------------------*/ var obj = document.elementFromPoint(e.pageX - window.scrollX, e.pageY - window.scrollY ), date = $(obj).attr('data-date'); /*--------------------------------------------* * date,vidが取得できなかったらキャンセル *--------------------------------------------*/ if ( (typeof date == 'undefined' || date == '') ) { util.reset_create_bar(); } /*--------------------------------------------* * 行を固定 *--------------------------------------------*/ var this_vid = $(obj).attr('data-vid'); if ( typeof this_vid == 'undefined' || this_vid == '' ) { util.reset_create_bar(); return false; } if (vid !== this_vid) { return false; } /*----------------------------------* * idを配列に格納 *----------------------------------*/ _selected_arr.push(date); selected_arr = util.push_id(_selected_arr, vid, date); } }, }); }, /*----------------------------------------* * * set_event_move_bar * * barをクリックした際のイベント * * *----------------------------------------*/ set_event_move_bar : function() { var clicks = 0, clickdowntime = 0, start_bar_x = 0, start_bar_y = 0, move_x = 0, click_pos_x = 0, click_pos_y = 0, bar_id = '', bar_left_position = '', bar_right_position = '', $bar_obj = {}, vid = '', plan_title = '', plan_id = '', plan_class = '', default_selected_arr, selected_arr = []; $(document).on({ 'mousedown' : function(e) { /*-------------------------------* * barのdata-id *-------------------------------*/ bar_id = $(this).attr('data-bar_id'); /*-------------------------------* * barのvid *-------------------------------*/ vid = $(this).attr('data-vid'); /*-------------------------------* * barのtitle *-------------------------------*/ plan_title = $(this).attr('title'); /*-------------------------------* * barのpid *-------------------------------*/ plan_id = $(this).attr('data-plan_id'); /*-------------------------------* * barのclass *-------------------------------*/ plan_class = $(this).attr('data-plan_class'); /*-------------------------------* * selected_arrを取得 *-------------------------------*/ selected_arr = util.get_consecutive_date_arr($(this).attr('data-start_date'), $(this).attr('data-end_date')); if ( !selected_arr ) { is_drag_move_bar = false; $('.bar').removeClass('selected'); $('.grabpoint').remove(); $(this).css('z-index','10'); selected_arr = []; return false; } /*-------------------------------* * クリック開始時間 *-------------------------------*/ clickdowntime = new Date().getTime(); /*-------------------------------* * barドラッグフラグを立てる *-------------------------------*/ is_drag_move_bar = true; /*-------------------------------* * barの X,Y 起点位置 *-------------------------------*/ start_bar_x = $(this).position().left; start_bar_y = $(this).position().top; /*-------------------------------* * クリックしたカーソルの位置を保存 *-------------------------------*/ click_pos_x = e.clientX; click_pos_y = e.clientY; /*------------------------------------------* * 左右へ移動したbarのpx(距離) *------------------------------------------*/ move_x = 0; /*----------------------------------* * z-index再設定 *----------------------------------*/ $(this).css('z-index','20'); /*----------------------------------* * barの右端と左端の位置 *----------------------------------*/ bar_left_position = parseInt($(this).css('left'),10); bar_right_position = parseInt($(this).css('left'),10) + (selected_arr.length * cell_w - (bar_padding)); /*----------------------------------* * 動かす前に最初の配列を保存 *----------------------------------*/ default_selected_arr = selected_arr; }, 'mousemove' : function(e) { if (is_drag_move_bar) { if ( $(this).hasClass('selected') ) { /*----------------------------------* * validation *----------------------------------*/ //移動量 move_x = e.clientX - click_pos_x; //動かしたブロック数 move_block_count = Math.round( move_x / cell_w ); /*-------------------------------* * selected_arrを取得 *-------------------------------*/ selected_arr = util.get_after_move_date_arr(bar_id, move_block_count); /*----------------------------------* * validation *----------------------------------*/ if ( !selected_arr ) { is_drag_move_bar = false; $('.bar').removeClass('selected'); $('.grabpoint').remove(); $(this).css('z-index','10'); selected_arr = []; return false; } /*----------------------------------* * barとgrabpointを移動 *----------------------------------*/ $(this).css('left', start_bar_x + move_x + 'px'); $('.grabpoint[data-bar_id="' + bar_id + '"][data-horizontal="left"]').css('left', bar_left_position + move_x + 'px'); $('.grabpoint[data-bar_id="' + bar_id + '"][data-horizontal="right"]').css('left', bar_right_position + move_x + 'px'); /*------------------------------------------* * delete_pointの移動 *------------------------------------------*/ add_dom.add_delete_point(bar_id); } } }, 'mouseup' : function(e) { /*----------------------------------* * キャンセル処理 *----------------------------------*/ if (!is_drag_move_bar) { return ; } /*----------------------------------* * bar移動フラグを下げる *----------------------------------*/ is_drag_move_bar = false; /*----------------------------------* * クリックアップ時刻を計測 *----------------------------------*/ var clickuptime = new Date().getTime(), $this_obj = $(this); // ★click up if (clickuptime - clickdowntime < dragging_time) { clicks++; if (clicks == 1) { setTimeout(function(){ //single if(clicks == 1) { if ( !$this_obj.hasClass('selected') ) { /*-------------------------------* * validation *-------------------------------*/ if ( !selected_arr ) { is_drag_move_bar = false; $('.bar').removeClass('selected'); $('.grabpoint').remove(); $(this).css('z-index','10'); selected_arr = []; return false; } /*-------------------------------* * grabpointを新規で作成 *-------------------------------*/ add_dom.add_grabpoint(bar_id); /*----------------------------------* * 配列をリセット *----------------------------------*/ selected_arr = []; } //double } else { add_dom.add_overlay(bar_id,plan_id); /*----------------------------------* * ほかの行の選択テキストを解除 *----------------------------------*/ window.getSelection().removeAllRanges(); } clicks = 0; }, click_span, $this_obj); } // ★drag up } else { /*----------------------------------* * validation *----------------------------------*/ if (bar_id != $(this).attr('data-bar_id')) { return ; } if ( !selected_arr || selected_arr.length === 0 ) { is_drag_move_bar = false; $('.bar').removeClass('selected'); $('.grabpoint').remove(); $(this).css('z-index','10'); selected_arr = []; return false; } /*----------------------------------* * z-index再設定 *----------------------------------*/ $(this).css('z-index','10');//戻す /*----------------------------------* * barをselected_arrから新規作成 *----------------------------------*/ add_dom.add_bar(bar_id, selected_arr, vid, plan_id, plan_class, plan_title); /*----------------------------------* * grabpointを追加 *----------------------------------*/ add_dom.add_grabpoint(bar_id); /*----------------------------------* * データベースに保存★ *----------------------------------*/ var ctrl_type = 'update', data = { "bar_id" : bar_id, "vid" : vid, "plan_id" : plan_id, "plan_class" : plan_class, "plan_title" : plan_title, "start_date" : selected_arr[0], "end_date" : selected_arr.slice(-1)[0], }; server.ctrl(ctrl_type, data); /*----------------------------------* * 移動数をリセット *----------------------------------*/ move_block_count = 0 /*----------------------------------* * 配列をリセット *----------------------------------*/ selected_arr = []; } }, 'mouseleave' : function(e) { if (is_drag_move_bar) { /*----------------------------------* * bar移動フラグを下げる *----------------------------------*/ is_drag_move_bar = false; /*----------------------------------* * validation *----------------------------------*/ if (bar_id != $(this).attr('data-bar_id')) { return ; } if ( !selected_arr ) { is_drag_move_bar = false; $('.bar').removeClass('selected'); $('.grabpoint').remove(); $(this).css('z-index','10'); selected_arr = []; return false; } /*----------------------------------* * z-index再設定 *----------------------------------*/ $(this).css('z-index','10');//戻す /*----------------------------------* * barをselected_arrから新規作成 *----------------------------------*/ add_dom.add_bar(bar_id, selected_arr, vid, plan_id, plan_class, plan_title); /*----------------------------------* * grabpointを追加 *----------------------------------*/ add_dom.add_grabpoint(bar_id); /*----------------------------------* * データベースに保存★ *----------------------------------*/ var ctrl_type = 'update', data = { "bar_id" : bar_id, "vid" : vid, "plan_id" : plan_id, "plan_class" : plan_class, "plan_title" : plan_title, "start_date" : selected_arr[0], "end_date" : selected_arr.slice(-1)[0], }; server.ctrl(ctrl_type, data); /*----------------------------------* * 配列をリセット *----------------------------------*/ selected_arr = []; /*----------------------------------* * 移動数をリセット *----------------------------------*/ move_block_count = 0 /*----------------------------------* * ほかの行の選択テキストを解除 *----------------------------------*/ window.getSelection().removeAllRanges(); } }, },'.bar'); }, /*----------------------------------------* * * set_event_move_vname * * barをクリックした際のイベント * * *----------------------------------------*/ set_event_move_vname : function() { var clicks = 0, clickdowntime = 0, start_row_index = 0, rowspan = 0, colspan = 0, new_order = 0, target_row_index = null, mode = 'table', child_tag = '', moved_flag = false, vid = '',//更新するdata-vid $surface_obj = {}, $current_row = {}, $parent = {}; /*---------------------------------------* * 移動したい要素の親にイベント設定する *---------------------------------------*/ $(sticky_table).on({ 'mousedown' : function(e) { $parent = $(this); /*-------------------------------* * 移動する子要素を決定 *-------------------------------*/ child_tag = $parent.children().eq(0).get(0).nodeName; if (child_tag === 'CAPTION' || child_tag === 'COLGROUP' || child_tag === 'THEAD') { child_tag = 'TBODY'; } /*----------------------------------------* * movableクラスがある要素だけ対象にする *----------------------------------------*/ if ($(e.target).hasClass('movable')) { /*-------------------------------* * moveobjドラッグフラグを立てる *-------------------------------*/ is_drag_move_sortobj = true; /*-------------------------------* * クリック開始時間 *-------------------------------*/ clickdowntime = new Date().getTime(); /*-------------------------------* * クリックしたDIV *-------------------------------*/ $surface_obj = $(e.target); $surface_obj.addClass('current'); vid = $surface_obj.attr('data-vid'); colspan = $parent.find('td').attr('colspan'); rowspan = $parent.find('td').attr('rowspan'); /*-------------------------------* * tableとその他でmodeを分ける *-------------------------------*/ if (child_tag === 'TBODY') { mode = 'table'; $current_row = $(e.target).closest('tr'); start_row_index = $(e.target).closest('tr').get(0).rowIndex; } else { mode = 'other'; $current_row = $(e.target).closest(child_tag); start_row_index = $parent.children().index( $(e.target).closest(child_tag) ); } } }, 'mousemove' : function(e) { var moving_direction = ''; if (is_drag_move_sortobj) { $parent.find('div').removeClass('hilight'); /*-------------------------------* * 移動先のrow番号 *-------------------------------*/ if (mode === 'table') { if (typeof $(e.target).closest('tr').get(0).rowIndex == 'undefined') { return false; } target_row_index = $(e.target).closest('tr').get(0).rowIndex; } else { target_row_index = $parent.children().index( $(e.target).closest(child_tag) ); } /*-------------------------------* * 移動方向を決定 *-------------------------------*/ if (start_row_index !== target_row_index) { if (target_row_index > start_row_index) { moving_direction = 'down'; } else if (target_row_index < start_row_index) { moving_direction = 'up'; } start_row_index = target_row_index; } else { return false; } var move_amount = 0; /*-------------------------------* * 移動するrowを決定 *-------------------------------*/ if (moving_direction === 'down') { move_amount = target_row_index + 1; } else if (moving_direction === 'up') { move_amount = target_row_index; } /*-------------------------------* * 移動 *-------------------------------*/ if (mode === 'table') { var node_length = $parent.get(0).rows.length; if (move_amount === node_length) { $parent.find('tbody').get(0).insertBefore($current_row.get(0), null); moved_flag = true; } else { if (typeof $parent.get(0).rows[move_amount] != 'undefined' && $parent.get(0).rows[move_amount].closest('tbody') !== null) { $parent.find('tbody').get(0).insertBefore($current_row.get(0), $parent.get(0).rows[move_amount]); moved_flag = true; } } //td要素を削除する $parent.find('td').remove(); //先頭にtd要素を追加する $('