liquid_feedback_frontend
view static/js/voting.js @ 1551:dac776cd0a7f
Added css style for trace
| author | bsw | 
|---|---|
| date | Tue Oct 27 12:35:39 2020 +0100 (2020-10-27) | 
| parents | e616d8f16f14 | 
| children | 
 line source
     1 jsProtect(function() {
     2   voting_text_approval_single               = "Approval"
     3   voting_text_approval_multi                = "Approval"
     4   voting_text_first_preference_single       = "Approval (first preference)"
     5   voting_text_first_preference_multi        = "Approval (first preference)"
     6   voting_text_second_preference_single      = "Approval (second preference)"
     7   voting_text_second_preference_multi       = "Approval (second preference)"
     8   voting_text_third_preference_single       = "Approval (third preference)"
     9   voting_text_third_preference_multi        = "Approval (third preference)"
    10   voting_text_numeric_preference_single     = "Approval (#th preference)"
    11   voting_text_numeric_preference_multi      = "Approval (#th preference)"
    12   voting_text_abstention_single             = "Abstention"
    13   voting_text_abstention_multi              = "Abstention"
    14   voting_text_disapproval_above_one_single  = "Disapproval (prefer to lower block)"
    15   voting_text_disapproval_above_one_multi   = "Disapproval (prefer to lower block)"
    16   voting_text_disapproval_above_many_single = "Disapproval (prefer to lower blocks)"
    17   voting_text_disapproval_above_many_multi  = "Disapproval (prefer to lower blocks)"
    18   voting_text_disapproval_above_last_single = "Disapproval (prefer to last block)"
    19   voting_text_disapproval_above_last_multi  = "Disapproval (prefer to last block)"
    20   voting_text_disapproval_single            = "Disapproval"
    21   voting_text_disapproval_multi             = "Disapproval"
    23   function voting_setCategoryHeadings() {
    24     var approvalCount = 0;
    25     var disapprovalCount = 0;
    26     var sections = document.getElementById("voting").childNodes;
    27     for (var i=0; i<sections.length; i++) {
    28       var section = sections[i];
    29       if (section.className == "approval")       approvalCount++;
    30       if (section.className == "disapproval") disapprovalCount++;
    31     }
    32     var approvalIndex = 0;
    33     var disapprovalIndex = 0;
    34     for (var i=0; i<sections.length; i++) {
    35       var section = sections[i];
    36       if (
    37         section.className == "approval" ||
    38         section.className == "abstention" ||
    39         section.className == "disapproval"
    40       ) {
    41         var setHeading = function(heading) {
    42           var headingNodes = section.childNodes;
    43           for (var j=0; j<headingNodes.length; j++) {
    44             var headingNode = headingNodes[j];
    45             if (headingNode.className == "cathead") {
    46               headingNode.textContent = heading;
    47             }
    48           }
    49         }
    50         var count = 0;
    51         var entries = section.childNodes;
    52         for (var j=0; j<entries.length; j++) {
    53           var entry = entries[j];
    54           if (entry.className == "movable") count++;
    55         }
    56         if (section.className == "approval") {
    57           if (approvalCount > 1) {
    58             if (approvalIndex == 0) {
    59               if (count == 1) setHeading(voting_text_first_preference_single);
    60               else setHeading(voting_text_first_preference_multi);
    61             } else if (approvalIndex == 1) {
    62               if (count == 1) setHeading(voting_text_second_preference_single);
    63               else setHeading(voting_text_second_preference_multi);
    64             } else if (approvalIndex == 2) {
    65               if (count == 1) setHeading(voting_text_third_preference_single);
    66               else setHeading(voting_text_third_preference_multi);
    67             } else {
    68               var text;
    69               if (count == 1) text = voting_text_numeric_preference_single;
    70               else text = voting_text_numeric_preference_multi;
    71               text = text.replace(/#/, "" + (approvalIndex + 1))
    72               setHeading(text);
    73             }
    74           } else {
    75             if (count == 1) setHeading(voting_text_approval_single);
    76             else setHeading(voting_text_approval_multi);
    77           }
    78           approvalIndex++;
    79         } else if (section.className == "abstention") {
    80           if (count == 1) setHeading(voting_text_abstention_single);
    81           else setHeading(voting_text_abstention_multi);
    82         } else if (section.className == "disapproval") {
    83           if (disapprovalCount > disapprovalIndex + 2) {
    84             if (count == 1) setHeading(voting_text_disapproval_above_many_single);
    85             else setHeading(voting_text_disapproval_above_many_multi);
    86           } else if (disapprovalCount == 2 && disapprovalIndex == 0) {
    87             if (count == 1) setHeading(voting_text_disapproval_above_one_single);
    88             else setHeading(voting_text_disapproval_above_one_multi);
    89           } else if (disapprovalIndex == disapprovalCount - 2) {
    90             if (count == 1) setHeading(voting_text_disapproval_above_last_single);
    91             else setHeading(voting_text_disapproval_above_last_multi);
    92           } else {
    93             if (count == 1) setHeading(voting_text_disapproval_single);
    94             else setHeading(voting_text_disapproval_multi);
    95           }
    96           disapprovalIndex++;
    97         }
    98       }
    99     }
   100   }
   101   function voting_calculateScoring() {
   102     var mainDiv = document.getElementById("voting");
   103     var form = document.getElementById("voting_form");
   104     var scoringString = "";
   105     var approvalCount = 0;
   106     var disapprovalCount = 0;
   107     var sections = mainDiv.childNodes;
   108     for (var j=0; j<sections.length; j++) {
   109       var section = sections[j];
   110       if (section.className == "approval")       approvalCount++;
   111       if (section.className == "disapproval") disapprovalCount++;
   112     }
   113     var approvalIndex = 0;
   114     var disapprovalIndex = 0;
   115     for (var j=0; j<sections.length; j++) {
   116       var section = sections[j];
   117       if (
   118         section.className == "approval"    ||
   119         section.className == "abstention"  ||
   120         section.className == "disapproval"
   121       ) {
   122         var score;
   123         if (section.className == "approval") {
   124           score = approvalCount - approvalIndex;
   125           approvalIndex++;
   126         } else if (section.className == "abstention") {
   127           score = 0;
   128         } else if (section.className == "disapproval") {
   129           score = -1 - disapprovalIndex;
   130           disapprovalIndex++;
   131         }
   132         var entries = section.childNodes;
   133         for (var k=0; k<entries.length; k++) {
   134           var entry = entries[k];
   135           if (entry.className == "movable") {
   136             var id = entry.id.match(/[0-9]+/);
   137             var field = document.createElement("input");
   138             scoringString += id + ":" + score + ";";
   139           }
   140         }
   141       }
   142     }
   143     var fields = form.childNodes;
   144     for (var j=0; j<fields.length; j++) {
   145       var field = fields[j];
   146       if (field.name == "scoring") {
   147         field.setAttribute("value", scoringString);
   148         return;
   149       }
   150     }
   151     throw('Hidden input field named "scoring" not found.');
   152   }
   153   function voting_move(element, up, dropX, dropY) {
   154     jsProtect(function() {
   155       if (typeof(element) == "string") element = document.getElementById(element);
   156       var mouse = (up == null);
   157       var oldParent = element.parentNode;
   158       if (mouse) var centerY = dropY + element.clientHeight / 2;
   159       var approvalCount = 0;
   160       var disapprovalCount = 0;
   161       var mainDiv = document.getElementById("voting");
   162       var sections = mainDiv.childNodes;
   163       for (var i=0; i<sections.length; i++) {
   164         var section = sections[i];
   165         if (section.className == "approval")       approvalCount++;
   166         if (section.className == "disapproval") disapprovalCount++;
   167       }
   168       if (mouse) {
   169         for (var i=0; i<sections.length; i++) {
   170           var section = sections[i];
   171           if (
   172             section.className == "approval" ||
   173             section.className == "abstention" ||
   174             section.className == "disapproval"
   175           ) {
   176             if (
   177               centerY >= section.offsetTop &&
   178               centerY <  section.offsetTop + section.clientHeight
   179             ) {
   180               var entries = section.childNodes;
   181               for (var j=0; j<entries.length; j++) {
   182                 var entry = entries[j];
   183                 if (entry.className == "movable") {
   184                   if (centerY < entry.offsetTop + entry.clientHeight / 2) {
   185                     if (element != entry) {
   186                       oldParent.removeChild(element);
   187                       section.insertBefore(element, entry);
   188                     }
   189                     break;
   190                   }
   191                 }
   192               }
   193               if (j == entries.length) {
   194                 oldParent.removeChild(element);
   195                 section.appendChild(element);
   196               }
   197               break;
   198             }
   199           }
   200         }
   201         if (i == sections.length) {
   202           var newSection = document.createElement("div");
   203           var cathead = document.createElement("div");
   204           cathead.setAttribute("class", "cathead");
   205           newSection.appendChild(cathead);
   206           for (var i=0; i<sections.length; i++) {
   207             var section = sections[i];
   208             if (
   209               section.className == "approval" ||
   210               section.className == "abstention" ||
   211               section.className == "disapproval"
   212             ) {
   213               if (centerY < section.offsetTop + section.clientHeight / 2) {
   214                 if (section.className == "disapproval") {
   215                   newSection.setAttribute("class", "disapproval");
   216                   disapprovalCount++;
   217                 } else {
   218                   newSection.setAttribute("class", "approval");
   219                   approvalCount++;
   220                 }
   221                 mainDiv.insertBefore(newSection, section);
   222                 break;
   223               }
   224             }
   225           }
   226           if (i == sections.length) {
   227             newSection.setAttribute("class", "disapproval");
   228             disapprovalCount++;
   229             mainDiv.appendChild(newSection);
   230           }
   231           oldParent.removeChild(element);
   232           newSection.appendChild(element);
   233         }
   234       } else {
   235         var oldFound = false;
   236         var prevSection = null;
   237         var nextSection = null;
   238         for (var i=0; i<sections.length; i++) {
   239           var section = sections[i];
   240           if (
   241             section.className == "approval" ||
   242             section.className == "abstention" ||
   243             section.className == "disapproval"
   244           ) {
   245             if (oldFound) {
   246               nextSection = section;
   247               break;
   248             } else if (section == oldParent) {
   249               oldFound = true;
   250             } else {
   251               prevSection = section;
   252             }
   253           }
   254         }
   255         var create;
   256         if (oldParent.className == "abstention") {
   257           create = true;
   258         } else {
   259           create = false;
   260           for (var i=0; i<oldParent.childNodes.length; i++) {
   261             var entry = oldParent.childNodes[i];
   262             if (entry.className == "movable") {
   263               if (entry != element) {
   264                 create = true;
   265                 break;
   266               }
   267             }
   268           }
   269         }
   270         var newSection;
   271         if (create) {
   272           newSection = document.createElement("div");
   273           var cathead = document.createElement("div");
   274           cathead.setAttribute("class", "cathead");
   275           newSection.appendChild(cathead);
   276           if (
   277             oldParent.className == "approval" ||
   278             (oldParent.className == "abstention" && up)
   279           ) {
   280             newSection.setAttribute("class", "approval");
   281             approvalCount++;
   282           } else {
   283             newSection.setAttribute("class", "disapproval");
   284             disapprovalCount++;
   285           }
   286           if (up) {
   287             mainDiv.insertBefore(newSection, oldParent);
   288           } else {
   289             if (nextSection) mainDiv.insertBefore(newSection, nextSection);
   290             else mainDiv.appendChild(newSection);
   291           }
   292         } else {
   293           if (up) newSection = prevSection;
   294           else newSection = nextSection;
   295         }
   296         if (newSection) {
   297           oldParent.removeChild(element);
   298           if (create || up) {
   299             newSection.appendChild(element);
   300           } else {
   301             var inserted = false;
   302             for (var i=0; i<newSection.childNodes.length; i++) {
   303               var entry = newSection.childNodes[i];
   304               if (entry.className == "movable") {
   305                 newSection.insertBefore(element, entry);
   306                 inserted = true;
   307                 break;
   308               }
   309             }
   310             if (!inserted) newSection.appendChild(element);
   311           }
   312         }
   313       }
   314       // sections = mainDiv.childNodes;
   315       for (i=0; i<sections.length; i++) {
   316         var section = sections[i];
   317         if (
   318           (section.className == "approval"    &&    approvalCount > 1) ||
   319           (section.className == "disapproval" && disapprovalCount > 1)
   320         ) {
   321           var entries = section.childNodes;
   322           for (var j=0; j<entries.length; j++) {
   323             var entry = entries[j];
   324             if (entry.className == "movable") break;
   325           }
   326           if (j == entries.length) {
   327             section.parentNode.removeChild(section);
   328           }
   329         }
   330       }
   331       voting_setCategoryHeadings();
   332       voting_calculateScoring();
   333     });
   334   }
   335   window.elementDropped = function(element, dropX, dropY) {
   336     voting_move(element, null, dropX, dropY);
   337   }
   338   window.addEventListener("load", function(event) {
   339     jsProtect(function() {
   340       voting_setCategoryHeadings();
   341       voting_calculateScoring();  // checks if script works fine
   342     });
   343   }, false);
   344   window.voting_moveUp = function(element) {
   345     return voting_move(element, true);
   346   }
   347   window.voting_moveDown = function(element) {
   348     return voting_move(element, false);
   349   }
   350 });
