<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>
var profileMainInfo = function () {
var teamItemTemplate = Handlebars.compile(document.getElementById('team-item-template').innerHTML);
var absenceInfoTemplate = Handlebars.compile(document.getElementById('absence-info-template').innerHTML);
var absenceInfoEmptyTemplate = Handlebars.compile(document.getElementById('absence-info-empty-template').innerHTML);
var absenceAddTemplate = Handlebars.compile(document.getElementById('absence-add-template').innerHTML);
var teamItemOnPage = profileTeamItemsPerPage;
var teamColleaguesPage = 1;
var teamBossesPage = 1;
var teamSubordinatesPage = 1;
var absenceAjaxWork = false;
var isEdit = false;
var absenceEdit = 0;
function initTeamModule() {
if (profileTeam != null) {
if (profileTeam.Colleagues != null && profileTeam.Colleagues.length) {
$('#profileColleaguesReplace').empty().append($(teamItemTemplate(profileTeam.Colleagues.slice(0, teamItemOnPage))));
}
if (profileTeam.Bosses != null && profileTeam.Bosses.length) {
$('#profileBossesReplace').empty().append($(teamItemTemplate(profileTeam.Bosses.slice(0, teamItemOnPage))));
}
if (profileTeam.Subordinates != null && profileTeam.Subordinates.length) {
$('#profileSubordinatesReplace').empty().append($(teamItemTemplate(profileTeam.Subordinates.slice(0, teamItemOnPage))));
}
}
$('.team-tabs .team-tab').click(function () {
if ($(this).hasClass('team-tab__active')) return;
$('.team-tab.team-tab__active').removeClass('team-tab__active').addClass('team-tab__inactive');
$(this).removeClass('team-tab__inactive').addClass('team-tab__active');
var tabName = $(this).data('tab');
$('.team-tabs-content .team-user-tab').removeClass('item__visible').addClass('item__hidden');
$('.team-tabs-content .team-user-tab[data-tab="' + tabName + '"]').removeClass('item__hidden').addClass('item__visible');
});
$('.team-nav.team-nav-left').click(function () {
var tabName = $(this).data('tab');
if (tabName == 'colleagues') {
if (teamColleaguesPage == 1) return;
teamColleaguesPage -= 1;
teamColleaguesPage = teamColleaguesPage > 0 ? teamColleaguesPage : 1;
$('#profileColleaguesReplace').empty().append($(teamItemTemplate(profileTeam.Colleagues.slice((teamColleaguesPage - 1) * teamItemOnPage, teamColleaguesPage * teamItemOnPage))));
}
if (tabName == 'boss') {
if (teamBossesPage == 1) return;
teamBossesPage -= 1;
teamBossesPage = teamBossesPage > 0 ? teamBossesPage : 1;
$('#profileBossesReplace').empty().append($(teamItemTemplate(profileTeam.Bosses.slice((teamBossesPage - 1) * teamItemOnPage, teamBossesPage * teamItemOnPage))));
}
if (tabName == 'subordinates') {
if (teamSubordinatesPage == 1) return;
teamSubordinatesPage -= 1;
teamSubordinatesPage = teamSubordinatesPage > 0 ? teamSubordinatesPage : 1;
$('#profileSubordinatesReplace').empty().append($(teamItemTemplate(profileTeam.Subordinates.slice((teamSubordinatesPage - 1) * teamItemOnPage, teamSubordinatesPage * teamItemOnPage))));
}
});
$('.team-nav.team-nav-right').click(function () {
var tabName = $(this).data('tab');
var pagesCount = 0;
if (tabName == 'colleagues') {
pagesCount = Math.ceil(profileTeam.Colleagues.length / teamItemOnPage);
if (teamColleaguesPage == pagesCount) return;
teamColleaguesPage += 1;
teamColleaguesPage = teamColleaguesPage <= pagesCount ? teamColleaguesPage : pagesCount;
$('#profileColleaguesReplace').empty().append($(teamItemTemplate(profileTeam.Colleagues.slice((teamColleaguesPage - 1) * teamItemOnPage, teamColleaguesPage * teamItemOnPage))));
}
if (tabName == 'boss') {
pagesCount = Math.ceil(profileTeam.Bosses.length / teamItemOnPage);
if (teamBossesPage == pagesCount) return;
teamBossesPage += 1;
teamBossesPage = teamBossesPage <= pagesCount ? teamBossesPage : pagesCount;
$('#profileBossesReplace').empty().append($(teamItemTemplate(profileTeam.Bosses.slice((teamBossesPage - 1) * teamItemOnPage, teamBossesPage * teamItemOnPage))));
}
if (tabName == 'subordinates') {
pagesCount = Math.ceil(profileTeam.Subordinates.length / teamItemOnPage);
if (teamSubordinatesPage == pagesCount) return;
teamSubordinatesPage += 1;
teamSubordinatesPage = teamSubordinatesPage <= pagesCount ? teamSubordinatesPage : pagesCount;
$('#profileSubordinatesReplace').empty().append($(teamItemTemplate(profileTeam.Subordinates.slice((teamSubordinatesPage - 1) * teamItemOnPage, teamSubordinatesPage * teamItemOnPage))));
}
});
}
function initInfoModule() {
$('.general-info-tabs .general-info-tab').click(function () {
if ($(this).hasClass('info-tab__active')) return;
$('.general-info-tab.info-tab__active').removeClass('info-tab__active').addClass('info-tab__inactive');
$(this).removeClass('info-tab__inactive').addClass('info-tab__active');
var tabName = $(this).data('tab');
$('.general-info-tabs-content .general-info-tab').removeClass('item__visible').addClass('item__hidden');
$('.general-info-tabs-content .general-info-tab[data-tab="' + tabName + '"]').removeClass('item__hidden').addClass('item__visible');
});
}
function initAbsenceInfo() {
moment.locale('ru');
drawAbsence(profileAbence);
}
function editOther() {
$('.editable-field-other').hide();
$('.editable-field-editor-other').show();
}
function saveOther() {
$('.editable-field-editor-other').hide();
$('.editable-field-other').show();
var newValue = $('#editableFieldInputOther').val();
$('.editable-field-other-value').text(newValue);
$.ajax({
url: '/Ajax.ashx',
data: ({
method: 'ProfileMainInfo:AjaxUpdateOtherField',
newValue: newValue,
rand: Math.random()
}),
type: 'POST',
dataType: 'json',
success: function (result) {
if (result.Error) {
if (window.console) console.log(result.Error);
alert('Произошла серверная ошибка');
}
},
error: function () {
alert('Произошла серверная ошибка');
}
});
}
function removeAbsence(absenceId) {
if (confirm('Вы уверены, что хотите удалить статус?')) {
$.ajax({
url: '/Ajax.ashx',
data: ({
method: 'ProfileMainInfo:AjaxDeleteAbsence',
absenceId: absenceId,
rand: Math.random()
}),
type: 'POST',
dataType: 'json',
success: function (result) {
if (result.Error) {
if (window.console) console.log(result.Error);
alert('Произошла серверная ошибка');
} else {
$('#absenceInfoContainer').empty();
drawAbsence(result.Item);
}
},
error: function () {
alert('Произошла серверная ошибка');
}
});
}
}
function editAbsence(absenceId) {
$('body').append($(absenceAddTemplate()));
var dateStart = moment(profileAbence.DateStart).format("DD.MM.YYYY")
var dateEnd = moment(profileAbence.DateEnd).format("DD.MM.YYYY")
var comment = profileAbence.Comment;
$('#absenceDateStart').bootstrapDatePicker("update", dateStart);
$('#absenceDateEnd').bootstrapDatePicker("update", dateEnd);
$('#absenceComment').val(comment);
isEdit = true;
absenceEdit = absenceId;
$('input.absence-date').bootstrapDatePicker({
language: 'ru',
format: 'dd.mm.yyyy',
autoclose: true
});
}
function addAbsence() {
$('body').append($(absenceAddTemplate()));
$('input.absence-date').bootstrapDatePicker({
language: 'ru',
format: 'dd.mm.yyyy',
autoclose: true
});
}
function cancelAddAbsence() {
$('#addAbsenceContainer').remove();
}
function drawAbsence(item) {
if (item != null && item.Id > 0) {
$('#absenceInfoContainer').empty().append($(absenceInfoTemplate(item)));
} else {
$('#absenceInfoContainer').empty().append($(absenceInfoEmptyTemplate()));
}
}
function saveAbsence() {
if (absenceAjaxWork)return;
var dateStart = $('#absenceDateStart').val();
var dateEnd = $('#absenceDateEnd').val();
var comment = $('#absenceComment').val();
absenceAjaxPost(dateStart, dateEnd, comment);
}
function absenceAjaxPost(dateStart, dateEnd, comment) {
if (dateStart.trim().length == 0) {
alert('Необходимо указать дату начала');
return;
}
var methodName;
!isEdit ? methodName = 'AjaxAddAbsence' : methodName = 'AjaxEditAbsence';
absenceAjaxWork = true;
$.ajax({
url: '/Ajax.ashx',
data: ({
method: 'ProfileMainInfo:' + methodName,
dateStart: dateStart,
dateEnd: dateEnd,
comment: comment,
absenceId: absenceEdit,
rand: Math.random()
}),
type: 'POST',
dataType: 'json',
success: function (result) {
if (result.Error) {
if (window.console) console.log(result.Error);
alert('Произошла серверная ошибка');
}
else if (result.EmptyFiels) {
alert('Необходимо указать дату начала');
}
else if (result.StartMoreEnd) {
alert('Дата начала не может быть меньше даты окончания');
}
else {
$('#absenceInfoContainer').empty();
drawAbsence(result.Item);
cancelAddAbsence();
}
absenceAjaxWork = false;
},
error: function () {
alert('Произошла серверная ошибка');
absenceAjaxWork = false;
}
});
}
function init() {
try {
initTeamModule();
initInfoModule();
initAbsenceInfo();
} catch (e) {
console.log(e);
}
}
return {
init: init,
editOther: editOther,
saveOther: saveOther,
editAbsence: editAbsence,
removeAbsence: removeAbsence,
addAbsence: addAbsence,
cancelAddAbsence: cancelAddAbsence,
saveAbsence: saveAbsence
}
}();
$(document).ready(function () {
profileMainInfo.init();
});