app/soc/content/js/slot-allocator-090320.js
changeset 2800 cd9eed2b787e
parent 2147 5dd2091c8f0c
equal deleted inserted replaced
2799:0fe7767592d0 2800:cd9eed2b787e
     7   "<h3>Slots</h3>",
     7   "<h3>Slots</h3>",
     8   "<p id='p_assigned_slots'></p>",
     8   "<p id='p_assigned_slots'></p>",
     9   "<p id='p_remaining_slots'></p>",
     9   "<p id='p_remaining_slots'></p>",
    10   "<p id='p_total_slots'></p></div>",
    10   "<p id='p_total_slots'></p></div>",
    11   "<div class='tooltip-bottom'></div>",
    11   "<div class='tooltip-bottom'></div>",
    12   "</div>",
    12   "</div>"
    13   ].join('');
    13 ].join('');
    14 
    14 
    15 $.postJSON = function (post_url, to_json, callback) {
    15 jQuery.postJSON = function (post_url, to_json, callback) {
    16     $.ajax({
    16   jQuery.ajax({
    17         url: post_url,
    17     url: post_url,
    18         type: 'POST',
    18     type: 'POST',
    19         processData: true,
    19     processData: true,
    20         data: {result: JSON.stringify(to_json)},
    20     data: {result: JSON.stringify(to_json)},
    21         contentType: 'application/json',
    21     contentType: 'application/json',
    22         dataType: 'json',
    22     dataType: 'json',
    23         success: callback,
    23     success: callback
    24     });
    24   });
    25 };
    25 };
       
    26 
       
    27 function updateCurrentSlots() {
       
    28   current_allocated_slots = 0;
       
    29   jQuery.each(current_slots, function (org_id, org_details) {
       
    30     current_allocated_slots =
       
    31       current_allocated_slots + Number(org_details.slots);
       
    32   });
       
    33 }
       
    34 
       
    35 function updateOverlay() {
       
    36   updateCurrentSlots();
       
    37   var remaining_slots = window.MAX_AVAILABLE_SLOTS - current_allocated_slots;
       
    38   jQuery("#p_assigned_slots")
       
    39     .html("<strong>Assigned slots:</strong> " + current_allocated_slots);
       
    40   jQuery("#p_remaining_slots")
       
    41     .html("<strong>Remaining slots:</strong> " + remaining_slots);
       
    42 }
    26 
    43 
    27 function updateFromJSON(data) {
    44 function updateFromJSON(data) {
    28   if (data) {
    45   if (data) {
    29     $(data.data).each(
    46     jQuery(data.data).each(
    30       function (intIndex,item) {
    47       function (intIndex, item) {
    31         $("#id_spin_slot_count_"+item.link_id).val(item.slots);
    48         jQuery("#id_spin_slot_count_" + item.link_id).val(item.slots);
    32         current_slots[item.link_id] = {slots: item.slots, locked: item.locked, adjustment: item.adjustment};
    49         current_slots[item.link_id] = {
    33         $("#id_locked_slot_"+item.link_id).attr("checked",item.locked);
    50           slots: item.slots,
    34         $("#id_spin_adjustment_count_"+item.link_id).val(item.adjustment);
    51           locked: item.locked,
       
    52           adjustment: item.adjustment
       
    53         };
       
    54         jQuery("#id_locked_slot_" + item.link_id)
       
    55           .attr("checked", item.locked);
       
    56         jQuery("#id_spin_adjustment_count_" + item.link_id)
       
    57           .val(item.adjustment);
    35       }
    58       }
    36     );
    59     );
    37     updateOverlay();
    60     updateOverlay();
    38   }
    61   }
    39 }
    62 }
    40 
    63 
    41 function retrieveJSON() {
    64 function retrieveJSON() {
    42   $.getJSON(RETURN_URL+"?_="+(new Date().getTime()), function(data) {
    65   jQuery.getJSON(
    43     if (data) {
    66     window.RETURN_URL + "?_=" + (new Date().getTime()),
    44       updateFromJSON(data);
    67     function (data) {
       
    68       if (data) {
       
    69         updateFromJSON(data);
       
    70       }
    45     }
    71     }
    46   }
       
    47   );
    72   );
    48 }
    73 }
    49 
    74 
    50 function reCalculate() {
    75 function reCalculate() {
    51   url = RETURN_URL+"?_="+(new Date().getTime())
    76   var url = window.RETURN_URL + "?_=" + (new Date().getTime());
    52   $.postJSON(url, current_slots, updateFromJSON);
    77   jQuery.postJSON(url, current_slots, updateFromJSON);
    53 }
    78 }
    54 
    79 
    55 function submit() {
    80 function submit() {
    56   url = RETURN_URL+"?submit=1&_="+(new Date().getTime())
    81   var url = window.RETURN_URL + "?submit=1&_=" + (new Date().getTime());
    57   $.postJSON(url, current_slots, updateFromJSON);
    82   jQuery.postJSON(url, current_slots, updateFromJSON);
    58 }
    83 }
    59 
    84 
    60 function load() {
    85 function load() {
    61   url = RETURN_URL+"?load=1&_="+(new Date().getTime())
    86   var url = window.RETURN_URL + "?load=1&_=" + (new Date().getTime());
    62   $.postJSON(url, current_slots, updateFromJSON);
    87   jQuery.postJSON(url, current_slots, updateFromJSON);
    63 }
    88 }
    64 
    89 
    65 function updateOverlay() {
    90 function lockSlots(checkbox) {
    66   updateCurrentSlots();
    91   var locked = jQuery(checkbox).attr("checked");
    67   var remaining_slots = MAX_AVAILABLE_SLOTS - current_allocated_slots;
       
    68   $("#p_assigned_slots").html("<strong>Assigned slots:</strong> "+current_allocated_slots);
       
    69   $("#p_remaining_slots").html("<strong>Remaining slots:</strong> "+remaining_slots);
       
    70 }
       
    71 
       
    72 function updateCurrentSlots() {
       
    73   current_allocated_slots = 0;
       
    74   for (var org_id in current_slots) {
       
    75     current_allocated_slots = current_allocated_slots+new Number(current_slots[org_id].slots);
       
    76   }
       
    77 }
       
    78 
       
    79 function lockSlots (checkbox) {
       
    80   var locked = $(checkbox).attr("checked");
       
    81   var re = /^id_locked_slot_(\w*)/;
    92   var re = /^id_locked_slot_(\w*)/;
    82   var org_link_id = checkbox.id.match(re)[1];
    93   var org_link_id = checkbox.id.match(re)[1];
    83   current_slots[org_link_id].locked = locked;
    94   current_slots[org_link_id].locked = locked;
    84 }
    95 }
    85 
    96 
    86 function assignSlots (counter) {
    97 function assignSlots(counter) {
    87   var re = /^id_spin_slot_count_(\w*)/;
    98   var re = /^id_spin_slot_count_(\w*)/;
    88   var org_link_id = counter.id.match(re)[1];
    99   var org_link_id = counter.id.match(re)[1];
    89   current_slots[org_link_id].slots = $(counter).val();
   100   current_slots[org_link_id].slots = jQuery(counter).val();
    90   updateCurrentSlots();
   101   updateCurrentSlots();
    91   updateOverlay();
   102   updateOverlay();
    92 }
   103 }
    93 
   104 
    94 function assignAdjustment (counter) {
   105 function assignAdjustment(counter) {
    95   var re = /^id_spin_adjustment_count_(\w*)/;
   106   var re = /^id_spin_adjustment_count_(\w*)/;
    96   var org_link_id = counter.id.match(re)[1];
   107   var org_link_id = counter.id.match(re)[1];
    97   current_slots[org_link_id].adjustment = $(counter).val();
   108   current_slots[org_link_id].adjustment = jQuery(counter).val();
    98 }
   109 }
    99 
   110