-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnasgor.js
117 lines (109 loc) · 4.56 KB
/
nasgor.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// NASGOR.js is Navigation Single-Page Orientation
// Make single page app with jquery with easy setup and small size library
// yout should import script jquery before script nasgor.js
// EXAMPLE:
// <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
// <script src="nasgor.js"></script>
// the data onclick navigation with tag <a> or some tag:
// navId: id for key the <a> href button to swith page in container
// route: is last pieces in url web, after symbol #, this mean route or link the page
// urlHtml: this address place html with extension (html) from root project (index.html)
// for DATA
// var data: "containerId", "listData", "isCantLeaveApp"
// containerId: this id for container can be switch page from any html
// listData: this id for container can be switch page from any html
// EXAMPLE:
// var listData = [
// {
// navId: "nav-first", // is navigation id for element if clicked
// route: "home", // after navigation id clicked, do change route last url endpoint
// urlHtml: "pages/mainpage.html" // this meanlink file .html for load page, location from root
// },
// ]
// for CALLBACK
// callback for do action if some event clicked or loaded
// var callback: onReady, onChange, onPush, onPop, onLastPop
// onReady: this mean run if function first start/ page first loaded
// onChange: this mean run if have any doing for changing route/ loaded html
// onPush: this mean run if have any doing for forward clicked to change the page route has loaded html
// onPop: this mean run if have any doing for backward clicked to change the page route has loaded html
// onLastPop: this mean run if have any doing for backward clicked and this is the first page when site is opened.
function initNasgor(data, callback) {
function getRouteNasgor() {
try {
var url = window.location.href
var route = url.split("#")[1]
return route
} catch (error) {
return ""
}
}
function getItemByRouteNasgor(route, list) {
try {
return list.find(function(e) {
return e.route == route
})
} catch (error) {
return null
}
}
$(document).ready(function(){
var isHasClicked = false
var isCantLeaveApp = false
if (data.isCantLeaveApp === undefined || data.isCantLeaveApp == null) {
isCantLeaveApp = false
} else {
isCantLeaveApp = data.isCantLeaveApp
}
$(`#nasgor-nav`).remove()
$("body").append(`<span id="nasgor-nav"></span>`)
data.listData.forEach(
function(e, i) {
$(`#nasgor-nav`).append(
`<a id="nasgor-${e.route}" href="#${e.route}"></a>`
)
$(`#${e.navId}`).click(
function() {
if(e.route == getRouteNasgor()) {
return
}
var navId = getItemByRouteNasgor(e.route, data.listData).navId
isHasClicked = true
$(`#${data.containerId}`).load(e.urlHtml)
$(`#nasgor-${e.route}`)[0].click();
callback.onChange(navId, e.route)
callback.onPush(navId, e.route)
}
)
}
);
window.onhashchange = function() {
var route = getRouteNasgor()
var item = getItemByRouteNasgor(route, data.listData)
if(item === undefined) {
var routeDefault = getRouteNasgor()
var firstData = data.listData[0]
callback.onPop(firstData.navId, routeDefault)
callback.onLastPop(firstData.navId, routeDefault)
if (isCantLeaveApp) {
$(`#${data.containerId}`).load(firstData.urlHtml)
$(`#nasgor-${firstData.route}`)[0].click();
}
return
}
callback.onChange(item.navId, route)
if (isHasClicked) {
isHasClicked = false
return
}
$(`#${data.containerId}`).load(item.urlHtml)
$(`#nasgor-${item.route}`)[0].click();
callback.onPop(item.navId, route)
}
var route = getRouteNasgor()
var firstData = data.listData[0]
callback.onReady(firstData.navId, route)
$(`#${data.containerId}`).load(firstData.urlHtml)
$(`#nasgor-${firstData.route}`)[0].click();
})
}