From 11afb4e9c1f5c0ce36a594c0fb6468ac441c4fc6 Mon Sep 17 00:00:00 2001
From: xamgore <xamgore@ya.ru>
Date: Sat, 9 Jul 2016 14:42:01 +0300
Subject: [PATCH] Refactor popup-windows code

---
 media/js/student/index.js                     | 12 ++--
 media/js/wnd/wnd.js                           | 71 ++++++++++---------
 .../application/classes/Controller/Window.php | 20 +++---
 3 files changed, 49 insertions(+), 54 deletions(-)

diff --git a/media/js/student/index.js b/media/js/student/index.js
index 31876579b..b5c439f5c 100644
--- a/media/js/student/index.js
+++ b/media/js/student/index.js
@@ -1,10 +1,6 @@
-$(function() {
-
-    $('#openHelp').click(function()
-    {
-        $.get(URLdir + "window/help:student:index", function(data){
-            wnd.open(JSON.parse(data));
-        });
+$(function () {
+    $('#openHelp').click(function () {
+        $.postJSON(URLdir + 'window/help:student:index')
+            .done(data => wnd.open(data))
     });
-
 });
\ No newline at end of file
diff --git a/media/js/wnd/wnd.js b/media/js/wnd/wnd.js
index ff5a87124..a89f454f8 100644
--- a/media/js/wnd/wnd.js
+++ b/media/js/wnd/wnd.js
@@ -1,41 +1,44 @@
-var wnd = {}
-wnd.$ = {}
+var wnd = {};
+wnd.$ = {};
 
 wnd.isShow = false;
 wnd.show = function () {
-  wnd.isShow = true;
-  wnd.scroll = $(window).scrollTop();
-  wnd.$.page.css("position", "fixed");
-  wnd.$.page.css("margin-top", -wnd.scroll);
-  wnd.$.window.fadeIn("fast");
-}
+    wnd.isShow = true;
+    wnd.scroll = $(window).scrollTop();
+    wnd.$.page.css({ position: 'fixed', 'margin-top': -wnd.scroll });
+    wnd.$.window.fadeIn("fast");
+};
+
 wnd.hide = function () {
-  wnd.isShow = false;
-  wnd.$.page.css("position", "static");
-  wnd.$.page.css("margin-top", 0);
-  $(window).scrollTop(wnd.scroll);
-  wnd.$.window.fadeOut("fast");
-}
-
-wnd.open = function(data) {
-  wnd.$.title.html(data.title || '');
-  wnd.$.content.html(data.content || '');
-  wnd.$.block.width(data.width || 600);
-  wnd.$.block.css("top", data.top || '15%');
-  
-  wnd.show();
-  
-  wnd.$.window.css("min-width", wnd.$.block.width() + 40);
-  wnd.$.window.css("min-height", wnd.$.block.height() + 50);
-}
+    wnd.isShow = false;
+    wnd.$.page.css({ position: 'static', 'margin-top', 0 });
+    $(window).scrollTop(wnd.scroll);
+    wnd.$.window.fadeOut("fast");
+};
+
+wnd.open = function ([title = '', width = 600, top = '15%', content = '']) {
+    wnd.$.title.html(title);
+    wnd.$.content.html(content);
+    wnd.$.block.width(width);
+    wnd.$.block.css("top", top);
+
+    wnd.show();
+
+    wnd.$.window.css({
+        'min-width':  wnd.$.block.width() + 40,
+        'min-height': wnd.$.block.height() + 50
+    });
+};
+
 wnd.close = wnd.hide;
 
-$(function() {
-  // Блок окна
-  $("body").append("<div class=\"window\"><div class=\"window-shadow\" onclick=\"wnd.close()\"></div><div class=\"window-block\"><div class=\"window-title\"></div><a href=\"javascript:wnd.close()\" class=\"window-close\"></a><div class=\"window-content\"></div></div></div>");
-  wnd.$.page = $(".page");
-  wnd.$.window = $(".window");
-  wnd.$.block = $(".window-block");
-  wnd.$.title = $(".window-title");
-  wnd.$.content = $(".window-content");
+$(function () {
+    // Блок окна
+    $("body").append("<div class=\"window\"><div class=\"window-shadow\" onclick=\"wnd.close()\"></div><div class=\"window-block\"><div class=\"window-title\"></div><a href=\"javascript:wnd.close()\" class=\"window-close\"></a><div class=\"window-content\"></div></div></div>");
+
+    wnd.$.page = $(".page");
+    wnd.$.window = $(".window");
+    wnd.$.block = $(".window-block");
+    wnd.$.title = $(".window-title");
+    wnd.$.content = $(".window-content");
 });
diff --git a/~dev_rating/application/classes/Controller/Window.php b/~dev_rating/application/classes/Controller/Window.php
index a3df0b2cf..2605ccf16 100644
--- a/~dev_rating/application/classes/Controller/Window.php
+++ b/~dev_rating/application/classes/Controller/Window.php
@@ -13,18 +13,14 @@ class Controller_Window extends Controller
 
     public function action_get() {
         $path = UTF8::str_ireplace(':', '/', $this->request->param('id'));
-        if (Kohana::find_file('views/popup', $path, 'twig')) {
-            $twig = Twig::factory('popup/' . $path);
-            $twig->User = $this->user;
-            $arr = explode('||', $twig->render());
-            $json = json_encode(array(
-                'title' => $arr[0],
-                'width' => $arr[1],
-                'top'   => $arr[2],
-                'content' => $arr[3]));
-            $this->response->body($json);
-        } else {
+
+        if (!Kohana::find_file('views/popup', $path, 'twig'))
             throw HTTP_Exception::factory(404, "Искомый шаблон $path не найден");
-        }
+
+        $twig = Twig::factory('popup/' . $path)
+            ->set('User', $this->user);
+
+        // title, width, top, content
+        $this->response->body(json_encode(explode('||', $twig->render())));
     }
 }
-- 
GitLab