forked from cmpolis/Pagify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagify.js
73 lines (60 loc) · 1.97 KB
/
pagify.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
/*
* Pagify - A jquery plugin for effortlessly creating single page web sites.
*
* Licensed under the MIT:
* http://www.opensource.org/licenses/mit-license.php
*
* Copyright (c) 2011, Chris Polis
*/
(function($) {
$.fn.pagify = function(options) {
var self = this;
this.defaults = {
pages: [],
'default': null,
animation: 'show',
onChange: function (page) {},
cache: false
};
this.settings = $.extend({}, this.defaults, options);
// Run after loading if caching, otherwise run immediately
var runAfterLoading = function() {
self.switchPage = function(page) {
// Page is selected from: passed in value, window.location, default
if(!page) {
page = window.location.hash.replace('#','') || self.settings['default'];
}
// Load page content from cache
if(self.settings.cache) {
$(self).hide().html(self.pages[page])[self.settings.animation]();
self.settings.onChange(page);
// Fetch page content
} else {
$.get(page+'.html', function(content) {
$(self).hide().html(content)[self.settings.animation]();
self.settings.onChange(page);
}, 'text');
}
}
// Respond to hash changes
$(window).bind('hashchange', function() {
self.switchPage();
});
// Load initial page - current hash or default page
if(window.location.hash) self.switchPage();
else if(self.settings['default']) self.switchPage(self.settings['default']);
};
// Cache pages
if(self.settings.cache) {
self.pages = {};
var pageLoads = self.settings.pages.length;
$.each(self.settings.pages, function(ndx, page) {
$.get(page+'.html', function(content) {
self.pages[page] = content;
pageLoads--;
if(!pageLoads) runAfterLoading();
}, 'text');
});
} else runAfterLoading();
};
})(jQuery);