사용자:Senior9324/Gadget-ReferenceDrawerDev.js: 두 판 사이의 차이

편집 요약 없음
편집 요약 없음
 
(같은 사용자의 중간 판 45개는 보이지 않습니다)
1번째 줄: 1번째 줄:
var isMobile = navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i);
var drawer, origin, content;
var drawer, origin, content;
function showDrawer(id, name) {
function showDrawer(id, name, orgId) {
origin.attr("href", "#" + id);
origin.attr("href", id);
origin.attr("data-target", id);
origin.attr("data-origin", orgId.replace(/\./g, "\\."));
origin.text(name);
origin.text(name);
content.html($("#" + id + " > .reference-text").html());
content.html($(id.replace(/\./g, "\\.") + " > .reference-text").html());
drawer.addClass("visible");
drawer.addClass("visible");
}
}


function getSettings(key) {
function getSettings(key, type) {
if ("localStorage" in window) {
if ("localStorage" in window) {
return window.localStorage[key];
if (type === "boolean") {
return (window.localStorage[key] === "true");
} else {
return window.localStorage[key];
}
} else {
} else {
var value = "; " + document.cookie;
var value = "; " + document.cookie;
var parts = value.split("; " + key + "=");
var parts = value.split("; " + key + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
if (parts.length == 2) {
if (type === "boolean") {
return (parts.pop().split(";").shift() === "true");
} else {
return parts.pop().split(";").shift();
}
}
}
}
}
}
28번째 줄: 39번째 줄:
document.cookie = key + "=" + value + ";expires=" + now.toGMTString() +";path=/";
document.cookie = key + "=" + value + ";expires=" + now.toGMTString() +";path=/";
}
}
}
function initSettings() {
if (document.getElementById("reference-settings")) {
$("#reference-settings").show();
return;
}
var setbox = $("<form>").attr("id", "reference-settings").append(
$("<div>").addClass("content").append(
$("<label>").append(
$("<input>").attr({"type": "checkbox", "name": "showRefOnHover", "checked": getSettings("showRefOnHover", "boolean")}),
"갖다 대서 주석을 띄웁니다."
)
),
$("<div>").addClass("foot").append(
$("<input>").attr({"type": "button", "value": "취소"}).click(function() {
$("#reference-settings").hide();
}),
$("<input>").addClass("save-settings").attr({"type": "submit", "value": "저장"})
)
).submit(function(e) {
e.preventDefault();
setSettings("showRefOnHover", $(this).find("[name=showRefOnHover]").is(":checked"));
location.reload();
});
$(document.body).append(setbox);
}
function showTooltip(elem) {
showDrawer(elem.parentNode.getAttribute("href"), elem.textContent, elem.parentNode.parentNode.getAttribute("id"));
drawer.show();
drawer.css({ top: ($(elem).offset().top - drawer.outerHeight()), left: $(elem).offset().left });
drawer.stop().animate({opacity: 1}, 100);
}
function hideTooltip() {
drawer.removeClass("visible");
drawer.animate({opacity: 0}, 100, function() { $(this).hide(); });
}
}


