From 996a5cf1c3c7380005cc2271ab06178962bbce93 Mon Sep 17 00:00:00 2001
From: RomanSteinberg <romanofficial@yandex.ru>
Date: Tue, 2 Jun 2015 16:46:03 +0300
Subject: [PATCH] ADD: esc button handler on rating page

---
 ~dev_rating/media/js/rating.js | 82 +++++++++++++++++++---------------
 1 file changed, 46 insertions(+), 36 deletions(-)

diff --git a/~dev_rating/media/js/rating.js b/~dev_rating/media/js/rating.js
index d8539aea6..05a8297c1 100644
--- a/~dev_rating/media/js/rating.js
+++ b/~dev_rating/media/js/rating.js
@@ -1,6 +1,7 @@
 var $ = jQuery;
 
 $(function() {
+    var cancelFlag = false;
 
     function AdjustTable() {
         var jWrap = $('div.main_layer');
@@ -182,15 +183,26 @@ $(function() {
 
     function Rating(jThis, oldRate) {
         oldRate = parseInt(oldRate);
+
         // Здесь jThis - div rateCell, а не input, который является дочкой
-        jThis.children("input").attr("disabled", true);
+        if (cancelFlag) {
+            var str = ""
+            if (oldRate != -1)
+                str = oldRate;
+            jThis.children("input").val(str);
+            cancelFlag = false;
+            return;
+        }
 
         var newRate = 0;
         if (jThis.children("input").val() !== "")
             newRate = parseInt(jThis.children("input").val());
+
         if (newRate == oldRate)
             return;
 
+        jThis.children("input").attr("disabled", true);
+
         var rateResult = newRate;
 
         // считаем баллы по строке
@@ -230,39 +242,39 @@ $(function() {
                 data: "student="+g_studentID+"&submodule="+g_submoduleID+"&rate="+newRate,
                 complete: function(jqXHR, textStatus) {
                     switch(jqXHR.status) {
-                    case  403:
-                        EventInspector_ShowMsg("Сессия истекла", "error");
-                        jThis.children("input").val(oldRate);
-                        jThis.children("input").removeAttr("disabled");
-                        window.location.replace(URLdir);
-                        break;
-                    case 200:
-                        data = $.parseJSON(jqXHR.responseText);
-                        if(data.success === true) {
-                            var correctRate = (rateResult > 100) ? '100+' : rateResult;
-                            jThis.siblings(".rateResultCell").text(correctRate);
-
-                            // Открываем доступ к след. ячейке добора баллов
-                            if (jThis.hasClass("additionalCell")) {
-                                nextAdditionalCell = $("#col_" + (g_col + 1) + "_row_" + g_row);
-                                placeholder_max = (rateResult <= 60) ? (60 - rateResult) : (60 - oldRate);
-
-                                if (placeholder_max > 0 && nextAdditionalCell.hasClass("additionalCell")) {
-                                    	nextAdditionalCell.find("input").attr("placeholder", "макс. " + (placeholder_max));
-                                    	nextAdditionalCell.find("input").removeAttr("disabled");
+                        case  403:
+                            EventInspector_ShowMsg("Сессия истекла", "error");
+                            jThis.children("input").val(oldRate);
+                            jThis.children("input").removeAttr("disabled");
+                            window.location.replace(URLdir);
+                            break;
+                        case 200:
+                            data = $.parseJSON(jqXHR.responseText);
+                            if(data.success === true) {
+                                var correctRate = (rateResult > 100) ? '100+' : rateResult;
+                                jThis.siblings(".rateResultCell").text(correctRate);
+
+                                // Открываем доступ к след. ячейке добора баллов
+                                if (jThis.hasClass("additionalCell")) {
+                                    nextAdditionalCell = $("#col_" + (g_col + 1) + "_row_" + g_row);
+                                    placeholder_max = (rateResult <= 60) ? (60 - rateResult) : (60 - oldRate);
+
+                                    if (placeholder_max > 0 && nextAdditionalCell.hasClass("additionalCell")) {
+                                        nextAdditionalCell.find("input").attr("placeholder", "макс. " + (placeholder_max));
+                                        nextAdditionalCell.find("input").removeAttr("disabled");
+                                    }
                                 }
-                            }
 
-                            EventInspector_ShowMsg("Балл добавлен/изменен", "success");
-                        }
-                        else EventInspector_ShowMsg("Не удалось добавить/изменить балл", "error");
-                        jThis.children("input").removeAttr("disabled");
-                        break;
-                    default:
-                        EventInspector_ShowMsg(" "+statusCode, "success");
+                                EventInspector_ShowMsg("Балл добавлен/изменен", "success");
+                            }
+                            else EventInspector_ShowMsg("Не удалось добавить/изменить балл", "error");
+                            jThis.children("input").removeAttr("disabled");
+                            break;
+                        default:
+                            EventInspector_ShowMsg(" "+statusCode, "success");
                     }
 
-                    }
+                }
                 //statusCode: {
                 //    403: function() {
                 //        EventInspector_ShowMsg("Сессия истекла", "error");
@@ -325,7 +337,7 @@ $(function() {
     $(".commonCell").focusout(function(){
         g_isFocusCell = false;
         Rating($(this), oldRate);
-        
+
         TdUnFocus();
         UnsetTdInfo($(this));
         controlRowVisualization($(this).parent());
@@ -386,7 +398,9 @@ $(function() {
         var col = g_col;
         var direction;
         switch (e.keyCode) {
+            case 27: cancelFlag = true; // esc
             case 13: // enter
+            case 40: // down arrow
                 direction = Direction.Down;
                 break;
             case 38: // up arrow
@@ -395,9 +409,6 @@ $(function() {
             case 39: // right arrow
                 direction = Direction.Right;
                 break;
-            case 40: // down arrow
-                direction = Direction.Down;
-                break;
             case 37: // left arrow
                 direction = Direction.Left;
                 break;
@@ -430,6 +441,7 @@ $(function() {
 
     // Нажатие на чекбокс "Неявка"
     $(".absenceCheck").click(function(event) {
+
         controlRowVisualization($(this).parent().parent());
     });
 
@@ -492,5 +504,3 @@ $(function() {
 
 
 });
-
-
-- 
GitLab