Created
November 7, 2011 17:32
-
-
Save tkh44/1345608 to your computer and use it in GitHub Desktop.
Main JS file for site
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
library = function() { | |
var elements = {}; | |
var values = { | |
searchHeight: null, | |
logoHeight: null, | |
selected: 'selected', | |
category: 'category', | |
open: 'open', | |
catSelected: 'cat-selected', | |
sub: 'sub', | |
hasFocus: true, | |
loader: '<div id="loader"></div>', | |
title: 'Homeland HealthCare Library' | |
}; | |
var keys = { | |
enter: 13, | |
escape: 27, | |
up: 38, | |
down: 40, | |
array: [13, 27, 38, 40] | |
} | |
function initialize() { | |
elements = { | |
search: $('#search-field'), | |
searchWrapper: $('#search'), | |
logo: $('#logo'), | |
content: $('#content'), | |
list: $('#static-list'), | |
window: $(window), | |
results: null, | |
category: null | |
}; | |
elements.results = jQuery('<ul>', { id: 'results' }).insertBefore(elements.list); | |
elements.category = $('.category', elements.list); | |
values.searchHeight = elements.searchWrapper.innerHeight(); | |
values.logoHeight = elements.logo.innerHeight(); | |
elements.window.resize(function() { | |
var winH = elements.window.height(); | |
var listH = winH - (values.searchHeight + values.logoHeight); | |
//console.log('window height: ' + winH); | |
//console.log('list height: ' + listH); | |
//console.log('list height + search height: ' + (listH + values.searchHeight)); | |
elements.list.height(listH); | |
elements.results.height(listH); | |
elements.content.height(winH-1); | |
//console.log('content height: ' + elements.content.height()); | |
elements.search.width(elements.searchWrapper.width() - 8); | |
elements.logo.width(elements.searchWrapper.width() - 7); | |
}) | |
.mousemove(function(event) { | |
if(event.pageX < elements.list.width()) searchFocus(); | |
}) | |
.keydown(function(event) { | |
if(event.keyCode == keys.escape) { | |
elements.search.val('').focus(); | |
elements.results.hide(); | |
elements.list.show(); | |
} | |
}) | |
.trigger('resize'); //trigger resize event to initially set sizes | |
elements.search.keyup(function(event) { | |
if($.inArray(event.keyCode, keys.array) != -1) { //it is an event key | |
handleKey(event.keyCode); | |
} else { //it is a character | |
startSearch(); | |
} | |
}) | |
.focus(function() { | |
values.hasFocus = true; | |
}) | |
.blur(function() { | |
values.hasFocus = false; | |
}) | |
.focus(); | |
$('.'+values.category+' > span', elements.list).toggle(function() { | |
clearSelected(); | |
searchFocus(); | |
$(this).parent().addClass(values.open).children('ul').show(); | |
}, function() { | |
clearSelected(); | |
searchFocus(); | |
$(this).parent().removeClass(values.open).children('ul').hide(); | |
}); | |
$('.product-link').live('click', function() { | |
var el = $(this); | |
//console.log('el: ' + el.attr('href')); | |
clearSelected(); | |
searchFocus(); | |
el.parent().addClass(values.selected); | |
$.bbq.pushState({ p: urlMethodName(el) }); | |
return false; | |
}); | |
$(window).bind('hashchange', function(event) { | |
var state = event.getState(); | |
//console.log('state.p: ' + state.p); | |
if(state.p) loadPage(state.p); | |
}).trigger('hashchange'); | |
zebraItems(elements.list); //zebra the items in the static list | |
} //-initialize | |
function searchFocus() { | |
elements.search.focus(); | |
} //-searchFocus | |
function zebraItems(list) { | |
$('.sub:odd', list).addClass('odd'); | |
} //-zebraItems | |
function clearSelected() { | |
$('.'+values.selected).removeClass(values.selected); | |
} //-clearSelected | |
function handleKey(key) { | |
if(values.hasFocus) { | |
var selVis = $('.'+values.selected+':visible'); | |
if(selVis.length) { | |
if(key == keys.up && selVis.prev().length) selVis.removeClass(values.selected).prev().addClass(values.selected); | |
if(key == keys.down && selVis.next().length) selVis.removeClass(values.selected).next().addClass(values.selected); | |
if(key == keys.enter) $.bbq.pushState({ p: urlMethodName(selVis.children('a')) }); | |
} else { //no visible selected item | |
var catSel = $('.'+values.catSelected, elements.list); | |
if(catSel.length) { //a category is selected | |
if(key == keys.up) catSel.removeClass(values.catSelected).prev().addClass(values.catSelected); | |
if(key == keys.down) catSel.removeClass(values.catSelected).next().addClass(values.catSelected); | |
if(key == keys.enter) catSel.removeClass(values.catSelected).children('span').trigger('click'); | |
} else { //no category selected | |
var subVis = $('.'+values.sub+':visible', elements.list); | |
if(subVis.length) { //there are visible subs in the static list | |
if(key == keys.up) subVis.filter(':last').addClass(values.selected); | |
if(key == keys.down) subVis.filter(':first').addClass(values.selected); | |
} else { //only categories are shown | |
if(key == keys.up) elements.category.last().addClass(values.catSelected); | |
if(key == keys.down) elements.category.first().addClass(values.catSelected); | |
} | |
} | |
} | |
} | |
} //-handleKey | |
function urlMethodName(link) { | |
//console.log('link: ' + link.attr('href')); | |
var href = link.attr('href'); | |
//console.log('href: ' + href); | |
//console.log('href.substr(1, href.length): ' + href.substr(1, href.length)); | |
return href.substr(1, href.length); | |
} //-urlMethodName | |
function loadPage(link) { | |
//console.log('link: ' + link); | |
elements.content.html(values.loader).load(link + "/" + " #inside-content", function() { | |
document.title = elements.content.find('#product-name').text() + ' | ' + values.title; | |
formatPage(); | |
}); | |
} //-loadPage | |
function formatPage() { | |
//console.log('formatting page'); | |
$('.tabs').tabs(); | |
$('div.slide > div').hide(); | |
$('div.slide >h4').toggle(function(){ | |
$(this).next().show(); | |
$(this).addClass('active'); | |
$(this).children("span:first").removeClass('ui-icon-triangle-1-e').addClass('ui-icon-triangle-1-s'); | |
}, function() { | |
$(this).next().hide(); | |
$(this).removeClass('active'); | |
$(this).children("span:first").removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-e'); | |
}); | |
var topNav = $('#topnav'); | |
$('#content').scroll(function() { | |
if($(this).scrollTop() > 30) { | |
topNav.fadeOut('fast'); | |
} else { | |
topNav.fadeIn('fast'); | |
} | |
}); | |
var admin = $('.admin-link').click(function() { | |
var link = $(this).attr('href'); | |
var fwindow = $('#admin-window'); | |
var foverlay = $('#admin-overlay'); | |
if(!fwindow.length) { | |
var bod = $('body'); | |
var win = $(window); | |
var winw = win.width(); | |
//console.log("win.width: " + win.width()); | |
var winh = win.height(); | |
//console.log("win.height: " + win.height()) ; | |
fwindow = jQuery('<div>', { | |
id: 'admin-window', | |
css: { left: 50, width: winw - 100, height: winh - 100 }, | |
html: jQuery('<iframe>', { src: link }) | |
}).appendTo(bod); | |
foverlay = jQuery('<div>', { | |
id: 'admin-overlay', | |
css: { width: winw, height: winh }, | |
click: function() { | |
$(this).fadeOut('fast'); | |
fwindow.fadeOut('fast'); | |
} | |
}).appendTo(bod); | |
jQuery('<a>', { | |
html: '× Close', | |
href: '#close', | |
css: { right: (100) / 2, }, | |
click: function() { | |
foverlay.click(); | |
return false; | |
} | |
}).appendTo(foverlay); | |
} | |
foverlay.fadeIn('fast'); | |
fwindow.fadeIn('fast'); | |
return false; | |
}); | |
$(".alert-message").alert(); | |
$("a[rel=popover]") | |
.popover({ | |
offset: 10, | |
trigger: "hover", | |
placement: "above" | |
}); | |
} //-formatPage | |
function startSearch() { | |
elements.search.doTimeout('text-type', 300, function() { | |
var term = elements.search.val(); | |
if(term.length) { | |
elements.results.html('').show(); | |
elements.list.hide(); | |
var lastPos = 100; | |
var winner = $; | |
$('.searchable', elements.list).each(function() { | |
var el = $(this); | |
var name = el.text(); | |
var pos = name.toLowerCase().indexOf(term.toLowerCase()); | |
if(pos != -1 && elements.results.text().indexOf(name) == -1) { | |
var lastLi = jQuery('<li>', { | |
'class': 'sub', | |
html: el.parent().parent().html() | |
}).appendTo(elements.results); | |
if(pos < lastPos) { | |
lastPos = pos; | |
winner = lastLi; | |
} | |
} | |
}); | |
elements.results.prepend(winner).highlight(term, true, 'highlight').children('li:first').addClass(values.selected); | |
zebraItems(elements.results); | |
} else { | |
elements.results.hide(); | |
elements.list.show(); | |
} | |
}); | |
} //-startSearch | |
return { | |
initialize: initialize | |
} | |
}(); | |
$(document).ready(function() { | |
//initialize document | |
var navigation_el = library.initialize(); | |
//activate alert messages on dashboard | |
$(".alert-message").alert(); | |
//initialize topbar and hide it on scroll | |
var topNav = $('#topnav'); | |
$('#content').scroll(function() { | |
if($(this).scrollTop() > 30) { | |
topNav.fadeOut('fast'); | |
} else { | |
topNav.fadeIn('fast'); | |
} | |
}); | |
//profile actions activate | |
profile = { | |
trigger: $('#profile-edit-trigger'), | |
popup: $('#profile-edit-modal'), | |
popupcontent: $('#changepass'), | |
} | |
profile.popup.modal(); | |
profile.trigger.click(function(){ | |
profile.popupcontent.load('/passchange' + "/" + " #inside-content", function() {}); | |
// profile.popup.modal('show'); | |
// profile.popup.show(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment