Created
October 6, 2016 16:27
-
-
Save frxncisjoseph/f00222fa6fc9e6c3887ffd0ef053b016 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
/** | |
* The RateFinder constructor. | |
* @param {Function} RateFinder. | |
* @constructor | |
*/ | |
var RateFinder = RateFinder || function(){}; | |
/** | |
* The rates array. | |
* @property {Array} rates. | |
*/ | |
var rates = []; | |
/** | |
* Initializes all components within the RateFinder. | |
* @param {Function} init. | |
*/ | |
RateFinder.prototype.init = function() { | |
if (rates.length > 0) | |
rates.splice(0); | |
$.getJSON('./assets/rates.json', function(data) { | |
$.each(data, function(key, rate) { | |
rates.push(rate); | |
}); | |
// Bind events now that the deferred promise has been made. | |
rateFinder.bindEvents(); | |
// Check the hostname starts with the development address. | |
if (window.location.hostname.startsWith('dev.bibitel.com')) | |
console.log('Loaded ' + rates.length + ' rates.'); | |
}); | |
}; | |
/** | |
* Binds all events within the RateFinder. | |
* @param {Function} bindEvents. | |
*/ | |
RateFinder.prototype.bindEvents = function() { | |
var inputElement = $('.rates-input'); | |
///////////////////////////////////////////////////////////////////////// | |
// Initialize the 'autocomplete' event listener for the input element. // | |
///////////////////////////////////////////////////////////////////////// | |
$('#remote_input').autocomplete({source:[{ | |
data: rates, | |
getTitle:function(rate){ | |
return rate.country | |
}, | |
getValue:function(rate){ | |
return rate.country | |
}, | |
valueKey: 'country', | |
}]}).on('selected.xdsoft', function(e, i){ | |
///////////////////////////////////////////////////////// | |
// Hacky code, needs to be replaced at some point. - F // | |
///////////////////////////////////////////////////////// | |
$('.table-rates-container').slideUp('medium', function() { | |
$('.table-rates-container td:not(.table-rates-container-component)').empty(); | |
// Show the rate using the parsed item. | |
rateFinder.showRate(i); | |
}); | |
}); | |
////////////////////////////////////////////////////////////////// | |
// Initialize the 'click' event listener for the input element. // | |
////////////////////////////////////////////////////////////////// | |
$('button#open').click(function(){ | |
$('#remote_input').trigger('open'); | |
$('#remote_input').focus(); | |
}); | |
////////////////////////////////////////////////////////////////// | |
// Initialize the 'keyup' event listener for the input element. // | |
////////////////////////////////////////////////////////////////// | |
inputElement.keyup(function(e) { | |
if (e.keyCode == 8) | |
// Hide the existing rate. | |
rateFinder.hideRate(); | |
}); | |
}; | |
/** | |
* Shows a rate using the specified data. | |
* @property {Function} showRate. | |
* @param {String} rate. | |
*/ | |
RateFinder.prototype.showRate = function(rate) { | |
var mobileRateElement = $('.table-rates-container .mobile-rates td'); | |
var landlineRateElement = $('.table-rates-container .landline-rates td'); | |
var providerIndex = 1; | |
for (var provider in rate.providers) { | |
var mobileRate = rate.providers[provider].mobile; | |
var landlineRate = rate.providers[provider].landline; | |
// Check if the rate parsed is an integer. | |
if (!isNaN(mobileRate)) mobileRate += "p"; | |
if (!isNaN(landlineRate)) landlineRate += "p"; | |
mobileRateElement.eq(providerIndex).append(mobileRate); | |
landlineRateElement.eq(providerIndex).append(landlineRate); | |
providerIndex++; | |
} | |
// Set the visibility of the table rates component element to hidden. | |
$('.table-rates-container-component:last-child a span').css('visibility','hidden'); | |
$('.bundles-container .bundle').each(function(i,e) { | |
// Check if the rate parsed currently has a relative bundle. | |
if ($(e).find('h2').text().toLowerCase().indexOf(rate.country) != -1) { | |
$('.table-rates-container-component:last-child a span').css('visibility','initial'); | |
} | |
}); | |
$('.table-rates-container').slideDown('medium'); | |
}; | |
/** | |
* Hies a rate using the specified data. | |
* @property {Function} showRate. | |
* @param {String} rate. | |
*/ | |
RateFinder.prototype.hideRate = function() { | |
$('.table-rates-container').slideUp('medium', function() { | |
$('.table-rates-container td:not(.table-rates-container-component)').empty(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment