app/soc/content/js/slot-allocator-090825.js
author Mario Ferraro <fadinlight@gmail.com>
Tue, 25 Aug 2009 16:15:07 +0100
changeset 2801 0ee67cc9bd20
parent 2800 app/soc/content/js/slot-allocator-090320.js@cd9eed2b787e
permissions -rw-r--r--
Renamed all JS files affected by former commit. And also changed templates accordingly.
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>",
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    12
  "</div>"
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    13
].join('');
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    14
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    15
jQuery.postJSON = function (post_url, to_json, callback) {
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    16
  jQuery.ajax({
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    17
    url: post_url,
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    18
    type: 'POST',
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    19
    processData: true,
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    20
    data: {result: JSON.stringify(to_json)},
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    21
    contentType: 'application/json',
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    22
    dataType: 'json',
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    23
    success: callback
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    24
  });
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    25
};
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    27
function updateCurrentSlots() {
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    28
  current_allocated_slots = 0;
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    29
  jQuery.each(current_slots, function (org_id, org_details) {
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    30
    current_allocated_slots =
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    31
      current_allocated_slots + Number(org_details.slots);
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    32
  });
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    33
}
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    34
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    35
function updateOverlay() {
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    36
  updateCurrentSlots();
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    37
  var remaining_slots = window.MAX_AVAILABLE_SLOTS - current_allocated_slots;
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    38
  jQuery("#p_assigned_slots")
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    39
    .html("<strong>Assigned slots:</strong> " + current_allocated_slots);
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    40
  jQuery("#p_remaining_slots")
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    41
    .html("<strong>Remaining slots:</strong> " + remaining_slots);
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    42
}
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
function updateFromJSON(data) {
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
  if (data) {
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    46
    jQuery(data.data).each(
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    47
      function (intIndex, item) {
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    48
        jQuery("#id_spin_slot_count_" + item.link_id).val(item.slots);
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    49
        current_slots[item.link_id] = {
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    50
          slots: item.slots,
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    51
          locked: item.locked,
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    52
          adjustment: item.adjustment
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    53
        };
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    54
        jQuery("#id_locked_slot_" + item.link_id)
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    55
          .attr("checked", item.locked);
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    56
        jQuery("#id_spin_adjustment_count_" + item.link_id)
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    57
          .val(item.adjustment);
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
      }
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
    );
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
    updateOverlay();
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
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
function retrieveJSON() {
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    65
  jQuery.getJSON(
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    66
    window.RETURN_URL + "?_=" + (new Date().getTime()),
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    67
    function (data) {
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    68
      if (data) {
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    69
        updateFromJSON(data);
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    70
      }
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
    71
    }
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
  );
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
}
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
function reCalculate() {
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    76
  var url = window.RETURN_URL + "?_=" + (new Date().getTime());
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    77
  jQuery.postJSON(url, current_slots, updateFromJSON);
1734
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
2136
5d55e61dd338 Add a submit method and button to slots view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1953
diff changeset
    80
function submit() {
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    81
  var url = window.RETURN_URL + "?submit=1&_=" + (new Date().getTime());
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    82
  jQuery.postJSON(url, current_slots, updateFromJSON);
2136
5d55e61dd338 Add a submit method and button to slots view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1953
diff changeset
    83
}
5d55e61dd338 Add a submit method and button to slots view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1953
diff changeset
    84
2147
5dd2091c8f0c Allow loading a previously saved slot allocation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2136
diff changeset
    85
function load() {
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    86
  var url = window.RETURN_URL + "?load=1&_=" + (new Date().getTime());
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    87
  jQuery.postJSON(url, current_slots, updateFromJSON);
2147
5dd2091c8f0c Allow loading a previously saved slot allocation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2136
diff changeset
    88
}
5dd2091c8f0c Allow loading a previously saved slot allocation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2136
diff changeset
    89
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    90
function lockSlots(checkbox) {
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    91
  var locked = jQuery(checkbox).attr("checked");
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
  var re = /^id_locked_slot_(\w*)/;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
  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
    94
  current_slots[org_link_id].locked = locked;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
    97
function assignSlots(counter) {
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
  var re = /^id_spin_slot_count_(\w*)/;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
  var org_link_id = counter.id.match(re)[1];
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
   100
  current_slots[org_link_id].slots = jQuery(counter).val();
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
  updateCurrentSlots();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
  updateOverlay();
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
   105
function assignAdjustment(counter) {
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
  var re = /^id_spin_adjustment_count_(\w*)/;
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
  var org_link_id = counter.id.match(re)[1];
2800
cd9eed2b787e Made all JS code JSLint compliant.
Mario Ferraro <fadinlight@gmail.com>
parents: 2147
diff changeset
   108
  current_slots[org_link_id].adjustment = jQuery(counter).val();
1734
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
}
c9ecc7449632 Add a slot allocation script and template
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110