-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookstrap.js
180 lines (127 loc) · 4.09 KB
/
bookstrap.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// functions
var DEBUG = false,
URLparams = {
'rParse': function (SearchStr) {
if (!this.parsed) {
var params = {}, // will be returned
opts = SearchStr || window.location.search; // "?debug=true&fun"
opts = opts.substr(1).split('&') // ["debug=true", "fun"]
opts.forEach(function (str) {
var optArr = str.split('=');
// params.push({ (optArr[0]): (optArr[1])});
if (optArr[1] === undefined || optArr[1] === "true" || optArr[1] === "yes" || optArr[1] === "y" ) {
optArr[1] = true;
}
if (optArr[1] === "false" || optArr[1] === "no" || optArr[1] === "n" ) {
optArr[1] = false;
}
params[optArr[0]] = optArr[1];
}); //
return params;
}
},
'apply': function () {
var params = this.rParse();
console.log('parameters: ', params);
// debug
if (params.debug) {
window.DEBUG=true;
console.log("Debug!");
$('body').addClass('debug');
}
['grid', 'stripes'].forEach(function (p) {
if (params[p]) {
console.log(p)
$('body').addClass(p);
}
});
// fun
if (params.fun) {
window.FUN=true;
console.log("Fun!");
$('body').addClass('fun');
}
}
},
util = {};
util.debug = function (str, obj) {
if (DEBUG) {
console.log(str.toString(), obj)
}
}
util.TOC = function () {
$('.navbar ul.nav').append($('<ul id="TOC-navbar"></ul>'));
$('#TOC-navbar').append($('#TOC ul')[0].children).addClass('dropdown-menu');
}
// hacks
var hacks = {
// fix ID's ending with a dot
'fixIDsAndHrefs': function () {
var ids = [], hrefs = [];
// jquery: loop over all elements with an 'id' property
$('[id]').each(function() {
// … adding them to a list of id strings.
ids.push(($(this).attr('id')).toString());
});
hrefs = Array.prototype.slice.call(document.querySelectorAll('a'));
// console.log('ids:', ids);
// check for bad strings
ids.forEach(function (id) {
// is last character a "." ?
if (id.indexOf('.') !== -1) {
// remove the trailing dot (last char)
var fixedid = id;
while (fixedid.indexOf('.') !== -1) {
fixedid = fixedid.replace(/\./,'');
}
util.debug("fixed:", fixedid);
// DOM: get the el with the bad id, set new id
// jquery: get the el, set attr
$(document.getElementById(id)).attr('id', fixedid);
// also fix any hrefs with that value
hrefs.forEach(function (href) {
if (href.getAttribute('href') === '#' + id) {
href.setAttribute('href', '#' + fixedid);
util.debug("fixed href:", '#' + fixedid);
}
});
}
});
util.debug('fixIDsAndHrefs(): done');
}
}
var bootstrap = function (document) {
// add class `.nav` to all `<ul>` elements inside a `<nav>`:
$('nav ul').addClass('nav');
util.debug('bootstrap(): done');
};
// loading
function startup () {
URLparams.apply();
hacks.fixIDsAndHrefs();
bootstrap();
// activate bootstrap's scrollspy
// - add data to targets
$('body').attr('data-spy', 'scroll');
// - activate srollspy with target #TOC
$('[data-spy="scroll"]').scrollspy({
"target": "#TOC",
"offset": 0 //px
});
$('body [id]').each(function () {
$(this).on('activate', function (e) {
console.log(e);
console.log(e["target"].children);
})
});
// event handler when window sizing changes (also on zoom in webkit)
window.onresize = function resizeHandler(argument) {
util.debug("Event Handler: Resize!");
// refresh scrollspy
$('[data-spy="scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh')
});
};
// util.TOC();
};
$(document).ready(startup)