Advertisement
kepo4ka

kinozall svg jquery

Nov 11th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvasMask;
  2. $j = jQuery.noConflict();
  3.  
  4.  
  5. $j(document).ready(function () {
  6.  
  7.  
  8.     var seating_charts = $j('#seating_charts')[0];
  9.  
  10.     var main_drag = {};
  11.     main_drag.elem = $j("#main_drag_countainer");
  12.     main_drag.padding = parseInt(main_drag.elem.children().css("padding"), 10);
  13.  
  14.     main_drag.width = main_drag.elem.width();
  15.     main_drag.height = main_drag.elem.height();
  16.     var circle_raduis_min = 6;
  17.     var circle_raduis_max = 9;
  18.     var isdrag = false;
  19.     var iszoomed = false;
  20.  
  21.     var svg_chart = d3.select("#seating_charts");
  22.     var svg_cont = $j("#seating_charts");
  23.  
  24.     var svgCoor = getCoords(svg_cont[0]);
  25.  
  26.  
  27.     var seat_prices_info_ul = $j('#seat_prices_filter');
  28.     var seat_prices = [];
  29.  
  30.     var seats_order = [];
  31.     var isdrag = false;
  32.  
  33.     var price_booked_type_id;
  34.  
  35.     DrawGrid();
  36.  
  37.     getPricesAjax();
  38.     getSeatsAjax();
  39.  
  40.  
  41.     var seats = svg_chart.selectAll("circle");
  42.  
  43.  
  44.     //  $j("#main_drag_countainer > .handle").css("transform", "scale(0.5) translate(-50%, -50%)");
  45.     // $j("#main_drag_countainer > .handle").css("width", "101%");
  46.     // $j("#main_drag_countainer > .handle").css("height", "101%");
  47.  
  48.     $j(".mmain").on("click", function (e) {
  49.     });
  50.     var dragged = false;
  51.  
  52.  
  53.     // main_drag.elem[0].onmouseenter = function()
  54.     // {
  55.     //     if (dragged == false)
  56.     //     {
  57.     //         console.log("1233");
  58.     //         enableDrag();
  59.     //         dragged = true;
  60.     //         //  main_drag.elem[0].onmouseenter = false;
  61.     //     }
  62.  
  63.     // };
  64.  
  65.     $j('#plus_zoom_chart').on("click", function (e) {
  66.         $j("#main_drag_countainer > .handle").css("width", "202%");
  67.         $j("#main_drag_countainer > .handle").css("height", "200%");
  68.         svg_cont.css("transform", "scale(2) translate(25%, 25%)");
  69.         enableDrag();
  70.     });
  71.  
  72.     $j('#minus_zoom_chart').on("click", function (e) {
  73.         $j("#main_drag_countainer > .handle").css("width", "100%");
  74.         $j("#main_drag_countainer > .handle").css("height", "100%");
  75.         svg_cont.css("transform", "");
  76.  
  77.         canvasMask.setValue(0, 0);
  78.         canvasMask.disable();
  79.     });
  80.  
  81.  
  82.     $j("#seat_prices_filter").on("click", "li", function () {
  83.         var li = $j(this);
  84.         var list = $j("#seat_prices_filter li");
  85.  
  86.  
  87.         if (li.attr('id') == "reset_price_filter") {
  88.             hideDiffPrices();
  89.             $j("#reset_price_filter").css("display", "none");
  90.             return;
  91.         }
  92.  
  93.         var curr_selected = true;
  94.         if (li.hasClass("li_selected") == true) {
  95.             list.each(function (k, v) {
  96.                 if (k == 0) {
  97.                     return true;
  98.                 }
  99.                 if ($j(v).is(li)) {
  100.  
  101.  
  102.                     return true;
  103.                 }
  104.                 if ($j(v).hasClass('li_selected') == false) {
  105.                     curr_selected = false;
  106.                     return false;
  107.                 }
  108.             });
  109.         }
  110.         if (curr_selected) {
  111.             $j("#reset_price_filter").css("display", "inline");
  112.         } else {
  113.             $j("#reset_price_filter").css("display", "none");
  114.         }
  115.         var type_id = li.attr("seat_type_id");
  116.  
  117.         showDiffPrices(type_id);
  118.  
  119.         li.toggleClass("li_selected");
  120.     });
  121.  
  122.  
  123.     svg_chart.on("mousedown", function () {
  124.         isdrag = true;
  125.         document.onmouseup = function () {
  126.             isdrag = false;
  127.         }
  128.     });
  129.  
  130.  
  131.     seats.on("mouseenter", function () {
  132.         if (isdrag == true) {
  133.             return;
  134.         }
  135.         var seat = d3.select(this);
  136.         var e = d3.event;
  137.         var seatCoor = getCoords(this);
  138.  
  139.  
  140.         if (seat.attr("seat_isbooked") == "true") {
  141.             html = "Место недоступно";
  142.         }
  143.         else {
  144.             html = "<p>" + seat.attr("seat_price") + "₽" + "</p>" +
  145.                 "<p>Ряд - " + seat.attr("seat_row") + ", Место - " + seat.attr("seat_number") + "</p>";
  146.  
  147.             seat.attr("r", "12");
  148.  
  149.             seat.style("cursor", "pointer");
  150.         }
  151.         showSeatInfo(seatCoor.left + seatCoor.width * 0.5, seatCoor.top - seatCoor.height * 0.5, html, true);
  152.     });
  153.  
  154.  
  155.     seats.on("mouseleave", function () {
  156.         var seat = d3.select(this);
  157.  
  158.         showSeatInfo(0, 0, "", false);
  159.         seat.attr("r", "10");
  160.     });
  161.  
  162.     $j("#fixed_seat_info").on("click", function () {
  163.         showSeatInfo(0, 0, "", false);
  164.     });
  165.  
  166.  
  167.     $j("#seat_order_list").on("click", ".seat_order_remove_btn", function () {
  168.         var thisRemoveId = parseInt($j(this).attr("seat_removeid"));
  169.  
  170.         $j(this).parent().parent().remove();
  171.  
  172.         var seats = svg_chart.selectAll("circle");
  173.         var seat_change_style;
  174.         seats.each(function (d, i) {
  175.             var thisCir = d3.select(this);
  176.             var cir_id = parseInt(thisCir.attr("seat_id"));
  177.  
  178.             if (thisRemoveId === cir_id) {
  179.                 seat_change_style = thisCir;
  180.                 return false;
  181.             }
  182.         });
  183.  
  184.         seat_change_style = seatStyleDeselected(seat_change_style);
  185.  
  186.         removefromOrder(thisRemoveId);
  187.         logOrder();
  188.     });
  189.  
  190.  
  191.     function showSeatInfo(left, top, html, k) {
  192.         if (k) {
  193.             $j("#fixed_seat_info").addClass("display_block");
  194.         } else {
  195.             //  $j("#fixed_seat_info").removeClass("display_block");
  196.         }
  197.  
  198.         $j("#fixed_seat_info").css("left", left - pageXOffset);
  199.         $j("#fixed_seat_info").css("top", top - pageYOffset);
  200.         $j("#fixed_seat_info").html(html);
  201.     }
  202.  
  203.     // Задать стиль места (seat)
  204.     function seatStyleSelected(seat) {
  205.         seat
  206.             .style("stroke", seat.attr("seat_color"))
  207.             .style("stroke-width", circle_raduis_min)
  208.             .style("fill", "white");
  209.         seat.attr("seat_isselected", true);
  210.         return seat;
  211.     }
  212.  
  213.     // Задать стиль места (seat)
  214.     function seatStyleDeselected(seat) {
  215.         seat
  216.             .style("stroke", "")
  217.             .style("stroke-width", 1)
  218.             .style("fill", seat.attr("seat_color"));
  219.         seat.attr("seat_isselected", false);
  220.         return seat;
  221.     }
  222.  
  223.  
  224.     function logOrder() {
  225.         console.log("order.lenght:", seats_order.length);
  226.         for (i = 0; i < seats_order.length; i++) {
  227.             console.log(seats_order[i]);
  228.         }
  229.         console.log("-------------");
  230.     }
  231.  
  232.  
  233.     // Добавить выбранное место к заказу
  234.     function addtoOrder(seat) {
  235.         var temp_seat_info = {
  236.             seat_price: parseInt(seat.attr("seat_price")),
  237.             seat_id: parseInt(seat.attr("seat_id")),
  238.             seat_row: parseInt(seat.attr("seat_row")),
  239.             seat_number: parseInt(seat.attr("seat_number"))
  240.         }
  241.  
  242.         seats_order.push(temp_seat_info);
  243.  
  244.         orderSummaryChange(temp_seat_info.seat_price);
  245.  
  246.  
  247.         var order_li_item = document.createElement("li");
  248.         order_li_item.setAttribute("seat_id", temp_seat_info.seat_id);
  249.         order_li_item_html = '<div class="seat_order_item">' +
  250.             '<span class="fa-stack fa-lg seat_order_remove_btn" seat_removeid="' +
  251.             temp_seat_info.seat_id +
  252.             '">' +
  253.             '<i class="fa fa-circle fa-stack-2x" ></i>' +
  254.             '<i class="fa fa-times fa-stack-1x fa-inverse" ></i>' +
  255.             '</span>' +
  256.             '<div class="one_ticket_order_info">' +
  257.             '<div class="position">' +
  258.             '<p>Ряд ' + temp_seat_info.seat_row + '</p>' +
  259.             '<p>Место ' + temp_seat_info.seat_number + '</p>' +
  260.             '</div>' +
  261.             '<div class="price">' +
  262.             temp_seat_info.seat_price + " ₽" +
  263.             '</div></div></div>';
  264.  
  265.         order_li_item.innerHTML = order_li_item_html;
  266.         $j("#seat_order_list").append(order_li_item);
  267.     }
  268.  
  269.     // Удалить место из заказа
  270.     function removefromOrder(id) {
  271.         for (i = 0; i < seats_order.length; i++) {
  272.             var thisId = parseInt(seats_order[i].seat_id);
  273.             if (id === thisId) {
  274.                 orderSummaryChange(-seats_order[i].seat_price);
  275.                 seats_order.splice(i, 1);
  276.                 break;
  277.             }
  278.         }
  279.  
  280.         $j("#seat_order_list li").each(function () {
  281.             var thisId = parseInt($j(this).attr("seat_id"));
  282.             if (id === thisId) {
  283.                 $j(this).remove();
  284.                 return false;
  285.             }
  286.         });
  287.     }
  288.  
  289.     // Рассчитать итоговую сумму
  290.     function orderSummaryChange(price) {
  291.         summary_order_elem = $j("#order_summary_price");
  292.         summary_order_price = parseInt(summary_order_elem.html());
  293.         if (isNaN(summary_order_price) == false) {
  294.             summary_order_price += price;
  295.         }
  296.         else {
  297.             summary_order_price = price;
  298.         }
  299.         summary_order_elem.html(summary_order_price);
  300.         if (summary_order_price > 0) {
  301.             $j("#order_continue_btn").prop("disabled", "");
  302.         }
  303.         else {
  304.             $j("#order_continue_btn").prop("disabled", "disabled");
  305.         }
  306.     }
  307.  
  308.  
  309.     // Перетаскивание схемы
  310.     function enableDrag() {
  311.         canvasMask = new Dragdealer('main_drag_countainer', {
  312.             x: 0,
  313.             y: 0,
  314.             vertical: true,
  315.             speed: 3,
  316.             loose: true,
  317.             requestAnimationFrame: true
  318.         });
  319.         canvasMask.enable();
  320.     }
  321.  
  322.     // Получить координаты элемента на svg
  323.     function getCoords(elem) {
  324.         var box = elem.getBoundingClientRect();
  325.         return {
  326.             top: box.top + pageYOffset,
  327.             left: box.left + pageXOffset,
  328.             right: box.right + pageXOffset,
  329.             bottom: box.bottom + pageYOffset,
  330.             width: box.width,
  331.             height: box.height
  332.         };
  333.     }
  334.  
  335.     // Увеличение масштаба элемента
  336.     function seatScale(elem) {
  337.         elem.toggleClass("seat_scaled");
  338.     }
  339.  
  340.     // Получить информацию о местах в зале
  341.     function getSeatsAjax() {
  342.         $j.ajax({
  343.             type: "GET",
  344.  
  345.             url: my_ajax_url,
  346.             data: {
  347.                 action: 'getSeats'
  348.             },
  349.             success: function (response) {
  350.  
  351.                 var data = JSON.parse(response);
  352.                 addSeatsInfo(data);
  353.  
  354.             },
  355.             error: function (res) {
  356.                 console.log("error : ", res);
  357.             }
  358.         });
  359.     }
  360.  
  361.  
  362.     // Получить список категорий Цен
  363.     function getPricesAjax() {
  364.         $j.ajax({
  365.             type: "GET",
  366.  
  367.             url: my_ajax_url,
  368.             data: {
  369.                 action: 'getPrices'
  370.             },
  371.             success: function (response) {
  372.  
  373.                 console.log(response);
  374.                 var data = JSON.parse(response);
  375.                 addPricesInfo(data);
  376.  
  377.             },
  378.             error: function (res) {
  379.                 console.log("error : ", res);
  380.             }
  381.         });
  382.     }
  383.  
  384.  
  385.     // Создать список категорий цен
  386.     function addPricesInfo(data) {
  387.         seat_prices = [];
  388.  
  389.         for (var key in data) {
  390.             var price_info = {};
  391.             price_info.id = data[key].id;
  392.             price_info.price = data[key].price;
  393.             price_info.name = data[key].name || "";
  394.             price_info.color = data[key].color;
  395.             seat_prices.push(price_info);
  396.         }
  397.  
  398.         for (i = 0; i < seat_prices.length; i++) {
  399.             if (seat_prices[i].name == "booked") {
  400.                 price_booked_type_id = seat_prices[i].id;
  401.                 continue;
  402.             }
  403.             var li_price = document.createElement('li');
  404.             li_price.setAttribute("seat_type_id", seat_prices[i].id);
  405.             var html = "" + seat_prices[i].price + "₽";
  406.             li_price.innerHTML = html;
  407.             $j(li_price).css("border-top-color", seat_prices[i].color);
  408.  
  409.             seat_prices_info_ul[0].appendChild(li_price);
  410.         }
  411.     }
  412.  
  413.  
  414.     // Создать места в зале
  415.     function addSeatsInfo(data) {
  416.         var seats_array = [];
  417.  
  418.         for (var key in data) {
  419.             var seat = {};
  420.  
  421.             seat.seat_id = data[key].id;
  422.             seat.seat_type_id = data[key].type_id;
  423.             seat.seat_price = data[key].price;
  424.             seat.seat_color = data[key].color;
  425.             seat.seat_left = data[key].left;
  426.             seat.seat_top = data[key].top;
  427.             seat.seat_row = data[key].row;
  428.             seat.seat_number = data[key].number;
  429.             //   seat.seat_isbooked = data[key].booked;
  430.             seat.seat_isselected = false;
  431.             seats_array.push(seat);
  432.         }
  433.  
  434.         for (i = 0; i < seats_array.length; i++) {
  435.             createSeatfromObj(seats_array[i]);
  436.         }
  437.  
  438.         hideToolTabs();
  439.     }
  440.  
  441.     // Задать информацию о месте на основе объекта obj
  442.     function createSeatfromObj(obj) {
  443.         svg_chart.append('circle')
  444.             .attr("seat_id", obj.seat_id)
  445.             .attr("seat_type_id", obj.seat_type_id)
  446.             .attr("seat_price", obj.seat_price)
  447.             .attr("cx", obj.seat_left)
  448.             .attr("cy", obj.seat_top)
  449.             .attr("seat_color", obj.seat_color)
  450.             .style("fill", obj.seat_color)
  451.             .attr("seat_isselected", obj.seat_isselected)
  452.             .attr("seat_row", obj.seat_row)
  453.             .attr("seat_number", obj.seat_number)
  454.             .attr("r", circle_raduis_min)
  455.             .on("mouseenter", function () {
  456.                 seatmouseEnterHandler(d3.select(this), d3.event);
  457.             })
  458.             .on("mouseleave", function () {
  459.                 showSeatInfo(0, 0, "", false);
  460.             })
  461.             .on("click", function () {
  462.                 seatmouseClickHandler(d3.select(this));
  463.             });
  464.     }
  465.  
  466.  
  467.     function ShowToolTab(tabId) {
  468.         $j('.tool_menu_tabs .tool_tab').css("display", "none");
  469.         $j('.tool_menu_tabs').css('display', "block");
  470.         $j(tabId).css("display", "block");
  471.     }
  472.  
  473.     function hideToolTabs() {
  474.         $j('.tool_menu_tabs').css('display', "none");
  475.         $j('.tool_menu_tabs .tool_tab').css("display", "none");
  476.     }
  477.  
  478.     // Выделить места из соотвествующей ценовой категории
  479.     function showDiffPrices(id) {
  480.         var seats = svg_chart.selectAll("circle");
  481.         seats.each(function (d, i) {
  482.             var thisCir = d3.select(this);
  483.             var cir_id = thisCir.attr("seat_type_id");
  484.  
  485.             if (id == cir_id) {
  486.                 if (thisCir.style('opacity') == 0.6) {
  487.                     thisCir.style('opacity', 1);
  488.                 }
  489.                 else {
  490.                     thisCir.style('opacity', 0.6);
  491.                 }
  492.             }
  493.         });
  494.     }
  495.  
  496.     // Добавление/удаление место из заказа
  497.     function seatmouseClickHandler(seat) {
  498.  
  499.             if (seat.attr("seat_type_id")==price_booked_type_id)
  500.             {
  501.                 return false;
  502.             }
  503.  
  504.             if (seat.attr("seat_isselected") == "false") {
  505.  
  506.                 seat = seatStyleSelected(seat);
  507.                 addtoOrder(seat);
  508.             }
  509.             else {
  510.  
  511.                 seat = seatStyleDeselected(seat);
  512.                 var seat_remove_id = parseInt(seat.attr("seat_id"));
  513.                 removefromOrder(seat_remove_id);
  514.             }
  515.     }
  516.  
  517.     // Подробная информация о месте при наведении
  518.     function seatmouseEnterHandler(seat, event) {
  519.         if (isdrag) {
  520.             return;
  521.         }
  522.  
  523.         var e = event;
  524.         var seatCoor = getCoords(seat._groups[0][0]);
  525.  
  526.         var html = "";
  527.         if (seat.attr("seat_type_id") == price_booked_type_id) {
  528.             html = "<p>Место недоступно</p>";
  529.         }
  530.         else {
  531.             html = "<p>" + seat.attr("seat_price") + "₽" + "</p>";
  532.         }
  533.  
  534.         html += "<p>Ряд - " + seat.attr("seat_row") + ", Место - " + seat.attr("seat_number") + "</p>";
  535.  
  536.         seat.style("cursor", "pointer");
  537.  
  538.  
  539.         showSeatInfo(seatCoor.left + seatCoor.width * 0.5, seatCoor.top - seatCoor.height * 0.5, html, true);
  540.     }
  541.  
  542.     function hideDiffPrices() {
  543.         $j("#seat_prices_filter li").removeClass("li_selected");
  544.         var seats = svg_chart.selectAll("circle");
  545.         seats.each(function (d, i) {
  546.             var thisCir = d3.select(this);
  547.             var cir_id = thisCir.attr("seat_type_id");
  548.             thisCir.style('opacity', 0.6);
  549.  
  550.         });
  551.     }
  552.  
  553.     // Начальная настройка схемы зала
  554.     function DrawGrid() {
  555.         var seat = svg_chart.select("circle");
  556.  
  557.         var cw = parseInt(svg_chart.attr("width"));
  558.         var ch = parseInt(svg_chart.attr("height"));
  559.         var cwCount = cw / circle_raduis_min;
  560.         var chCount = ch / circle_raduis_min;
  561.  
  562.  
  563.  
  564.         start_x =cw * 0.1;
  565.         start_y = end_y = ch - ch * 0.05;
  566.         x1 = x2 = cw* 0.5;
  567.         x1 = cw * 0.4;
  568.         x2 = cw * 0.6;
  569.         y1 = y2 = start_y * 0.9;
  570.         end_x = cw * 0.9;
  571.  
  572.         var path_value = "M " + start_x + ", " + start_y + " C " + x1 + ", " + y1 + " " + x2 + ", " + y2 + " " + end_x + ", " + end_y;
  573.  
  574.  
  575.         svg_chart.append("path")
  576.             .attr("d", path_value)
  577.             .attr("stroke", "gray")
  578.             .attr("stroke-width", 3)
  579.             .attr("fill", "none");
  580.  
  581.         var text_elem = svg_chart.append("text")
  582.             .attr("x", cw / 2)
  583.             .attr("y", ch * 0.95)
  584.             .text("СЦЕНА")
  585.             .attr("fill", "gray")
  586.             .attr("font-size", "35px");
  587.         text_elem.attr("x", parseInt(text_elem.attr("x")) - 56);
  588.     }
  589.  
  590.  
  591.  
  592.     function random(min, max) {
  593.         var rand = min + Math.random() * (max + 1 - min);
  594.         rand = Math.floor(rand);
  595.         return rand;
  596.     }
  597.  
  598.     // getPrices();
  599.  
  600.     // function getPrices()
  601.     // {
  602.     //     $j.ajax({
  603.     //         type: "GET",
  604.     //         url: my_ajax_url + "?page,
  605.     //         data: {
  606.     //             action: 'do_something'
  607.     //         },
  608.     //         success: function(response) {
  609.     //             console.log('AJAX response : ', response);
  610.     //         },
  611.     //         error: function(res)
  612.     //         {
  613.     //             console.log("error : ", res);
  614.     //         }
  615.     //     });
  616.     // }
  617.  
  618.  
  619. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement