app/soc/content/js/slot-allocator-090825.js
changeset 2801 0ee67cc9bd20
parent 2800 cd9eed2b787e
equal deleted inserted replaced
2800:cd9eed2b787e 2801:0ee67cc9bd20
       
     1 var current_allocated_slots = 0;
       
     2 var current_slots = {};
       
     3 var tooltip = [
       
     4   "<div class='tooltip'>",
       
     5   "<div class='tooltip-body'>",
       
     6   "<img src='/soc/content/images/purrInfo.png' alt='' />",
       
     7   "<h3>Slots</h3>",
       
     8   "<p id='p_assigned_slots'></p>",
       
     9   "<p id='p_remaining_slots'></p>",
       
    10   "<p id='p_total_slots'></p></div>",
       
    11   "<div class='tooltip-bottom'></div>",
       
    12   "</div>"
       
    13 ].join('');
       
    14 
       
    15 jQuery.postJSON = function (post_url, to_json, callback) {
       
    16   jQuery.ajax({
       
    17     url: post_url,
       
    18     type: 'POST',
       
    19     processData: true,
       
    20     data: {result: JSON.stringify(to_json)},
       
    21     contentType: 'application/json',
       
    22     dataType: 'json',
       
    23     success: callback
       
    24   });
       
    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 }
       
    43 
       
    44 function updateFromJSON(data) {
       
    45   if (data) {
       
    46     jQuery(data.data).each(
       
    47       function (intIndex, item) {
       
    48         jQuery("#id_spin_slot_count_" + item.link_id).val(item.slots);
       
    49         current_slots[item.link_id] = {
       
    50           slots: item.slots,
       
    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);
       
    58       }
       
    59     );
       
    60     updateOverlay();
       
    61   }
       
    62 }
       
    63 
       
    64 function retrieveJSON() {
       
    65   jQuery.getJSON(
       
    66     window.RETURN_URL + "?_=" + (new Date().getTime()),
       
    67     function (data) {
       
    68       if (data) {
       
    69         updateFromJSON(data);
       
    70       }
       
    71     }
       
    72   );
       
    73 }
       
    74 
       
    75 function reCalculate() {
       
    76   var url = window.RETURN_URL + "?_=" + (new Date().getTime());
       
    77   jQuery.postJSON(url, current_slots, updateFromJSON);
       
    78 }
       
    79 
       
    80 function submit() {
       
    81   var url = window.RETURN_URL + "?submit=1&_=" + (new Date().getTime());
       
    82   jQuery.postJSON(url, current_slots, updateFromJSON);
       
    83 }
       
    84 
       
    85 function load() {
       
    86   var url = window.RETURN_URL + "?load=1&_=" + (new Date().getTime());
       
    87   jQuery.postJSON(url, current_slots, updateFromJSON);
       
    88 }
       
    89 
       
    90 function lockSlots(checkbox) {
       
    91   var locked = jQuery(checkbox).attr("checked");
       
    92   var re = /^id_locked_slot_(\w*)/;
       
    93   var org_link_id = checkbox.id.match(re)[1];
       
    94   current_slots[org_link_id].locked = locked;
       
    95 }
       
    96 
       
    97 function assignSlots(counter) {
       
    98   var re = /^id_spin_slot_count_(\w*)/;
       
    99   var org_link_id = counter.id.match(re)[1];
       
   100   current_slots[org_link_id].slots = jQuery(counter).val();
       
   101   updateCurrentSlots();
       
   102   updateOverlay();
       
   103 }
       
   104 
       
   105 function assignAdjustment(counter) {
       
   106   var re = /^id_spin_adjustment_count_(\w*)/;
       
   107   var org_link_id = counter.id.match(re)[1];
       
   108   current_slots[org_link_id].adjustment = jQuery(counter).val();
       
   109 }
       
   110