<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<h2>Create your snippet's HTML, CSS and Javascript in the editor tabs</h2>
</div>
</div>
Handlebars.registerHelper('pagination', function (pages, block) {
var ret = '';
for (var i = 1; i <= pages; i++) {
ret += block.fn(i);
}
return ret;
});
var portalSearch = (function () {
var searchType,
searchQuery,
currentPage = 1,
totalPages,
templates = {},
toolbarInit = {},
toolbarDispose = {};
function getSearchResult() {
return $.post('/search/find/', getFiltersValues(), 'json');
}
function onSearchTypeChanged() {
var dispose = toolbarDispose[searchType];
if (typeof (dispose) === 'function') {
dispose();
}
searchType = this.value;
$('.js-search-toolbar').html(templates[searchType].toolbar(searchQuery));
var init = toolbarInit[searchType];
if (typeof (init) === 'function') {
init();
}
onFiltersChanged();
}
toolbarInit['news'] = function () {
function getDate(element) {
var date;
try {
date = $.datepicker.parseDate(dateFormat, element.value);
} catch (error) {
date = null;
}
}
$.getJSON('/news/getNewsFeeds').then(function (response) {
$('.js-search-toolbar .js-category').select2({
data: response.map(function (f) {
return {
id: f.ID,
text: f.Title
}
})
}).on('change', onFiltersChanged);
});
var dateFormat = 'dd.mm.yyyy',
from = $('.js-search-toolbar .js-datepicker-from').datepicker({
defaultDate: 'today'
}).on("change", function () {
to.datepicker("option", "minDate", getDate(this));
onFiltersChanged();
}),
to = $('.js-search-toolbar .js-datepicker-to').datepicker({
defaultDate: 'today'
}).on("change", function () {
from.datepicker("option", "maxDate", getDate(this));
onFiltersChanged();
});
}
toolbarDispose['news'] = function () {
$('.js-search-toolbar .js-category').select2('destroy');
$('.js-search-toolbar .js-datepicker-from').datepicker('destroy');
$('.js-search-toolbar .js-datepicker-to').datepicker('destroy');
}
function onFiltersChanged() {
$('.js-pagination').empty();
onPageChanged(1);
}
function onPageChanged(page) {
currentPage = page;
getSearchResult().then(function (response) {
if (response.Pages > 1) {
totalPages = response.Pages;
$('.js-pagination').html(templates['pagination'](totalPages));
}
var template = templates[searchType].result;
$('.js-search-result').html(template(response.Items));
$('.pag-nav-page__active').removeClass('pag-nav-page__active');
$('.js-page-num').eq(page - 1).addClass('pag-nav-page__active');
});
}
function getFiltersValues() {
var dateFrom, dateTo,
$dateFrom = $('.js-search-toolbar .js-datepicker-from'),
$dateTo = $('.js-search-toolbar .js-datepicker-to');
if ($dateFrom.length) {
dateFrom = $dateFrom.datepicker('getDate');
}
if ($dateTo.length) {
dateTo = $dateTo.datepicker('getDate')
}
searchQuery = $('.js-search-toolbar .js-search-query').val();
return {
searchType: searchType,
page: currentPage,
search: searchQuery,
dateFrom: dateFrom && dateFrom.toISOString(),
dateTo: dateTo && dateTi.toISOString(),
category: $('.js-search-toolbar .js-category').val()
}
}
function initMainSearch(searchType, query) {
$('input[name="js-search-type"]').each(function () {
var type = $(this).val(),
toolbarSource = $('#' + type + 'SearchToolbar').html(),
resultSource = $('#' + type + 'SearchResult').html();
templates[type] = {
toolbar: Handlebars.compile(toolbarSource),
result: Handlebars.compile(resultSource)
}
});
templates['pagination'] = Handlebars.compile($('#paginationTemplate').html());
$('input[name="js-search-type"]').on('change', onSearchTypeChanged);
$('.js-search-toolbar').on('keyup', '.js-search-query', function (e) {
var k = e.keyCode;
if ((k < 48 && k > 90) && (k != 46 || k != 8))
return;
onFiltersChanged();
});
$('.js-pagination').on('click', 'li', function () {
var $src = $(this),
nextPage;
switch (true) {
case $src.hasClass('js-page-prev'):
if (currentPage > 1) {
nextPage = currentPage - 1;
}
break;
case $src.hasClass('js-page-next'):
if (currentPage < totalPages) {
nextPage = currentPage + 1;
}
break;
case $src.hasClass('js-page-num'):
var pageNum = $src.data('page');
if (pageNum != currentPage) {
nextPage = pageNum;
}
break;
}
if (nextPage) {
onPageChanged(nextPage);
}
});
var $type = $('input[name="js-search-type"]');
if(searchType && searchType != 'none') {
$type = $type.filter('[value="' + searchType + '"]');
} else {
$type = $type.first();
}
if (query && query.length) {
searchQuery = query;
}
$type.trigger('click');
}
return {
initMainSearch: initMainSearch,
initGlobalSearch: null
}
})();