|
- function on_confirm_all(){}
-
- (function ($) {
- $(function () {
- /*_____________________________________________*/
-
- function test_jobs(){
- for(var i =0; i<12; i++)
- {
- var h = $('#jobtemplate').wrap('<p/>').parent().html();
- $('#jobtemplate').unwrap();
- var el = $(h);
- el.attr('id', 'job_' + i);
- $('#jobtemplate').after(el);
- }
- }
-
- function set_user_name_at_summary(name){
- var title = '';
- if ( typeof name != 'undefined' && name != "")
- title = name + "'s Job";
- else
- title = "Job Arrangement";
- $('.jobsummary h2').html(title);
- }
-
-
- function get_my_jobs(){
- $.post(bts().ajax_url, { // POST request
- _ajax_nonce: bts().nonce, // nonce
- action: "list_job_by_staff", // action
- start: get_this_week_start(),
- finish: get_this_week_end(),
- login: get_staff_login(),
- }, function(response, status, xhr){
- if (response.status == "success"){
- pre_process(response);
- //console.log("%o", response);
- load_staff_jobs(response);
- }else{
- display_error(response);
- }
- });
- }
-
- function get_staff_login()
- {
- //pathname: "/task/a6536a3b-ef22-4a28-8d55-e2a26d4ae227/"
- var path = window.location.pathname;
- var p = path.substr(6);//remove /task/
- var s = p.substring(0, p.length - 1); //remove last /
- //console.log(s);
- return s;
- }
- //add extra info to response.jobs
- function pre_process(response)
- {
- var newjobs = [];
- $.each(response.jobs, function(idx, val){
- if (val.ack != 0){
- val.readonly = "checked disabled";
- val.confirm = "Already Confirmed";
- }else{
- val.confirm = "Confirm";
- val.readonly = "";
- }
- val.start_day = get_weekday_name(val.start);
- val.finish_day = get_weekday_name(val.finish);
- newjobs.push(val);
- });
- response.jobs = newjobs;
- //console.log("%o", response);
- }
-
- function get_weekday_name(dateString)
- {
- var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
- var d = new Date(dateString);
- var dayName = days[d.getDay()];
- return dayName;
- }
-
- function load_staff_jobs(response)
- {
- set_user_name_at_summary(response.staff_name);
- var template = get_staff_job_template();
- var html = Mustache.render(template, response);
- $('#jobtemplate').after(html);
- }
-
- function get_staff_job_template()
- {
- var h = get_html_include_tag($('#jobtemplate'));
- var h = get_html_include_tag($(h).removeAttr('id'));
- return " {{#jobs}}" + h + " {{/jobs}} ";
- }
-
- function get_html_include_tag(el)
- {
- var h = el.wrap('<p/>').parent().html();
- el.unwrap();
- return h;
- }
-
- function display_error()
- {
- 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.");
- }
- function format_date(date){
- var dd = date.getDate();
- var mm = date.getMonth() + 1; //January is 0!
- var hh = date.getHours();
- var ii = date.getMinutes();
- var ss = date.getSeconds();
- var yyyy = date.getFullYear();
-
- if (dd < 10) {
- dd = '0' + dd;
- }
- if (mm < 10) {
- mm = '0' + mm;
- }
- if (hh< 10){
- hh = '0' + hh;
- }
- if (ii < 10){
- ii = '0' + ii;
- }
-
- if (ss <10 ){
- ss = '0' + ss;
- }
- return yyyy + '-' + mm + '-' +dd + " " +hh +":" + ii + ":" + ss;
- }
-
- function get_this_week_start(){
- var curr = new Date; // get current date
- // First day is the day of the month - the day of the week
- var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first
- var last = first + 6; // last day is the first day + 6
-
- var firstday = new Date(curr.setDate(first)); //Mon
- firstday.setHours(0,0,0);
- return format_date(firstday);
- }
-
- function get_this_week_end(){
- var curr = new Date; // get current date
- // First day is the day of the month - the day of the week
- var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first
- var last = first + 6; // last day is the first day + 6
- var lastday = new Date(curr.setDate(last)); //Sat
- lastday.setHours(23,59,59);
- return format_date(lastday);
- }
-
- function do_on_confirm_all()
- {
- var job_ids=[];
- $('.jobcard input[type="checkbox"]').prop('checked', true);
-
- if ( $('.jobcard input[type="checkbox"]:checked').length ==0 )
- {
- err_message_box("Warning", "You have no job to Confirm");
- return;
- }
-
- $('.jobcard input[type="checkbox"]:checked').each(function(e){
- var ack = $(this).is(':checked');
- var lb = $(this).closest('label[name="recordinfo"]');
- var id = lb.attr('data-record-id');
- if ( id != '{{id}}' ) //the template
- job_ids.push({id:id, ack:ack});
- });
- do_update_ack(job_ids);
- 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.");
- }
-
- function info_message_box(title, message)
- {
- message_box('.job_ok_box', title, message);
- }
-
- function err_message_box(title, message)
- {
- message_box('.job_error_box', title, message);
- }
-
- function message_box(selector, title,message)
- {
- set_modal_title(selector, title);
- set_modal_message(selector, message);
- $(selector + '_trigger').trigger('click');
- }
-
- function set_modal_title(selector, title)
- {
- var el = selector + ' .ult_modal-title ';
- $(el).html(title);
- }
-
- function set_modal_message(selector, msg)
- {
- var el = selector + ' .ult_modal-body ';
- $(el).html(msg);
- }
-
- function do_update_ack(job_ids)
- {
- $.post(bts().ajax_url, { // POST request
- _ajax_nonce: bts().nonce, // nonce
- action: "staff_ack_job", // action
- jobs: job_ids,
- }, function(response, status, xhr){
- if (response.status == "success"){
- var txt = response.ack? 'Confirmed':'Click to Confirm';
- span.html(txt);
- }else{
- err_message_box("Network Error", "Try again later");
- }
- });
- }
-
- //register events
- $(document).on('change', 'div.confirmfield input', function(){
- var ack = $(this).is(':checked');
- var lb = $(this).closest('label[name="recordinfo"]');
- var id = lb.attr('data-record-id');
- var span = lb.find('span');
- span.html('reporting ...');
-
- $.post(bts().ajax_url, { // POST request
- _ajax_nonce: bts().nonce, // nonce
- action: "staff_ack_job", // action
- jobs: [{id:id, ack:ack}],
- }, function(response, status, xhr){
- if (response.status == "success"){
- var txt = ack? 'Confirmed':'Click to Confirm';
- span.html(txt);
- }else{
- err_message_box("Network Error", "Try again later");
- }
- });
- });
-
-
- //init
- set_user_name_at_summary();
- get_my_jobs();
- //get_staff_login();
- on_confirm_all = do_on_confirm_all;
- /*_____________________________________________*/
- });
- })(jQuery);
-
|