-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathdjangoproject.js
More file actions
250 lines (190 loc) · 6.68 KB
/
Copy pathdjangoproject.js
File metadata and controls
250 lines (190 loc) · 6.68 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Toggle persistent display of documentation version and language options
document.querySelectorAll('.doc-switcher li.current').forEach(function (el) {
el.addEventListener('click', function () {
this.parentElement.classList.toggle('open');
});
});
// Propagate the current fragment identifier when switching docs versions
document.querySelectorAll('#doc-versions a').forEach(function (el) {
el.addEventListener('click', function () {
this.href = this.href.split('#')[0] + window.location.hash;
});
});
// Fade out and remove message elements when close icon is clicked
document.querySelectorAll('.messages li .close').forEach(function (el) {
el.addEventListener('click', function () {
this.parentElement.addEventListener('transitionend', function () {
this.style.display = 'none';
});
this.parentElement.classList.add('fade-out');
});
});
// Check all console tab inputs of the same type when one's label is clicked
document.querySelectorAll('.console-block label').forEach(function (el) {
el.addEventListener('click', function (e) {
const input_id = e.currentTarget.getAttribute('for');
const selector = input_id.endsWith('unix') ? '.c-tab-unix' : '.c-tab-win';
document.querySelectorAll(selector).forEach(function (input_el) {
input_el.checked = true;
});
});
});
// Add animation class to feature icons when they are fully visible
(function () {
const observer = new IntersectionObserver(
function (entries) {
entries.forEach(function (entry) {
if (!entry.isIntersecting) {
return;
}
entry.target.classList.add('inview');
observer.unobserve(entry.target);
});
},
{ threshold: 1.0 },
);
document.querySelectorAll('.list-features i').forEach(function (el) {
observer.observe(el);
});
})();
// Toggle mobile menu on button click
document.querySelector('.menu-button').addEventListener('click', function () {
const menu_el = document.querySelector('#top nav');
this.classList.toggle('active');
menu_el.classList.toggle('active');
});
// Update search input placeholder text based on the user's operating system
(function () {
const el = document.getElementById('id_q');
if (!el) {
return;
}
const original_placeholder = el.getAttribute('placeholder');
const is_mac = navigator.userAgent.indexOf('Mac') !== -1;
const new_value = `${original_placeholder} (${is_mac ? '⌘\u200aK' : 'Ctrl+K'})`;
el.setAttribute('placeholder', new_value);
})();
// Focus, select, and scroll to search input when key combination is pressed
window.addEventListener('keydown', function (e) {
const is_ctrl_k = (e.metaKey || e.ctrlKey) && e.key === 'k';
if (!(is_ctrl_k || e.key === '/')) {
return;
}
if (['INPUT', 'TEXTAREA'].includes(document.activeElement.tagName)) {
return;
}
e.preventDefault();
const el = document.querySelector('#id_q');
el.select();
el.focus();
window.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
});
// Add copy buttons to code snippets and console tabs
(function () {
const button_el = document.createElement('span');
button_el.classList.add('btn-clipboard');
button_el.setAttribute('title', 'Copy this code');
button_el.innerHTML = '<i class="icon icon-clipboard"></i>';
document
.querySelectorAll('.snippet-filename, .code-block-caption')
.forEach(function (el) {
el.insertBefore(button_el.cloneNode(true), null);
});
document.querySelectorAll('.console-block > section').forEach(function (el) {
const highlight_el = el.querySelector('.highlight');
if (highlight_el) {
highlight_el.appendChild(button_el.cloneNode(true));
}
});
})();
// Attach copy functionality to clipboard buttons
document.querySelectorAll('.btn-clipboard').forEach(function (el) {
el.addEventListener('click', function () {
const existing_el = this.querySelector('.clipboard-success');
if (existing_el) {
existing_el.remove();
}
const success_el = document.createElement('span');
success_el.classList.add('clipboard-success');
this.prepend(success_el);
success_el.addEventListener('transitionend', function () {
this.remove();
});
function on_success() {
success_el.innerText = 'Copied!';
setTimeout(function () {
success_el.classList.add('fade-out');
}, 1000);
}
function on_error() {
success_el.innerText = 'Could not copy!';
setTimeout(function () {
success_el.classList.add('fade-out');
}, 5000);
}
let text;
const console_section = this.closest('.console-block > section');
const prompt_regex = /^\$ |^\.\.\.\\>/gm;
if (console_section) {
const pre_el = console_section.querySelector('.highlight pre');
text = pre_el.textContent.replace(prompt_regex, '');
} else {
text = this.parentElement.nextElementSibling.textContent;
}
navigator.clipboard.writeText(text.trim()).then(on_success, on_error);
});
});
// Update donate button text on fundraising page based on interval selection
(function () {
const el = document.querySelector('#donate #id_interval');
if (!el) {
return;
}
el.addEventListener('change', function () {
const text = this.value === 'onetime' ? 'Donate' : `Donate ${this.value}`;
document.getElementById('donate-button').value = text;
});
})();
// Manage custom donation amount input on fundraising page
(function () {
const el = document.querySelector('#donate #id_amount');
if (!el) {
return;
}
el.addEventListener('change', function () {
if (this.value !== 'custom') {
return;
}
const input_el = document.createElement('input');
input_el.setAttribute('type', 'number');
input_el.setAttribute('name', 'amount');
const custom_donation_el = document.querySelector('.custom-donation');
custom_donation_el.appendChild(input_el);
custom_donation_el.style.display = 'block';
this.remove();
input_el.focus();
input_el.value = '25';
});
})();
// Manage amount and membership level fields on corporate membership page
(function () {
const form_el = document.querySelector('.corporate-membership-join-form');
if (!form_el) {
return;
}
const amount_el = form_el.querySelector('#id_amount');
const level_el = form_el.querySelector('#id_membership_level');
const levels = [-Infinity, 2000, 5000, 12500, 30000, 100000];
amount_el.addEventListener('change', function () {
let value;
for (let i = 0; i < levels.length; i++) {
if (this.value >= levels[i]) {
value = i;
}
level_el.value = value || '';
}
});
level_el.addEventListener('change', function () {
amount_el.value = this.value ? levels[Number(this.value)] : '';
});
})();