$(document).ready(function($) {
$(document).ready(function($) {
if (isMobile) {
$(document.body).addClass("mode-drawer");
} else {
$(document.body).addClass("mode-tooltip");
}
/* create drawer */
/* create drawer */
drawer = $("<div></div>").attr("id", "reference-drawer");
drawer = $("<div>").attr("id", "reference-drawer");
origin = $("<a></a>")
origin = $("<a>").attr("id", "reference-origin").click(function() {
.attr("id", "reference-origin")
$('html, body').animate({scrollTop: ($($(this).attr("href")).offset().top - 60)}, 400);
.click(function() {
});
$('html, body').animate({scrollTop: ($("#" + $(this).attr("data-target")).offset().top - 60)}, 400);
content = $("<span>").attr("id", "reference-drawer-text");
var settingsIcon = $("<span>").addClass("settings-icon").click(function() {
initSettings();
});
var closeDrawer = $("<span>").addClass("close-icon").click(function() {
drawer.removeClass("visible");
});
});
content = $("<span></span>").attr("id", "reference-drawer-text");
drawer.append(settingsIcon);
drawer.append(closeDrawer);
drawer.append(origin);
drawer.append(origin);
drawer.append(content);
drawer.append(content);
44번째 줄: 104번째 줄:
$(document).click(function(e) {
$(document).click(function(e) {
if (!$(event.target).closest("#reference-drawer").length) {
if (!$(e.target).closest("#reference-drawer").length) {
drawer.removeClass("visible");
if (!isMobile) {
if ($(window).width() >= 768) {
hideTooltip();
drawer.animate({opacity: 0}, 100, function() { $(this).css({visibility: "hidden", width: 0, height: 0}); });
} else {
drawer.removeClass("visible");
}
}
}
}
}).scroll(function(e) {
}).scroll(function(e) {
if ("ontouchstart" in window && !$(window).width() < 768) {
if ("ontouchstart" in window && isMobile) {
drawer.removeClass("visible");
drawer.removeClass("visible");
}
}
});
});
$(".reference a").each(function() {
$(".reference a").each(function() {
/* 상위 요소에 걸려있는 이벤트를 무시하기 위해 하위 요소를 만들어서 이벤트 확산을 방지합니다. */
var span = document.createElement("span");
var span = document.createElement("span");
span.className = "reference-hooker";
span.className = "reference-hooker";
61번째 줄: 123번째 줄:
this.appendChild(span);
this.appendChild(span);
});
});
if (getSettings("showRefOnHover")) {
if (getSettings("showRefOnHover", "boolean") && !isMobile) {
drawer.hover(function(e) {
showTooltip($("#" + origin.attr("data-origin") + " .reference-hooker").get(0));
}, function(e) {
hideTooltip();
});
$(".reference-hooker").hover(function(e) {
$(".reference-hooker").hover(function(e) {
if ($(window).width() >= 768) {
showTooltip(this);
showDrawer(this.parentNode.href.split("#")[1], this.textContent);
drawer.css({visibility: "visible", width: "auto", height: "auto"});
drawer.css({ top: ($(this).offset().top - drawer.outerHeight()), left: $(this).offset().left });
drawer.stop().animate({opacity: 1}, 100);
}
}, function(e) {
}, function(e) {
if ($(window).width() >= 768) {
hideTooltip();
drawer.removeClass("visible");
drawer.animate({opacity: 0}, 100, function() { $(this).css({visibility: "hidden", width: 0, height: 0}); });
}
});
});
}
}
$(".reference-hooker").click(function(e) {
$(".reference-hooker").click(function(e) {
if ($(window).width() < 768) {
if (isMobile) {
e.preventDefault();
e.preventDefault();
e.stopPropagation();
e.stopPropagation();
showDrawer(this.parentNode.href.split("#")[1], this.textContent);
showDrawer(this.parentNode.getAttribute("href"), this.textContent, this.parentNode.parentNode.getAttribute("id"));
drawer.attr("style", "");
drawer.addClass("visible");
drawer.addClass("visible");
} else if (!getSettings("showRefOnHover")) {
} else if (!getSettings("showRefOnHover", "boolean")) {
e.preventDefault();
e.preventDefault();
e.stopPropagation();
e.stopPropagation();
showDrawer(this.parentNode.href.split("#")[1], this.textContent);
showTooltip(this);
drawer.css({visibility: "visible", width: "auto", height: "auto"});
drawer.css({ top: ($(this).offset().top - drawer.outerHeight()), left: $(this).offset().left });
drawer.stop().animate({opacity: 1}, 100);
} else {
$('html, body').animate({scrollTop: ($("#" + $(this).attr("data-target")).offset().top - 60)}, 400);
}
}
});
});
});
});

2015년 12월 14일 (월) 12:50 기준 최신판

var isMobile = navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i);
var drawer, origin, content;
function showDrawer(id, name, orgId) {
	origin.attr("href", id);
	origin.attr("data-origin", orgId.replace(/\./g, "\\."));
	origin.text(name);
	content.html($(id.replace(/\./g, "\\.") + " > .reference-text").html());
	drawer.addClass("visible");
}

function getSettings(key, type) {
	if ("localStorage" in window) {
		if (type === "boolean") {
			return (window.localStorage[key] === "true");
		} else {
			return window.localStorage[key];
		}
	} else {
		var value = "; " + document.cookie;
		var parts = value.split("; " + key + "=");
		if (parts.length == 2) {
			if (type === "boolean") {
				return (parts.pop().split(";").shift() === "true");
			} else {
				return parts.pop().split(";").shift();
			}
		}
	}
}

