Subversion Repositories SmartDukaan

Rev

Rev 17774 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
17766 manish.sha 1
$('.itemquantity').attr('disabled', 'true');
2
var cart_details = localStorage.getItem('cart_details');
3
if(cart_details==undefined){
4
	$(".pmfooter span.carttotalval").text('0');
5
	$(".pmfooter span.badge").text('0');
6
	$(".pmfooter").addClass('hidden');
7
}else{
8
	var cartDetailsObj = JSON.parse(cart_details);
9
	if(cartDetailsObj.totalCartQuantity > 0){
10
		$(".pmfooter span.badge").text(cartDetailsObj.totalCartQuantity.toString());
11
		var totalCartValue = cartDetailsObj.totalCartValue;
12
		var priceChangeMap = {};
13
		$.each(cartDetailsObj.cartItems, function(key,val) {
14
			var newUnitPrice = $(".number-spinner button[data-id='"+key+"']").data('price');
15
			$(".number-spinner button[data-id='"+key+"']").closest('.number-spinner').find('input').val(val.quantity);
16
			if(parseInt(newUnitPrice)!=parseInt(val.unitprice)){
17
				totalCartValue = totalCartValue - (parseInt(val.unitprice)*parseInt(val.quantity)) + (parseInt(newUnitPrice)*parseInt(val.quantity));
18
				priceChangeMap[key] = newUnitPrice;
19
			}
20
		});
21
		$(".pmfooter span.carttotalval").text(numberWithCommas(totalCartValue));
22
		cartDetailsObj.totalCartValue = totalCartValue;
23
		var cartItems = cartDetailsObj.cartItems;
24
		$.each(priceChangeMap, function(key,val) {
25
			cartItems[sku.toString()].unitprice = unitPrice;
26
		});
27
		cartDetailsObj.cartItems = cartItems;
28
		localStorage.setItem('cart_details',JSON.stringify(cartDetailsObj));
29
		$(".pmfooter").removeClass('hidden');
30
	}else{
31
		$(".pmfooter span.carttotalval").text('0');
32
		$(".pmfooter span.badge").text('0');
33
		$(".pmfooter").addClass('hidden');
34
	}
35
 
36
}
37
$(".number-spinner button").click(function () {
38
    btn = $(this);
39
    input = btn.closest('.number-spinner').find('input');
40
    oldValue = btn.closest('.number-spinner').find('input').val().trim(),
41
	newVal = 0;
42
    btn.closest('.number-spinner').find('button').prop("disabled", false);
43
    var totalCartQuantity = 0;
44
    var totalCartValue = 0;
45
    var cartDetailsObj; 
46
    var jsonObjToBeStored = {};
47
    var cartItems = {};
48
    var cart_details = localStorage.getItem('cart_details');
49
    if(cart_details!=undefined){
50
    	cartDetailsObj = JSON.parse(cart_details);
51
    	totalCartQuantity = cartDetailsObj.totalCartQuantity;
52
    	totalCartValue = cartDetailsObj.totalCartValue;
53
    	cartItems = cartDetailsObj.cartItems;
54
    }
55
    var inc = 0;
56
    var dec = 0;
57
    var sku = btn.data('id');
58
    var unitPrice = btn.data('price');
59
    var prodname = $(".number-spinner").data('name');
60
    var brandname = $(".number-spinner").data('brand');
61
    if (btn.attr('data-dir') == 'up') {
62
		if ( input.attr('max') == undefined || parseInt(input.val()) < parseInt(input.attr('max')) ) {
63
			if(parseInt(input.val())<5){
64
				inc = 1;
65
				newVal = parseInt(oldValue) + inc;
66
			}else{
67
				inc = 5-(parseInt(input.val())%5);
68
				newVal = parseInt(oldValue) + inc;
69
			}
70
		}else{
71
			newVal = parseInt(oldValue);
72
			btn.prop("disabled", true);
73
		}
74
		totalCartQuantity = totalCartQuantity + inc;
75
	} else {
76
		if ( input.attr('min') == undefined || parseInt(input.val()) > parseInt(input.attr('min')) ) {
77
		    dec = 1;
78
			newVal = parseInt(oldValue) - dec;
79
		}else{
80
			btn.prop("disabled", true);
81
		}
82
		totalCartQuantity = totalCartQuantity - dec;
83
	}
84
 
85
	btn.closest('.number-spinner').find('input').val(newVal);
86
	if(cartDetailsObj==undefined){
87
		jsonObjToBeStored['totalCartQuantity'] = totalCartQuantity;
88
		totalCartValue = totalCartValue + (unitPrice * newVal);
89
		jsonObjToBeStored['totalCartValue'] = totalCartValue;
90
		var itemDetail = {};
91
		itemDetail['quantity']=newVal;
92
		itemDetail['unitprice']=unitPrice;
93
		itemDetail['productname']=prodname;
94
		itemDetail['brand']=brandname;
95
		cartItems[sku.toString()]=itemDetail;
96
		jsonObjToBeStored['cartItems']=cartItems;
97
		localStorage.setItem('cart_details',JSON.stringify(jsonObjToBeStored));
98
	}else{
99
		cartDetailsObj.totalCartQuantity = totalCartQuantity;
100
		var itemDetail = cartItems[sku.toString()];
101
		if(itemDetail==undefined){
102
			var itemDetail = {};
103
			itemDetail['quantity']=newVal;
104
			itemDetail['unitprice']=unitPrice;
105
			itemDetail['productname']=prodname;
106
			itemDetail['brand']=brandname;
107
			totalCartValue = totalCartValue + (unitPrice * newVal);
108
			cartItems[sku.toString()]=itemDetail;
109
		}else{
110
			totalCartValue = totalCartValue - (cartItems[sku.toString()].quantity * cartItems[sku.toString()].unitprice) + (unitPrice * newVal);
111
			if(newVal==0){
112
				delete cartItems[sku.toString()];
113
			}else{
114
				cartItems[sku.toString()].quantity = newVal;
115
				cartItems[sku.toString()].unitprice = unitPrice;
116
			}
117
 
118
		}
119
		cartDetailsObj.totalCartValue = totalCartValue;
120
		cartDetailsObj.cartItems = cartItems;
121
		localStorage.setItem('cart_details',JSON.stringify(cartDetailsObj));
122
	}
123
	if(totalCartQuantity > 0){
124
		$(".pmfooter span.carttotalval").text(numberWithCommas(totalCartValue));
125
		$(".pmfooter span.badge").text(totalCartQuantity);
126
		$(".pmfooter").removeClass('hidden');
127
	}else{
128
		$(".pmfooter span.carttotalval").text('0');
129
		$(".pmfooter span.badge").text('0');
130
		$(".pmfooter").addClass('hidden');
131
	}
132
});
133
 
