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.

257 lines
7.3KB

  1. function on_confirm_all(){}
  2. (function ($) {
  3. $(function () {
  4. /*_____________________________________________*/
  5. function test_jobs(){
  6. for(var i =0; i<12; i++)
  7. {
  8. var h = $('#jobtemplate').wrap('<p/>').parent().html();
  9. $('#jobtemplate').unwrap();
  10. var el = $(h);
  11. el.attr('id', 'job_' + i);
  12. $('#jobtemplate').after(el);
  13. }
  14. }
  15. function set_user_name_at_summary(name){
  16. var title = '';
  17. if ( typeof name != 'undefined' && name != "")
  18. title = name + "'s Job";
  19. else
  20. title = "Job Arrangement";
  21. $('.jobsummary h2').html(title);
  22. }
  23. function get_my_jobs(){
  24. $.post(bts().ajax_url, { // POST request
  25. _ajax_nonce: bts().nonce, // nonce
  26. action: "list_job_by_staff", // action
  27. start: get_this_week_start(),
  28. finish: get_this_week_end(),
  29. login: get_staff_login(),
  30. }, function(response, status, xhr){
  31. if (response.status == "success"){
  32. pre_process(response);
  33. //console.log("%o", response);
  34. load_staff_jobs(response);
  35. }else{
  36. display_error(response);
  37. }
  38. });
  39. }
  40. function get_staff_login()
  41. {
  42. //pathname: "/task/a6536a3b-ef22-4a28-8d55-e2a26d4ae227/"
  43. var path = window.location.pathname;
  44. var p = path.substr(6);//remove /task/
  45. var s = p.substring(0, p.length - 1); //remove last /
  46. //console.log(s);
  47. return s;
  48. }
  49. //add extra info to response.jobs
  50. function pre_process(response)
  51. {
  52. var newjobs = [];
  53. $.each(response.jobs, function(idx, val){
  54. if (val.ack != 0){
  55. val.readonly = "checked disabled";
  56. val.confirm = "Already Confirmed";
  57. }else{
  58. val.confirm = "Confirm";
  59. val.readonly = "";
  60. }
  61. val.start_day = get_weekday_name(val.start);
  62. val.finish_day = get_weekday_name(val.finish);
  63. newjobs.push(val);
  64. });
  65. response.jobs = newjobs;
  66. //console.log("%o", response);
  67. }
  68. function get_weekday_name(dateString)
  69. {
  70. var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  71. var d = new Date(dateString);
  72. var dayName = days[d.getDay()];
  73. return dayName;
  74. }
  75. function load_staff_jobs(response)
  76. {
  77. set_user_name_at_summary(response.staff_name);
  78. var template = get_staff_job_template();
  79. var html = Mustache.render(template, response);
  80. $('#jobtemplate').after(html);
  81. }
  82. function get_staff_job_template()
  83. {
  84. var h = get_html_include_tag($('#jobtemplate'));
  85. var h = get_html_include_tag($(h).removeAttr('id'));
  86. return " {{#jobs}}" + h + " {{/jobs}} ";
  87. }
  88. function get_html_include_tag(el)
  89. {
  90. var h = el.wrap('<p/>').parent().html();
  91. el.unwrap();
  92. return h;
  93. }
  94. function display_error()
  95. {
  96. err_message_box("Network Error", "Your Jobs for this week cannot be loaded, please try again later. For urgent job arrangement please contact <strong>Helen</strong> directly.");
  97. }
  98. function format_date(date){
  99. var dd = date.getDate();
  100. var mm = date.getMonth() + 1; //January is 0!
  101. var hh = date.getHours();
  102. var ii = date.getMinutes();
  103. var ss = date.getSeconds();
  104. var yyyy = date.getFullYear();
  105. if (dd < 10) {
  106. dd = '0' + dd;
  107. }
  108. if (mm < 10) {
  109. mm = '0' + mm;
  110. }
  111. if (hh< 10){
  112. hh = '0' + hh;
  113. }
  114. if (ii < 10){
  115. ii = '0' + ii;
  116. }
  117. if (ss <10 ){
  118. ss = '0' + ss;
  119. }
  120. return yyyy + '-' + mm + '-' +dd + " " +hh +":" + ii + ":" + ss;
  121. }
  122. function get_this_week_start(){
  123. var curr = new Date; // get current date
  124. // First day is the day of the month - the day of the week
  125. var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first
  126. var last = first + 6; // last day is the first day + 6
  127. var firstday = new Date(curr.setDate(first)); //Mon
  128. firstday.setHours(0,0,0);
  129. return format_date(firstday);
  130. }
  131. function get_this_week_end(){
  132. var curr = new Date; // get current date
  133. // First day is the day of the month - the day of the week
  134. var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first
  135. var last = first + 6; // last day is the first day + 6
  136. var lastday = new Date(curr.setDate(last)); //Sat
  137. lastday.setHours(23,59,59);
  138. return format_date(lastday);
  139. }
  140. function do_on_confirm_all()
  141. {
  142. var job_ids=[];
  143. $('.jobcard input[type="checkbox"]').prop('checked', true);
  144. if ( $('.jobcard input[type="checkbox"]:checked').length ==0 )
  145. {
  146. err_message_box("Warning", "You have no job to Confirm");
  147. return;
  148. }
  149. $('.jobcard input[type="checkbox"]:checked').each(function(e){
  150. var ack = $(this).is(':checked');
  151. var lb = $(this).closest('label[name="recordinfo"]');
  152. var id = lb.attr('data-record-id');
  153. if ( id != '{{id}}' ) //the template
  154. job_ids.push({id:id, ack:ack});
  155. });
  156. do_update_ack(job_ids);
  157. info_message_box("Congratulations", "All Your Jobs has been confirmed. Your payment will be calculated based on them. For urgent job changes please contact <strong>Helen</strong> directly.");
  158. }
  159. function info_message_box(title, message)
  160. {
  161. message_box('.job_ok_box', title, message);
  162. }
  163. function err_message_box(title, message)
  164. {
  165. message_box('.job_error_box', title, message);
  166. }
  167. function message_box(selector, title,message)
  168. {
  169. set_modal_title(selector, title);
  170. set_modal_message(selector, message);
  171. $(selector + '_trigger').trigger('click');
  172. }
  173. function set_modal_title(selector, title)
  174. {
  175. var el = selector + ' .ult_modal-title ';
  176. $(el).html(title);
  177. }
  178. function set_modal_message(selector, msg)
  179. {
  180. var el = selector + ' .ult_modal-body ';
  181. $(el).html(msg);
  182. }
  183. function do_update_ack(job_ids)
  184. {
  185. $.post(bts().ajax_url, { // POST request
  186. _ajax_nonce: bts().nonce, // nonce
  187. action: "staff_ack_job", // action
  188. jobs: job_ids,
  189. }, function(response, status, xhr){
  190. if (response.status == "success"){
  191. var txt = response.ack? 'Confirmed':'Click to Confirm';
  192. span.html(txt);
  193. }else{
  194. err_message_box("Network Error", "Try again later");
  195. }
  196. });
  197. }
  198. //register events
  199. $(document).on('change', 'div.confirmfield input', function(){
  200. var ack = $(this).is(':checked');
  201. var lb = $(this).closest('label[name="recordinfo"]');
  202. var id = lb.attr('data-record-id');
  203. var span = lb.find('span');
  204. span.html('reporting ...');
  205. $.post(bts().ajax_url, { // POST request
  206. _ajax_nonce: bts().nonce, // nonce
  207. action: "staff_ack_job", // action
  208. jobs: [{id:id, ack:ack}],
  209. }, function(response, status, xhr){
  210. if (response.status == "success"){
  211. var txt = ack? 'Confirmed':'Click to Confirm';
  212. span.html(txt);
  213. }else{
  214. err_message_box("Network Error", "Try again later");
  215. }
  216. });
  217. });
  218. //init
  219. set_user_name_at_summary();
  220. get_my_jobs();
  221. //get_staff_login();
  222. on_confirm_all = do_on_confirm_all;
  223. /*_____________________________________________*/
  224. });
  225. })(jQuery);