// ' +
'
' +
'
' +
'

' +
'
' +
'
' +
'
' + user.name.toLowerCase() + '
' +
'
' +
'' + user.location + '' +
'
' +
'
' +
'' + user.certificationLevel + ' Certified' +
'
' +
'
' +
'
' +
'
' +
'';
}
var FancyList = function (obj) {
var parent = this;
this.containerId = '#' + obj.containerId;
this.dataURL = obj.dataURL;
this.data = obj.data;
this.template = obj.template;
this.perPage = obj.perPage;
this.filters = obj.filters;
this.page = 1;
parent.filterValues = [];
parent.systemGeneratedAll = 'All-System-Generated-^&!&HEwpzY3';
var cAjax = function (obj, loading, logInRequired) {
var isUserLoggedIn = Drupal.settings.username;
if ((logInRequired && isUserLoggedIn) || !logInRequired) {
if (parent.multipart && obj['type'] == "POST") {
return $.ajax({
dataType: obj['dataType'] ? obj['dataType'] : "JSON",
url: obj['url'],
type: obj['type'] ? obj['type'] : "GET",
data: obj['data'],
retryCount: obj['retryCount'] ? obj['retryCount'] : 0,
retryLimit: 5,
processData: false,
contentType: false,
success: function (data) {
if (data.status === 'rate_limit_exceeded') {
window.location.reload();
} else if (typeof obj['success'] == 'function') {
obj['success'](data);
}
//todo:add loaders
//parent.set('loading', false);
},
error: function (data) {
if (data.status >= 499 && this.retryCount < this.retryLimit) {
obj['retryCount'] = obj['retryCount'] ? obj['retryCount'] +
1 : 1;
setTimeout(function () {
cAjax(obj, loading, logInRequired);
}, 2000);
} else {
if (typeof obj['error'] == 'function') {
obj['error'](data);
}
}
//parent.set('loading', false);
}
});
} else {
return $.ajax({
dataType: obj['dataType'] ? obj['dataType'] : "JSON",
url: obj['url'],
type: obj['type'] ? obj['type'] : "GET",
data: obj['data'],
retryCount: obj['retryCount'] ? obj['retryCount'] : 0,
retryLimit: 5,
success: function (data) {
if (typeof obj['success'] == 'function') {
obj['success'](data);
}
//todo:add loaders
//parent.set('loading', false);
},
error: function (data) {
if (data.status >= 499 && this.retryCount < this.retryLimit) {
obj['retryCount'] = obj['retryCount'] ? obj['retryCount'] +
1 : 1;
setTimeout(function () {
cAjax(obj, loading, logInRequired);
}, 2000);
} else {
if (typeof obj['error'] == 'function') {
obj['error'](data);
}
}
//parent.set('loading', false);
}
});
}
}
};
this.cAjax = cAjax;
this.getContainerHTML = function () {
return '
' +
'
' +
'';
};
this.fetchData = function () {
var parent = this;
this.cAjax({
url: parent.dataURL,
error: parent.errorP,
success: parent.successP
}, true, false, false);
};
this.successP = function (data) {
parent.data = data;
parent.printSkeleton(true);
parent.bindListeners();
parent.printData();
};
this.errorP = function () {
console.log("Died, can't get data");
};
this.filterData = function (filterValues, data) {
var filteredData = [];
if (filterValues.length) {
$.each(data, function (indexO, value) {
var present = true;
$.each(filterValues, function (indexI, filter) {
present = present && value[filter.key] == filter.value;
if (!present) {
return;
}
});
if (present) {
filteredData.push(value);
}
});
return filteredData;
}
return data;
};
this.paginateData = function (page, data) {
var startIndex = (page - 1) * this.perPage;
var paginatedData = [];
for (var i = startIndex; i < Math.min(data.length, startIndex + this.perPage); i++) {
paginatedData.push(data[i]);
}
return paginatedData;
};
this.getData = function () {
var filterValues = parent.filterValues;
var data = this.data;
var page = this.page;
parent.filteredData = this.filterData(filterValues, data);
data = this.paginateData(page, parent.filteredData);
return data;
};
this.unbindListeners = function () {
this.unbindPagination();
this.unbindFilters();
};
this.getPaginatorHTML = function () {
var data = parent.filteredData;
var paginatorHTML = '';
var maxPage = parseInt(data.length / this.perPage);
if (data.length > 0) {
paginatorHTML += '
';
}
if (this.page >= 2) {
var previousPage = this.page - 1;
paginatorHTML += '
';
}
if (this.page > maxPage && this.page != 1) {
this.page = 1;
this.printData();
return;
}
for (var i = 1; i <= Math.min(5, maxPage); i++) {
paginatorHTML += '
' + i + '';
}
if (this.page > Math.min(5, maxPage)) {
paginatorHTML += ' ..
' + this.page + '';
}
if (this.page < maxPage) {
var nextPage = parseInt(this.page) + 1;
paginatorHTML += '
';
}
if (data.length > 0) {
paginatorHTML += '
';
}
if (maxPage <= 1) {
paginatorHTML = '';
}
return paginatorHTML;
};
this.printPaginator = function () {
var paginatorHTML = this.getPaginatorHTML();
$(this.containerId + ' > .paginator').html(paginatorHTML);
};
this.printData = function () {
var data = this.getData();
$(parent.containerId + ' > .cards').html('');
$.each(data, function (index, value) {
$(parent.containerId + ' > .cards').append(parent.template(value));
});
this.printPaginator();
this.unbindListeners();
this.bindListeners();
$('.page__loader').hide();
$('html, body').animate({
scrollTop: $(parent.containerId).offset().top
}, 800);
};
this.getDistinctFilterValues = function (filter) {
var values = [];
var map = {};
$.each(this.data, function (index, value) {
if (!map.hasOwnProperty(value[filter]) && value[filter] != null) {
map[value[filter]] = true;
values.push(value[filter]);
}
});
return values;
};
this.getFiltersHTML = function () {
var filters = this.filters;
var filterHTML = '';
filterHTML +=
'
' +
'
' +
'
';
$.each(filters, function (index, filter) {
var filterValues = parent.getDistinctFilterValues(filter.name);
filterHTML += '
';
filterHTML += '
';
filterHTML += '
' + filter.title + '
';
if (filter.type == "radio") {
filterHTML += '' +
'All ';
$.each(filterValues, function (indexI, value) {
filterHTML += '' +
'' + value + ' ';
});
} else if (filter.type == "select") {
filterValues.sort();
filterHTML += '';
filterHTML += 'All';
$.each(filterValues, function (indexI, value) {
filterHTML += '' + value +
'';
});
filterHTML += '';
}
filterHTML += '' +
'
' +
'
';
});
filterHTML +=
'
Clear Filter' +
'
' +
'
';
return filterHTML;
};
this.printSkeleton = function (withFilter) {
var html = '';
if (withFilter) {
html += this.getFiltersHTML();
}
html += this.getContainerHTML();
$(this.containerId).html(html);
$(this.containerId + ' .knowmore').unbind('click');
$(this.containerId + ' .knowmore').click(function (event) {
event.preventDefault();
var that = this;
var filterName = $(this).attr('data-name');
for (var i = 0; i < parent.filters.length; i++) {
var value = parent.filters[i];
if (value.name == filterName) {
if (typeof value.knowMoreOnclick == "function") {
value.knowMoreOnclick(that);
}
break;
}
}
});
if ($.fn.chosen) {
$(this.containerId + ' .filter.select').chosen();
}
};
this.bindPagination = function () {
var parent = this;
$(this.containerId + ' .paginator .page').click(function (event) {
event.preventDefault();
parent.page = parseInt($(this).attr('data-page'));
parent.printData();
});
};
this.unbindPagination = function () {
$(this.containerId + ' .pages').unbind('click');
};
this.bindFilters = function () {
$(this.containerId + ' .filter').change(function () {
var name = $(this).attr('name');
var val = $(this).val();
var found = false;
var resetPage = false;
var index = -1;
for (var i = 0; i < parent.filterValues.length; i++) {
var current = parent.filterValues[i];
if (current.key == name) {
found = true;
if (current.val != val) {
parent.filterValues[i] = {
key: name,
value: val
};
resetPage = true;
index = i;
break;
}
}
}
if (!found) {
parent.filterValues.push({
key: name,
value: val
});
resetPage = true;
index = parent.filterValues.length - 1;
}
if (val == parent.systemGeneratedAll && index != -1) {
parent.filterValues.splice(index, 1);
}
if (resetPage) {
parent.page = 1;
}
parent.printData();
});
$(this.containerId + ' .filter.clear').click(function (event) {
event.preventDefault();
var filters = parent.filters;
$.each(filters, function (index, filter) {
if (filter.type == 'select') {
$(parent.containerId).find('select[name=' + filter.name +
'].filter').prop('selectedIndex', 0);
if ($.fn.chosen) {
$(parent.containerId).find('select[name=' + filter.name +
'].filter').trigger('chosen:updated');
}
} else if (filter.type == 'radio') {
$(parent.containerId).find('input[name=' + filter.name +
'].filter').attr('checked', false);
$(parent.containerId).find('input[value="' + parent.systemGeneratedAll +
'"]').prop('checked', true);
}
});
parent.filterValues = [];
parent.printData();
});
};
this.unbindFilters = function () {
$(this.containerId + ' .filter').unbind('change');
$(this.containerId + ' .filter.clear').unbind('click');
};
this.bindListeners = function () {
this.bindPagination();
this.bindFilters();
};
this.init = function () {
this.printSkeleton(false);
this.fetchData();
};
return this;
};
function knowMoreOnclickLevels(context) {
var modal =
'
' +
'
×Foundation Level Certificate
' +
'
The Foundation level is open to any and all interested individuals, there is no prerequisite for ' +
'appearing for this exam. This exam level tests candidates on topics including Basic data structures, ' +
'Arrays, Strings, Stacks, Queues, Dynamic Programming etc.
Advanced Level Certificate
' +
'The Advanced Level is open to candidates who are already Foundation Certified or who have a 3*rating or ' +
'greater on CodeChef. This exam tests candidates on Advanced data structures likeHeaps, ' +
'Trees, Graphs, Segment Trees, Binary Index Tree, Advanced Dynamic Programming etc.
';
$(context).parent().append(modal);
var knowMoreModal = document.querySelector('.section__filter--certificationLevel--knowmore--modal');
knowMoreModal.style.display = 'block';
var knowMoreCloseModal = document.querySelector(
'.section__filter--certificationLevel--knowmore--modal .close--modal');
knowMoreCloseModal.addEventListener('click', function (e) {
knowMoreModal.style.display = 'none';
e.preventDefault();
});
}
var obj = {
containerId: 'fancyList',
dataURL: dataURL,
data: [],
template: userCards,
perPage: 10000,
filters: [{
title: 'Certification Level',
type: 'radio',
link: 'abhij lagayenge',
linkPrefix: '',
linkLabel: 'Know More',
linkSuffix: 'about certification levels',
name: 'certificationLevel',
knowMoreOnclick: knowMoreOnclickLevels
},
{
title: 'Location',
type: 'select',
link: '',
linkPrefix: '',
linkLabel: '',
linkSuffix: '',
name: 'location',
knowMoreOnclick: null
},
{
title: 'Graduation Year',
type: 'select',
link: '',
linkPrefix: '',
linkLabel: '',
linkSuffix: '',
name: 'graduation_year',
knowMoreOnclick: null
}
],
page: 1
};
$.getScript('/em/libs/chosen/chosen.js?1', function () {
var fancyList = new FancyList(obj);
fancyList.init();
});
}());
// ]]>
