-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.js
executable file
·90 lines (74 loc) · 2.25 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/// Copyright 2011, Ian Gilman
/// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
///
/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.js" />
/// <reference path="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.0.6-development-only.js" />
// ----------
function stateFromPath(path) {
var base = "/Menu/";
var parts = path.replace(base, "").split("/");
return {
meal: (parts.length >= 1 ? parts[0] : ""),
dish: (parts.length >= 2 ? parts[1] : "")
};
}
// ----------
function updateState(state) {
if (!state)
state = stateFromPath(location.pathname);
// ___ menu
var $selectedMenu = $(".menu[data-meal='" + state.meal + "']");
$(".menu").not($selectedMenu).removeClass("active");
$selectedMenu.addClass("active");
var $selectedItem = $(".menu-item[data-dish='" + state.dish + "']");
$(".menu-item").not($selectedItem).removeClass("selected");
$selectedItem.addClass("selected");
// ___ preview
var $dishContainer = $(".preview-dish");
var $oldDish = $dishContainer.find("section");
if (!state.dish) {
$oldDish.fadeOut(function() {
$oldDish.remove();
});
return;
}
var url = "/Preview/Index/" + state.dish;
$.ajax({
url: url,
dataType: "html",
success: function(data) {
var $newDish = $(data);
$newDish.find("img").load(function() {
$newDish.fadeIn(function() {
$newDish.css("z-index", 0);
});
$oldDish.fadeOut(function() {
$oldDish.remove();
});
});
$newDish
.css("z-index", 1)
.hide()
.appendTo($dishContainer);
}
});
}
// ----------
$(document).ready(function () {
if (!Modernizr.history)
return;
// ___ history event
$(window).bind("popstate", function(event) {
updateState(history.state);
});
// ___ clicks
$(".menu-item").click(function(event) {
event.preventDefault();
event.stopPropagation();
var $target = $(this);
var url = $target.find("a").attr("href");
var state = stateFromPath(url);
history.pushState(state, "", url);
updateState(state);
});
});