$(function () { window.config = { api_key: 'woh2boopuphai7ieye6Sexeepequoh8k' // private_listed };
var serviceProviderMap = { 'swisscom': 'Swisscom', 'alex': 'Stadtnetze', 'litex': 'Litex', 'fibx': 'Lenzburg', 'localdb': 'Lokalnetze' };
var serviceTypeMap = { 'adsl': 'DSL Internet', 'vdsl': 'DSL Internet', 'fiber': 'Glasfasernetz Schweizweit', 'xgspon': 'XGS-PON Glasfasernetz', };
// change form fields on search type change $('input[name=search_type]').change(function () { $('div[data-show-if]').hide(); $('div[data-show-if=' + $(this).val() + ']').fadeIn(); $('div.result').hide(); });
// init form fields on load $('input[name=search_type]:checked').change();
// handle form submit $('form').submit(function (e) {
// prevent form submission e.preventDefault();
// trigger error checking on all visible fields $('form input[type=text]:visible').blur();
// if there are form errors show error message if ($('form .has-error:visible').length > 0) { $('div.messagebox').text('Nicht alle Felder ausgefüllt').removeClass('hidden');
// return; } $('div.messagebox').addClass('hidden');
// collect form data var formData = {}; $(this).serializeArray().map(function (item) { formData[item.name] = item.value; });
// disable form, animate refresh icon, change button text while checking $('form input').attr('disabled', 'disabled'); $('form button[type=submit] span.glyphicon').removeClass('hidden').addClass('icon-refresh-animate'); $('form button[type=submit]').attr('disabled', 'disabled').find('span.text').text('Überprüfung läuft...');
// hide any old results $('div.result').hide(); $('#console').html(prettyPrintJson.toHtml({}))
$.ajax({ url: '/api/v2/check/' + formData['search_type'] + '.json', data: $.extend({ api_key: window.config['api_key'] }, formData), success: function (response) { console.log(response);
$('#console').html(prettyPrintJson.toHtml(response))
if (response.success) {
var results = response.results;
var list = '';
$.each(results, function (k, result) { list += '
' + result.address.label + '
'
$.each(result.providers, function (providerName, providerData) { var allProfiles = providerData.profiles;
if (allProfiles.length > 0) { list += '
' + serviceProviderMap[providerName] + '
'; list += '
- ';
- ' + profile.name + '
- ' + profile.name + '
// swisscom groups profiles by service type according to serviceTypeMap if (providerName == 'swisscom') { // remove duplicates because adsl and vdsl are not listed separately $.each(_.groupBy(_.uniqBy(allProfiles, 'customerId'), function (v) { return _.get(serviceTypeMap, v.serviceType, v.serviceType); }), function (serviceLabel, profiles) { list += '
' + serviceLabel + '
'; list += '
- ';
$.each(profiles, function (k, profile) { list += '
' });
list += '
'; }); } // other providers simply display all profiles as list else $.each(allProfiles, function (k, profile) { list += '
' });
list += '
'; } }); });
$('div.result').hide(); $('div.result-list div.hook').empty().append(list); $('div.result-list').removeClass('hidden').hide().fadeIn();
// if there are no results show not found message if (results.length == 0) { $('div.result').hide(); $('div.result-not-found').removeClass('hidden').hide().fadeIn(); } }
// on error show error message else { $('div.result').hide(); $('div.result-error').removeClass('hidden').hide().fadeIn(); } },
// on error show error message error: function (jqXHR) { $('div.result').hide();
// blocked if (jqXHR.status == 420) $('div.result-blocked').removeClass('hidden').hide().fadeIn();
// ratelimited else if (jqXHR.status == 429) $('div.result-ratelimited').removeClass('hidden').hide().fadeIn();
// generic else $('div.result-error').removeClass('hidden').hide().fadeIn(); },
// in any case reset form to be submitted again complete: function () { $('form input').removeAttr('disabled'); $('form button[type=submit] span.glyphicon').addClass('hidden').removeClass('icon-refresh-animate'); $('form button[type=submit]').removeAttr('disabled').find('span.text').text('Überprüfen'); } }) }).on('blur', 'input[type=text]', function () { // on form field blur check if it is empty and mark as error if ($(this).val() == '') $(this).parent().addClass('has-error'); else $(this).parent().removeClass('has-error');
if ($('form .has-error:visible').length == 0) { // remove warning message if there are no more missing fields $('div.messagebox').addClass('hidden'); } }).on('change', 'input[type=text]', function () { // on form field change remove any results $('div.result').hide(); });
$('.debug').click(function () { $('#console').fadeIn(); $('.debug').remove(); }); });