diff --git a/js/bts_timesheet.js b/js/bts_timesheet.js index bb09929..4f951f6 100644 --- a/js/bts_timesheet.js +++ b/js/bts_timesheet.js @@ -1,5 +1,22 @@ (function ($) { $(function () { + // http://davidwalsh.name/javascript-debounce-function + function debounce(func, wait, immediate) { + var timeout; + return function () { + var context = this, args = arguments; + var later = function () { + timeout = null; + if (!immediate) + func.apply(context, args); + }; + var callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) + func.apply(context, args); + }; + }; /*____________________________________________________________________________________*/ class People{ constructor(selector, data){ @@ -81,24 +98,73 @@ } } function list_staff() { + $('div.stafflist div.peopleitem').remove(); $.post(bts().ajax_url, { // POST request _ajax_nonce: bts().nonce, // nonce action: "list_staff", // action }, function(response, status, xhr){ if (response.status =='success'){ - response.staff.forEach(function(staff){ - var html = bts_people_html(staff); + response.users.forEach(function(u){ + var html = bts_people_html(u); jQuery('div.stafflist').append(html); - new People("#p" + staff.login, staff); + new People("#p" + u.login, u); }); }else{ alert('error getting staff list'); } }); } + + function list_clients() { + $('div.clientlist div.peopleitem').remove(); //clear it + $.post(bts().ajax_url, { // POST request + _ajax_nonce: bts().nonce, // nonce + action: "list_client", // action + }, function(response, status, xhr){ + if (response.status =='success'){ + response.users.forEach(function(u){ + var html = bts_people_html(u); + jQuery('div.clientlist').append(html); + new People("#p" + u.login, u); + }); + }else{ + alert('error getting Client list'); + } + }); + } + + function xero(t){ + if (t) + $('div.xero i').show(); + else + $('div.xero i').hide(); + } + + function wifi(t){ + if (t) + $('div.wifi i').show(); + else + $('div.wifi i').hide(); + } + + function init_user_search(){ + $('div.b_search input').keyup(debounce(function(){ + console.log('key pressed'); + }, 500)); + } + function init_ts(){ list_staff(); + list_clients(); + xero(false); + wifi(false); + init_user_search(); } + + + + + init_ts(); /*________________________________________________________________________*/ }); diff --git a/ts.php b/ts.php index d76724c..595b9c7 100644 --- a/ts.php +++ b/ts.php @@ -32,6 +32,7 @@ class AcareOffice{ add_shortcode( 'bts_people_item', array($this, 'bts_people_item')); add_action('wp_ajax_list_staff', array($this,'list_staff' )); + add_action('wp_ajax_list_client', array($this,'list_client' )); } @@ -127,19 +128,28 @@ class AcareOffice{ function list_staff(){ + check_ajax_referer('acaresydney'); + return $this->list_people_by_role('staff'); + } + function list_client(){ + check_ajax_referer('acaresydney'); + return $this->list_people_by_role('client'); + } + + function list_people_by_role($role){ check_ajax_referer('acaresydney'); // Handle the ajax request $response = array( 'status' =>'error', - 'staff' => [], + 'users' => [], ); //search all users that are staff - $staffq = new \WP_User_Query(array('role'=>'staff')); + $staffq = new \WP_User_Query(array('role'=>$role)); $staff = $staffq->get_results(); if (! empty($staff)){ $response['status'] = 'success'; foreach( $staff as $s){ - $response['staff'][] = array( + $response['users'][] = array( 'login' => $s->user_login, 'firstname'=> $s->first_name, 'lastname'=> $s->last_name, @@ -149,7 +159,7 @@ class AcareOffice{ 'hour' => 0 , 'OT' => 0 , 'petrol'=> 0 , - 'rating'=> 0, + 'rating'=> 0, 'unconfirmedjob'=> 0, ); }