timesheet source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

443 lines
13KB

  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, template, data){
  23. this.selector = selector;
  24. this.data = data;
  25. this.template = 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_staff_html(data){
  69. var template = $('#staff_item').html();
  70. var head = '<div class="peopleitem" id="p'+ data.login +'">';
  71. r = head + '</div>' ;
  72. return r;
  73. }
  74. function bts_client_html(data){
  75. var template = $('#client_item').html();
  76. var head = '<div class="peopleitem" id="p'+ data.login +'">';
  77. r = head + '</div>' ;
  78. return r;
  79. }
  80. function sample_staff(){
  81. for (var i=1; i<100; i++){
  82. var sample_people = {
  83. login: '01515b52-6936-46b2-a000-9ad4cd7a5b50' +i,
  84. firstname: "first"+i,
  85. lastname: "last",
  86. mobile: '041122221' +i,
  87. email: 'abc@gmail.com' + i,
  88. wages: 0,
  89. hour: i,
  90. OT: 3,
  91. petrol: 50 +i,
  92. rating: Math.floor(Math.random() * Math.floor(5)),
  93. unconfirmedjob: Math.floor(Math.random() * Math.floor(30)),
  94. };
  95. var html = bts_staff_html(sample_people);
  96. jQuery('div.stafflist').append(html);
  97. new People("#p" + sample_people.login, sample_people);
  98. }
  99. }
  100. function list_staff() {
  101. show_loading_staff();
  102. $('div.stafflist div.peopleitem').remove();
  103. $.post(bts().ajax_url, { // POST request
  104. _ajax_nonce: bts().nonce, // nonce
  105. action: "list_staff", // action
  106. }, function(response, status, xhr){
  107. if (response.status =='success'){
  108. hide_loading_staff();
  109. response.users.forEach(function(u){
  110. var html = bts_staff_html(u);
  111. jQuery('div.stafflist').append(html);
  112. new People("#p" + u.login,'#staff_item', u);
  113. });
  114. }else{
  115. alert('error getting staff list');
  116. }
  117. });
  118. }
  119. function list_clients() {
  120. show_loading_client();
  121. $('div.clientlist div.peopleitem').remove(); //clear it
  122. $.post(bts().ajax_url, { // POST request
  123. _ajax_nonce: bts().nonce, // nonce
  124. action: "list_client", // action
  125. }, function(response, status, xhr){
  126. if (response.status =='success'){
  127. response.users.forEach(function(u){
  128. hide_loading_client();
  129. var html = bts_client_html(u);
  130. jQuery('div.clientlist').append(html);
  131. new People("#p" + u.login, '#client_item' ,u);
  132. });
  133. }else{
  134. alert('error getting Client list');
  135. }
  136. });
  137. }
  138. function show_loading_staff(){
  139. jQuery('div.stafflist img').attr('src', bts().load_user_img).show();
  140. }
  141. function show_loading_client(){
  142. jQuery('div.clientlist img').attr('src', bts().load_user_img).show();
  143. }
  144. function hide_loading_staff(){
  145. jQuery('div.stafflist img').hide();;
  146. }
  147. function hide_loading_client(){
  148. jQuery('div.clientlist img').hide();
  149. }
  150. function xero(t){
  151. if (t)
  152. $('div.xero i').show();
  153. else
  154. $('div.xero i').hide();
  155. }
  156. function wifi(t){
  157. if (t)
  158. $('div.wifi i').show();
  159. else
  160. $('div.wifi i').hide();
  161. }
  162. function init_user_search(){
  163. $('div.b_search input').keyup(debounce(function(e){
  164. filter_user(e.target);
  165. }, 500));
  166. }
  167. function filter_user(input){
  168. var value = $(input).attr('value');
  169. value = value.toLowerCase();
  170. var selector = get_selector_for_filter_people(input);
  171. $.each( $(selector).find('div.peopleitem'), function(index, e){
  172. var html = $(e).find('div[name="title"] a').html();
  173. html = html.toLowerCase();
  174. if (-1 != html.indexOf(value)){//we find it;
  175. $(e).show();
  176. }else{
  177. $(e).hide();
  178. }
  179. });
  180. }
  181. function get_selector_for_filter_people(input){
  182. var selector='';
  183. var role = $(input).attr('placeholder');
  184. if (role == 'staff') //we filter staff
  185. selector = 'div.stafflist';
  186. else if (role = 'client')
  187. selector = 'div.clientlist';
  188. return selector;
  189. }
  190. function init_ts(){
  191. list_staff();
  192. list_clients();
  193. xero(false);
  194. wifi(false);
  195. init_user_search();
  196. ajax_earning_rate();
  197. }
  198. function ajax_earning_rate(){
  199. $.post(bts().ajax_url, { // POST request
  200. _ajax_nonce: bts().nonce, // nonce
  201. action: "earnings_rate", // action
  202. }, function(response, status, xhr){
  203. bts().earnings_rate = response;
  204. console.log("%o", bts().earnings_rate);
  205. });
  206. }
  207. init_ts();
  208. $(document).on('click', 'div.divTableHead.bdelete', function(){
  209. for (var i=1; i<10; i++){
  210. var o = new Job({i:i});
  211. }
  212. });
  213. $(document).on('click', 'div.divTableCell.bdelete', function(){
  214. if (confirm('delete this job?'))
  215. $(this).closest('div.divTable').remove();
  216. });
  217. $(document).on('mouseenter', 'div.divTableCell', function(){
  218. $(this).closest('div.divTable').addClass('highlight');
  219. });
  220. $(document).on('mouseleave', 'div.divTableCell', function(){
  221. $(this).closest('div.divTable').removeClass('highlight');
  222. });
  223. $(document).on('click', 'span.ticon.ticon-save', function(){
  224. var table = $(this).closest('div.divTable')
  225. table.data().job.do_save_record();
  226. });
  227. class Job{ //save data for the record, and display it as GUI
  228. constructor(data){
  229. var html = jQuery("#job_item").html();
  230. this.el = $(html);
  231. jQuery('div.workspace').append(this.el);
  232. this.load_data(data);
  233. dtp_init();
  234. }
  235. load_data(data)
  236. {
  237. this.set_job_id(data.id);
  238. this.set_tos(data.tos);
  239. this.set_start(data.start);
  240. this.set_finish(data.finish);
  241. this.set_rate(data.rate);
  242. this.set_staff(data.staff);
  243. this.set_client(data.client);
  244. this.set_ack(data.ack);
  245. //save to html element
  246. this.data = data;
  247. this.el.data({job:this, data:data});
  248. }
  249. get_job_id(){
  250. return this.el.find('input[name="id"]').attr('value');
  251. }
  252. set_job_id(val){
  253. return this.el.find('input[name="id"]').attr('value', val);
  254. }
  255. get_tos()
  256. {
  257. return this.el.find('div.btos select').children("option:selected").val();
  258. }
  259. set_tos(val)
  260. {
  261. if (typeof(val) =="undefined")
  262. return;
  263. this.el.find('div.btos select option[value="'+val+'"]').prop('selected',true);
  264. }
  265. get_start(){
  266. return this.el.find('div.bstart input').attr('value');
  267. }
  268. set_start(val)
  269. {
  270. if (typeof(val) =="undefined")
  271. return;
  272. this.el.find('div.bstart input').attr('value', val);
  273. }
  274. get_finish()
  275. {
  276. return this.el.find('div.bfinish input').attr('value');
  277. }
  278. set_finish(val)
  279. {
  280. if (typeof(val) == "undefined")
  281. return;
  282. this.el.find('div.bfinish input').attr('value', val);
  283. }
  284. get_rate()
  285. {
  286. return this.el.find('div.brate select').children("option:selected").val();
  287. }
  288. set_rate(val)
  289. {
  290. if (typeof(val) =="undefined")
  291. return;
  292. this.el.find('div.brate select option[value="'+val+'"]').prop('selected',true);
  293. }
  294. get_staff()
  295. {
  296. return this.el.find('div.bstaff select').children("option:selected").val();
  297. }
  298. set_staff(val)
  299. {
  300. if (typeof(val) =="undefined")
  301. return;
  302. this.el.find('div.bstaff select option[value="'+val+'"]').prop('selected',true);
  303. }
  304. get_client()
  305. {
  306. return this.el.find('div.bclient select').children("option:selected").val();
  307. }
  308. set_client(val)
  309. {
  310. if (typeof(val) =="undefined")
  311. return;
  312. this.el.find('div.bclient select option[value="'+val+'"]').prop('selected',true);
  313. }
  314. get_ack()
  315. {
  316. return this.el.find('div.bconfirmed input:checked').length > 0;
  317. }
  318. set_ack(val)
  319. {
  320. if (typeof(val) =="undefined")
  321. return;
  322. return this.el.find('div.bconfirmed input').prop('checked', val!=0);
  323. }
  324. get_record_from_ui(){
  325. var record = {};
  326. record.id = this.get_job_id();
  327. record.tos = this.get_tos();
  328. record.start = this.get_start();
  329. record.finish = this.get_finish();
  330. record.rate = this.get_rate();
  331. record.staff = this.get_staff();
  332. record.client = this.get_client();
  333. record.ack = this.get_ack();
  334. return record;
  335. }
  336. do_save_record(){
  337. var self = this;
  338. $.post(bts().ajax_url, { // POST request
  339. _ajax_nonce: bts().nonce, // nonce
  340. action: "save_job", // action
  341. record: this.get_record_from_ui(),
  342. }, function(response, status, xhr){
  343. console.log("response for save %o", response);
  344. self.load_data(response.newdata);
  345. });
  346. }
  347. }//end of class Job
  348. //global GUI summary
  349. function get_wages()
  350. {
  351. var txt = $('div.wages div').html();
  352. return parseInt(txt);
  353. }
  354. function set_wages(num){
  355. $('div.wages div').html(num);
  356. }
  357. function set_working_hours(num){
  358. $('input#woh').attr('value', num);
  359. }
  360. function get_working_hours(){
  361. var txt = $('input#woh').attr('value');
  362. return parseFloat(txt);
  363. }
  364. //modal box
  365. function set_modal_title(selector, title){
  366. var s = 'div.'+ selector +' .ult_modal-title';
  367. $(s).html(title);
  368. }
  369. function set_modal_content(selector, content){
  370. var s = 'div.'+ selector +' div.ult_modal-body.ult-html';
  371. $(s).html(content);
  372. }
  373. set_modal_title('abcdefg', 'suck title');
  374. set_modal_content('abcdefg', 'fucking details');
  375. /*________________________________________________________________________*/
  376. });
  377. })(jQuery);
  378. /*______________scrolling______________________________________________*/
  379. jQuery(document).ready(function(){
  380. var timeoutid =0;
  381. jQuery('button.peoplelist[name="down"]').mousedown(function(){
  382. var button = this;
  383. timeoutid = setInterval(function(){
  384. //console.log("down scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
  385. jQuery(button).parent().find(".userlist").get(0).scrollTop +=240;
  386. }, 100);
  387. }).on('mouseup mouseleave', function(){
  388. clearTimeout(timeoutid);
  389. });
  390. jQuery('button.peoplelist[name="up"]').mousedown(function(){
  391. var button = this;
  392. timeoutid = setInterval(function(){
  393. //console.log("up scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
  394. jQuery(button).parent().find(".userlist").get(0).scrollTop -=240;
  395. }, 100);
  396. }).on('mouseup mouseleave', function(){
  397. clearTimeout(timeoutid);
  398. });
  399. });