-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (142 loc) · 4.06 KB
/
script.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
/**
* Wikimedia Coding Exercise Script.js
*/
/*jslint plusplus:true */
/*globals console, Node, escape */
$(function(){
'use strict';
var citeCounter = 0, hasStorage, renderMarker;
renderMarker = function(id) {
var citation, marker = $('a.citation[data-cite-id="'+id+'"]');
if (hasStorage) {
citation = localStorage.getItem('citation.'+id);
if (citation) {
marker.text(+id + 1);
marker.parent('sup.missing').removeClass('missing').addClass('sourced');
}
}
};
// test for localStorage
hasStorage = (function() {
try {
localStorage.setItem('hasStorage', 'true');
localStorage.removeItem('hasStorage');
return true;
} catch(e) {
return false;
}
}());
function toggleSection(id) {
var heading = $(id),
section = heading.next('.section');
if (section.css('display') === 'none') {
section.prev('h2').children('button').click();
}
}
// build fixed ToC
$('#content>h2').each(function() {
var id = this.id,
navList = $('#TopNav>ul'),
text = $(this).contents()
.filter(function() {
return this.nodeType === Node.TEXT_NODE;
}).text();
if (id) {
$('<li><a href="#' + escape(id) + '" class="toc-link">no-XSS</a></li>')
.appendTo(navList).find('a.toc-link').text(text);
}
});
// highlight active nav item
$(window).bind('hashchange', function() {
var fragment = location.hash;
$('#TopNav>ul>li').removeClass('active');
$('#TopNav>ul>li>a[href$="'+fragment+'"]').parent().addClass('active');
toggleSection(fragment);
});
$('#TopNav>ul>li>a').click(function() {
$(window).trigger('hashchange');
});
// toggle section expand/collapse
$('.toggle>button').click(function(event) {
var heading = $(this).parents('.toggle'),
section = heading.next('.section'),
id = heading.attr('id'),
buttonText;
event.stopPropagation(); // don't need an infinite loop
section.toggle();
buttonText = section.css('display') === 'none'? 'show' : 'hide';
$(this).text(buttonText);
if (buttonText === 'show') {
$('div.edit-citation[data-cite-section="' + id + '"]').hide();
}
});
$('.toggle').click(function() {
$('button', this).click();
});
// easy citation
$('sup.missing:contains("citation needed")').each(function() {
$(this).html('<a href="#" class="citation">citation needed</a>');
});
$('a.citation')
.each(function(){
var id = citeCounter++, citation;
$(this).attr('data-cite-id', id);
renderMarker(id);
})
.click(function() {
if (!hasStorage) {
return false;
}
var id = $(this).attr('data-cite-id'),
citeEdit = $('div.edit-citation[data-cite-id="'+id+'"]'),
src, content, editBox,
pos = $(this).offset(),
section = $(this).parents('.section'),
sectionId = section.prev('h2').attr('id');
pos.top = pos.top + $(this).height();
if (!citeEdit.length) {
content = localStorage.getItem('citation.'+id);
src = $('#TmplEditCitation').html();
citeEdit = $(src)
.attr('data-cite-id', id)
.attr('data-cite-section', sectionId)
.appendTo('body');
editBox = citeEdit.children('.citation-content');
editBox.text(content);
} else {
citeEdit.toggle();
if (citeEdit.css('display') === 'none') {
// offset calculations become inaccurate
return;
}
}
if (pos.left + citeEdit.outerWidth() > $(document).innerWidth()) {
pos.left = $(document).innerWidth() - citeEdit.outerWidth();
}
citeEdit.offset(pos);
});
$('body')
.on('click', 'button.citation-cancel', function() {
var citeEdit = $(this).parent();
citeEdit.toggle();
})
.on('click', 'button.citation-save', function() {
if (!hasStorage) {
return false;
}
var citeEdit = $(this).parent(),
id = citeEdit.attr('data-cite-id'),
content = $(this).prev('.citation-content').text();
localStorage.setItem('citation.'+id, content);
citeEdit.toggle();
renderMarker(id);
});
// don't navigate
$('body').on('click', 'a.citation', function() {
return false;
});
// initialize UI state
$('.section').hide();
$('.toggle>button').text('show');
$(window).trigger('hashchange');
});