134
$(document).on('click','.see_more_colors',function(){
135
	$('.morecoloroptions_'+$(this).data('id')).toggleClass('hidden');
136
});
137
 
138
$(".button-checkbox > .btn").click(function(){
139
    $(this).addClass('colorbtn active').siblings().removeClass('colorbtn active');
140
    $(this).addClass().removeClass('btn-default');
141
});
142
 
143
$("#bottomNav").click(function(){
144
	alert(me);
145
    var url = apihost + 'cartdetails/?user_id='+me;
146
    var newForm = $('<form>', {
147
        'action': url,
148
        'method':'post'
149
    }).append($('<input>', {
150
        'name': 'cart_details',
151
        'value': localStorage.getItem('cart_details'),
152
        'type': 'hidden'
153
    }));
154
    newForm.submit();
155
});
156
 
157
function numberWithCommas(x) {
158
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
159
}
160
/*
161
$('.button-checkbox').each(function () {
162
 
163
    // Settings
164
    var $widget = $(this),
165
        $button = $widget.find('button'),
166
        $checkbox = $widget.find('input:checkbox'),
167
        color = $button.data('color'),
168
        settings = {
169
            on: {
170
                icon: 'glyphicon glyphicon-check'
171
            },
172
            off: {
173
                icon: 'glyphicon glyphicon-unchecked'
174
            }
175
        };
176
 
177
    // Event Handlers
178
    $button.on('click', function () {
179
        $checkbox.prop('checked', !$checkbox.is(':checked'));
180
        $checkbox.triggerHandler('change');
181
        updateDisplay();
182
    });
183
    $checkbox.on('change', function () {
184
        updateDisplay();
185
    });
186
 
187
    // Actions
188
    function updateDisplay() {
189
        var isChecked = $checkbox.is(':checked');
190
 
191
        // Set the button's state
192
        $button.data('state', (isChecked) ? "on" : "off");
193
 
194
        // Set the button's icon
195
        $button.find('.state-icon')
196
            .removeClass()
197
            .addClass('state-icon ' + settings[$button.data('state')].icon);
198
 
199
        // Update the button's color
200
        if (isChecked) {
201
            $button
202
                .removeClass('btn-default')
203
                .addClass('colorbtn active');
204
        }
205
        else {
206
            $button
207
                .removeClass('colorbtn active')
208
                .addClass('btn-default');
209
        }
210
    }
211
 
212
    // Initialization
213
    function init() {
214
 
215
        updateDisplay();
216
 
217
        // Inject the icon if applicable
218
        if ($button.find('.state-icon').length == 0) {
219
            $button.prepend('<i class="state-icon ' + settings[$button.data('state')].icon + '"></i>');
220
        }
221
    }
222
    init();
223
});*/