Blame view

js/basket.js 5.44 KB
bf807468   Alex Savenko   first commit
1
2
3
  (function($){

  

      $.fn.basket = function(callerSettings) {

5e4ecece   Виталий   qwerty
4
  

bf807468   Alex Savenko   first commit
5
6
  	var basket_id = this;

  

5e4ecece   Виталий   qwerty
7
8
9
  		var find_products = function(){

           $("a[rel~='product']").each(function (i) {

                  $(this).bind('click',function(){

bf807468   Alex Savenko   first commit
10
11
12
13
14
15
16
17
18
19
20
21
                  var rel = $(this).attr('rel');

                  var id = $('#product_id').val();

                  var count = $('#count').val();

  				$("#pic_notvisible").css('top','200px').css('left','110px');

  				$('#pic_notvisible').show();

  				var div2Pos = $(basket_id).position();

  				$("#pic_notvisible").animate({left:div2Pos.left, top:div2Pos.top, opacity: 0.25, height: 'toggle'}, 1000);

                  go_product({mod_id : id,count:count});

                  return false;

                  })

           });

          };

5e4ecece   Виталий   qwerty
22
  

bf807468   Alex Savenko   first commit
23
24
25
  		var go_product = function(data){

  		    var product_id = data.product_id;

  			$.post("/basket/add/",  data ,

5e4ecece   Виталий   qwerty
26
  			function(data){

bf807468   Alex Savenko   first commit
27
28
  				//alert_msg("Товар добавлен<br /> в корзину",product_id);

  				start_basket();

5e4ecece   Виталий   qwerty
29
  			});

bf807468   Alex Savenko   first commit
30
  		};

5e4ecece   Виталий   qwerty
31
  

bf807468   Alex Savenko   first commit
32
33
34
35
36
37
38
39
          var start_basket = function(){

  			$.post("/basket/info/",

  			function(data){

  				$(basket_id).html(data);

  			});

  

          };

  

5e4ecece   Виталий   qwerty
40
  		var alert_msg = function(msg,product_id){

bf807468   Alex Savenko   first commit
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  			winW = document.body.offsetWidth;

  			winH = document.body.offsetHeight

  			$('.modal_box').remove();

  			$('#data_box').remove();

  			$('body').append('<div class="modal_box"></div>');

  			$('body').append('<div id="data_box"></div>');

  			$('#data_box').append('<div class="data_wrp"></div>');

  					$('#data_box').css( "left", ((winW-400)/2)+'px' );

  

  			var scrollTop = document.documentElement.scrollTop

  			if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1 || navigator.userAgent.toLowerCase().indexOf('safari') > -1) {

  				scrollTop = document.body.scrollTop;

  			}

  			$('#data_box').css( "top", (scrollTop+150)+'px' );

  

  			app = '<center>';

  			app +='<h1>'+msg+'</h1>';

  			app += '<br /><input type="submit" class="submit4" style="text-transform:none;" value="Перейти в корзину" onClick="document.location=\'/yii2/basket/\'" />&nbsp;';

  			app += '<input type="submit" class="submit4" style="text-transform:none;" id="p_close" value="Продолжить покупки" />';

  			app += '</center>';

  			$('#data_box .data_wrp').append(app);

  

  

  			$(".modal_box, #modal_close, #p_close").click(function() {

  				$('.modal_box').remove();

  				$('#data_box').remove();

  			});

  		};

5e4ecece   Виталий   qwerty
69
  

bf807468   Alex Savenko   first commit
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  		find_products();

  		start_basket();

  

  		// frontend calculator

          var Model = {

              init : function() {

  				this.data = [];

  				this.setModel();

              },

  

              setModel : function() {

                  Model.data = [];

  				$(".basket_item").each(function(index, el) {

  					var objItem = {};

  					objItem.cost = parseFloat(

  						$(el).find("[name $= '[cost]']").val()

  					);

  					objItem.count = parseInt(

                          $(el).find("[name $= '[count]']").val()

  					);

  					Model.data.push(objItem);

                  });

              },

  

  			getData : function() {

  				return this.data;

              }

          };

  

          var Controll = {

              init : function() {

                  Model.init();

                  View.init();

              },

  

  			getData : function() {

  				return Model.getData();

              },

  

  			setData : function() {

  				Model.setModel();

              },

  

  			countSumCost : function(elementIndex) {

  				var obj = this.getData()[elementIndex];

  				var sumCost = obj.cost * obj.count;

  				if ( isNaN(sumCost) ){

  					return 0;

  				}

  				return parseFloat(sumCost.toFixed(2));

              },

  

  			countTotalCost : function() {

  				var totalCost = 0;

  				this.getData().forEach(function(obj, index) {

  					totalCost += Controll.countSumCost(index);

  				});

  				return parseFloat(totalCost.toFixed(2));

              }

          };

  

          var View = {

              init : function() {

  

              	this.xhr = null;

  

  				$(document).on( "keyup", "[name $= '[count]']", function(e) {

  					var form = $(this).parents("form");

  					Controll.setData();

  //					console.log(Controll.getData());

  					$("[name $= '[sum_cost]']").each(function(index, el) {

  						var sumCost = Controll.countSumCost(index);

  						$(el).val(sumCost);

  						$($(".t_sum_cost")[index]).text(sumCost);

                      });

  

  					var total_cost = Controll.countTotalCost();

  					$("#order-total").val(total_cost);

  					$(".t_total_cost").text(total_cost);

  //					console.log(total_cost);

  

  					if (View.xhr !== null){

                          View.xhr.abort();

  					}

  

                      View.xhr = $.ajax(

                          "/basket/update",

                          {

                              'data' : form.serializeArray(),

                              'method' : "POST"

                          }

                      )

                                  .done(function(data) {

  //                                    console.log("done");

                                      $(basket_id).html(data);

                                  })

                                  .fail(function() {

  //                                    console.error("ERROR");

                                  })

                                  .always(function() {

  //                                	console.log("xhr to null");

                                      View.xhr = null;

                                  });

  

                  } );

              }

5e4ecece   Виталий   qwerty
176
  

bf807468   Alex Savenko   first commit
177
178
179
180
          };

  

          Controll.init();

  		// frontend calculator

5e4ecece   Виталий   qwerty
181
  	}

bf807468   Alex Savenko   first commit
182
183
  

  })(jQuery);