timesheet source code
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

195 lines
5.9KB

  1. (function ($) {
  2. $(function () {
  3. // http://davidwalsh.name/javascript-debounce-function
  4. function debounce(func, wait, immediate) {
  5. var timeout;
  6. return function () {
  7. var context = this, args = arguments;
  8. var later = function () {
  9. timeout = null;
  10. if (!immediate)
  11. func.apply(context, args);
  12. };
  13. var callNow = immediate && !timeout;
  14. clearTimeout(timeout);
  15. timeout = setTimeout(later, wait);
  16. if (callNow)
  17. func.apply(context, args);
  18. };
  19. };
  20. /*____________________________________________________________________________________*/
  21. class People{
  22. constructor(selector, data){
  23. this.selector = selector;
  24. this.data = data;
  25. this.template = '#people_template';
  26. // this.sample_people = {
  27. // login: '01515b52-6936-46b2-a000-9ad4cd7a5b50',
  28. // firstname: "first",
  29. // lastname: "last",
  30. // phone: '041122221',
  31. // email: 'abc@gmail.com',
  32. // pay: 0,
  33. // hour: 12,
  34. // OT: 3,
  35. // petrol: 50,
  36. // rating: 1,
  37. // };
  38. this.load_data(this.data);
  39. }
  40. load_data(data){
  41. var template = $(this.template).html();
  42. var html = Mustache.render(template, data);
  43. $(this.selector).html(html);
  44. //save it
  45. $(this.selector).data(data);
  46. //draw rating star
  47. this.set_ratings(this.data.rating);
  48. this.set_unconfirmed_job(this.data.unconfirmedjob);
  49. }
  50. set_ratings(num){
  51. for (var i=1; i<= 5; i++){
  52. if (i <=num){
  53. $(this.selector + " div[name='rating'] span:nth-child(" +i+ ")").addClass('checked');
  54. }else{
  55. $(this.selector + " div[name='rating'] span:nth-child(" +i+ ")").removeClass('checked');
  56. }
  57. }
  58. this.data.rating = num;
  59. }
  60. set_unconfirmed_job(num){
  61. if( num == 0 )
  62. $(this.selector + " span[name='badge']").hide();
  63. else
  64. $(this.selector + " span[name='badge']").show();
  65. this.data.unconfirmedjob = num;
  66. }
  67. }//end of class People
  68. function bts_people_html(data){
  69. var template = $('#people_template').html();
  70. var head = '<div class="peopleitem" id="p'+ data.login +'">';
  71. r = head + '</div>' ;
  72. return r;
  73. }
  74. function sample_staff(){
  75. for (var i=1; i<100; i++){
  76. var sample_people = {
  77. login: '01515b52-6936-46b2-a000-9ad4cd7a5b50' +i,
  78. firstname: "first"+i,
  79. lastname: "last",
  80. mobile: '041122221' +i,
  81. email: 'abc@gmail.com' + i,
  82. wages: 0,
  83. hour: i,
  84. OT: 3,
  85. petrol: 50 +i,
  86. rating: Math.floor(Math.random() * Math.floor(5)),
  87. unconfirmedjob: Math.floor(Math.random() * Math.floor(30)),
  88. };
  89. var html = bts_people_html(sample_people);
  90. jQuery('div.stafflist').append(html);
  91. new People("#p" + sample_people.login, sample_people);
  92. }
  93. }
  94. function list_staff() {
  95. $('div.stafflist div.peopleitem').remove();
  96. $.post(bts().ajax_url, { // POST request
  97. _ajax_nonce: bts().nonce, // nonce
  98. action: "list_staff", // action
  99. }, function(response, status, xhr){
  100. if (response.status =='success'){
  101. response.users.forEach(function(u){
  102. var html = bts_people_html(u);
  103. jQuery('div.stafflist').append(html);
  104. new People("#p" + u.login, u);
  105. });
  106. }else{
  107. alert('error getting staff list');
  108. }
  109. });
  110. }
  111. function list_clients() {
  112. $('div.clientlist div.peopleitem').remove(); //clear it
  113. $.post(bts().ajax_url, { // POST request
  114. _ajax_nonce: bts().nonce, // nonce
  115. action: "list_client", // action
  116. }, function(response, status, xhr){
  117. if (response.status =='success'){
  118. response.users.forEach(function(u){
  119. var html = bts_people_html(u);
  120. jQuery('div.clientlist').append(html);
  121. new People("#p" + u.login, u);
  122. });
  123. }else{
  124. alert('error getting Client list');
  125. }
  126. });
  127. }
  128. function xero(t){
  129. if (t)
  130. $('div.xero i').show();
  131. else
  132. $('div.xero i').hide();
  133. }
  134. function wifi(t){
  135. if (t)
  136. $('div.wifi i').show();
  137. else
  138. $('div.wifi i').hide();
  139. }
  140. function init_user_search(){
  141. $('div.b_search input').keyup(debounce(function(e){
  142. filter_user(e.target);
  143. }, 500));
  144. }
  145. function filter_user(input){
  146. var value = $(input).attr('value');
  147. var selector = get_selector_for_filter_people(input);
  148. $.each( $(selector).find('div.peopleitem'), function(index, e){
  149. var html = $(e).find('div[name="title"] a').html();
  150. if (-1 != html.indexOf(value)){//we find it;
  151. $(e).show();
  152. }else{
  153. $(e).hide();
  154. }
  155. });
  156. }
  157. function get_selector_for_filter_people(input){
  158. var selector='';
  159. var role = $(input).attr('placeholder');
  160. if (role == 'staff') //we filter staff
  161. selector = 'div.stafflist';
  162. else if (role = 'client')
  163. selector = 'div.clientlist';
  164. return selector;
  165. }
  166. function init_ts(){
  167. list_staff();
  168. list_clients();
  169. xero(false);
  170. wifi(false);
  171. init_user_search();
  172. }
  173. init_ts();
  174. /*________________________________________________________________________*/
  175. });
  176. })(jQuery);