This repository has been archived by the owner on Nov 18, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
executable file
·163 lines (151 loc) · 4.91 KB
/
main.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
/* eslint-env browser */
import autosize from 'autosize';
import jquery from 'jquery';
import _ from 'underscore';
import 'jquery.scrollto';
(function($) {
'use strict';
// Check to make sure service workers are supported in the current browser,
// and that the current page is accessed from a secure origin. Using a
// service worker from an insecure origin will trigger JS console errors. See
// http://www.chromium.org/Home/chromium-security/prefer-secure-origins-for-powerful-new-features
var isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);
if (
'serviceWorker' in navigator &&
(window.location.protocol === 'https:' || isLocalhost)
) {
navigator.serviceWorker
.register('service-worker.js')
.then(function(registration) {
// updatefound is fired if service-worker.js changes.
registration.onupdatefound = function() {
// updatefound is also fired the very first time the SW is installed,
// and there's no need to prompt for a reload at that point.
// So check here to see if the page is already controlled,
// i.e. whether there's an existing service worker.
if (navigator.serviceWorker.controller) {
// The updatefound event implies that registration.installing is set:
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event
var installingWorker = registration.installing;
installingWorker.onstatechange = function() {
switch (installingWorker.state) {
case 'installed':
// At this point, the old content will have been purged and the
// fresh content will have been added to the cache.
// It's the perfect time to display a "New content is
// available; please refresh." message in the page's interface.
break;
case 'redundant':
throw new Error(
'The installing ' + 'service worker became redundant.'
);
default:
// Ignore
}
};
}
};
})
.catch(function(e) {
/* eslint-disable no-console */
console.error('Error during service worker registration:', e);
/* eslint-enable no-console */
});
}
// Your custom JavaScript goes here
const $body = $('body');
$body
.on(
'click',
'.js-smooth-scroll',
_.debounce(
e => {
e.preventDefault();
const hash = $(e.currentTarget).attr('href');
const $scrollTarget = $(hash);
$(window).scrollTo($scrollTarget, {
duration: 300,
offset: window.matchMedia('(min-width: 768px)').matches
? -$('.main-header').outerHeight()
: 0
});
},
200,
true
)
)
.on(
'click',
'.js-toggle-nav',
_.debounce(
e => {
$(e.currentTarget).blur();
$('body').toggleClass('nav-active');
},
200,
true
)
);
$(window).on('scroll', () => {
if (document.body.scrollTop === 0) {
$body.addClass('scroll-top');
} else {
$body.removeClass('scroll-top');
}
});
$('.js-ajax-form').on('submit', e => {
e.preventDefault();
const $form = $(e.currentTarget);
if ($form.data('loading')) {
return false;
}
$form.data('loading', true);
/* eslint-disable quote-props*/
// First we're going to submit the email signup to the Action Network API.
// Then we're going to submit the form to the Netlify form handler..
$.ajax({
url: 'https://actionnetwork.org/api/v2/people/',
type: 'POST',
data: JSON.stringify({
person: {
email_addresses: [
{
address: $form.find('[name="email"]').val()
}
]
}
}),
headers: {
'OSDI-API-Token': '6ff91ed1255d1966758bf3449043077a',
'Content-Type': 'application/json'
},
dataType: 'json'
})
.then(() => {
let dfd = new $.Deferred();
if ($form.attr('action')) {
dfd = $.post($form.attr('action'), $form.serialize());
} else {
dfd.resolve();
}
return dfd;
})
.then(() => {
$form.find('.form-confirmation').removeClass('hidden');
$form.find('input, textarea').val('');
})
.always(() => {
$form.data('loading', false);
});
/* eslint-enable quote-props*/
});
autosize($('textarea'));
})(jquery);