shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

common.js (13859B)


      1 function getURLVar(key) {
      2 	var value = [];
      3 
      4 	var query = String(document.location).split('?');
      5 
      6 	if (query[1]) {
      7 		var part = query[1].split('&');
      8 
      9 		for (i = 0; i < part.length; i++) {
     10 			var data = part[i].split('=');
     11 
     12 			if (data[0] && data[1]) {
     13 				value[data[0]] = data[1];
     14 			}
     15 		}
     16 
     17 		if (value[key]) {
     18 			return value[key];
     19 		} else {
     20 			return '';
     21 		}
     22 	}
     23 }
     24 
     25 $(document).ready(function() {
     26 	// Highlight any found errors
     27 	$('.text-danger').each(function() {
     28 		var element = $(this).parent().parent();
     29 
     30 		if (element.hasClass('form-group')) {
     31 			element.addClass('has-error');
     32 		}
     33 	});
     34 
     35 	// Currency
     36 	$('#form-currency .currency-select').on('click', function(e) {
     37 		e.preventDefault();
     38 
     39 		$('#form-currency input[name=\'code\']').val($(this).attr('name'));
     40 
     41 		$('#form-currency').submit();
     42 	});
     43 
     44 	// Language
     45 	$('#form-language .language-select').on('click', function(e) {
     46 		e.preventDefault();
     47 
     48 		$('#form-language input[name=\'code\']').val($(this).attr('name'));
     49 
     50 		$('#form-language').submit();
     51 	});
     52 
     53 	/* Search */
     54 	$('#search input[name=\'search\']').parent().find('button').on('click', function() {
     55 		var url = $('base').attr('href') + 'index.php?route=product/search';
     56 
     57 		var value = $('header #search input[name=\'search\']').val();
     58 
     59 		if (value) {
     60 			url += '&search=' + encodeURIComponent(value);
     61 		}
     62 
     63 		location = url;
     64 	});
     65 
     66 	$('#search input[name=\'search\']').on('keydown', function(e) {
     67 		if (e.keyCode == 13) {
     68 			$('header #search input[name=\'search\']').parent().find('button').trigger('click');
     69 		}
     70 	});
     71 
     72 	// Menu
     73 	$('#menu .dropdown-menu').each(function() {
     74 		var menu = $('#menu').offset();
     75 		var dropdown = $(this).parent().offset();
     76 
     77 		var i = (dropdown.left + $(this).outerWidth()) - (menu.left + $('#menu').outerWidth());
     78 
     79 		if (i > 0) {
     80 			$(this).css('margin-left', '-' + (i + 10) + 'px');
     81 		}
     82 	});
     83 
     84 	// Product List
     85 	$('#list-view').click(function() {
     86 		$('#content .product-grid > .clearfix').remove();
     87 
     88 		$('#content .row > .product-grid').attr('class', 'product-layout product-list col-xs-12');
     89 		$('#grid-view').removeClass('active');
     90 		$('#list-view').addClass('active');
     91 
     92 		localStorage.setItem('display', 'list');
     93 	});
     94 
     95 	// Product Grid
     96 	$('#grid-view').click(function() {
     97 		// What a shame bootstrap does not take into account dynamically loaded columns
     98 		var cols = $('#column-right, #column-left').length;
     99 
    100 		if (cols == 2) {
    101 			$('#content .product-list').attr('class', 'product-layout product-grid col-lg-6 col-md-6 col-sm-12 col-xs-12');
    102 		} else if (cols == 1) {
    103 			$('#content .product-list').attr('class', 'product-layout product-grid col-lg-4 col-md-4 col-sm-6 col-xs-12');
    104 		} else {
    105 			$('#content .product-list').attr('class', 'product-layout product-grid col-lg-3 col-md-3 col-sm-6 col-xs-12');
    106 		}
    107 
    108 		$('#list-view').removeClass('active');
    109 		$('#grid-view').addClass('active');
    110 
    111 		localStorage.setItem('display', 'grid');
    112 	});
    113 
    114 	if (localStorage.getItem('display') == 'list') {
    115 		$('#list-view').trigger('click');
    116 		$('#list-view').addClass('active');
    117 	} else {
    118 		$('#grid-view').trigger('click');
    119 		$('#grid-view').addClass('active');
    120 	}
    121 
    122 	// Checkout
    123 	$(document).on('keydown', '#collapse-checkout-option input[name=\'email\'], #collapse-checkout-option input[name=\'password\']', function(e) {
    124 		if (e.keyCode == 13) {
    125 			$('#collapse-checkout-option #button-login').trigger('click');
    126 		}
    127 	});
    128 
    129 	// tooltips on hover
    130 	$('[data-toggle=\'tooltip\']').tooltip({container: 'body'});
    131 
    132 	// Makes tooltips work on ajax generated content
    133 	$(document).ajaxStop(function() {
    134 		$('[data-toggle=\'tooltip\']').tooltip({container: 'body'});
    135 	});
    136 });
    137 
    138 // Cart add remove functions
    139 var cart = {
    140 	'add': function(product_id, quantity) {
    141 		$.ajax({
    142 			url: 'index.php?route=checkout/cart/add',
    143 			type: 'post',
    144 			data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
    145 			dataType: 'json',
    146 			beforeSend: function() {
    147 				$('#cart > button').button('loading');
    148 			},
    149 			complete: function() {
    150 				$('#cart > button').button('reset');
    151 			},
    152 			success: function(json) {
    153 				$('.alert-dismissible, .text-danger').remove();
    154 
    155 				if (json['redirect']) {
    156 					location = json['redirect'];
    157 				}
    158 
    159 				if (json['success']) {
    160 					$('#content').parent().before('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
    161 
    162 					// Need to set timeout otherwise it wont update the total
    163 					setTimeout(function () {
    164 						$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
    165 					}, 100);
    166 
    167 					$('html, body').animate({ scrollTop: 0 }, 'slow');
    168 
    169 					$('#cart > ul').load('index.php?route=common/cart/info ul li');
    170 				}
    171 			},
    172 			error: function(xhr, ajaxOptions, thrownError) {
    173 				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    174 			}
    175 		});
    176 	},
    177 	'update': function(key, quantity) {
    178 		$.ajax({
    179 			url: 'index.php?route=checkout/cart/edit',
    180 			type: 'post',
    181 			data: 'key=' + key + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
    182 			dataType: 'json',
    183 			beforeSend: function() {
    184 				$('#cart > button').button('loading');
    185 			},
    186 			complete: function() {
    187 				$('#cart > button').button('reset');
    188 			},
    189 			success: function(json) {
    190 				// Need to set timeout otherwise it wont update the total
    191 				setTimeout(function () {
    192 					$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
    193 				}, 100);
    194 
    195 				if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
    196 					location = 'index.php?route=checkout/cart';
    197 				} else {
    198 					$('#cart > ul').load('index.php?route=common/cart/info ul li');
    199 				}
    200 			},
    201 			error: function(xhr, ajaxOptions, thrownError) {
    202 				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    203 			}
    204 		});
    205 	},
    206 	'remove': function(key) {
    207 		$.ajax({
    208 			url: 'index.php?route=checkout/cart/remove',
    209 			type: 'post',
    210 			data: 'key=' + key,
    211 			dataType: 'json',
    212 			beforeSend: function() {
    213 				$('#cart > button').button('loading');
    214 			},
    215 			complete: function() {
    216 				$('#cart > button').button('reset');
    217 			},
    218 			success: function(json) {
    219 				// Need to set timeout otherwise it wont update the total
    220 				setTimeout(function () {
    221 					$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
    222 				}, 100);
    223 
    224 				if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
    225 					location = 'index.php?route=checkout/cart';
    226 				} else {
    227 					$('#cart > ul').load('index.php?route=common/cart/info ul li');
    228 				}
    229 			},
    230 			error: function(xhr, ajaxOptions, thrownError) {
    231 				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    232 			}
    233 		});
    234 	}
    235 }
    236 
    237 var voucher = {
    238 	'add': function() {
    239 
    240 	},
    241 	'remove': function(key) {
    242 		$.ajax({
    243 			url: 'index.php?route=checkout/cart/remove',
    244 			type: 'post',
    245 			data: 'key=' + key,
    246 			dataType: 'json',
    247 			beforeSend: function() {
    248 				$('#cart > button').button('loading');
    249 			},
    250 			complete: function() {
    251 				$('#cart > button').button('reset');
    252 			},
    253 			success: function(json) {
    254 				// Need to set timeout otherwise it wont update the total
    255 				setTimeout(function () {
    256 					$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
    257 				}, 100);
    258 
    259 				if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
    260 					location = 'index.php?route=checkout/cart';
    261 				} else {
    262 					$('#cart > ul').load('index.php?route=common/cart/info ul li');
    263 				}
    264 			},
    265 			error: function(xhr, ajaxOptions, thrownError) {
    266 				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    267 			}
    268 		});
    269 	}
    270 }
    271 
    272 var wishlist = {
    273 	'add': function(product_id) {
    274 		$.ajax({
    275 			url: 'index.php?route=account/wishlist/add',
    276 			type: 'post',
    277 			data: 'product_id=' + product_id,
    278 			dataType: 'json',
    279 			success: function(json) {
    280 				$('.alert-dismissible').remove();
    281 
    282 				if (json['redirect']) {
    283 					location = json['redirect'];
    284 				}
    285 
    286 				if (json['success']) {
    287 					$('#content').parent().before('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
    288 				}
    289 
    290 				$('#wishlist-total span').html(json['total']);
    291 				$('#wishlist-total').attr('title', json['total']);
    292 
    293 				$('html, body').animate({ scrollTop: 0 }, 'slow');
    294 			},
    295 			error: function(xhr, ajaxOptions, thrownError) {
    296 				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    297 			}
    298 		});
    299 	},
    300 	'remove': function() {
    301 
    302 	}
    303 }
    304 
    305 var compare = {
    306 	'add': function(product_id) {
    307 		$.ajax({
    308 			url: 'index.php?route=product/compare/add',
    309 			type: 'post',
    310 			data: 'product_id=' + product_id,
    311 			dataType: 'json',
    312 			success: function(json) {
    313 				$('.alert-dismissible').remove();
    314 
    315 				if (json['success']) {
    316 					$('#content').parent().before('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
    317 
    318 					$('#compare-total').html(json['total']);
    319 
    320 					$('html, body').animate({ scrollTop: 0 }, 'slow');
    321 				}
    322 			},
    323 			error: function(xhr, ajaxOptions, thrownError) {
    324 				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    325 			}
    326 		});
    327 	},
    328 	'remove': function() {
    329 
    330 	}
    331 }
    332 
    333 /* Agree to Terms */
    334 $(document).delegate('.agree', 'click', function(e) {
    335 	e.preventDefault();
    336 
    337 	$('#modal-agree').remove();
    338 
    339 	var element = this;
    340 
    341 	$.ajax({
    342 		url: $(element).attr('href'),
    343 		type: 'get',
    344 		dataType: 'html',
    345 		success: function(data) {
    346 			html  = '<div id="modal-agree" class="modal">';
    347 			html += '  <div class="modal-dialog">';
    348 			html += '    <div class="modal-content">';
    349 			html += '      <div class="modal-header">';
    350 			html += '        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>';
    351 			html += '        <h4 class="modal-title">' + $(element).text() + '</h4>';
    352 			html += '      </div>';
    353 			html += '      <div class="modal-body">' + data + '</div>';
    354 			html += '    </div>';
    355 			html += '  </div>';
    356 			html += '</div>';
    357 
    358 			$('body').append(html);
    359 
    360 			$('#modal-agree').modal('show');
    361 		}
    362 	});
    363 });
    364 
    365 // Autocomplete */
    366 (function($) {
    367 	$.fn.autocomplete = function(option) {
    368 		return this.each(function() {
    369 			this.timer = null;
    370 			this.items = new Array();
    371 
    372 			$.extend(this, option);
    373 
    374 			$(this).attr('autocomplete', 'off');
    375 
    376 			// Focus
    377 			$(this).on('focus', function() {
    378 				this.request();
    379 			});
    380 
    381 			// Blur
    382 			$(this).on('blur', function() {
    383 				setTimeout(function(object) {
    384 					object.hide();
    385 				}, 200, this);
    386 			});
    387 
    388 			// Keydown
    389 			$(this).on('keydown', function(event) {
    390 				switch(event.keyCode) {
    391 					case 27: // escape
    392 						this.hide();
    393 						break;
    394 					default:
    395 						this.request();
    396 						break;
    397 				}
    398 			});
    399 
    400 			// Click
    401 			this.click = function(event) {
    402 				event.preventDefault();
    403 
    404 				value = $(event.target).parent().attr('data-value');
    405 
    406 				if (value && this.items[value]) {
    407 					this.select(this.items[value]);
    408 				}
    409 			}
    410 
    411 			// Show
    412 			this.show = function() {
    413 				var pos = $(this).position();
    414 
    415 				$(this).siblings('ul.dropdown-menu').css({
    416 					top: pos.top + $(this).outerHeight(),
    417 					left: pos.left
    418 				});
    419 
    420 				$(this).siblings('ul.dropdown-menu').show();
    421 			}
    422 
    423 			// Hide
    424 			this.hide = function() {
    425 				$(this).siblings('ul.dropdown-menu').hide();
    426 			}
    427 
    428 			// Request
    429 			this.request = function() {
    430 				clearTimeout(this.timer);
    431 
    432 				this.timer = setTimeout(function(object) {
    433 					object.source($(object).val(), $.proxy(object.response, object));
    434 				}, 200, this);
    435 			}
    436 
    437 			// Response
    438 			this.response = function(json) {
    439 				html = '';
    440 
    441 				if (json.length) {
    442 					for (i = 0; i < json.length; i++) {
    443 						this.items[json[i]['value']] = json[i];
    444 					}
    445 
    446 					for (i = 0; i < json.length; i++) {
    447 						if (!json[i]['category']) {
    448 							html += '<li data-value="' + json[i]['value'] + '"><a href="#">' + json[i]['label'] + '</a></li>';
    449 						}
    450 					}
    451 
    452 					// Get all the ones with a categories
    453 					var category = new Array();
    454 
    455 					for (i = 0; i < json.length; i++) {
    456 						if (json[i]['category']) {
    457 							if (!category[json[i]['category']]) {
    458 								category[json[i]['category']] = new Array();
    459 								category[json[i]['category']]['name'] = json[i]['category'];
    460 								category[json[i]['category']]['item'] = new Array();
    461 							}
    462 
    463 							category[json[i]['category']]['item'].push(json[i]);
    464 						}
    465 					}
    466 
    467 					for (i in category) {
    468 						html += '<li class="dropdown-header">' + category[i]['name'] + '</li>';
    469 
    470 						for (j = 0; j < category[i]['item'].length; j++) {
    471 							html += '<li data-value="' + category[i]['item'][j]['value'] + '"><a href="#">&nbsp;&nbsp;&nbsp;' + category[i]['item'][j]['label'] + '</a></li>';
    472 						}
    473 					}
    474 				}
    475 
    476 				if (html) {
    477 					this.show();
    478 				} else {
    479 					this.hide();
    480 				}
    481 
    482 				$(this).siblings('ul.dropdown-menu').html(html);
    483 			}
    484 
    485 			$(this).after('<ul class="dropdown-menu"></ul>');
    486 			$(this).siblings('ul.dropdown-menu').delegate('a', 'click', $.proxy(this.click, this));
    487 
    488 		});
    489 	}
    490 })(window.jQuery);