app/soc/content/js/slot-allocator-090320.js
author Sverre Rabbelier <sverre@rabbelier.nl>
Mon, 25 May 2009 23:40:07 +0200
changeset 2346 d830123c1be1
parent 2147 5dd2091c8f0c
child 2800 cd9eed2b787e
permissions -rw-r--r--
Do not die on NeedIndexErrors
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
var current_allocated_slots = 0;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
var current_slots = {};
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
var tooltip = [
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
  "<div class='tooltip'>",
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
  "<div class='tooltip-body'>",
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
  "<img src='/soc/content/images/purrInfo.png' alt='' />",
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
  "<h3>Slots</h3>",
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
  "<p id='p_assigned_slots'></p>",
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
  "<p id='p_remaining_slots'></p>",
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
  "<p id='p_total_slots'></p></div>",
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
  "<div class='tooltip-bottom'></div>",
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
  "</div>",
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
  ].join('');
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
$.postJSON = function (post_url, to_json, callback) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
    $.ajax({
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
        url: post_url,
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
        type: 'POST',
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
        processData: true,
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
        data: {result: JSON.stringify(to_json)},
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
        contentType: 'application/json',
1744
0288cb88edd2 Treat the response from the server as a JSON object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1734
diff changeset
    22
        dataType: 'json',
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
        success: callback,
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
    });
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
};
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
function updateFromJSON(data) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
  if (data) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
    $(data.data).each(
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
      function (intIndex,item) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
        $("#id_spin_slot_count_"+item.link_id).val(item.slots);
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
        current_slots[item.link_id] = {slots: item.slots, locked: item.locked, adjustment: item.adjustment};
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
        $("#id_locked_slot_"+item.link_id).attr("checked",item.locked);
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
        $("#id_spin_adjustment_count_"+item.link_id).val(item.adjustment);
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
      }
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
    );
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
    updateOverlay();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
  }
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
function retrieveJSON() {
1952
7c97323fd20b Slot allocator Javascript fix. This prevents from calling updateFromJSON function if there is no data.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1747
diff changeset
    42
  $.getJSON(RETURN_URL+"?_="+(new Date().getTime()), function(data) {
7c97323fd20b Slot allocator Javascript fix. This prevents from calling updateFromJSON function if there is no data.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1747
diff changeset
    43
    if (data) {
7c97323fd20b Slot allocator Javascript fix. This prevents from calling updateFromJSON function if there is no data.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1747
diff changeset
    44
      updateFromJSON(data);
7c97323fd20b Slot allocator Javascript fix. This prevents from calling updateFromJSON function if there is no data.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1747
diff changeset
    45
    }
7c97323fd20b Slot allocator Javascript fix. This prevents from calling updateFromJSON function if there is no data.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1747
diff changeset
    46
  }
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
  );
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
function reCalculate() {
1747
02f15a955c42 Provide the 'return_url' in the allocate slots view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1744
diff changeset
    51
  url = RETURN_URL+"?_="+(new Date().getTime())
02f15a955c42 Provide the 'return_url' in the allocate slots view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1744
diff changeset
    52
  $.postJSON(url, current_slots, updateFromJSON);
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
2136
5d55e61dd338 Add a submit method and button to slots view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1953
diff changeset
    55
function submit() {
5d55e61dd338 Add a submit method and button to slots view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1953
diff changeset
    56
  url = RETURN_URL+"?submit=1&_="+(new Date().getTime())
5d55e61dd338 Add a submit method and button to slots view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1953
diff changeset
    57
  $.postJSON(url, current_slots, updateFromJSON);
5d55e61dd338 Add a submit method and button to slots view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1953
diff changeset
    58
}
5d55e61dd338 Add a submit method and button to slots view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1953
diff changeset
    59
2147
5dd2091c8f0c Allow loading a previously saved slot allocation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2136
diff changeset
    60
function load() {
5dd2091c8f0c Allow loading a previously saved slot allocation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2136
diff changeset
    61
  url = RETURN_URL+"?load=1&_="+(new Date().getTime())
5dd2091c8f0c Allow loading a previously saved slot allocation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2136
diff changeset
    62
  $.postJSON(url, current_slots, updateFromJSON);
5dd2091c8f0c Allow loading a previously saved slot allocation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2136
diff changeset
    63
}
5dd2091c8f0c Allow loading a previously saved slot allocation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2136
diff changeset
    64
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
function updateOverlay() {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
  updateCurrentSlots();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
  var remaining_slots = MAX_AVAILABLE_SLOTS - current_allocated_slots;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
  $("#p_assigned_slots").html("<strong>Assigned slots:</strong> "+current_allocated_slots);
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
  $("#p_remaining_slots").html("<strong>Remaining slots:</strong> "+remaining_slots);
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
function updateCurrentSlots() {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
  current_allocated_slots = 0;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
  for (var org_id in current_slots) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
    current_allocated_slots = current_allocated_slots+new Number(current_slots[org_id].slots);
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
  }
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
function lockSlots (checkbox) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
  var locked = $(checkbox).attr("checked");
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
  var re = /^id_locked_slot_(\w*)/;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
  var org_link_id = checkbox.id.match(re)[1];
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
  current_slots[org_link_id].locked = locked;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
function assignSlots (counter) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
  var re = /^id_spin_slot_count_(\w*)/;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
  var org_link_id = counter.id.match(re)[1];
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
  current_slots[org_link_id].slots = $(counter).val();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
  updateCurrentSlots();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
  updateOverlay();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
function assignAdjustment (counter) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
  var re = /^id_spin_adjustment_count_(\w*)/;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
  var org_link_id = counter.id.match(re)[1];
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
  current_slots[org_link_id].adjustment = $(counter).val();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99