function BasketObj(){ var obj = this; var lock = false; var opened_window = false; this.buffer = {'window_info': {},'widjet': {}} this.basket_namespaces = { 'basket': { 'orders': { 'buttons': { 'del': function(event){obj.del(event);}, 'minus': function(event){obj.minus(event);}, 'plus': function(event){obj.plus(event);} }, 'data': { 'count': function(event){obj.enter(event);}, } }, 'catalog': { 'buttons': { 'put_basket': function(event){obj.put_basket(event);} } } } } this.plus = function(event){ if (lock){return false;} lock = true; var id = event.target.id.split('o_')[1]; var count = parseInt($('#basket\\.orders\\.data\\.count\\.o_' + id).val(),10); if (isNaN(count)){ lock = false; return false; } this.send_request(id, 'recount', count+1); } this.minus = function(event){ if (lock){return false;} lock = true; var id = event.target.id.split('o_')[1]; var count = parseInt($('#basket\\.orders\\.data\\.count\\.o_'+id).val(),10); if (isNaN(count) || count-1<1){ lock = false; return false; } this.send_request(id, 'recount', count-1); } this.enter = function(event){ if (lock){return false;} lock = true; var id = event.target.id.split('o_')[1]; var count = parseInt($('#basket\\.orders\\.data\\.count\\.o_'+id).val(),10); if (isNaN(count) || count - 1 < 1) count = 1; this.send_request(id, 'recount', count); } this.del = function(event){ if (lock){return false;} lock = true; var id = event.target.id.split('o_')[1]; this.send_request(id, 'del'); } this.send_request = function(){ var param = {'do': arguments[1], 'count': arguments[2],'order': arguments[0]} if (arguments[3]) param = $.extend(param, arguments[3]); $.post('/do-basket', param, function(orders){ if ($('#basket\\.main').index('#basket\\.main') != -1) $('#basket\\.main').html(orders); lock = false; obj.get_widjet(); }); } this.init_buttons = function(){ var basket_namespace = {} var line_namespace = next_line_namespace = nsp = ''; var event_type = 'click'; if (arguments[0] != undefined){ basket_namespace = arguments[0]; line_namespace = arguments[1]; }else{ basket_namespace = this.basket_namespaces; } for (var name_space in basket_namespace){ if ((typeof basket_namespace[name_space]) != 'function'){ (line_namespace)? next_line_namespace = line_namespace + '.' + name_space: next_line_namespace = name_space; this.init_buttons(basket_namespace[name_space], next_line_namespace); }else{ //alert(line_namespace + '.' + name_space); nsp = line_namespace + '.' + name_space; nsp = nsp.split('.').join('\\.'); event_type = 'click'; if ($("[id ^= " + nsp + "]").index("input") != -1) event_type = 'change'; $("[id ^= " + nsp + "]").live(event_type, basket_namespace[name_space]); } } } this.open_window = function(event, id, initial_func){ lock = true; opened_window = id; if ($(window).height() < event.pageY) $(id).css('top', (event.pageY - 200) + 'px'); if (initial_func()) $(id).css('display', 'block'); } this.close_window = function(){ $(opened_window).css('display', 'none'); opened_window = false; this.buffer.window_info = {} lock = false; } this.get_widjet = function(){ $.post('/do-basket', {'do': 'get_widjet'}, function(w){ if($('#basket_widjet').index('#basket_widjet') != -1) $('#basket_widjet').html(w); }); } this.del_widjet = function(){ $('#basket_widjet').html(''); } this.put_basket = function(event){ if (lock){return false;} lock = true; var id = event.target.id.split('o_')[1]; this.send_request(id, 'add'); //показывает сообщение о том, что товар добавлен в корзину if ($('.basket_add_info').size()){ $('.basket_add_info').css({'top': event.pageY,'display': 'block'}).fadeOut(3000); } this.get_widjet(); } this.init_buttons(); } function BasketObjExp(){ var lock = false; var obj = this; this.basket_namespaces['basket']['orders']['selects'] = { 'set_material': function(event){obj.set_select(event, 'material');}, 'set_color': function(event){obj.set_select(event, 'color');}, 'set_size': function(event){obj.set_select(event, 'size');} } this.basket_namespaces['basket']['orders']['data']['send_order'] = function(event){obj.send_order(event);} this.set_select = function(event, type){ var value = $(event.target).html(); var id = $(event.target).parent().attr('id').split('o_')[1]; $.post('/do-basket',{'do': 'set_select', 'id': id,'value': value, 'type': type},function(orders){ //alert(orders); if ($('#basket\\.main').index('#basket\\.main') != -1) $('#basket\\.main').html(orders); lock = false; }); } this.send_order = function(event){ var dop_params = {} $('#basket\\.main').find('input').each(function(){ if ($(this).attr('name')) dop_params[$(this).attr('name')] = $(this).val(); }); this.send_request(true, 'send_order', true, dop_params); } this.init_buttons(); } BasketObjExp.prototype = new BasketObj; var basket = new BasketObjExp();