Commit 69992268 authored by Feng Xue's avatar Feng Xue

Remove unnecessary codes

add lastTime parameter to the callback function to indicate if enable scrolllisten
add rememberLastSelected option to let plugin remember the last selected item
add lastSelectedItemKey option to distinguish the different lists
parent 0f4bcffe
/*jshint browser:true jquery:true */
/*global window, document, $, sessionStorage */
(function ($) {
"use strict";
var settings = {},
......@@ -8,39 +8,16 @@
scrollListenersEnabled = false,
targetTop = 0;
var debounce = function (func, threshold, execAsap) {
var timeout;
return function debounced() {
var obj = this, args = arguments;
function delayed() {
if (!execAsap) {
func.apply(obj, args);
}
timeout = null;
}
if (timeout) {
clearTimeout(timeout);
} else if (execAsap) {
func.apply(obj, args);
}
timeout = setTimeout(delayed, threshold || timeout);
};
};
var invisibleSelectTarget = function () {
$("#" + settings.revealDivId).css("visibility", "hidden");
$("#" + settings.targetId).css("visibility", "hidden");
$("#" + settings.itemInfoDivId).css("visibility", "hidden");
$("#" + settings.sightScopeId).css("visibility", "hidden");
peekedList.children('li.' + settings.selectedClass).removeClass(settings.selectedClass);
};
var showExpandedInfo = function () {
var listElem = peekedList.children("li." + settings.selectedClass),
details,
targetElement = $('#' + settings.targetId),
revealDivPart = $('#' + settings.revealDivId);
var showExpandedInfo = function (listElem) {
var details,
targetElement = $('#' + settings.sightScopeId),
revealDivPart = $('#' + settings.itemInfoDivId);
scrollListenersEnabled = false;
......@@ -48,19 +25,22 @@
targetElement.offset({'top': listElem.offset().top});
revealDivPart.offset({'top': listElem.offset().top + targetElement.height()});
settings.extendedInfoFunc(listElem.attr("id"), function (details) {
settings.extendedInfoFunc(listElem.attr("id"), function (details, isLastTime) {
if (details !== null) {
revealDivPart.html(details);
targetElement.css("visibility", "visible");
revealDivPart.css("visibility", "visible");
var revealDivExceedWindowHeight = listElem.offset().top - $(window).scrollTop() + listElem.height() + revealDivPart.height() - $(window).height();
var revealDivExceedWindowHeight = listElem.offset().top - $(window).scrollTop() + listElem.outerHeight() + revealDivPart.outerHeight() - $(window).height();
if (revealDivExceedWindowHeight > 0) {
scrollListenersEnabled = false;
$('body, html').animate({scrollTop: revealDivExceedWindowHeight + $(window).scrollTop()}, 'fast', function () {
targetElement.offset({'top': listElem.position().top});
revealDivPart.offset({'top': listElem.position().top + targetElement.height()});
setTimeout(function () {
scrollListenersEnabled = true;
}, 100);
if (isLastTime) {
setTimeout(function () {
scrollListenersEnabled = true;
}, 100);
}
});
} else {
scrollListenersEnabled = true;
......@@ -69,18 +49,32 @@
});
};
var setSelectedElement = function (element, asap) {
var setSelectedElement = function (element) {
var listElem,
sessionStorageKey = settings.lastSelectedItemKey + '_lastSelected';
if (!element) {
if (typeof(sessionStorage) !== "undefined" && settings.rememberLastSelected && sessionStorage[sessionStorageKey]) {
element = peekedList.find('li[id=\'' + sessionStorage[sessionStorageKey] + '\']').get();
} else if (settings.initialElement) {
element = peekedList.find(' li[id=\'' + settings.initialElement + '\']').get();
if (element.length < 1) {
element = peekedList.children('li:first-child').get();
}
}
}
if (element) {
var listElem = $(element).closest('li');
if (listElem.is("LI") && listElem !== focusedListElement) {
listElem = $(element).closest('li');
if (listElem.is("LI")) {
invisibleSelectTarget();
listElem.addClass(settings.selectedClass);
focusedListElement = listElem;
if (asap) {
debounce(showExpandedInfo, 50, false)();
} else {
debounce(showExpandedInfo, 500, false)();
if (typeof(sessionStorage) !=="undefined" && settings.rememberLastSelected) {
sessionStorage[sessionStorageKey] = listElem.attr('id');
}
showExpandedInfo(listElem);
} else {
invisibleSelectTarget();
}
......@@ -102,32 +96,31 @@
} else {
setSelectedElement();
}
listHeight = peekedList.outerHeight();
bodyHeight = $('body').outerHeight();
viewPortHeight = $(window).height();
distanceFromListEndToBodyEnd = bodyHeight - targetTop - listHeight;
distanceFromTargetToViewportEnd = viewPortHeight - targetTop - $('#' + settings.targetId).height();
distanceFromTargetToViewportEnd = viewPortHeight - targetTop - $('#' + settings.sightScopeId).height();
if (distanceFromListEndToBodyEnd < distanceFromTargetToViewportEnd) {
peekedList.css("padding-bottom", "+=" + (distanceFromTargetToViewportEnd - distanceFromListEndToBodyEnd));
}
invisibleSelectTarget();
};
var registerEventHandlers = function () {
peekedList.children("li").click(function () {
var listElem = $($(this).get());
if (focusedListElement && listElem.attr("id") === focusedListElement.attr("id") && "visible" === $("#" + settings.targetId).css("visibility")) {
if (focusedListElement && listElem.attr("id") === focusedListElement.attr("id") && "visible" === $("#" + settings.sightScopeId).css("visibility")) {
invisibleSelectTarget();
focusedListElement = null;
} else {
setSelectedElement(listElem, true);
setSelectedElement(listElem);
}
});
$(window).resize(function () {
debounce(resetGeometry, 500, false)();
resetGeometry();
});
$(window).scroll(function () {
......@@ -145,11 +138,13 @@
$.fn.listEntryInfo = function (options) {
settings = $.extend({
selectedClass: "listPeekSelected",
targetId: "listPeekTargetArea",
revealDivId: "listPeakRevealArea",
selectedClass: "listEntryInfoSelected",
sightScopeId: "listSightScope",
itemInfoDivId: "selectedEntryItemInfo",
rememberLastSelected: false,
lastSelectedItemKey: "list",
initialElement: undefined,
timeout: 100,
backgroundColor: "white",
extendedInfoFunc: function (id, callback) {
callback("<strong>" + id + "</strong>");
}
......@@ -158,24 +153,21 @@
peekedList = this;
targetTop = peekedList.position().top;
$('<div></div>').attr('id', settings.targetId).css({
width: '100%',
left: 0,
'min-height': '1em',
'z-index': '-2',
padding: '0px',
position: 'fixed',
if (typeof peekedList.position() !== 'object') {
console.log("Unable to find list element!");
return;
}
if ($("#" + settings.sightScopeId).length !== 0 || $("#" + settings.itemInfoDivId).length !== 0) {
console.log("Target element already exists!");
return;
}
$('<div></div>').attr('id', settings.sightScopeId).css({
top: targetTop
}).insertAfter(peekedList);
$('<div></div>').attr('id', settings.revealDivId).css({
position: 'fixed',
width: '80%',
left: '10%',
'z-index': 2,
'max-height': '70%',
top: targetTop + $('#' + settings.targetId).height()
}).insertAfter(peekedList);
$('<div></div>').attr('id', settings.itemInfoDivId).insertAfter(peekedList);
registerEventHandlers();
resetGeometry();
......@@ -183,4 +175,8 @@
return this;
};
}(jQuery));
\ No newline at end of file
$.fn.hideEntryInfo = function () {
invisibleSelectTarget();
};
}(jQuery));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment