timesheet source code
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

2225 lines
62KB

  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({obj:this, 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. reset_summary() {
  68. this.summary = {
  69. wages : 0,
  70. normal_hour : 0,
  71. ot_hour : 0,
  72. petrol : 0,
  73. };
  74. this.update_summary_in_gui();
  75. }
  76. add_payment_summary(ps)
  77. {
  78. //{ot: false, hour: "2.67", money: "76.90"}
  79. this.summary.wages += ps.money;
  80. if (! ps.ot )
  81. this.summary.normal_hour += ps.hour;
  82. else
  83. this.summary.ot_hour += ps.hour;
  84. this.update_summary_in_gui();
  85. }
  86. update_summary_in_gui()
  87. {
  88. var msg = '$' + this.summary.wages.toFixed(2);
  89. $(this.selector).find('div[name="wages"]').html(msg);
  90. msg = this.summary.normal_hour.toFixed(2) + '+' +this.summary.ot_hour.toFixed(2) + 'hr';
  91. $(this.selector).find('div[name="hours"]').html(msg);
  92. msg = 'petrol:' + this.summary.petrol.toFixed(2) + 'km';
  93. $(this.selector).find('div[name="petrol"]').html(msg);
  94. }
  95. }//end of class People
  96. function bts_staff_html(data){
  97. var template = $('#staff_item').html();
  98. var head = '<div class="peopleitem" id="p'+ data.login +'">';
  99. r = head + '</div>' ;
  100. return r;
  101. }
  102. function bts_client_html(data){
  103. var template = $('#client_item').html();
  104. var head = '<div class="peopleitem" id="p'+ data.login +'">';
  105. r = head + '</div>' ;
  106. return r;
  107. }
  108. function sample_staff(){
  109. for (var i=1; i<100; i++){
  110. var sample_people = {
  111. login: '01515b52-6936-46b2-a000-9ad4cd7a5b50' +i,
  112. firstname: "first"+i,
  113. lastname: "last",
  114. mobile: '041122221' +i,
  115. email: 'abc@gmail.com' + i,
  116. wages: 0,
  117. hour: i,
  118. OT: 3,
  119. petrol: 50 +i,
  120. rating: Math.floor(Math.random() * Math.floor(5)),
  121. unconfirmedjob: Math.floor(Math.random() * Math.floor(30)),
  122. };
  123. var html = bts_staff_html(sample_people);
  124. jQuery('div.stafflist').append(html);
  125. new People("#p" + sample_people.login, sample_people);
  126. }
  127. }
  128. function list_staff() {
  129. show_loading_staff();
  130. $('div.stafflist div.peopleitem').remove();
  131. $.post(bts().ajax_url, { // POST request
  132. _ajax_nonce: bts().nonce, // nonce
  133. action: "list_staff", // action
  134. }).done(function(response, status, xhr){
  135. if (response.status =='success'){
  136. bts().staff = response.users;
  137. bts().staff_map = {};
  138. bts().staff_people= {};
  139. response.users.forEach(function(u){
  140. bts().staff_map[u.login] = u;
  141. var html = bts_staff_html(u);
  142. jQuery('div.stafflist').append(html);
  143. var staff_obj = new People("#p" + u.login,'#staff_item', u);
  144. bts().staff_people[u.login] = staff_obj;
  145. });
  146. hide_loading_staff();
  147. calculate_total_hour_and_money();
  148. }else{
  149. alert('error getting staff list');
  150. }
  151. });
  152. }
  153. function list_clients() {
  154. show_loading_client();
  155. $('div.clientlist div.peopleitem').remove(); //clear it
  156. $.post(bts().ajax_url, { // POST request
  157. _ajax_nonce: bts().nonce, // nonce
  158. action: "list_client", // action
  159. }, function(response, status, xhr){
  160. if (response.status =='success'){
  161. bts().client = response.users;
  162. bts().client_map = {};
  163. response.users.forEach(function(u){
  164. bts().client_map[u.login] = u;
  165. hide_loading_client();
  166. var html = bts_client_html(u);
  167. jQuery('div.clientlist').append(html);
  168. new People("#p" + u.login, '#client_item' ,u);
  169. });
  170. }else{
  171. alert('error getting Client list');
  172. }
  173. });
  174. }
  175. function list_tos() {
  176. wifi(true);
  177. $.post(bts().ajax_url, { // POST request
  178. _ajax_nonce: bts().nonce, // nonce
  179. action: "list_tos", // action
  180. }, function(response, status, xhr){
  181. if (response.status =='success'){
  182. bts().tos = response.tos;
  183. wifi(false);
  184. }else{
  185. alert('error getting Type of Service list');
  186. }
  187. });
  188. }
  189. function show_loading_staff(){
  190. jQuery('div.stafflist img').attr('src', bts().load_user_img).show();
  191. }
  192. function show_loading_client(){
  193. jQuery('div.clientlist img').attr('src', bts().load_user_img).show();
  194. }
  195. function hide_loading_staff(){
  196. jQuery('div.stafflist img').hide();
  197. }
  198. function hide_loading_client(){
  199. jQuery('div.clientlist img').hide();
  200. }
  201. function show_loading_jobs(){
  202. jQuery('div.workspace img').attr('src', bts().load_job_img).show();
  203. }
  204. function hide_loading_jobs(){
  205. jQuery('div.workspace img').hide();
  206. }
  207. function xero(t){
  208. if (t)
  209. $('div.xero i').show();
  210. else
  211. $('div.xero i').hide();
  212. }
  213. function wifi(t){
  214. if (t)
  215. $('div.wifi i').show();
  216. else
  217. $('div.wifi i').hide();
  218. }
  219. function csv(t){
  220. if (t)
  221. $('div.csv i').show();
  222. else
  223. $('div.csv i').hide();
  224. }
  225. function init_user_search(){
  226. $('div.b_search input').keyup(debounce(function(e){
  227. filter_user(e.target);
  228. }, 500));
  229. }
  230. function filter_user(input){
  231. var value = $(input).attr('value');
  232. value = value.toLowerCase();
  233. var selector = get_selector_for_filter_people(input);
  234. $.each( $(selector).find('div.peopleitem'), function(index, e){
  235. //uncheck everyone
  236. $(e).find('input[type="checkbox"]').prop('checked', false);
  237. var html = $(e).find('div[name="title"] a').html();
  238. html = html.toLowerCase();
  239. if (-1 != html.indexOf(value)){//we find it;
  240. $(e).show();
  241. }else{
  242. $(e).hide();
  243. }
  244. });
  245. }
  246. function get_selector_for_filter_people(input){
  247. var selector='';
  248. var role = $(input).attr('placeholder');
  249. if (role == 'staff') //we filter staff
  250. selector = 'div.stafflist';
  251. else if (role = 'client')
  252. selector = 'div.clientlist';
  253. return selector;
  254. }
  255. $(document).on('click', 'div.jobTable div.brate.error', function(){
  256. var msg = $(this).attr('title');
  257. if (msg != "")
  258. alert(msg);
  259. });
  260. $(document).on('click', 'div.workspace div.bedit span.ticon-edit', function(){
  261. var el = $(this).closest('div.jobTable');
  262. el.addClass('Editing');
  263. var newjob_id = el.attr('data-newjob_id');
  264. if (newjob_id != ''){
  265. do_edit_new_job(newjob_id);
  266. }else{
  267. var id = el.attr('data-id');
  268. do_edit_job(id);
  269. }
  270. });
  271. // $(document).on('click', 'div.bts_editor div.ult-overlay-close', function(){
  272. // $('.Editing').addClass('blink_me');
  273. // setTimeout(function(){
  274. // $(".Editing").removeClass('Editing blink_me');
  275. // }, 1000);
  276. //
  277. // });
  278. $(document).on('click', 'div.bts_editor div.ult-overlay-close', function(){
  279. var el = $('.Editing');
  280. el.addClass("blink_me").removeClass('Editing');
  281. setTimeout(function(){
  282. el.removeClass('blink_me');
  283. }, 600);
  284. });
  285. function close_editor(){
  286. $('div.bts_editor div.ult-overlay-close').trigger('click');
  287. }
  288. $(document).on('jobEditor:close', 'div.divTable.Editor', function(e,data){
  289. var editor = data.editor;
  290. var job = data.job;
  291. //console.log('close_editor event %o, %o', editor, job);
  292. var templ = $("#jobv1_item").html();
  293. var html = Mustache.render(templ, {jobs:job}); //job id should be available;
  294. if ( $('div.jobTable.Editing').length == 0){
  295. $('div.workspace').append(html);
  296. $('#job_'+job.id).get(0).scrollIntoView();
  297. }else{
  298. //create new job underneath it
  299. var templ = $("#jobv1_item").html();
  300. var html = Mustache.render(templ, {jobs:job}); //job id should be available;
  301. $('div.jobTable.Editing').after(html).remove();
  302. }
  303. var el = $('#job_' + job.id).addClass("blink_me");
  304. setTimeout(function(){
  305. el.removeClass('blink_me');
  306. }, 1500);
  307. debounced_calculate();
  308. });
  309. $(document).on('click', 'div.divTableHead.bdelete span.ticon-trash', function(){
  310. var len = $('div.jobTable.to_be_deleted_duplicate').length;
  311. if (len <= 0){
  312. if (confirm("No duplicates to delete, trying to find duplicates?")){
  313. check_duplicate();
  314. }
  315. return;
  316. }else{
  317. if (!confirm("Delete " + len + " duplicates? "))
  318. return;
  319. }
  320. var ids = [];
  321. $('div.jobTable.to_be_deleted_duplicate').each(function(){
  322. var id = $(this).attr('data-id');
  323. ids.push(id);
  324. });
  325. $.post(bts().ajax_url, { // POST request
  326. _ajax_nonce: bts().nonce, // nonce
  327. action: "delete_jobv1", // action
  328. jobs: ids, //delete multiple ids
  329. }, function(response, status, xhr){
  330. if (response.status=='success'){
  331. $('div.jobTable.to_be_deleted_duplicate').each(function(){
  332. var id = $(this).attr('data-id');
  333. delete bts().job_map[id];
  334. $(this).get(0).scrollIntoView();
  335. fade_and_delete(this);
  336. });
  337. debounced_calculate();
  338. }else{
  339. alert( 'error deleting duplicates:\n\nError:\n\n' + response.error + "\n\n");
  340. }
  341. });
  342. });
  343. function fade_and_delete(el)
  344. {
  345. $(el).fadeOut(300);
  346. setTimeout(function(){
  347. $(el).remove();
  348. },300);
  349. }
  350. function add_new_empty_job(job){
  351. var title = "";
  352. if (typeof job == 'undefined' || ! (job instanceof Job) ){
  353. job = new Job({empty:true});
  354. job.is_new = true;
  355. title = "Create New Job ";
  356. }else if (job instanceof Job){
  357. tile = "Create new Job by Copy Existing one";
  358. }
  359. job.editorid = Math.floor(Math.random() * Math.floor(99999)); // a random number;
  360. //show editor
  361. var html = $('#jobv1_editor').html();
  362. html = Mustache.render(html, job);
  363. set_modal_content('editor', html);
  364. //update GUI
  365. open_modal('editor');
  366. set_modal_title('editor', title);
  367. dtp_init();
  368. //init editor
  369. var e = new JobEditor('#editor_' + job.editorid, job);
  370. console.log("%o is instance of JobEditor %o", e, e instanceof JobEditor);
  371. }
  372. $(document).on('click', 'div[name="copyschedule"]', function(e){
  373. e.stopPropagation();
  374. add_new_empty_job();
  375. });
  376. $(document).on('click', 'div.divTableCell.bdelete', function(){
  377. var el = $(this).closest('div.jobTable');
  378. var data = el.data();
  379. if (typeof data.id =='undefined' || data.id == '')
  380. el.remove();
  381. else{
  382. var id = data.id;
  383. if (confirm('delete this job?')){
  384. $.post(bts().ajax_url, { // POST request
  385. _ajax_nonce: bts().nonce, // nonce
  386. action: "delete_job", // action
  387. jobid: id,
  388. }, function(response, status, xhr){
  389. if (response.status=='success'){
  390. var id = el.attr('data-id');
  391. delete bts().job_map[id];
  392. //console.log("delete %s , job_map[%s]=%o ", id, id, bts().job_map[id]);
  393. el.addClass('blink_me');
  394. el.fadeOut(900);
  395. setTimeout(function(){
  396. el.remove();
  397. }, 900);
  398. }else{
  399. alert( 'error saving data, please check your network');
  400. }
  401. });
  402. }
  403. }
  404. debounced_calculate();
  405. });
  406. $(document).on('mouseenter', 'div.divTableCell', function(){
  407. $(this).closest('div.divTable').addClass('highlight');
  408. });
  409. $(document).on('mouseleave', 'div.divTableCell', function(){
  410. $(this).closest('div.divTable').removeClass('highlight');
  411. });
  412. $(document).on('click', 'div.workspace span.ticon.ticon-save', function(){
  413. var table = $(this).closest('div.divTable');
  414. table.data().job.do_save_record();
  415. });
  416. $(document).on('click', '.divTableHeading span.ticon.ticon-save', function(){
  417. //save all
  418. $('div.workspace span.ticon.ticon-save').each(function (i,e){
  419. if ($(this).is(":visible"))
  420. $(this).trigger('click');
  421. });
  422. });
  423. $(document).on('click', 'span.ticon.ticon-copy', function(){
  424. var table = $(this).closest('div.jobTable');
  425. var record = bts().job_map[table.data().id].get_record();
  426. //create new job;
  427. var newj = clone_data_create_new_job(record);
  428. add_new_empty_job(newj);
  429. });
  430. function mark_highlight_me(el, ms){
  431. el.addClass('blink_me');
  432. el.addClass('highlight');
  433. el.addClass('newcopy');
  434. setTimeout(function(){
  435. el.removeClass('blink_me');
  436. el.removeClass('highlight');
  437. el.removeClass('newcopy');
  438. },ms);
  439. }
  440. class Job{
  441. constructor(record)
  442. {
  443. this.original_attr = Object.keys(record);
  444. $.extend(this, record);
  445. this.derive_attr();
  446. }
  447. get_record()
  448. {//without extended attributes;
  449. var self = this;
  450. var r = {};
  451. this.original_attr.forEach(function(attr){
  452. r[attr] = self[attr];
  453. });
  454. return r;
  455. }
  456. derive_attr()
  457. {
  458. this.tos_name(this);
  459. this.staff_name(this);
  460. this.client_name(this);
  461. this.rate_name(this);
  462. this.rating_range(this);
  463. this.identify_job_week(this);
  464. this.job_acked(this);
  465. }
  466. tos_name(e){
  467. if (typeof bts().tos[e.tos] != 'undefined'){
  468. e.tos_name = bts().tos[e.tos].tos_full_str;
  469. }else{
  470. e.tos_name = "(deleted) ";
  471. e.tos_err="Missing: " + e.tos;
  472. }
  473. }
  474. staff_name(e){
  475. if (typeof bts().staff_map[e.staff] != 'undefined'){
  476. e.staff_name = bts().staff_map[e.staff].display_name;
  477. }else{
  478. e.staff_name = "(deleted) ";
  479. e.staff_err="Missing: " + e.staff;
  480. }
  481. }
  482. client_name(e){
  483. if (typeof bts().client_map[e.client] != 'undefined'){
  484. e.client_name = bts().client_map[e.client].display_name;
  485. }else{
  486. e.client_name ="(deleted)";
  487. e.client_err ="Missing: " + e.client;
  488. }
  489. }
  490. rate_name(e){
  491. if (typeof bts().earnings_rate[e.rate] != 'undefined') {
  492. e.rate_name = bts().earnings_rate[e.rate].RatePerUnit + "-" + bts().earnings_rate[e.rate].Name;
  493. if (! has_txt_hour( bts().earnings_rate[e.rate].TypeOfUnits )){
  494. e.rate_non_hour = true;
  495. e.rate_err = `Rate unit must be ⟦ Hours ⟧
  496. Possible solution:
  497. 1. Change it in Xero
  498. 2. Delete this job`;
  499. }
  500. }else{
  501. e.rate_name = "(deleted)";
  502. e.rate_err = "Missing: " + e.rate;
  503. }
  504. }
  505. rating_range(e){
  506. if (e.rating <0 || e.rating >5){
  507. e.rating_err = "Rating 0-5 Only";
  508. }
  509. }
  510. identify_job_week(e){
  511. if (job_is_week1(e.start)){
  512. e.is_week1=true;
  513. }else if (job_is_week2(e.start)){
  514. e.is_week2=true;
  515. }
  516. }
  517. job_acked(e){
  518. if (typeof e == 'undefined'){
  519. console.log('break;');
  520. }
  521. if (e.ack != 0){
  522. e.is_confirmed = true;
  523. }
  524. }
  525. is_job_valid(){
  526. var error_found = false;
  527. var self = this;
  528. this.original_attr.forEach(function(attr){
  529. var col = attr + "_err";
  530. if (typeof self[col] == 'undefined')
  531. return;
  532. if (self[attr+"_err"] != "")
  533. error_found = true;
  534. });
  535. return !error_found;
  536. }
  537. is_high_pay()
  538. {
  539. var job = this;
  540. var rate_info = bts().earnings_rate[job.rate];
  541. var keywords =bts().high_pay_keywords;
  542. var found = false;
  543. keywords.forEach(function(e){
  544. if (-1 != rate_info.Name.toLowerCase().indexOf(e.toLowerCase()) )
  545. found = true;
  546. });
  547. return found;
  548. }
  549. get_payment_summary()
  550. {
  551. var job = this;
  552. var result ={};
  553. result.ot = job.is_high_pay();
  554. result.hour = job.get_working_duration();
  555. result.money = job.get_wages();
  556. return result;
  557. }
  558. get_working_duration()
  559. {
  560. var job = this;
  561. //finish - start
  562. var f = new Date(job.finish);
  563. var s = new Date(job.start);
  564. var diff = f.getTime() - s.getTime();
  565. var hours = Math.floor(diff / 1000 / 60 / 60);
  566. diff -= hours * 1000 * 60 * 60;
  567. var minutes = Math.floor(diff / 1000 / 60);
  568. var minute_to_hour = minutes/60;
  569. return (hours + minute_to_hour);
  570. }
  571. get_wages()
  572. {
  573. var job = this;
  574. var hour = job.get_working_duration(job);
  575. var rate_info = bts().earnings_rate[job.rate];
  576. if ( has_txt_hour( bts().earnings_rate[job.rate].TypeOfUnits ) )
  577. {
  578. return hour * rate_info.RatePerUnit;
  579. }else{
  580. return 1 * rate_info.RatePerUnit;
  581. }
  582. }
  583. };
  584. class JobEditor{ //save data for the record, and display it as GUI
  585. constructor(selector, job){
  586. this.el = $(selector);
  587. this.load_data(job);
  588. console.assert(job instanceof Job);
  589. this.init_event_handler();
  590. }
  591. load_data(data)
  592. {
  593. //save to html element
  594. this.data = data;
  595. this.el.data({job:this, data:data});
  596. //draw GUI
  597. this.clear_err_msg();
  598. this.set_job_id(data.id);
  599. this.set_tos(data.tos);
  600. this.set_start(data.start);
  601. this.set_finish(data.finish);
  602. this.set_rate(data.rate);
  603. this.set_staff(data.staff);
  604. this.set_client(data.client);
  605. this.set_ack(data.ack);
  606. this.set_rating(data.rating);
  607. //draw GUI by other
  608. this.mark_dirty_on_new_record(data);
  609. this.mark_week_color();
  610. //this.validate(); //also triggers mark errors
  611. }
  612. init_event_handler(){
  613. var self = this;
  614. this.el.find("div.btos select").change(function(){
  615. if ( self.validate_tos() ) {
  616. self.data.tos = self.get_tos();
  617. self.set_err_msg_tos('');
  618. }
  619. });
  620. this.el.find("div.bstart input").change(function(){
  621. if (self.validate_start()){
  622. self.data.start = self.get_start();
  623. self.set_err_msg_start('');
  624. self.validate_start_and_finish();
  625. }
  626. });
  627. this.el.find("div.bfinish input").change(function(){
  628. if (self.validate_finish()){
  629. self.data.finish = self.get_finish();
  630. self.set_err_msg_finish('');
  631. self.validate_start_and_finish();
  632. }
  633. });
  634. this.el.find("div.bstaff select").change(function(){
  635. if (self.validate_staff()){
  636. self.data.staff = self.get_staff();
  637. self.set_err_msg_staff('');
  638. }
  639. });
  640. this.el.find("div.bclient select").change(function(){
  641. if (self.validate_client()){
  642. self.data.client = self.get_client();
  643. self.set_err_msg_client('');
  644. }
  645. });
  646. this.el.find("div.brate select").change(function(){
  647. if (self.validate_rate()){
  648. self.data.rate = self.get_rate();
  649. self.set_err_msg_rate('');
  650. }
  651. });
  652. this.el.find("div.bconfirmed input").change(function(){
  653. if(self.validate_ack()){
  654. self.data.ack = self.get_ack();
  655. }
  656. });
  657. this.el.find("div.brating select").change(function(){
  658. if( self.validate_rating()){
  659. self.data.rating =self.get_rating();
  660. }
  661. });
  662. this.el.find("div.bsave span.ticon-save").click(function(e){
  663. if ( self.validate() ){
  664. self.do_save_record();
  665. }else{
  666. self.set_err_msg_save('Data Error');
  667. }
  668. });
  669. }
  670. off_event_handler(){
  671. this.el.off('change',"div.btos select");
  672. //TODO
  673. this.el.find("div.bstart input").change(function(){
  674. if (self.validate_start()){
  675. self.data.start = self.get_start();
  676. self.set_err_msg_start('');
  677. self.validate_start_and_finish();
  678. }
  679. });
  680. this.el.find("div.bfinish input").change(function(){
  681. if (self.validate_finish()){
  682. self.data.finish = self.get_finish();
  683. self.set_err_msg_finish('');
  684. self.validate_start_and_finish();
  685. }
  686. });
  687. this.el.find("div.bstaff select").change(function(){
  688. if (self.validate_staff()){
  689. self.data.staff = self.get_staff();
  690. self.set_err_msg_staff('');
  691. }
  692. });
  693. this.el.find("div.bclient select").change(function(){
  694. if (self.validate_client()){
  695. self.data.client = self.get_client();
  696. self.set_err_msg_client('');
  697. }
  698. });
  699. this.el.find("div.brate select").change(function(){
  700. if (self.validate_rate()){
  701. self.data.rate = self.get_rate();
  702. self.set_err_msg_rate('');
  703. }
  704. });
  705. this.el.find("div.bconfirmed input").change(function(){
  706. if(self.validate_ack()){
  707. self.data.ack = self.get_ack();
  708. }
  709. });
  710. this.el.find("div.brating select").change(function(){
  711. if( self.validate_rating()){
  712. self.data.rating =self.get_rating();
  713. }
  714. });
  715. this.el.find("div.bsave span.ticon-save").click(function(e){
  716. if ( self.validate() ){
  717. self.do_save_record();
  718. }else{
  719. self.set_err_msg_save('Data Error');
  720. }
  721. });
  722. }
  723. get_job_id(){
  724. return this.el.attr('data-id');
  725. }
  726. set_job_id(val){
  727. if (typeof val == 'undefined' || val == '')
  728. {//remove data-id and id
  729. this.el.removeAttr('data-id');
  730. this.el.removeAttr('id');
  731. }else{
  732. this.el.attr('data-id', val);
  733. this.el.attr('id', 'job_' + val);
  734. }
  735. }
  736. get_tos()
  737. {
  738. return this.el.find('div.btos select').children("option:selected").val();
  739. }
  740. set_tos(val)
  741. {
  742. if (typeof(val) =="undefined")
  743. return;
  744. this.el.find('div.btos select option[value="'+val+'"]').prop('selected',true);
  745. if ( this.get_tos() != val ){
  746. this.set_err_msg_tos("Missing:" + this.data.tos)
  747. var o = new Option("(deleted)", this.data.tos);
  748. this.el.find('div.btos select').prepend(o);
  749. this.el.find('div.btos select option[value="'+this.data.tos+'"]').prop('selected',true);
  750. }
  751. }
  752. get_start(){
  753. return this.el.find('div.bstart input').attr('value');
  754. }
  755. set_start(val)
  756. {
  757. if (typeof(val) =="undefined"){
  758. this.set_err_msg_start("need start");
  759. return;
  760. }
  761. this.el.find('div.bstart input').attr('value', val);
  762. }
  763. get_finish()
  764. {
  765. return this.el.find('div.bfinish input').attr('value');
  766. }
  767. set_finish(val)
  768. {
  769. if (typeof(val) == "undefined"){
  770. this.set_err_msg_finish("need finish");
  771. return;
  772. }
  773. this.el.find('div.bfinish input').attr('value', val);
  774. }
  775. get_rate()
  776. {
  777. return this.el.find('div.brate select').children("option:selected").val();
  778. }
  779. set_rate(val)
  780. {
  781. if (typeof(val) =="undefined")
  782. return;
  783. this.el.find('div.brate select option[value="'+val+'"]').prop('selected',true);
  784. if ( this.get_rate() != val ){
  785. var o = new Option("(deleted)", this.data.rate);
  786. this.el.find('div.brate select').prepend(o);
  787. this.set_err_msg_rate('Missing:' + this.data.rate);
  788. this.el.find('div.brate select option[value="'+this.data.rate+'"]').prop('selected',true);
  789. }
  790. }
  791. get_staff()
  792. {
  793. return this.el.find('div.bstaff select').children("option:selected").val();
  794. }
  795. set_staff(val)
  796. {
  797. if (typeof(val) =="undefined")
  798. return;
  799. this.el.find('div.bstaff select option[value="'+val+'"]').prop('selected',true);
  800. if ( this.get_staff() != val ){
  801. var o = new Option("(deleted)", this.data.staff);
  802. this.el.find('div.bstaff select').prepend(o);
  803. this.set_err_msg_staff('Missing:' + this.data.staff);
  804. this.el.find('div.bstaff select option[value="'+this.data.staff+'"]').prop('selected',true);
  805. }
  806. }
  807. get_client()
  808. {
  809. return this.el.find('div.bclient select').children("option:selected").val();
  810. }
  811. set_client(val)
  812. {
  813. if (typeof(val) =="undefined")
  814. return;
  815. this.el.find('div.bclient select option[value="'+val+'"]').prop('selected',true);
  816. if ( this.get_client() != val ){
  817. var o = new Option("(deleted)", this.data.client);
  818. this.el.find('div.bclient select').prepend(o);
  819. this.set_err_msg_client('Missing:' + this.data.client);
  820. this.el.find('div.bclient select option[value="'+this.data.client+'"]').prop('selected',true);
  821. }
  822. }
  823. get_ack()
  824. {
  825. return this.el.find('div.bconfirmed input:checked').length > 0? 1:0;
  826. }
  827. set_ack(val)
  828. {
  829. if (typeof(val) =="undefined")
  830. return;
  831. return this.el.find('div.bconfirmed input').prop('checked', val!=0);
  832. }
  833. get_rating(){
  834. return this.el.find('div.brating select').children("option:selected").val();
  835. }
  836. set_rating(num){
  837. if (!(0 <= num && num <=5))
  838. return;
  839. this.el.find('div.brating select option[value="'+num+'"]').prop('selected',true);
  840. }
  841. get_record_from_ui(){
  842. var record = {};
  843. record.id = this.get_job_id();
  844. record.tos = this.get_tos();
  845. record.start = this.get_start();
  846. record.finish = this.get_finish();
  847. record.rate = this.get_rate();
  848. record.staff = this.get_staff();
  849. record.client = this.get_client();
  850. record.ack = this.get_ack();
  851. record.rating = this.get_rating();
  852. return record;
  853. }
  854. do_save_record(){
  855. var self = this;
  856. $.post(bts().ajax_url, { // POST request
  857. _ajax_nonce: bts().nonce, // nonce
  858. action: "save_job", // action
  859. record: self.get_record_from_ui(),
  860. }, function(response, status, xhr){
  861. if (response.status=='success'){
  862. self.load_data(response.newdata);
  863. var job = new Job(response.newdata);
  864. job.saved = true;
  865. job.is_new = response.isNew;
  866. bts().job_map[job.id] = job;
  867. var data = {editor:self, job:job};
  868. self.el.trigger('jobEditor:close', data);
  869. close_editor();
  870. }else{
  871. self.self.set_err_msg_save('Not saved');
  872. alert( 'error saving data, please check your network');
  873. }
  874. });
  875. }
  876. mark_dirty_on_new_record(data){
  877. if (typeof(data.id) === 'undefined' || data.id == ''){
  878. this.mark_dirty();
  879. this.mark_new();
  880. }
  881. else{
  882. this.mark_saved();
  883. }
  884. }
  885. mark_dirty() //need save
  886. {
  887. var d = this.el.find('.bsave');
  888. d.removeClass('saved');
  889. d.addClass('blink_me');
  890. setTimeout(function(){
  891. d.removeClass('blink_me');
  892. d.removeClass('saved');
  893. },1000);
  894. }
  895. mark_saved()
  896. {
  897. var d = this.el.find('.bsave');
  898. d.addClass('blink_me');
  899. setTimeout(function(){
  900. d.removeClass('blink_me');
  901. d.addClass('saved');
  902. },1000);
  903. }
  904. //newly created empty record
  905. mark_new()
  906. {
  907. this.el.addClass('emptyrecord');
  908. }
  909. mark_old()
  910. {
  911. this.el.removeClass('emptyrecord');
  912. }
  913. is_start_valid(){
  914. var s = this.get_start();
  915. return is_valid_date_str(s);
  916. }
  917. is_finish_valid(){
  918. var f = this.get_finish();
  919. if (!is_valid_date_str(f))
  920. return false;
  921. }
  922. is_finish_resonable(){
  923. var f = this.get_finish();
  924. if (!is_valid_date_str(f))
  925. return false;
  926. var s = this.get_start();
  927. s = new Date(s);
  928. f = new Date(f);
  929. return (s < f);
  930. }
  931. validate()
  932. {
  933. var ok_tos = this.validate_tos();
  934. var ok_time = this.validate_start() && this.validate_finish() && this.validate_start_and_finish();
  935. var ok_staff = this.validate_staff();
  936. var ok_client = this.validate_client();
  937. var ok_rate = this.validate_rate() ; //make sure this validate is executed
  938. var ok = ok_tos && ok_time && ok_staff && ok_client && ok_rate;
  939. if (ok){
  940. this.el.removeClass('invalidjob');
  941. }else{
  942. this.el.addClass('invalidjob');
  943. }
  944. return ok;
  945. }
  946. validate_start(){
  947. var str = this.get_start();
  948. if (str == ""){
  949. this.set_err_msg_start('need start');
  950. return false;
  951. }
  952. if (!is_valid_date_str(str) ){
  953. this.mark_start_invalid();
  954. this.set_err_msg_start('wrong date');
  955. return false;
  956. }
  957. return true;
  958. }
  959. validate_finish()
  960. {
  961. var str = this.get_finish();
  962. if (str == ""){
  963. this.set_err_msg_finish('need finish');
  964. return false;
  965. }
  966. if (! is_valid_date_str(str)){
  967. this.set_err_msg_finish('wrong date');
  968. this.mark_finish_invalid();
  969. return false;
  970. }
  971. return true;
  972. }
  973. validate_start_and_finish()
  974. {
  975. if (! this.validate_start() || ! this.validate_finish())
  976. return;
  977. if (!this.is_finish_resonable()){
  978. this.set_err_msg_finish("older than start");
  979. this.set_err_msg_start("after finish");
  980. this.mark_start_invalid();
  981. this.mark_finish_invalid();
  982. return false;
  983. }else{
  984. this.mark_start_valid();
  985. this.mark_finish_valid();
  986. this.set_err_msg_finish("");
  987. this.set_err_msg_start("");
  988. return true;
  989. }
  990. }
  991. validate_rate()
  992. {
  993. var rate_info = this.get_rate_info_by_id(this.get_rate());
  994. if ( rate_info.RatePerUnit <= 0){
  995. this.set_err_msg_rate('bad rate');
  996. this.mark_rate_invalid();
  997. return false;
  998. }
  999. this.set_err_msg_rate('');
  1000. this.mark_rate_valid();
  1001. return true;
  1002. }
  1003. validate_tos(){
  1004. if ( typeof bts().tos[this.get_tos()] == 'undefined') {
  1005. this.set_err_msg_tos('missing ' + this.get_tos());
  1006. return false;
  1007. }else{
  1008. this.set_err_msg_tos('');
  1009. return true;
  1010. }
  1011. }
  1012. validate_staff(){
  1013. if ( typeof bts().staff_map[this.get_staff()] == 'undefined') {
  1014. this.set_err_msg_staff('missing ' + this.get_staff());
  1015. return false;
  1016. }else{
  1017. this.set_err_msg_staff('');
  1018. return true;
  1019. }
  1020. }
  1021. validate_client(){
  1022. if ( typeof bts().client_map[this.get_client()] == 'undefined') {
  1023. this.set_err_msg_client('missing ' + this.get_client());
  1024. return false;
  1025. }else{
  1026. this.set_err_msg_client('');
  1027. return true;
  1028. }
  1029. }
  1030. validate_rating(){
  1031. return;
  1032. }
  1033. clear_err_msg(){
  1034. this.el.find('.divTableRow.errmsg > div').html('');
  1035. }
  1036. set_err_msg_start(str)
  1037. {
  1038. this.el.find('div.bstart_err').html(str);
  1039. }
  1040. set_err_msg_finish(str)
  1041. {
  1042. this.el.find('div.bfinish_err').html(str);
  1043. }
  1044. set_err_msg_staff(str)
  1045. {
  1046. this.el.find('div.bstaff_err').html(str);
  1047. }
  1048. set_err_msg_client(str)
  1049. {
  1050. this.el.find('div.bclient_err').html(str);
  1051. }
  1052. set_err_msg_rate(str)
  1053. {
  1054. this.el.find('div.brate_err').html(str);
  1055. }
  1056. set_err_msg_save(str)
  1057. {
  1058. this.el.find('div.bsave_err').html(str);
  1059. }
  1060. set_err_msg_tos(str)
  1061. {
  1062. this.el.find('div.btos_err').html(str);
  1063. }
  1064. mark_tos_valid(){
  1065. this.el.find('div.btos select').removeClass('invalid');
  1066. }
  1067. mark_tos_invalid(){
  1068. this.el.find('div.btos select').addClass('invalid');
  1069. }
  1070. mark_start_valid(){
  1071. this.el.find('div.bstart input').removeClass('invalid');
  1072. }
  1073. mark_start_invalid(){
  1074. this.el.find('div.bstart input').addClass('invalid');
  1075. }
  1076. mark_finish_valid(){
  1077. this.el.find('div.bfinish input').removeClass('invalid');
  1078. }
  1079. mark_finish_invalid(){
  1080. this.el.find('div.bfinish input').addClass('invalid');
  1081. }
  1082. mark_rate_valid(){
  1083. this.el.find('div.brate select').removeClass('invalid');
  1084. }
  1085. mark_rate_invalid(){
  1086. this.el.find('div.brate select').addClass('invalid');
  1087. }
  1088. mark_week_color(){
  1089. this.el.find('div.brating').removeClass('week1color');
  1090. this.el.find('div.brating').removeClass('week2color');
  1091. if (this.is_week1()){
  1092. this.el.find('div.brating').addClass('week1color');
  1093. }else if (this.is_week2()){
  1094. this.el.find('div.brating').addClass('week2color');
  1095. }
  1096. }
  1097. is_week1()
  1098. {
  1099. var w1_begin = new Date($('span[name="w1d1"]').data().date) ;
  1100. var w1_end = new Date($('span[name="w1d7"]').data().date);
  1101. w1_begin.setHours(0,0,0,0);
  1102. w1_end.setHours(23,59,59);
  1103. //console.log("week1 begin %o, end %o", w1_begin, w1_end);
  1104. //w1_end = new Date (w1_end.setDate(w1_end.getDate()+1)); //from 00:00 to 23:59;
  1105. var me = new Date(this.data.start);
  1106. return (w1_begin <= me && me <= w1_end );
  1107. }
  1108. is_week2()
  1109. {
  1110. var w2_begin = new Date($('span[name="w2d1"]').data().date);
  1111. var w2_end = new Date($('span[name="w2d7"]').data().date);
  1112. w2_begin.setHours(0,0,0,0);
  1113. w2_end.setHours(23,59,59);
  1114. var me = new Date(this.data.start);
  1115. return (w2_begin <= me && me <= w2_end );
  1116. }
  1117. get_payment_summary(){
  1118. var result ={};
  1119. result.ot = this.get_is_high_pay();
  1120. result.hour = this.get_working_duration();
  1121. result.money = this.get_wages();
  1122. return result;
  1123. }
  1124. get_is_high_pay()
  1125. {
  1126. var rate_info = this.get_rate_info_by_id(this.get_rate());
  1127. return this.is_high_pay_hour(rate_info);
  1128. }
  1129. get_working_duration()
  1130. {
  1131. //finish - start
  1132. var f = new Date(this.get_finish());
  1133. var s = new Date(this.get_start());
  1134. var diff = f.getTime() - s.getTime();
  1135. var hours = Math.floor(diff / 1000 / 60 / 60);
  1136. diff -= hours * 1000 * 60 * 60;
  1137. var minutes = Math.floor(diff / 1000 / 60);
  1138. var minute_to_hour = minutes/60;
  1139. return (hours + minute_to_hour);
  1140. }
  1141. get_wages(){
  1142. var hour = this.get_working_duration();
  1143. var rate_info = this.get_rate_info_by_id(this.get_rate());
  1144. return hour * rate_info.RatePerUnit;
  1145. }
  1146. get_rate_info_by_id(id){
  1147. var rate_info = {};
  1148. var rates = bts().earnings_rate;
  1149. for(var i =0; i< rates.length; i++){
  1150. var r = rates[i];
  1151. if(r.EarningsRateID == id){
  1152. rate_info = $.extend(true,{}, r);//make a copy
  1153. break;
  1154. }
  1155. }
  1156. return rate_info;
  1157. }
  1158. is_high_pay_hour(rate_info){
  1159. var keywords =bts().high_pay_keywords;
  1160. var found = false;
  1161. return false;
  1162. keywords.forEach(function(e){
  1163. if (-1 != rate_info.Name.toLowerCase().indexOf(e.toLowerCase()) )
  1164. found = true;
  1165. });
  1166. return found;
  1167. }
  1168. }//end of class Job
  1169. //global GUI summary
  1170. function get_wages()
  1171. {
  1172. var txt = $('div.wages div').html();
  1173. return parseInt(txt);
  1174. }
  1175. function set_wages(num){
  1176. $('div.wages div').html(num);
  1177. }
  1178. function set_working_hours(num){
  1179. $('input#woh').attr('value', num);
  1180. }
  1181. function get_working_hours(){
  1182. var txt = $('input#woh').attr('value');
  1183. return parseFloat(txt);
  1184. }
  1185. //modal box
  1186. function set_modal_title(selector, title){
  1187. var s = 'div.bts_'+ selector +' .ult_modal-title';
  1188. $(s).html(title);
  1189. }
  1190. function set_modal_content(selector, content){
  1191. var s = 'div.bts_'+ selector +' div.ult_modal-body.ult-html';
  1192. $(s).html(content);
  1193. }
  1194. function open_modal (selector){
  1195. var s='div.bts_'+selector+'_button';
  1196. $(s).trigger('click');
  1197. }
  1198. function set_modal_data(selector, data){
  1199. var s = 'div.bts_'+ selector;
  1200. $(s).data(data);
  1201. }
  1202. function get_modal_data(selector, data){
  1203. var s = 'div.bts_'+ selector;
  1204. $(s).data();
  1205. }
  1206. // setTimeout(function(){
  1207. // set_modal_title('warning', 'suck title');
  1208. // set_modal_content('warning', 'fucking details');
  1209. // //open_modal('warning');
  1210. // }, 1000);
  1211. //
  1212. // setTimeout(function(){
  1213. // set_modal_title('error', 'error title');
  1214. // set_modal_content('error', 'error details');
  1215. // //open_modal('error');
  1216. // }, 5000);
  1217. $(document).on('mouseenter', 'div.week1 div', function(){
  1218. $(this).addClass('blink_me');
  1219. get_week2_partner(this).addClass('blink_me');
  1220. blink_same_date_by_div(this);
  1221. });
  1222. $(document).on('mouseleave', 'div.week1 div', function(){
  1223. $(this).removeClass('blink_me');
  1224. get_week2_partner(this).removeClass('blink_me');
  1225. unblink_all_date();
  1226. });
  1227. function get_week2_partner(div){
  1228. var index = $(div).index()+1;
  1229. return $('div.week2 div:nth-child('+index+')');
  1230. }
  1231. function init_weekdays(){
  1232. var curr = new Date; // get current date
  1233. init_weekdays_by_anchor(curr, true);
  1234. return;
  1235. }
  1236. function init_weekdays_by_anchor(anchor, is_week1){
  1237. var curr = new Date(anchor); // get the date;
  1238. if (!is_week1){ //it is week2, shift for 7 days;
  1239. curr.setDate(curr.getDate() -7); //curr will be changed;
  1240. }
  1241. var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first
  1242. var last = first + 6; // last day is the first day + 6
  1243. if (curr.getDay() == 0 ){// it's Sunday;
  1244. last = curr.getDate();
  1245. first = last - 6;
  1246. }
  1247. var pos = 1; //first lot
  1248. for (var i=first; i<=last; i++)
  1249. {
  1250. var now = new Date(curr);
  1251. var d1 = new Date(now.setDate(i));
  1252. now = new Date(curr);
  1253. var d2 = new Date(now.setDate(i+7));
  1254. set_day_number(1,pos, d1); //week 1
  1255. set_day_number(2,pos, d2); //week 2
  1256. pos +=1;
  1257. }
  1258. }
  1259. function set_day_number(week, index, date){
  1260. var selector = 'span[name="w'+week+'d'+index+'"]';
  1261. $(selector).html(date.getDate());
  1262. $(selector).data({date:date});
  1263. //console.log('set w%d-d%d %o', week,index,date);
  1264. }
  1265. function set_today(){
  1266. var selector = 'div.sheettitle span[name="today"]';
  1267. var curr = new Date;
  1268. $(selector).html(format_date(curr));
  1269. }
  1270. Date.prototype.get_week_number = function(){
  1271. var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
  1272. var dayNum = d.getUTCDay() || 7;
  1273. d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  1274. var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
  1275. return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
  1276. };
  1277. function set_week_number(){
  1278. var date = $('span[name="w1d1"]').data().date;
  1279. //console.log("date %o", date);
  1280. var num = date.get_week_number();
  1281. $('div.weekly span[name="week1"]').html(num);
  1282. $('div.weekly span[name="week2"]').html(num+1);
  1283. set_week_boundry();
  1284. }
  1285. function set_week_boundry()
  1286. {
  1287. var date = $('span[name="w1d1"]').data().date;
  1288. $('#week1b').attr('value', format_date(date));
  1289. var date = $('span[name="w2d7"]').data().date;
  1290. $('#week2b').attr('value', format_date(date));
  1291. }
  1292. function number_of_unsaved_job(){
  1293. var count =0;
  1294. var total_job = $('div.jobTable').length;
  1295. var total_saved = $('div.jobTable.saved').length;
  1296. var empty = $('div.emptyrecord').length;
  1297. count = total_job - total_saved - empty;
  1298. return count;
  1299. }
  1300. $('div.prevweek.left').click(function(){
  1301. if (number_of_unsaved_job() > 0){
  1302. if(!confirm("you have unsaved jobs, proceed will lost them"))
  1303. return;
  1304. }
  1305. $('div.weekdays span.weekday').each(function(i, e){
  1306. var date = $(e).data().date;
  1307. var newdate = new Date(date.setDate(date.getDate() -7 ));
  1308. $(e).html(newdate.getDate());
  1309. $(e).data({data:newdate});
  1310. });
  1311. set_week_number();
  1312. debounced_load_timesheet();
  1313. });
  1314. $('div.nextweek.right').click(function(){
  1315. if (number_of_unsaved_job() > 0){
  1316. if(!confirm("you have unsaved jobs,proceed will lost them"))
  1317. return;
  1318. }
  1319. $('div.weekdays span.weekday').each(function(i, e){
  1320. var date = $(e).data().date;
  1321. var newdate = new Date(date.setDate(date.getDate() +7 ));
  1322. $(e).html(newdate.getDate());
  1323. $(e).data({data:newdate});
  1324. });
  1325. set_week_number();
  1326. debounced_load_timesheet();
  1327. });
  1328. $('div.weekly div.weekname.prev >input ').click(function(e){
  1329. e.stopPropagation();
  1330. });
  1331. $('div.weekly div.weekname.prev >input ').change(function(e){
  1332. var date = $('#week1b').attr('value');
  1333. init_weekdays_by_anchor(date, true);
  1334. set_week_number();
  1335. debounced_load_timesheet();
  1336. });
  1337. $('div.weekly div.weekname.prev').click(function(){
  1338. if (!confirm ('copy entire week to next week? '))
  1339. return;
  1340. var jobs = [];
  1341. $('div.week1 >div').each(function(i,e){
  1342. var date = new Date($(e).find('span.weekday').data().date);
  1343. var strDate = format_date(date); //yyyy-mm-dd
  1344. $('div.bstart:visible').each(function(i,e){
  1345. var value = $(e).html();
  1346. if( -1 != value.indexOf(strDate) ) //found
  1347. {
  1348. var el = $(e).closest('div.jobTable');
  1349. if (el.is(":visible") && el.hasClass('saved')){
  1350. var id = el.data().id;
  1351. var j = bts().job_map[id];
  1352. var newj = clone_data_create_new_job(j.get_record(),7);//add 7 days
  1353. add_new_job_to_map(newj);
  1354. jobs.push(newj);
  1355. }
  1356. }
  1357. });
  1358. });
  1359. show_jobs(jobs);
  1360. debounced_calculate();
  1361. });
  1362. $('div.weekly div.weekname.next > input').click(function(e){
  1363. e.stopPropagation();
  1364. });
  1365. $('div.weekly div.weekname.next >input ').change(function(e){
  1366. e.stopPropagation();
  1367. var date = $('#week2b').attr('value');
  1368. init_weekdays_by_anchor(date, false);
  1369. set_week_number();
  1370. debounced_load_timesheet();
  1371. });
  1372. $('div.weekly div.weekname.next').click(function(e){
  1373. $(e).find('input').trigger('click');
  1374. });
  1375. $('div.week1 > div').click(function(e){
  1376. e.stopPropagation();
  1377. if ($('div.bstart.blink_me').length == 0){
  1378. alert("nothing to copy");
  1379. return;
  1380. }
  1381. if (!confirm ('copy to next week'))
  1382. return;
  1383. var new_jobs = [];
  1384. $('div.bstart.blink_me').each(function(i,e){
  1385. var r = copy_single_day_to_next_week(e);
  1386. if (r != false){
  1387. add_new_job_to_map(r);
  1388. new_jobs.push(r);
  1389. }
  1390. });
  1391. show_jobs(new_jobs);
  1392. unblink_all_date();
  1393. });
  1394. $('div.week1,div.week2').click(function(e){
  1395. e.stopPropagation();
  1396. $(this).toggleClass('filtered');
  1397. do_filter_workspace();
  1398. });
  1399. function copy_single_day_to_next_week(el){
  1400. var tb = $(el).closest('div.jobTable');
  1401. if (tb.is(':visible') && tb.hasClass('saved')){
  1402. var id = tb.data().id;
  1403. var j = bts().job_map[id];
  1404. var newj = clone_data_create_new_job(j.get_record() , 7); // +7 days
  1405. return newj;
  1406. }
  1407. return false;
  1408. }
  1409. function clone_data_create_new_job(val, num_of_shifted_days){
  1410. var data = $.extend(true, {}, val);//make a copy
  1411. num_of_shifted_days = typeof num_of_shifted_days !=='undefined'? num_of_shifted_days: 0;// 0 days
  1412. //reset
  1413. data.id='';
  1414. data.ack = 0;
  1415. data.rating = 0;
  1416. if (is_valid_date_str(data.start)){
  1417. var s = new Date(data.start);
  1418. var s1 = s.getDate() + num_of_shifted_days;
  1419. s = new Date(s.setDate(s1));
  1420. data.start = format_date_time(s);
  1421. }
  1422. if (is_valid_date_str(data.finish)){
  1423. var f = new Date(data.finish);
  1424. var f1 = f.getDate() + num_of_shifted_days;
  1425. f = new Date(f.setDate(f1));
  1426. data.finish = format_date_time(f);
  1427. }
  1428. var newj = new Job(data);
  1429. //return;
  1430. return newj;
  1431. }
  1432. function add_new_job_to_map(newj)
  1433. {
  1434. //add to job map
  1435. newj.newjob_id = "new_" + bts_unique_ID();
  1436. if (typeof bts().job_map_new == 'undefined'){
  1437. bts().job_map_new = [];
  1438. }
  1439. bts().job_map_new[newj.newjob_id] = newj;
  1440. }
  1441. function is_valid_date_str(val){
  1442. var d = new Date(val);
  1443. if (d.toString()== 'Invalid Date')
  1444. return false;
  1445. return true;
  1446. }
  1447. function blink_same_date_by_div(div){
  1448. var date = new Date($(div).find('span.weekday').data().date);
  1449. blink_same_date(date);
  1450. }
  1451. function blink_same_date(date){
  1452. var strDate = format_date(date); //yyyy-mm-dd
  1453. var els=[];
  1454. unblink_all_date();
  1455. var first_into_view = false; //make sure first row in match is visible
  1456. $('div.bstart').each(function(i,e){
  1457. if ( $(e).is(":visible") ){
  1458. var value = $(e).html();
  1459. if( -1 != value.indexOf(strDate) ) //found
  1460. {
  1461. if ( !first_into_view ){ //scroll to top
  1462. first_into_view = true;
  1463. $(e).get(0).scrollIntoView();
  1464. }
  1465. els.push(e);
  1466. $(e).addClass('blink_me');
  1467. }
  1468. }
  1469. });
  1470. }
  1471. function unblink_all_date(){
  1472. $('div.bstart').removeClass('blink_me');
  1473. }
  1474. $('div.sheettitle h1 span').click(function(){
  1475. reset_title_to_today();
  1476. load_timesheet();
  1477. });
  1478. function reset_title_to_today(){
  1479. set_today();
  1480. init_weekdays();
  1481. set_week_number();
  1482. }
  1483. var debounced_load_timesheet = debounce(load_timesheet,1000);
  1484. function load_timesheet()
  1485. {
  1486. clear_workspace();
  1487. var first = $('span[name="w1d1"]').data().date;
  1488. var last = $('span[name="w2d7"]').data().date;
  1489. $.post(bts().ajax_url, { // POST request
  1490. _ajax_nonce: bts().nonce, // nonce
  1491. action: "list_jobv1", // action
  1492. start: format_date(first),
  1493. finish: format_date(last),
  1494. }, function(response, status, xhr){
  1495. if (response.status =='success'){
  1496. display_jobs_after_staff_client_tos_info_ready(response);
  1497. }else{
  1498. alert('error loading job');
  1499. hide_loading_jobs();
  1500. }
  1501. });
  1502. }
  1503. function display_jobs_after_staff_client_tos_info_ready(response)
  1504. {
  1505. var b = bts();
  1506. if ((typeof b.staff_map != "undefined" && Object.keys(b.staff_map).length > 0) &&
  1507. (typeof b.client_map != "undefined" && Object.keys(b.client_map).length > 0) &&
  1508. (typeof b.tos != "undefined" && Object.keys(b.tos).length > 0 ) &&
  1509. (typeof b.earnings_rate != "undefined" && Object.keys(b.earnings_rate).length > 0 ))
  1510. {
  1511. var job_map={};
  1512. var jobs = [];
  1513. //map data for each jobTable
  1514. response.jobs.forEach(function(e){
  1515. //job_derive_attr(e);
  1516. var j = new Job(e)
  1517. j.saved=true; //this is a record from database;
  1518. job_map[e.id] = j;
  1519. jobs.push(j);
  1520. });
  1521. bts().job_map = job_map;
  1522. //we do works, load timesheets
  1523. var template = $("#jobv1_item").html();
  1524. var html = Mustache.render(template, {jobs:jobs});
  1525. $('div.workspace').append(html);
  1526. hide_loading_jobs();
  1527. //filter it if reqired
  1528. debounced_filter_workspace();
  1529. return;
  1530. }
  1531. //console.log('wating staff/client/tos info to be ready');
  1532. setTimeout(function(){
  1533. display_jobs_after_staff_client_tos_info_ready(response);
  1534. }, 500); //try it half seconds later
  1535. }
  1536. function has_txt_hour(str){
  1537. var s = str.toLowerCase();
  1538. return s.indexOf('hour') != -1;
  1539. }
  1540. function show_jobs(jobs){
  1541. if (jobs.length >0){
  1542. var templ = $("#jobv1_item").html();
  1543. var html = Mustache.render(templ, {jobs:jobs}); //job id should be available;
  1544. var el = $(html);
  1545. $('div.workspace').append(el);
  1546. el.get(0).scrollIntoView();
  1547. }
  1548. debounced_calculate();
  1549. }
  1550. function format_date(date){
  1551. var dd = date.getDate();
  1552. var mm = date.getMonth() + 1; //January is 0!
  1553. var yyyy = date.getFullYear();
  1554. if (dd < 10) {
  1555. dd = '0' + dd;
  1556. }
  1557. if (mm < 10) {
  1558. mm = '0' + mm;
  1559. }
  1560. return yyyy + '-' + mm + '-' +dd ;
  1561. }
  1562. function format_date_time(date){
  1563. var strdate = format_date(date);
  1564. var hh = date.getHours();
  1565. if (hh<10){
  1566. hh= '0' + hh;
  1567. }
  1568. var mm = date.getMinutes();
  1569. if (mm<10){
  1570. mm='0' + mm;
  1571. }
  1572. return strdate + ' ' + hh + ":" + mm;
  1573. }
  1574. function clear_workspace()//clear all timesheet jobs
  1575. {
  1576. $('div.workspace > div.divTable').remove();
  1577. //clear datetime picker
  1578. $('div.xdsoft_datetimepicker').remove();
  1579. //
  1580. show_loading_jobs();
  1581. }
  1582. $('div.workinghours').click(function(){
  1583. $('div.bts_message_button').trigger('click');
  1584. });
  1585. $('button[name="confirmschedule"]').click(function(){//TODO: check error before confirm
  1586. if (!confirm('sending email to each staff for their job arrangement?'))
  1587. return;
  1588. $('div.bts_message .ult-overlay-close-inside').hide();
  1589. $('div.bts_message_button').trigger('click');
  1590. setTimeout(do_email_jobs, 2000);//2 seconds for dialog to popup
  1591. });
  1592. $('button[name="confirmschedule"]').mouseenter(function(){
  1593. $('div.week2').addClass('blink_me');
  1594. })
  1595. $('button[name="confirmschedule"]').mouseleave(function(){
  1596. $('div.week2').removeClass('blink_me');
  1597. })
  1598. var debounced_filter_workspace = debounce(do_filter_workspace, 1000);
  1599. $(document).on('click','div.userlist', debounced_filter_workspace);
  1600. function do_filter_workspace(){
  1601. var staffs =[];
  1602. $('div.stafflist div.peopleitem :checked').each(function(i, e){
  1603. if ($(e).parent().is(':visible')){
  1604. var id = $(e).parent().attr('data-id');
  1605. //console.log("%o, id=%s", e, id);
  1606. staffs.push(id.substring(1));
  1607. }
  1608. });
  1609. var clients =[];
  1610. $('div.clientlist div.peopleitem :checked').each(function(i, e){
  1611. if ($(e).parent().is(':visible')){
  1612. var id = $(e).parent().attr('data-id');
  1613. //console.log("%o, id=%s", e, id);
  1614. clients.push(id.substring(1));
  1615. }
  1616. });
  1617. filter_workspace(staffs, clients);
  1618. filter_workspace_by_weeks();
  1619. debounced_calculate();
  1620. }
  1621. function filter_workspace(staffs, clients){
  1622. //if both array is empty
  1623. if( (staffs === undefined || staffs.length ==0) &&
  1624. (clients===undefined || clients.length ==0)){
  1625. //show all
  1626. $('div.workspace div.divTable').show();
  1627. return;
  1628. }
  1629. //if staffs is empty, we only filter by client
  1630. if (staffs === undefined || staffs.length ==0){
  1631. filter_workspace_by_client(clients);
  1632. return;
  1633. }
  1634. //if clients is empty, we only filter by staff
  1635. if (clients===undefined || clients.length ==0){
  1636. filter_workspace_by_staff(staffs);
  1637. return;
  1638. }
  1639. //filter by both
  1640. filter_workspace_by_both(staffs, clients);
  1641. }
  1642. function filter_workspace_by_staff(staffs)
  1643. {
  1644. var class_name='to_be_shown';
  1645. //filter some of them;
  1646. staffs.forEach(function(e){
  1647. $('div.workspace div.jobTable[data-staff="' + e + '"]').addClass(class_name);
  1648. });
  1649. $('div.workspace div.jobTable.' + class_name).fadeIn();
  1650. $('div.workspace div.jobTable:not(.'+ class_name +')').hide();
  1651. $('.' + class_name).removeClass(class_name);
  1652. }
  1653. function filter_workspace_by_client(clients)
  1654. {
  1655. var class_name='to_be_shown';
  1656. //filter some of them;
  1657. clients.forEach(function(e){
  1658. $('div.workspace div.jobTable[data-client="' + e + '"]').addClass(class_name);
  1659. });
  1660. $('div.workspace div.jobTable.' + class_name).fadeIn();
  1661. $('div.workspace div.jobTable:not(.'+ class_name +')').hide();
  1662. $('.' + class_name).removeClass(class_name);
  1663. }
  1664. function filter_workspace_by_both(staffs, clients)
  1665. {
  1666. var class_name='hide';
  1667. //filter some of them;
  1668. clients.forEach(function(e){
  1669. $('div.workspace div.jobTable:not([data-client="' + e + '"])').addClass(class_name);
  1670. });
  1671. staffs.forEach(function(e){
  1672. $('div.workspace div.jobTable:not([data-staff="' + e + '"])').addClass(class_name);
  1673. });
  1674. $('div.workspace div.jobTable.' + class_name).hide();
  1675. $('div.workspace div.jobTable:not(.'+ class_name +')').show();
  1676. $('.' + class_name).removeClass(class_name);
  1677. }
  1678. function filter_workspace_by_weeks(){
  1679. var hide_week1 = $('div.week1').hasClass('filtered');
  1680. var hide_week2 = $('div.week2').hasClass('filtered');
  1681. if (hide_week1 && hide_week2 ){
  1682. alert("You are hiding both weeks");
  1683. $('div.jobTable').show();//show non-week1 and none-week2
  1684. $('div.jobTable.week1job,div.jobTable.week2job').hide(); //hide week1 or week2;
  1685. }else if (hide_week1){
  1686. $('div.jobTable:not(.week1job)').show();//show non-week1
  1687. $('div.jobTable.week1job').hide(); //hide week1;
  1688. }else if (hide_week2){
  1689. $('div.jobTable.week2job').hide(); //show non-week2
  1690. $('div.jobTable:not(.week2job)').show(); //hide week2
  1691. }
  1692. }
  1693. var debounced_calculate = debounce(calculate_total_hour_and_money, 500);
  1694. function calculate_total_hour_and_money()
  1695. {
  1696. //init pays for all staff;
  1697. var pays={
  1698. total: 0,
  1699. hours: 0,
  1700. };
  1701. $('.stafflist > div.peopleitem').each(function(i,e){
  1702. var people = $(this).data().obj;
  1703. people.reset_summary();
  1704. });
  1705. $('div.workspace > .divTable.jobTable').each(function(i,e){
  1706. if (! $(e).is(':visible'))
  1707. return;
  1708. var id = $(e).attr('data-id');
  1709. var job = bts().job_map[id];
  1710. if (id == ''){
  1711. //is this a new Job without id?
  1712. var newjob_id = $(e).attr('data-newjob_id');
  1713. if ( typeof newjob_id != "undefined"){
  1714. id = newjob_id;
  1715. job = bts().job_map_new[newjob_id];
  1716. }
  1717. }
  1718. if (typeof job === 'undefined' || !job.is_job_valid() )
  1719. return;
  1720. var ps = job.get_payment_summary();
  1721. pays.total += ps.money;
  1722. pays.hours += ps.hour;
  1723. var staff = job.staff;
  1724. var people = bts().staff_people[staff]; //class People
  1725. if (people !=false)
  1726. people.add_payment_summary(ps);
  1727. });
  1728. set_wages(pays.total.toFixed(2));
  1729. set_working_hours(pays.hours.toFixed(2));
  1730. }
  1731. function find_staff(login)
  1732. {
  1733. var d = $('#p'+login).data();
  1734. if (typeof d === 'undefined')
  1735. return false;
  1736. return $('#p'+login).data().obj;
  1737. }
  1738. $(document).on('change', '.divTableRow input[name="ack"]', function(e) {
  1739. var el = $(this).closest('.jobTable');
  1740. var data = el.data();
  1741. data.ack = e.checked? 1: 0;
  1742. job_mark_dirty(el);
  1743. });
  1744. function job_mark_dirty(el)
  1745. {
  1746. el.removeClass('saved');
  1747. el.addClass('dirty');
  1748. }
  1749. function job_mark_clean(el)
  1750. {
  1751. el.addClass('saved');
  1752. el.removeClass('dirty');
  1753. }
  1754. function job_is_week1(t)
  1755. {
  1756. var w1_begin = new Date($('span[name="w1d1"]').data().date) ;
  1757. var w1_end = new Date($('span[name="w1d7"]').data().date);
  1758. w1_begin.setHours(0,0,0,0);
  1759. w1_end.setHours(23,59,59);
  1760. //console.log("week1 begin %o, end %o", w1_begin, w1_end);
  1761. //w1_end = new Date (w1_end.setDate(w1_end.getDate()+1)); //from 00:00 to 23:59;
  1762. var me = new Date(t);
  1763. return (w1_begin <= me && me <= w1_end );
  1764. }
  1765. function job_is_week2(t)
  1766. {
  1767. var w2_begin = new Date($('span[name="w2d1"]').data().date);
  1768. var w2_end = new Date($('span[name="w2d7"]').data().date);
  1769. w2_begin.setHours(0,0,0,0);
  1770. w2_end.setHours(23,59,59);
  1771. var me = new Date(t);
  1772. return (w2_begin <= me && me <= w2_end );
  1773. }
  1774. function init_ts(){
  1775. xero(false);
  1776. wifi(false);
  1777. csv(false);
  1778. show_loading_jobs();
  1779. list_staff();
  1780. list_clients();
  1781. list_tos();
  1782. ajax_earning_rate();
  1783. //setTimeout(list_staff, 5000); // for testing delayed loading of jobs only
  1784. //setTimeout(list_clients, 8000); // for testing delayed loading of jobs only
  1785. //setTimeout(list_tos, 10000); // for testing delayed loading of jobs only
  1786. init_user_search();
  1787. reset_title_to_today();
  1788. load_timesheet();
  1789. }
  1790. function do_email_jobs()
  1791. {
  1792. var selector = 'div.bts_message div.ult_modal-body';
  1793. $(selector).html('Analysis staff jobs ... ok');
  1794. var staff = bts().staff.slice(0);//copy this array;
  1795. var s = staff.pop();
  1796. //week2 start
  1797. var w2_begin = new Date($('span[name="w2d1"]').data().date);
  1798. var w2_end = new Date($('span[name="w2d7"]').data().date);
  1799. var start = format_date(w2_begin);
  1800. var finish = format_date(w2_end);
  1801. function do_staff(){
  1802. var el = $('<p> Checking ' + s.firstname + "....</p>");
  1803. $(selector).append(el);
  1804. el[0].scrollIntoView();
  1805. $.post(bts().ajax_url, { // POST request
  1806. _ajax_nonce: bts().nonce, // nonce
  1807. action: "email_job", // action
  1808. staff : s.login,
  1809. start : start,
  1810. finish: finish,
  1811. }, function(response, status, xhr){
  1812. if (response.status == 'success'){
  1813. if (response.sent){
  1814. el.append('<span class="sent">' + response.emailstatus + '</span>');
  1815. }else{
  1816. el.append('<span class="nojob">' + response.emailstatus + '</span>');
  1817. }
  1818. }else{
  1819. el.append('<span class="error"> Error[' + response.error + ' ...]</span>');
  1820. }
  1821. }).fail(function(){
  1822. el.append('<span class="error">' + 'Network Error occured' + '</span>');
  1823. //clear staff pending list, stop further processing
  1824. s = [];
  1825. }).always(function(){//next staff
  1826. if (staff.length >0){
  1827. s = staff.pop();
  1828. setTimeout(do_staff, 100); //a short delay makes it looks nice
  1829. }else{
  1830. $('div.bts_message .ult-overlay-close-inside').show();
  1831. $('div.bts_message .ult-overlay-close-inside').addClass('blink_me');
  1832. $('div.week2').removeClass('blink_me');
  1833. $(selector).append('<span class="sent">All staff confirmed! </span>');
  1834. }
  1835. });
  1836. }
  1837. //execute
  1838. do_staff();
  1839. }
  1840. function ajax_earning_rate(){
  1841. $.post(bts().ajax_url, { // POST request
  1842. _ajax_nonce: bts().nonce, // nonce
  1843. action: "earnings_rate", // action
  1844. }, function(response, status, xhr){
  1845. bts().earnings_rate = {};
  1846. response.options.forEach(function(e){
  1847. bts().earnings_rate[e.EarningsRateID]=e;
  1848. });
  1849. //console.log("%o", bts().earnings_rate);
  1850. });
  1851. }
  1852. function check_duplicate()
  1853. {
  1854. var to_be_deleted=[];
  1855. //loop through jobs
  1856. for(var id1 in bts().job_map){
  1857. var job1 = bts().job_map[id1];
  1858. if (typeof job1.parent != 'undefined')
  1859. continue; //bypass it it has already found parents
  1860. job1.compared_as_master = true;//mark it
  1861. job1.duplicates={};
  1862. //console.log('investigating %s' , job1.id);
  1863. //match job2
  1864. for(var id2 in bts().job_map){
  1865. var job2 = bts().job_map[id2];
  1866. if (typeof job2.compared_as_master != 'undefined')
  1867. continue;
  1868. if (typeof job2.parent != "undefined")
  1869. continue; //it has already parent;
  1870. //console.log('comareing %s vs %s', job1.id, job2.id);
  1871. if (is_same_job(job1,job2)){
  1872. job2.parent = job1.id;
  1873. job1.duplicates[id2] = job2;
  1874. //console.warn("found: %s = %s", job1.id, job2.id);
  1875. to_be_deleted.push(id2);
  1876. }
  1877. }
  1878. }
  1879. if (to_be_deleted.length == 0){
  1880. alert("No duplicate found!");
  1881. return;
  1882. }
  1883. bts().to_be_deleted_duplicate = to_be_deleted;
  1884. //console.log('all-done, found %d duplicates: %o', to_be_deleted.length, to_be_deleted);
  1885. mark_duplicate(to_be_deleted);
  1886. }
  1887. function is_same_job(job1, job2)
  1888. {
  1889. if ( (job1.tos == job2.tos) &&
  1890. (job1.staff == job2.staff) &&
  1891. (job1.client == job2.client) &&
  1892. (job1.start == job2.start) &&
  1893. (job1.finish == job2.finish) )
  1894. {
  1895. return true;
  1896. }
  1897. return false;
  1898. }
  1899. function mark_duplicate(ids)
  1900. {
  1901. ids.forEach(function(id){
  1902. var selector = '#job_' +id;
  1903. $(selector).get(0).scrollIntoView();
  1904. $(selector).addClass('to_be_deleted_duplicate');
  1905. });
  1906. }
  1907. function do_edit_new_job(id)
  1908. {
  1909. open_modal('editor');
  1910. set_modal_title('editor', "Editing New Job ");
  1911. var new_job = bts().job_map_new[id];
  1912. new_job.editorid = id;
  1913. //set_modal_data('editor', {jobid: id, job_copy:job_copy});
  1914. var html = $('#jobv1_editor').html();
  1915. html = Mustache.render(html, new_job);
  1916. set_modal_content('editor', html);
  1917. //update GUI
  1918. dtp_init();
  1919. //init editor
  1920. var e = new JobEditor('#editor_' + id, new_job);
  1921. //console.log("e is instance of JobEditor %o", e instanceof JobEditor);
  1922. }
  1923. function do_edit_job(id)
  1924. {
  1925. open_modal('editor');
  1926. set_modal_title('editor', "Editing Job: " + id);
  1927. //make a copy of the job
  1928. var child = bts().job_map[id];
  1929. var job_copy = Object.assign(Object.create(Object.getPrototypeOf(child)), child); //a shallow copy only;
  1930. job_copy.editorid = bts_random_number();
  1931. //set_modal_data('editor', {jobid: id, job_copy:job_copy});
  1932. var html = $('#jobv1_editor').html();
  1933. html = Mustache.render(html, job_copy);
  1934. set_modal_content('editor', html);
  1935. //update GUI
  1936. dtp_init();
  1937. //init editor
  1938. var e = new JobEditor('#editor_' + job_copy.editorid, job_copy);
  1939. //console.log("e is instance of JobEditor %o", e instanceof JobEditor);
  1940. }
  1941. $( ".boundary_datepicker" ).datepicker();
  1942. $( ".boundary_datepicker" ).datepicker("option", "dateFormat", "yy-mm-dd");
  1943. $(document).on('click', 'div.clientlist div[name="title"] a', function(e){
  1944. e.preventDefault();
  1945. e.stopPropagation();
  1946. var id = $(this).closest('label.peopleitem').attr('data-id').substring(1);
  1947. var str = 'https://acaresydncy.com.au/feedback_card/' + id;
  1948. var name = $(this).html();
  1949. if ( confirm ("Email feedback link of : " + name + "\n\n\n" + str + "\n\n\n to helen@acaresydney.com.au?")){
  1950. $.post(bts().ajax_url, { // POST request
  1951. _ajax_nonce: bts().nonce, // nonce
  1952. action: "email_feedback_url", // action
  1953. client : id,
  1954. }, function(response, status, xhr){
  1955. //alert('please check your email on the phone and SMS the link to your client');
  1956. }).fail(function(){
  1957. alert('network error ');
  1958. });
  1959. }
  1960. });
  1961. init_ts();
  1962. $('div.divTableHeading div.bsave span.ticon-search').click(do_test);
  1963. function do_test(){ //TODO: remove this search function
  1964. open_modal('editor');
  1965. set_modal_title('editor', "title");
  1966. set_modal_content('editor', $('div.workspace').html());
  1967. }
  1968. /*________________________________________________________________________*/
  1969. });
  1970. })(jQuery);
  1971. /*______________scrolling______________________________________________*/
  1972. jQuery(document).ready(function(){
  1973. var timeoutid =0;
  1974. jQuery('button.peoplelist[name="down"]').mousedown(function(){
  1975. var button = this;
  1976. timeoutid = setInterval(function(){
  1977. //console.log("down scrotop %d ", jQuery(button).parent().find(".userlist").get(0).scrollTop );
  1978. jQuery(button).parent().find(".userlist").get(0).scrollTop +=240;
  1979. }, 100);
  1980. }).on('mouseup mouseleave', function(){
  1981. clearTimeout(timeoutid);
  1982. });
  1983. jQuery('button.peoplelist[name="up"]').mousedown(function(){
  1984. var button = this;
  1985. timeoutid = setInterval(function(){
  1986. //console.log("up scrotop %d ",jQuery(button).parent().find(".userlist").get(0).scrollTop );
  1987. jQuery(button).parent().find(".userlist").get(0).scrollTop -=240;
  1988. }, 100);
  1989. }).on('mouseup mouseleave', function(){
  1990. clearTimeout(timeoutid);
  1991. });
  1992. });