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

720 lines
20KB

  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. this.init_start_rating();
  235. }
  236. init_start_rating(){
  237. var self = this;
  238. this.el.find("div.brating span").click(function(){
  239. var r = $(this).attr('data-rating');
  240. self.set_rating(r);
  241. })
  242. this.el.find("div.brating").mouseenter(function(){
  243. //change to all hollow star
  244. $(this).find('span').html('☆');
  245. });
  246. this.el.find("div.brating").mouseleave(function(){
  247. self.set_rating(self.data.rating);
  248. });
  249. }
  250. load_data(data)
  251. {
  252. this.set_job_id(data.id);
  253. this.set_tos(data.tos);
  254. this.set_start(data.start);
  255. this.set_finish(data.finish);
  256. this.set_rate(data.rate);
  257. this.set_staff(data.staff);
  258. this.set_client(data.client);
  259. this.set_ack(data.ack);
  260. //save to html element
  261. this.data = data;
  262. this.el.data({job:this, data:data});
  263. }
  264. get_job_id(){
  265. return this.el.find('input[name="id"]').attr('value');
  266. }
  267. set_job_id(val){
  268. return this.el.find('input[name="id"]').attr('value', val);
  269. }
  270. get_tos()
  271. {
  272. return this.el.find('div.btos select').children("option:selected").val();
  273. }
  274. set_tos(val)
  275. {
  276. if (typeof(val) =="undefined")
  277. return;
  278. this.el.find('div.btos select option[value="'+val+'"]').prop('selected',true);
  279. }
  280. get_start(){
  281. return this.el.find('div.bstart input').attr('value');
  282. }
  283. set_start(val)
  284. {
  285. if (typeof(val) =="undefined")
  286. return;
  287. this.el.find('div.bstart input').attr('value', val);
  288. }
  289. get_finish()
  290. {
  291. return this.el.find('div.bfinish input').attr('value');
  292. }
  293. set_finish(val)
  294. {
  295. if (typeof(val) == "undefined")
  296. return;
  297. this.el.find('div.bfinish input').attr('value', val);
  298. }
  299. get_rate()
  300. {
  301. return this.el.find('div.brate select').children("option:selected").val();
  302. }
  303. set_rate(val)
  304. {
  305. if (typeof(val) =="undefined")
  306. return;
  307. this.el.find('div.brate select option[value="'+val+'"]').prop('selected',true);
  308. }
  309. get_staff()
  310. {
  311. return this.el.find('div.bstaff select').children("option:selected").val();
  312. }
  313. set_staff(val)
  314. {
  315. if (typeof(val) =="undefined")
  316. return;
  317. this.el.find('div.bstaff select option[value="'+val+'"]').prop('selected',true);
  318. }
  319. get_client()
  320. {
  321. return this.el.find('div.bclient select').children("option:selected").val();
  322. }
  323. set_client(val)
  324. {
  325. if (typeof(val) =="undefined")
  326. return;
  327. this.el.find('div.bclient select option[value="'+val+'"]').prop('selected',true);
  328. }
  329. get_ack()
  330. {
  331. return this.el.find('div.bconfirmed input:checked').length > 0;
  332. }
  333. set_ack(val)
  334. {
  335. if (typeof(val) =="undefined")
  336. return;
  337. return this.el.find('div.bconfirmed input').prop('checked', val!=0);
  338. }
  339. get_rating(){
  340. var count =0;
  341. this.el.find('div.brating span').each(function(i,e){
  342. if ($(e).html()=='★')
  343. count +=1;
  344. });
  345. return count;
  346. }
  347. set_rating(num){
  348. if (!(1 <= num && num <=5))
  349. return;
  350. this.data.rating=num;
  351. this.el.find('div.brating span').each(function(i,e){
  352. var rating = $(e).attr('data-rating');
  353. var rating = parseInt(rating);
  354. if (rating <= num)
  355. $(e).html('★');
  356. else
  357. $(e).html('☆');
  358. });
  359. }
  360. get_record_from_ui(){
  361. var record = {};
  362. record.id = this.get_job_id();
  363. record.tos = this.get_tos();
  364. record.start = this.get_start();
  365. record.finish = this.get_finish();
  366. record.rate = this.get_rate();
  367. record.staff = this.get_staff();
  368. record.client = this.get_client();
  369. record.ack = this.get_ack();
  370. return record;
  371. }
  372. do_save_record(){
  373. var self = this;
  374. $.post(bts().ajax_url, { // POST request
  375. _ajax_nonce: bts().nonce, // nonce
  376. action: "save_job", // action
  377. record: this.get_record_from_ui(),
  378. }, function(response, status, xhr){
  379. console.log("response for save %o", response);
  380. self.load_data(response.newdata);
  381. });
  382. }
  383. }//end of class Job
  384. //global GUI summary
  385. function get_wages()
  386. {
  387. var txt = $('div.wages div').html();
  388. return parseInt(txt);
  389. }
  390. function set_wages(num){
  391. $('div.wages div').html(num);
  392. }
  393. function set_working_hours(num){
  394. $('input#woh').attr('value', num);
  395. }
  396. function get_working_hours(){
  397. var txt = $('input#woh').attr('value');
  398. return parseFloat(txt);
  399. }
  400. //modal box
  401. function set_modal_title(selector, title){
  402. var s = 'div.bts_'+ selector +' .ult_modal-title';
  403. $(s).html(title);
  404. }
  405. function set_modal_content(selector, content){
  406. var s = 'div.bts_'+ selector +' div.ult_modal-body.ult-html';
  407. $(s).html(content);
  408. }
  409. function open_modal (selector){
  410. var s='div.bts_'+selector+'_button';
  411. $(s).trigger('click');
  412. }
  413. // setTimeout(function(){
  414. // set_modal_title('warning', 'suck title');
  415. // set_modal_content('warning', 'fucking details');
  416. // //open_modal('warning');
  417. // }, 1000);
  418. //
  419. // setTimeout(function(){
  420. // set_modal_title('error', 'error title');
  421. // set_modal_content('error', 'error details');
  422. // //open_modal('error');
  423. // }, 5000);
  424. $(document).on('mouseenter', 'div.week1 div', function(){
  425. $(this).addClass('blink_me');
  426. get_week2_partner(this).addClass('blink_me');
  427. });
  428. $(document).on('mouseleave', 'div.week1 div', function(){
  429. $(this).removeClass('blink_me');
  430. get_week2_partner(this).removeClass('blink_me');
  431. });
  432. function get_week2_partner(div){
  433. var index = $(div).index()+1;
  434. return $('div.week2 div:nth-child('+index+')');
  435. }
  436. function init_weekdays(){
  437. var curr = new Date; // get current date
  438. // First day is the day of the month - the day of the week
  439. var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first
  440. var last = first + 6; // last day is the first day + 6
  441. //var firstday = new Date(curr.setDate(first)); //Mon
  442. //var lastday = new Date(curr.setDate(last)); //Sun
  443. var pos = 1; //first lot
  444. for (var i=first; i<=last; i++)
  445. {
  446. var d1 = new Date(curr.setDate(i));
  447. var d2 = new Date(curr.setDate(i+7));
  448. set_day_number(1,pos, d1); //week 1
  449. set_day_number(2,pos, d2); //week 2
  450. pos +=1;
  451. }
  452. }
  453. function set_day_number(week, index, date){
  454. var selector = 'span[name="w'+week+'d'+index+'"]';
  455. $(selector).html(date.getDate());
  456. $(selector).data({date:date});
  457. }
  458. function format_date(date) {
  459. var monthNames = [
  460. "January", "February", "March",
  461. "April", "May", "June", "July",
  462. "August", "September", "October",
  463. "November", "December"
  464. ];
  465. var day = date.getDate();
  466. var monthIndex = date.getMonth();
  467. var year = date.getFullYear();
  468. return day + ' ' + monthNames[monthIndex] + ' ' + year;
  469. }
  470. function set_today(){
  471. var selector = 'div.sheettitle span[name="today"]';
  472. var curr = new Date;
  473. $(selector).html(format_date(curr));
  474. }
  475. Date.prototype.get_week_number = function(){
  476. var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
  477. var dayNum = d.getUTCDay() || 7;
  478. d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  479. var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
  480. return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
  481. };
  482. function set_week_number(){
  483. var date = $('span[name="w1d1"]').data().date;
  484. console.log("date %o", date);
  485. var num = date.get_week_number();
  486. $('div.weekly span[name="week1"]').html(num);
  487. $('div.weekly span[name="week2"]').html(num+1);
  488. }
  489. $('div.prevweek.left').click(function(){
  490. $('div.weekdays span.weekday').each(function(i, e){
  491. var date = $(e).data().date;
  492. var newdate = new Date(date.setDate(date.getDate() -7 ));
  493. $(e).html(newdate.getDate());
  494. $(e).data({data:newdate});
  495. });
  496. set_week_number();
  497. load_timesheet();
  498. });
  499. $('div.nextweek.right').click(function(){
  500. $('div.weekdays span.weekday').each(function(i, e){
  501. var date = $(e).data().date;
  502. var newdate = new Date(date.setDate(date.getDate() +7 ));
  503. $(e).html(newdate.getDate());
  504. $(e).data({data:newdate});
  505. });
  506. set_week_number();
  507. load_timesheet();
  508. });
  509. $('div.weekly div.weekname.prev').click(function(){
  510. if (!confirm ('copy entire week to next week? '))
  511. return;
  512. });
  513. $('div.weekly div.weekname.next').click(function(){
  514. if (!confirm ('copy entire week to previous week? '))
  515. return;
  516. });
  517. $('div.week1 > div').click(function(){
  518. if (!confirm ('copy to next week'))
  519. return;
  520. });
  521. $('div.sheettitle h1').click(function(){
  522. reset_title_to_today();
  523. })
  524. function reset_title_to_today(){
  525. set_today();
  526. init_weekdays();
  527. set_week_number();
  528. load_timesheet();
  529. }
  530. function load_timesheet()
  531. {
  532. clear_workspace();
  533. var first = $('span[name="w1d1"]').data().date;
  534. var last = $('span[name="w2d7"]').data().date;
  535. $.post(bts().ajax_url, { // POST request
  536. _ajax_nonce: bts().nonce, // nonce
  537. action: "list_job", // action
  538. start: format_date(first),
  539. finish: format_date(last),
  540. }, function(response, status, xhr){
  541. if (response.status =='success'){
  542. response.jobs.forEach(function(job){
  543. new Job(job);
  544. });
  545. //filter it if reqired
  546. do_filter_workspace();
  547. }else{
  548. alert('error loading job');
  549. }
  550. });
  551. }
  552. function format_date(date){
  553. var dd = date.getDate();
  554. var mm = date.getMonth() + 1; //January is 0!
  555. var yyyy = date.getFullYear();
  556. if (dd < 10) {
  557. dd = '0' + dd;
  558. }
  559. if (mm < 10) {
  560. mm = '0' + mm;
  561. }
  562. return yyyy + '-' + mm + '-' +dd ;
  563. }
  564. function clear_workspace()//clear all timesheet jobs
  565. {
  566. $('div.workspace > div.divTable').remove();
  567. //clear datetime picker
  568. $('div.xdsoft_datetimepicker').remove();
  569. }
  570. $('button[name="confirmschedule"]').click(function(){
  571. $('span.ticon.ticon-save').trigger('click');
  572. });
  573. $(document).on('click','div.userlist', debounce(do_filter_workspace));
  574. function do_filter_workspace(){
  575. var staffs =[];
  576. $('div.stafflist div.peopleitem :checked').each(function(i, e){
  577. var id = $(e).parent().attr('data-id');
  578. //console.log("%o, id=%s", e, id);
  579. staffs.push(id.substring(1));
  580. });
  581. var clients =[];
  582. $('div.clientlist div.peopleitem :checked').each(function(i, e){
  583. var id = $(e).parent().attr('data-id');
  584. //console.log("%o, id=%s", e, id);
  585. clients.push(id.substring(1));
  586. });
  587. console.log('staffs %o' , staffs);
  588. console.log('clients %o' , clients);
  589. filter_workspace(staffs, clients);
  590. }
  591. function filter_workspace(staffs, clients){
  592. //if both array is empty
  593. if( (staffs === undefined || staffs.length ==0) &&
  594. (clients===undefined || clients.length ==0)){
  595. //show all
  596. $('div.workspace div.divTable').show();
  597. return;
  598. }
  599. //filter some of them;
  600. $('div.workspace div.divTable').each(function(i,e){
  601. var job = $(e).data().job;
  602. var s = job.get_staff();
  603. var c = job.get_client();
  604. if (staffs.indexOf(s) ==-1 && clients.indexOf(c) ==-1)
  605. $(this).fadeOut();
  606. else
  607. $(this).fadeIn();
  608. });
  609. }
  610. reset_title_to_today();
  611. /*________________________________________________________________________*/
  612. });
  613. })(jQuery);
  614. /*______________scrolling______________________________________________*/
  615. jQuery(document).ready(function(){
  616. var timeoutid =0;
  617. jQuery('button.peoplelist[name="down"]').mousedown(function(){
  618. var button = this;
  619. timeoutid = setInterval(function(){
  620. //console.log("down scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
  621. jQuery(button).parent().find(".userlist").get(0).scrollTop +=240;
  622. }, 100);
  623. }).on('mouseup mouseleave', function(){
  624. clearTimeout(timeoutid);
  625. });
  626. jQuery('button.peoplelist[name="up"]').mousedown(function(){
  627. var button = this;
  628. timeoutid = setInterval(function(){
  629. //console.log("up scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
  630. jQuery(button).parent().find(".userlist").get(0).scrollTop -=240;
  631. }, 100);
  632. }).on('mouseup mouseleave', function(){
  633. clearTimeout(timeoutid);
  634. });
  635. });