|
- (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, template, data){
- this.selector = selector;
- this.data = data;
- this.template = template;
- // this.sample_people = {
- // login: '01515b52-6936-46b2-a000-9ad4cd7a5b50',
- // firstname: "first",
- // lastname: "last",
- // phone: '041122221',
- // email: 'abc@gmail.com',
- // pay: 0,
- // hour: 12,
- // OT: 3,
- // petrol: 50,
- // rating: 1,
- // };
- this.load_data(this.data);
- }
-
- load_data(data){
- var template = $(this.template).html();
- var html = Mustache.render(template, data);
- $(this.selector).html(html);
- //save it
- $(this.selector).data(data);
- //draw rating star
- this.set_ratings(this.data.rating);
- this.set_unconfirmed_job(this.data.unconfirmedjob);
- }
-
- set_ratings(num){
- for (var i=1; i<= 5; i++){
- if (i <=num){
- $(this.selector + " div[name='rating'] span:nth-child(" +i+ ")").addClass('checked');
- }else{
- $(this.selector + " div[name='rating'] span:nth-child(" +i+ ")").removeClass('checked');
- }
- }
- this.data.rating = num;
- }
-
- set_unconfirmed_job(num){
- if( num == 0 )
- $(this.selector + " span[name='badge']").hide();
- else
- $(this.selector + " span[name='badge']").show();
- this.data.unconfirmedjob = num;
- }
-
- }//end of class People
-
- function bts_staff_html(data){
- var template = $('#staff_item').html();
- var head = '<div class="peopleitem" id="p'+ data.login +'">';
- r = head + '</div>' ;
- return r;
- }
-
- function bts_client_html(data){
- var template = $('#client_item').html();
- var head = '<div class="peopleitem" id="p'+ data.login +'">';
- r = head + '</div>' ;
- return r;
- }
-
- function sample_staff(){
- for (var i=1; i<100; i++){
- var sample_people = {
- login: '01515b52-6936-46b2-a000-9ad4cd7a5b50' +i,
- firstname: "first"+i,
- lastname: "last",
- mobile: '041122221' +i,
- email: 'abc@gmail.com' + i,
- wages: 0,
- hour: i,
- OT: 3,
- petrol: 50 +i,
- rating: Math.floor(Math.random() * Math.floor(5)),
- unconfirmedjob: Math.floor(Math.random() * Math.floor(30)),
- };
- var html = bts_staff_html(sample_people);
- jQuery('div.stafflist').append(html);
- new People("#p" + sample_people.login, sample_people);
- }
- }
- function list_staff() {
- show_loading_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'){
- hide_loading_staff();
- response.users.forEach(function(u){
- var html = bts_staff_html(u);
- jQuery('div.stafflist').append(html);
- new People("#p" + u.login,'#staff_item', u);
- });
- }else{
- alert('error getting staff list');
- }
- });
- }
-
- function list_clients() {
- show_loading_client();
- $('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){
- hide_loading_client();
- var html = bts_client_html(u);
- jQuery('div.clientlist').append(html);
- new People("#p" + u.login, '#client_item' ,u);
- });
- }else{
- alert('error getting Client list');
- }
- });
- }
-
- function show_loading_staff(){
- jQuery('div.stafflist img').attr('src', bts().load_user_img).show();
- }
- function show_loading_client(){
- jQuery('div.clientlist img').attr('src', bts().load_user_img).show();
- }
- function hide_loading_staff(){
- jQuery('div.stafflist img').hide();;
- }
- function hide_loading_client(){
- jQuery('div.clientlist img').hide();
- }
-
-
- 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(e){
- filter_user(e.target);
- }, 500));
- }
-
- function filter_user(input){
- var value = $(input).attr('value');
- value = value.toLowerCase();
- var selector = get_selector_for_filter_people(input);
- $.each( $(selector).find('div.peopleitem'), function(index, e){
- var html = $(e).find('div[name="title"] a').html();
- html = html.toLowerCase();
- if (-1 != html.indexOf(value)){//we find it;
- $(e).show();
- }else{
- $(e).hide();
- }
- });
- }
-
- function get_selector_for_filter_people(input){
- var selector='';
- var role = $(input).attr('placeholder');
- if (role == 'staff') //we filter staff
- selector = 'div.stafflist';
- else if (role = 'client')
- selector = 'div.clientlist';
- return selector;
- }
-
- function init_ts(){
- list_staff();
- list_clients();
- xero(false);
- wifi(false);
- init_user_search();
- ajax_earning_rate();
- }
-
- function ajax_earning_rate(){
- $.post(bts().ajax_url, { // POST request
- _ajax_nonce: bts().nonce, // nonce
- action: "earnings_rate", // action
- }, function(response, status, xhr){
- console.log("%o", response);
- });
- }
-
- init_ts();
-
- $(document).on('click', 'div.divTableHead.bdelete', function(){
- for (var i=1; i<10; i++){
- var html = jQuery("#job_item").html();
- jQuery('div.workspace').append(html);
- }
- dtp_init();
- });
-
- $(document).on('click', 'div.divTableCell.bdelete', function(){
- $(this).closest('div.divTable').remove();
- });
-
- $(document).on('mouseenter', 'div.divTableCell', function(){
- $(this).closest('div.divTable').addClass('highlight');
- });
- $(document).on('mouseleave', 'div.divTableCell', function(){
- $(this).closest('div.divTable').removeClass('highlight');
- });
-
-
- /*________________________________________________________________________*/
- });
- })(jQuery);
-
-
- /*______________scrolling______________________________________________*/
- jQuery(document).ready(function(){
- var timeoutid =0;
-
- jQuery('button.peoplelist[name="down"]').mousedown(function(){
- var button = this;
- timeoutid = setInterval(function(){
- //console.log("down scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
- jQuery(button).parent().find(".userlist").get(0).scrollTop +=240;
- }, 100);
- }).on('mouseup mouseleave', function(){
- clearTimeout(timeoutid);
- });
-
- jQuery('button.peoplelist[name="up"]').mousedown(function(){
- var button = this;
- timeoutid = setInterval(function(){
- //console.log("up scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
- jQuery(button).parent().find(".userlist").get(0).scrollTop -=240;
- }, 100);
- }).on('mouseup mouseleave', function(){
- clearTimeout(timeoutid);
- });
-
- });
-
|