-
Notifications
You must be signed in to change notification settings - Fork 0
/
presenter.js
87 lines (74 loc) · 2.44 KB
/
presenter.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
$(function() {
var $examples = $('.dropdown-menu');
var pages = {};
var loadPage = function(url) {
if(url in pages) {
var $page = $(pages[url]);
var title = $page.find('title').text();
var less = $page.find('script[type=less]').text().trim();
var html = $page.find('script[type=html]').text().trim();
lessEditor.setValue(less);
htmlEditor.setValue(html);
$('h1').html(title);
} else {
$.ajax({
url: url,
cache: false,
dataType: 'html',
success: function(data) {
var $page = $(data);
pages[url] = data;
loadPage(url);
}
});
}
};
var getCurrentPage = function() {
var hash = (''+window.location.hash);
if(hash) {
return hash.substring(1);
}
};
$(window).on('hashchange', function() {
loadPage(getCurrentPage());
});
$(document).on('click', '.examples .next, .examples .prev', function() {
var $curr = $examples.find('a[href="#'+getCurrentPage()+'"]');
if($(this).is('.next')) {
window.location.hash = $curr.parent().next().children().attr('href');
} else {
window.location.hash = $curr.parent().prev().children().attr('href');
}
});
var findPages = function(idx) {
var url = 'pages/' + idx.join('.') + '.html';
$.ajax({
url: url,
cache: false,
dataType: 'html',
success: function(data) {
var $page = $(data);
var title = $page.find('title').text();
pages[url] = data;
var $el = $('<li><a href="#' + url + '">' + title + '</a></li>');
$examples.append($el);
var $li = $examples.children('li').detach();
$li.sort(function(e1,e2) {
var s1 = $(e1).children().attr('href').replace(/\.html/g,'');
var s2 = $(e2).children().attr('href').replace(/\.html/g,'')
return s1 < s2 ? -1 : 1;
});
$examples.append($li);
var sub = idx.slice();
sub.push(1);
findPages(sub); // test for subpages
idx[idx.length-1] = idx[idx.length-1] + 1;
findPages(idx); // test for next pages
}
});
};
findPages([1]);
if(getCurrentPage()) {
loadPage(getCurrentPage());
}
});