From 4b02928b1547266f38a0f09e63ac77dcfb90372b Mon Sep 17 00:00:00 2001 From: patrick Date: Thu, 18 Jul 2019 18:33:10 +1000 Subject: [PATCH] performance issues with too many jobs added at once --- js/bts_timesheet.js | 86 ++++++++++++++++++++++++++++++--------------- ts.php | 6 ++++ 2 files changed, 63 insertions(+), 29 deletions(-) diff --git a/js/bts_timesheet.js b/js/bts_timesheet.js index 70ad56e..59df7a9 100644 --- a/js/bts_timesheet.js +++ b/js/bts_timesheet.js @@ -223,6 +223,9 @@ value = value.toLowerCase(); var selector = get_selector_for_filter_people(input); $.each( $(selector).find('div.peopleitem'), function(index, e){ + //uncheck everyone + $(e).find('input[type="checkbox"]').prop('checked', false); + var html = $(e).find('div[name="title"] a').html(); html = html.toLowerCase(); if (-1 != html.indexOf(value)){//we find it; @@ -245,17 +248,18 @@ $(document).on('click', 'div.divTableHead.bdelete', function(){ - var o = new Job({empty:true}); + add_new_empty_job(); }); - + + function add_new_empty_job(){ + var o = new Job({empty:true}); + $('div.workspace').append(o.el); + o.el.get(0).scrollIntoView(); + dtp_init(); + } $(document).on('click', 'div[name="copyschedule"]', function(e){ e.stopPropagation(); - if (!confirm('add 10 empty new jobs?')) - return; - for (var i=1; i<=10; i++){ - var o = new Job({empty:true}); - } - + add_new_empty_job(); }); $(document).on('click', 'div.divTableCell.bdelete', function(){ @@ -311,17 +315,16 @@ var table = $(this).closest('div.divTable'); var job = table.data().job; clone_data_create_new_job(job.get_record_from_ui()); + dtp_init(); }); class Job{ //save data for the record, and display it as GUI constructor(data){ var html = jQuery("#job_item").html(); this.el = $(html); - jQuery('div.workspace').append(this.el); + //jQuery('div.workspace').append(this.el); this.load_data(data); - dtp_init(); - this.init_start_rating() - + this.init_start_rating(); } init_start_rating(){ @@ -330,7 +333,6 @@ var r = $(this).attr('data-rating'); self.mark_dirty(); self.set_rating(r); - }) this.el.find("div.brating").mouseenter(function(){ @@ -912,8 +914,11 @@ var value = $(e).attr('value'); if( -1 != value.indexOf(strDate) ) //found { - var j = $(e).closest('div.divTable').data().job; - jobs.push(j); + var el = $(e).closest('div.divTable'); + if (el.is(":visible")){ + var j = el.data().job; + jobs.push(j); + } } }); }); @@ -921,6 +926,8 @@ jobs.forEach(function(e){ clone_data_create_new_job(e.get_record_from_ui(),7);//add 7 days }); + dtp_init(); + debounced_calculate(); }); $('div.weekly div.weekname.next').click(function(){ @@ -944,12 +951,16 @@ $('div.week1,div.week2').click(function(e){ e.stopPropagation(); $(this).toggleClass('filtered'); - filter_workspace_by_weeks(); + do_filter_workspace(); }); function copy_single_day_to_next_week(el){ - var j = $(el).closest('div.divTable').data().job; - clone_data_create_new_job(j.get_data_from_ui() , 7); // +7 days + var tb = $(el).closest('div.divTable'); + if (tb.is(':visible')){ + var j = $(tb).data().job; + clone_data_create_new_job(j.get_record_from_ui() , 7); // +7 days + } + dtp_init(); } function clone_data_create_new_job(val, num_of_shifted_days){ @@ -1030,9 +1041,13 @@ finish: format_date(last), }, function(response, status, xhr){ if (response.status =='success'){ + var job_els = []; response.jobs.forEach(function(job){ - new Job(job); + console.log('loading job... %o', job); + var o = new Job(job); + job_els.push(o.el); }); + show_jobs(job_els); //filter it if reqired do_filter_workspace(); }else{ @@ -1043,6 +1058,13 @@ } + function show_jobs(job_els){ + if (job_els.length >0){ + $('div.workspace').append(job_els); + job_els[0].get(0).scrollIntoView(); + } + } + function format_date(date){ var dd = date.getDate(); var mm = date.getMonth() + 1; //January is 0! @@ -1075,6 +1097,8 @@ $('div.workspace > div.divTable').remove(); //clear datetime picker $('div.xdsoft_datetimepicker').remove(); + // + show_loading_jobs(); } $('button[name="confirmschedule"]').click(function(){ @@ -1088,20 +1112,27 @@ function do_filter_workspace(){ var staffs =[]; $('div.stafflist div.peopleitem :checked').each(function(i, e){ - var id = $(e).parent().attr('data-id'); - //console.log("%o, id=%s", e, id); - staffs.push(id.substring(1)); + if ($(e).parent().is(':visible')){ + var id = $(e).parent().attr('data-id'); + //console.log("%o, id=%s", e, id); + staffs.push(id.substring(1)); + } }); var clients =[]; $('div.clientlist div.peopleitem :checked').each(function(i, e){ - var id = $(e).parent().attr('data-id'); - //console.log("%o, id=%s", e, id); - clients.push(id.substring(1)); + if ($(e).parent().is(':visible')){ + var id = $(e).parent().attr('data-id'); + //console.log("%o, id=%s", e, id); + clients.push(id.substring(1)); + } }); + console.log('filter worksspace'); filter_workspace(staffs, clients); - calculate_total_hour_and_money(); + console.log('filter by weeks'); + filter_workspace_by_weeks(); + debounced_calculate(); } function filter_workspace(staffs, clients){ @@ -1140,11 +1171,8 @@ if ((hide_week1 && job.is_week1()) || (hide_week2 && job.is_week2()) ){ $(e).fadeOut(); - }else{ - $(e).fadeIn(); } }); - debounced_calculate(); } var debounced_calculate = debounce(calculate_total_hour_and_money, 2000); diff --git a/ts.php b/ts.php index b408327..5e52d29 100644 --- a/ts.php +++ b/ts.php @@ -464,6 +464,7 @@ class AcareOffice{ //ajax browse job with different filters function list_job(){ + $func_start = time(); check_ajax_referer('acaresydney'); $start = $_POST['start']; $finish = $_POST['finish']; @@ -491,6 +492,11 @@ class AcareOffice{ ); } } + $func_end = time(); + $func_diff = $func_end - $func_start; + $response['duration_start'] = $func_start; + $response['duration_end'] = $func_end; + $response['duration'] = $func_diff; wp_send_json($response); wp_die(); }