app/soc/content/js/slot-allocator-090320.js
author Pawel Solyga <Pawel.Solyga@gmail.com>
Thu, 02 Apr 2009 10:11:38 +0000
changeset 2051 cecbef1289a5
parent 1953 6bd827320b3d
child 2136 5d55e61dd338
permissions -rw-r--r--
Make the mentor and organization admin comment submission on student proposal nicer. Now drop down box allows you to select comment type and based on selected type and your current Role shows you available comment options. Left TODO to put inline JS into separate JS file. Patch by: Tim Ansell (small fixes by Pawel Solyga) Reviewed by: Pawel Solyga
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
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
function updateOverlay() {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
  updateCurrentSlots();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
  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
    58
  $("#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
    59
  $("#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
    60
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
function updateCurrentSlots() {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
  current_allocated_slots = 0;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
  for (var org_id in current_slots) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
    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
    66
  }
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
function lockSlots (checkbox) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
  var locked = $(checkbox).attr("checked");
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
  var re = /^id_locked_slot_(\w*)/;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
  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
    73
  current_slots[org_link_id].locked = locked;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
function assignSlots (counter) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
  var re = /^id_spin_slot_count_(\w*)/;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
  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
    79
  current_slots[org_link_id].slots = $(counter).val();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
  updateCurrentSlots();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
  updateOverlay();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
function assignAdjustment (counter) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
  var re = /^id_spin_adjustment_count_(\w*)/;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
  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
    87
  current_slots[org_link_id].adjustment = $(counter).val();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89