liquid_feedback_frontend

changeset 946:c328da62f45c

Robustify voting JavaScript: Calculate scoring after each moving
author jbe
date Thu Nov 08 15:16:17 2012 +0100 (2012-11-08)
parents b865f87ea810
children 46275b784974
files static/js/voting.js
line diff
     1.1 --- a/static/js/voting.js	Thu Nov 08 13:17:37 2012 +0100
     1.2 +++ b/static/js/voting.js	Thu Nov 08 15:16:17 2012 +0100
     1.3 @@ -98,6 +98,58 @@
     1.4        }
     1.5      }
     1.6    }
     1.7 +  function voting_calculateScoring() {
     1.8 +    var mainDiv = document.getElementById("voting");
     1.9 +    var form = document.getElementById("voting_form");
    1.10 +    var scoringString = "";
    1.11 +    var approvalCount = 0;
    1.12 +    var disapprovalCount = 0;
    1.13 +    var sections = mainDiv.childNodes;
    1.14 +    for (var j=0; j<sections.length; j++) {
    1.15 +      var section = sections[j];
    1.16 +      if (section.className == "approval")       approvalCount++;
    1.17 +      if (section.className == "disapproval") disapprovalCount++;
    1.18 +    }
    1.19 +    var approvalIndex = 0;
    1.20 +    var disapprovalIndex = 0;
    1.21 +    for (var j=0; j<sections.length; j++) {
    1.22 +      var section = sections[j];
    1.23 +      if (
    1.24 +        section.className == "approval"    ||
    1.25 +        section.className == "abstention"  ||
    1.26 +        section.className == "disapproval"
    1.27 +      ) {
    1.28 +        var score;
    1.29 +        if (section.className == "approval") {
    1.30 +          score = approvalCount - approvalIndex;
    1.31 +          approvalIndex++;
    1.32 +        } else if (section.className == "abstention") {
    1.33 +          score = 0;
    1.34 +        } else if (section.className == "disapproval") {
    1.35 +          score = -1 - disapprovalIndex;
    1.36 +          disapprovalIndex++;
    1.37 +        }
    1.38 +        var entries = section.childNodes;
    1.39 +        for (var k=0; k<entries.length; k++) {
    1.40 +          var entry = entries[k];
    1.41 +          if (entry.className == "movable") {
    1.42 +            var id = entry.id.match(/[0-9]+/);
    1.43 +            var field = document.createElement("input");
    1.44 +            scoringString += id + ":" + score + ";";
    1.45 +          }
    1.46 +        }
    1.47 +      }
    1.48 +    }
    1.49 +    var fields = form.childNodes;
    1.50 +    for (var j=0; j<fields.length; j++) {
    1.51 +      var field = fields[j];
    1.52 +      if (field.name == "scoring") {
    1.53 +        field.setAttribute("value", scoringString);
    1.54 +        return;
    1.55 +      }
    1.56 +    }
    1.57 +    throw('Hidden input field named "scoring" not found.');
    1.58 +  }
    1.59    function voting_move(element, up, dropX, dropY) {
    1.60      jsProtect(function() {
    1.61        if (typeof(element) == "string") element = document.getElementById(element);
    1.62 @@ -277,6 +329,7 @@
    1.63          }
    1.64        }
    1.65        voting_setCategoryHeadings();
    1.66 +      voting_calculateScoring();
    1.67      });
    1.68    }
    1.69    function elementDropped(element, dropX, dropY) {
    1.70 @@ -284,89 +337,19 @@
    1.71    }
    1.72    window.addEventListener("load", function(event) {
    1.73      jsProtect(function() {
    1.74 -      var jsTest = true;
    1.75 -      var jsTestSuccess = false;
    1.76        voting_setCategoryHeadings();
    1.77 -      var mainDiv = document.getElementById("voting");
    1.78 -      var form = document.getElementById("voting_form");
    1.79 +      // TODO: replace the following code by non-JavaScript HTML attributes
    1.80        var elements = document.getElementsByTagName("input");
    1.81        for (var i=0; i<elements.length; i++) {
    1.82          var element = elements[i];
    1.83 -        if (element.className == "voting_done1" ||
    1.84 -            element.className == "voting_done2" ||
    1.85 -            element.name == "preview") {
    1.86 +        if (element.name == "preview") {
    1.87            element.addEventListener("click", function(event) {
    1.88              jsProtect(function() {
    1.89 -              event.preventDefault();
    1.90 -              if (event.target.name == "preview") {
    1.91 -                document.getElementById("preview2").value = "1";
    1.92 -              }
    1.93 -              var scoringString = "";
    1.94 -              var approvalCount = 0;
    1.95 -              var disapprovalCount = 0;
    1.96 -              var sections = mainDiv.childNodes;
    1.97 -              for (var j=0; j<sections.length; j++) {
    1.98 -                var section = sections[j];
    1.99 -                if (section.className == "approval")       approvalCount++;
   1.100 -                if (section.className == "disapproval") disapprovalCount++;
   1.101 -              }
   1.102 -              var approvalIndex = 0;
   1.103 -              var disapprovalIndex = 0;
   1.104 -              for (var j=0; j<sections.length; j++) {
   1.105 -                var section = sections[j];
   1.106 -                if (
   1.107 -                  section.className == "approval"    ||
   1.108 -                  section.className == "abstention"  ||
   1.109 -                  section.className == "disapproval"
   1.110 -                ) {
   1.111 -                  var score;
   1.112 -                  if (section.className == "approval") {
   1.113 -                    score = approvalCount - approvalIndex;
   1.114 -                    approvalIndex++;
   1.115 -                  } else if (section.className == "abstention") {
   1.116 -                    score = 0;
   1.117 -                  } else if (section.className == "disapproval") {
   1.118 -                    score = -1 - disapprovalIndex;
   1.119 -                    disapprovalIndex++;
   1.120 -                  }
   1.121 -                  var entries = section.childNodes;
   1.122 -                  for (var k=0; k<entries.length; k++) {
   1.123 -                    var entry = entries[k];
   1.124 -                    if (entry.className == "movable") {
   1.125 -                      var id = entry.id.match(/[0-9]+/);
   1.126 -                      var field = document.createElement("input");
   1.127 -                      scoringString += id + ":" + score + ";";
   1.128 -                    }
   1.129 -                  }
   1.130 -                }
   1.131 -              }
   1.132 -              var fields = form.childNodes;
   1.133 -              for (var j=0; j<fields.length; j++) {
   1.134 -                var field = fields[j];
   1.135 -                if (field.name == "scoring") {
   1.136 -                  field.setAttribute("value", scoringString);
   1.137 -                  if (jsTest) {
   1.138 -                    jsTestSuccess = success;
   1.139 -                    return;
   1.140 -                  } else {
   1.141 -                    form.submit();
   1.142 -                    return;
   1.143 -                  }
   1.144 -                }
   1.145 -              }
   1.146 -              alert('Hidden input field named "scoring" not found.');
   1.147 +              document.getElementById("preview2").value = "1";
   1.148              });
   1.149            }, false);
   1.150          }
   1.151        }
   1.152 -      for (var i=0; i<elements.length; i++) {
   1.153 -        var element = elements[i];
   1.154 -        if (element.className == "voting_done1") {
   1.155 -          element.click();
   1.156 -        }
   1.157 -      }
   1.158 -      jsTest = false;
   1.159 -      if (!jsTestSuccess) jsFail = true;
   1.160      });
   1.161    }, false);
   1.162    function voting_moveUp(element) {

Impressum / About Us