function setSettings(key, value) {
	if ("localStorage" in window) {
		window.localStorage[key] = value;
	} else {
		var now = new Date();
		var time = now.getTime();
		var expireTime = time + (10 * 365 * 24 * 24);
		now.setTime(expireTime);
		document.cookie = key + "=" + value + ";expires=" + now.toGMTString() +";path=/";
	}
}

function initSettings() {
	if (document.getElementById("reference-settings")) {
		$("#reference-settings").show();
		return;
	}
	var setbox = $("<form>").attr("id", "reference-settings").append(
		$("<div>").addClass("content").append(
			$("<label>").append(
				$("<input>").attr({"type": "checkbox", "name": "showRefOnHover", "checked": getSettings("showRefOnHover", "boolean")}),
				"갖다 대서 주석을 띄웁니다."
			)
		),
		$("<div>").addClass("foot").append(
			$("<input>").attr({"type": "button", "value": "취소"}).click(function() {
				$("#reference-settings").hide();
			}),
			$("<input>").addClass("save-settings").attr({"type": "submit", "value": "저장"})
		)
	).submit(function(e) {
		e.preventDefault();
		setSettings("showRefOnHover", $(this).find("[name=showRefOnHover]").is(":checked"));
		location.reload();
	});
	$(document.body).append(setbox);
}

function showTooltip(elem) {
	showDrawer(elem.parentNode.getAttribute("href"), elem.textContent, elem.parentNode.parentNode.getAttribute("id"));
	drawer.show();
	drawer.css({ top: ($(elem).offset().top - drawer.outerHeight()), left: $(elem).offset().left });
	drawer.stop().animate({opacity: 1}, 100);
}

function hideTooltip() {
	drawer.removeClass("visible");
	drawer.animate({opacity: 0}, 100, function() { $(this).hide(); });
}

$(document).ready(function($) {
	if (isMobile) {
		$(document.body).addClass("mode-drawer");
	} else {
		$(document.body).addClass("mode-tooltip");
	}
	/* create drawer */
	drawer = $("<div>").attr("id", "reference-drawer");
	origin = $("<a>").attr("id", "reference-origin").click(function() {
		$('html, body').animate({scrollTop: ($($(this).attr("href")).offset().top - 60)}, 400);
	});
	content = $("<span>").attr("id", "reference-drawer-text");
	var settingsIcon = $("<span>").addClass("settings-icon").click(function() {
		initSettings();
	});
	var closeDrawer = $("<span>").addClass("close-icon").click(function() {
		drawer.removeClass("visible");
	});
	drawer.append(settingsIcon);
	drawer.append(closeDrawer);
	drawer.append(origin);
	drawer.append(content);
	$(document.body).append(drawer);
	
	$(document).click(function(e) {
		if (!$(e.target).closest("#reference-drawer").length)  {
			if (!isMobile) {
				hideTooltip();
			} else {
				drawer.removeClass("visible");
			}
		}
	}).scroll(function(e) {
		if ("ontouchstart" in window && isMobile) {
			drawer.removeClass("visible");
		}
	});
	$(".reference a").each(function() {
		/* 상위 요소에 걸려있는 이벤트를 무시하기 위해 하위 요소를 만들어서 이벤트 확산을 방지합니다. */
		var span = document.createElement("span");
		span.className = "reference-hooker";
		span.appendChild(this.childNodes[0]);
		this.appendChild(span);
	});
	if (getSettings("showRefOnHover", "boolean") && !isMobile) {
		drawer.hover(function(e) {
			showTooltip($("#" + origin.attr("data-origin") + " .reference-hooker").get(0));
		}, function(e) {
			hideTooltip();
		});
		$(".reference-hooker").hover(function(e) {
			showTooltip(this);
		}, function(e) {
			hideTooltip();
		});
	}
	$(".reference-hooker").click(function(e) {
		if (isMobile) {
			e.preventDefault();
			e.stopPropagation();
			showDrawer(this.parentNode.getAttribute("href"), this.textContent, this.parentNode.parentNode.getAttribute("id"));
			drawer.addClass("visible");
		} else if (!getSettings("showRefOnHover", "boolean")) {
			e.preventDefault();
			e.stopPropagation();
			showTooltip(this);
		}
	});
});