/na!
Created
October 6, 2016 16:08
This file contains hidden or 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
var $actionBox = '#search'; | |
var $contentBox = '#customer'; | |
var $location = window.location; | |
var $hash = $location.hash.replace(/#/g, "").trim(); | |
var $deeplink = getDeepLinkParams($hash); | |
/** Check Deeplink */ | |
if ($deeplink.customer) { | |
applyCustomer(false, $deeplink.customer); | |
} | |
/** | |
* Get DeepLink Params. | |
* -------------------- | |
* | |
* @param $hash | |
* @returns {{}} | |
*/ | |
function getDeepLinkParams($hash) { | |
var $results = {}; | |
var $params = $hash.split('/'); | |
$params.forEach(function ($pair) { | |
if ($pair.trim() != '') { | |
$pair = $pair.split('='); | |
if ($pair[0] && $pair[1]) { | |
$results[$pair[0]] = $pair[1]; | |
} | |
} | |
}); | |
return $results; | |
} | |
/** | |
* Update Deeplink Params. | |
* ---------------------- | |
* | |
* @param $params | |
*/ | |
function updateDeeplink($params) { | |
$.each($params, function ($key, $value) { | |
$deeplink[$key] = $value; | |
}); | |
var $pairs = []; | |
$.each($deeplink, function ($key, $value) { | |
if ($key && $value) { | |
$pairs.push($key + '=' + $value); | |
} | |
}); | |
$location.hash = '/' + $pairs.join('/'); | |
} | |
/** | |
* Show Customer Details. | |
* ---------------------- | |
* | |
*/ | |
$($actionBox).on('click', '.btn-apply', function () { | |
applyCustomer($(this), false); | |
return false; | |
}); | |
/** | |
* Apply Customer. | |
* --------------- | |
* | |
* @param $button | |
* @param $customerId | |
*/ | |
function applyCustomer($button, $customerId) { | |
var $form = $($actionBox).find('form'); | |
if ($button) { | |
var $row = $button.closest('tr'); | |
var $table = $row.closest('table'); | |
$customerId = $row.find('td').first().text().trim(); | |
} | |
/** @todo tabs loop vars */ | |
var $orderBox = '#orders'; | |
var $tab = $($orderBox).find('.tab-data'); | |
var $counter = $($contentBox).find('[href="' + $orderBox + '"] .entry-count'); | |
$.ajax({ | |
url: $form.data('history-url'), | |
method: $form.attr('method'), | |
dataType: 'json', | |
data: {id: $customerId}, | |
beforeSend: function () { | |
/** Clear Boxes */ | |
$($contentBox).hide(); | |
$($contentBox).find('.customer-name').empty(); | |
$tab.empty(); | |
/** Check Active Table Row */ | |
if ($button) { | |
$table.find('td').removeClass('info'); | |
$row.find('td').addClass('info'); | |
} | |
} | |
}).done(function ($response) { | |
if ($response.status) { | |
/** Set Tab counter */ | |
$counter.text($response.count); | |
/** Set Customer Title */ | |
var $customer = $response.customer; | |
var $customerTitle = '<b>' + $customer.number + '</b> - ' + $customer.firstname + " " + $customer.lastname; | |
$($contentBox).find('.customer-name').html($customerTitle); | |
/** Set Tab HTML */ | |
$tab.append($response.html); | |
/** Customer Tabs */ | |
if (!$deeplink.tab) { | |
$deeplink.tab = $('#customer-tabs .active a[data-toggle="tab"]').attr('href').replace(/#/g, ''); | |
} | |
$('#customer-tabs') | |
.find('[href="#' + $deeplink.tab + '"]') | |
.tab('show'); | |
/** Items List Tabs */ | |
var $tabName = $deeplink.tab.substring(0, $deeplink.tab.length - 1); | |
if (!$deeplink.item) { | |
var $items = $('#' + $deeplink.tab).find('[href="#' + $tabName + '_' + $deeplink.item + '"]'); | |
if ($items.size()) { | |
$deeplink.item = $items.first().attr('href').replace(/#/g, ''); | |
} | |
} | |
$('#' + $deeplink.tab).find('[href="#' + $tabName + '_' + $deeplink.item + '"]').tab('show'); | |
/** Update Deeplink */ | |
updateDeeplink({ | |
customer: $customerId | |
}); | |
/** Correct Content Block Position */ | |
$($contentBox).slideDown(function () { | |
$('html, body').animate({ | |
scrollTop: $($contentBox).offset().top | |
}, 500); | |
}); | |
} else { | |
showMessage($response.notify.title, $response.notify.message, $response.notify.trace, "error"); | |
updateDeeplink({ | |
customer: null, | |
tab: null, | |
item: null | |
}); | |
} | |
}).fail(function (jqXHR) { | |
var $trace = "url: " + $form.attr('action') + "<br>" + "response: " + jqXHR.statusText + " [" + jqXHR.status + "]"; | |
showMessage("XHR Error", "", $trace, "error"); | |
}); | |
} | |
/** | |
* @todo don't like a > a ... | |
*/ | |
$('.btn-add-new').on('click', function () { | |
window.location.href = $(this).data('href'); | |
}); | |
/** | |
* Customer Tabs Callbacks. | |
* ------------------------ | |
*/ | |
$(document).on('shown.bs.tab', 'a[data-toggle="tab"]', function ($event) { | |
var $tabType = $($event.target).closest("ul.nav-tabs").data('tabs-type'); | |
var $tabTarget = $($event.target).attr("href").replace(/#/g, '').trim(); | |
if ($tabTarget.indexOf('_') != -1) { | |
$tabTarget = $tabTarget.split('_'); | |
$tabTarget = $tabTarget[$tabTarget.length - 1]; | |
} | |
/** Update Deeplink */ | |
var $data = {}; | |
$data[$tabType] = $tabTarget; | |
/** Clear Item Type */ | |
if ($tabType == 'tab') { | |
$data['item'] = null; | |
} | |
updateDeeplink($data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment