it/itf/static/js/insidepage.js

173 lines
4.8 KiB
JavaScript

$('#listLeft ul li a').live("click", function() {
// alert("foo");
// e.preventDefault();
if ($(this).hasClass("listLeftSelected")) {
return false;
}
$('.listLeftSelected').removeClass("listLeftSelected");
$(this).addClass('listLeftSelected');
var objId = $(this).attr("data-id");
var tabId = $('.innerSelected').attr("data-id");
$.getJSON("/m/get_details", {
'tab_id': tabId,
'object_id': objId
}, function(data) {
$('#textRight').text(data.title);
$('#bottomRight').html(data.html);
// console.log(data);
});
return false;
});
/* Get a jquery <option> row for a sort field, requires s.operator, s.field_name, s.friendly_name */
function getOption(s) {
return $('<option />').val(s.operator + s.field_name).text(s.friendly_name);
}
/* Get a Jquery List Row object for an item - requires item.id and item.title*/
function getLi(item) {
var titleLength = 30;
var $li = $('<li />').addClass("tabListItem");
var $a = $('<a />').attr("href", '#').attr("data-id", item.id).appendTo($li);
// console.log(item.title.length);
var $span = $('<span />').text(item.title).appendTo($a);
return $li;
}
$(function() {
/* search button toggle */
$('.searchInnerField').hide();
$('#searchInner').toggle(function() {
$('.searchInnerField').slideDown();
}, function() {
$('.searchInnerField').slideUp();
});
/* search button end */
$('#listLeft ul li a').eq(0).click();
// alert("hi");
$('.tabCategory').click(function() {
var $this = $(this);
if ($this.hasClass('.innerSelected')) {
return false;
}
$('.innerSelected').removeClass("innerSelected");
$this.addClass("innerSelected");
$('.displayedTab').removeClass("displayedTab");
var tabId = $this.attr("data-id");
$('#tabText_' + tabId).addClass("displayedTab");
doListLoading();
$.getJSON("/m/get_tab", {
'tab_id': tabId,
// 'page_no': 1
}, function(data) {
// $('#loadingList').hide();
stopListLoading(data.page);
/* begin if for has_list, ideally make into separate render function */
// if (data.has_list) {
/* handle sort options on list */
$select = $('#orderBySelect');
$select.empty();
var sorts = data.sorts;
if (sorts.length == 0) { $select.hide(); } else { $select.show(); }
for (var j=0; j<sorts.length; j++) {
var s = sorts[j];
var $opt = getOption(s);
$select.append($opt);
}
/* end handle sort */
/* handle displaying initial list on page */
displayList(data.page.items);
/* end display initial list */
// }
/* end if has_list */
});
});
$('#orderBySelect').change(function(e) {
var sortString = $(this).val();
doListLoading();
$.getJSON("/m/get_list", {
'tab_id': $('.innerSelected').attr("data-id"),
'sort': sortString
}, function(data) {
stopListLoading(data);
displayList(data.items);
});
});
$('#triangleUp').click(function() {
goPreviousPage();
});
$('#triangleDown').click(function() {
goNextPage();
});
});
function displayList(items) {
for (var i=0; i<items.length; i++) {
var $li = getLi(items[i]);
$('#listLeft ul').append($li);
}
$('#listLeft ul li a').eq(0).click();
}
function doListLoading() {
$('#loadingList').show();
$('.tabListItem').remove();
}
function stopListLoading(page) {
$('#currPageNo').text(page.page_no);
$('#totalPages').text(page.num_pages);
$('#loadingList').hide();
if (page.page_no == 1) {
$('#triangleUp').hide();
} else {
$('#triangleUp').show();
}
if (page.page_no == page.num_pages) {
$('#triangleDown').hide();
} else {
$('#triangleDown').show();
}
}
function goNextPage() {
var currPage = parseInt($('#currPageNo').text());
var totalPages = parseInt($('#totalPages').text());
if (currPage < totalPages) {
getPage(currPage + 1);
}
}
function goPreviousPage() {
var currPage = parseInt($('#currPageNo').text());
if (currPage > 1) {
getPage(currPage - 1);
}
}
function getPage(pageNo) {
doListLoading();
$.getJSON("/m/get_list", {
'tab_id': $('.innerSelected').attr("data-id"),
'page': pageNo,
'sort': $('#orderBySelect').val()
}, function(page) {
stopListLoading(page);
displayList(page.items);
});
}