From 8345760abf4953980a85e0d5f9cade421f73d4ff Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Fri, 17 Apr 2015 23:34:27 +0300 Subject: [PATCH] Upgrade to ECMAScript 6 --- js/10-modal.js | 12 +++++------ js/80-sidebar.js | 2 +- js/90-divine-format.js | 4 +--- js/90-form.js | 19 +++++++---------- js/90-mobilenav.js | 4 +--- js/90-nav.js | 20 +++++++++--------- js/90-reqjs.js | 6 +----- js/90-themes.js | 10 +++------ js/90-timers.js | 10 ++++----- js/90-tracker.js | 48 ++++++++++++++++++------------------------ js/95-login.js | 4 ++-- make_static.PL | 2 +- 12 files changed, 57 insertions(+), 84 deletions(-) diff --git a/js/10-modal.js b/js/10-modal.js index 980bab7..1c290ef 100644 --- a/js/10-modal.js +++ b/js/10-modal.js @@ -1,4 +1,4 @@ -var modal, hiding; +let modal, hiding; function show_modal () { modal.classList.remove('hidden'); @@ -13,19 +13,17 @@ function hide_modal () { $(function() { modal = $('.modal')[0]; - $(modal).on('transitionend', function (){ + $(modal).on('transitionend', el => { if(!document.body.classList.contains('modal-open')) modal.classList.add('hidden'); }); - $('.backdrop,.modal').each(function(el){ - document.body.appendChild(el); - }); - $('#solution').on('click', function (e){ + $('.backdrop,.modal').each(el => document.body.appendChild(el) ); + $('#solution').on('click', e => { show_modal(); e.preventDefault(); }); $('.backdrop').on('click', hide_modal); - window.onkeyup = function (event) { + window.onkeyup = event => { if (event.keyCode === 27) hide_modal(); } diff --git a/js/80-sidebar.js b/js/80-sidebar.js index ee77744..505dc0f 100644 --- a/js/80-sidebar.js +++ b/js/80-sidebar.js @@ -1,6 +1,6 @@ $(function() { if(!document.getElementById('sidebar')) { - var content = $('main')[0]; + let content = $('main')[0]; content.innerHTML = '
' + content.innerHTML + '
'; content.classList.add('row'); } diff --git a/js/90-divine-format.js b/js/90-divine-format.js index e44bf90..ab5a5f2 100644 --- a/js/90-divine-format.js +++ b/js/90-divine-format.js @@ -37,6 +37,4 @@ function divine_format() { $('#prog_format')[0].value = ext_table[ext]; } -$(function() { - $('#prog').on('change', divine_format); -}); +$(() => $('#prog').on('change', divine_format)); diff --git a/js/90-form.js b/js/90-form.js index 25a11f7..0b76776 100644 --- a/js/90-form.js +++ b/js/90-form.js @@ -1,17 +1,14 @@ $(function(){ - $('.jsform > input[type="submit"]').on('click', function(e){ - var form_data = new FormData(this.parentElement); - var form = this.parentNode; - var xhr = new XMLHttpRequest(); + let result = $('#result')[0]; + $('.jsform > input[type="submit"]').on('click', e => { + let form_data = new FormData(this.parentElement); + let form = this.parentNode; + let xhr = new XMLHttpRequest(); xhr.open(form.getAttribute('method'), form.getAttribute('action')); - xhr.onload = function() { - $('#result')[0].innerHTML = this.responseText; - }; - xhr.onerror = function() { - $('#result')[0].innerHTML = 'Error!'; - }; + xhr.onload = () => result.innerHTML = this.responseText; + xhr.onerror = () => result.innerHTML = 'Error!'; window.scrollTo(0, 0); - $('#result')[0].innerHTML = 'Loading...'; + result.innerHTML = 'Loading...'; xhr.send(form_data); e.preventDefault(); }); diff --git a/js/90-mobilenav.js b/js/90-mobilenav.js index 8577ecc..fb575d9 100644 --- a/js/90-mobilenav.js +++ b/js/90-mobilenav.js @@ -3,7 +3,5 @@ $(function(){ return; $('#title')[0].insertAdjacentHTML('beforebegin', '
Tap title to toggle menu
'); $('nav')[0].classList.add('hidden'); - $('#title').on('click', function () { - $('nav')[0].classList.toggle('hidden'); - }); + $('#title').on('click', () => $('nav')[0].classList.toggle('hidden')); }); diff --git a/js/90-nav.js b/js/90-nav.js index 4b59af4..03dcb82 100644 --- a/js/90-nav.js +++ b/js/90-nav.js @@ -1,16 +1,16 @@ -var NAVS = { - 'home': /^\/$/, - 'pb': /^\/(?:pb|sol)\//, - 'ct': /^\/(?:ct|st|ed)\//, - 'log': /^\/log\//, - 'us': /^\/us\//, - 'account': /^\/account$/, - 'contribute': /^\/contribute$/, +const NAVS = { + home: /^\/$/, + pb: /^\/(?:pb|sol)\//, + ct: /^\/(?:ct|st|ed)\//, + log: /^\/log\//, + us: /^\/us\//, + account: /^\/account$/, + contribute: /^\/contribute$/, }; $(function(){ - var path = location.pathname; - for (var nav in NAVS) + let path = location.pathname; + for (let nav in NAVS) if(path.match(NAVS[nav])) $('#nav-' + nav)[0].classList.add('active'); }); diff --git a/js/90-reqjs.js b/js/90-reqjs.js index c4b492f..e35dde5 100644 --- a/js/90-reqjs.js +++ b/js/90-reqjs.js @@ -1,5 +1 @@ -$(function() { - $('.reqjs').each(function(e){ - e.classList.remove('reqjs'); - }); -}); +$(() => $('.reqjs').each(e => e.classList.remove('reqjs'))); diff --git a/js/90-themes.js b/js/90-themes.js index c280266..c872919 100644 --- a/js/90-themes.js +++ b/js/90-themes.js @@ -1,7 +1,5 @@ function set_style(name){ - $('link[rel~="stylesheet"]').each(function (e){ - e.disabled = true; - }); + $('link[rel~="stylesheet"]').each(e => e.disabled = true); $('link[title="' + name + '"]')[0].disabled = false; localStorage.setItem("theme", name); } @@ -13,12 +11,10 @@ $(function() { '
  • White'; var sidebar = $('#sidebar')[0]; sidebar.insertBefore(m(theme_ul), sidebar.firstChild); - $('#themes a').on('click', function(e){ - set_style(this.dataset.theme); - }); + $('#themes a').on('click', e => set_style(this.dataset.theme)); }); -var theme = localStorage.getItem("theme"); +let theme = localStorage.getItem("theme"); if(theme) { if(theme == "cerulean" || theme == "cosmo") theme = "readable"; diff --git a/js/90-timers.js b/js/90-timers.js index 7281f6f..af971be 100644 --- a/js/90-timers.js +++ b/js/90-timers.js @@ -23,9 +23,7 @@ function update_timer(timer){ } } -$(function() { - $('.timer').each(function(item){ - update_timer(item); - setInterval(function(){update_timer(item)}, 1000); - }); -}); +$(() => $('.timer').each(item => { + update_timer(item); + setInterval(() => update_timer(item), 1000); +})); diff --git a/js/90-tracker.js b/js/90-tracker.js index 42ee1f7..5e1c9a2 100644 --- a/js/90-tracker.js +++ b/js/90-tracker.js @@ -1,30 +1,25 @@ -var TICK = '✔'; -var XMARK = '✘'; +const TICK = '✔'; +const XMARK = '✘'; function clean_tracker(){ - $('.tracker-mark').each(function(e){ - e.parentNode.removeChild(e); - }); - $('.tracker-info').each(function(e){ + $('.tracker-mark').each(e => e.parentNode.removeChild(e)); + $('.tracker-info').each(e => { e.classList.remove('tracker-info'); e.classList.remove('info'); }); } -var tracker; +let tracker; function update_tracker(){ - var data = localStorage.getItem('tracker_data'); - var user = localStorage.getItem('tracker_username'); + let data = localStorage.getItem('tracker_data'); + let user = localStorage.getItem('tracker_username'); if(!data) return; data = JSON.parse(data); - var solved = {}; - var attempted = {}; - var solvednr = 0; - var attemptednr = 0; - data.problems.forEach(function(e){ - if(e.solved){ + let contests = {}, solved = {}, attempted = {}, solvednr = 0, attemptednr = 0; + data.problems.forEach(e => { + if(e.solved) { solved[e.problem] = 1; solvednr++; } else { @@ -33,10 +28,7 @@ function update_tracker(){ } }); - var contests = {}; - data.contests.forEach(function(e){ - contests[e.contest] = 1; - }); + data.contests.forEach(e => contests[e.contest] = 1); clean_tracker(); $('#tracker_userlink')[0].innerHTML = data.name; @@ -46,8 +38,8 @@ function update_tracker(){ $('#tracker_log')[0].setAttribute('href', '/log/?owner=' + user); if(location.pathname == '/pb/') - $('table .name a').each(function(el) { - var id = el.getAttribute('href').split('?', 2)[0]; + $('table .name a').each(el => { + let id = el.getAttribute('href').split('?', 2)[0]; if(solved[id]) el.insertAdjacentHTML('beforebegin', TICK); else if(attempted[id]) @@ -55,15 +47,15 @@ function update_tracker(){ }); if(location.pathname == '/ct/') - $('table .name a').each(function(el) { - var id = el.getAttribute('href').substr(4); + $('table .name a').each(el => { + let id = el.getAttribute('href').substr(4); if(contests[id]) el.insertAdjacentHTML('beforebegin', TICK); }); if(location.pathname == '/log/') - $('table tbody tr').each(function(el) { - var owner = el.querySelector('.owner a'); + $('table tbody tr').each(el => { + let owner = el.querySelector('.owner a'); if(!owner || owner.getAttribute('href') != '/us/' + user) return; var state = el.getElementsByClassName('r0').length ? TICK : XMARK; @@ -71,7 +63,7 @@ function update_tracker(){ }); if(location.pathname.match(/^\/st\//) || location.pathname == '/us/') - $('table tbody tr').each(function(el) { + $('table tbody tr').each(el => { if(el.querySelector('.user a').getAttribute('href') == '/us/' + user){ el.classList.add('info'); el.classList.add('tracker-info'); @@ -94,7 +86,7 @@ function refresh_tracker(){ var user = localStorage.getItem('tracker_username'); var xhr = new XMLHttpRequest(); xhr.open('GET', '/us/' + user + '?format=json'); - xhr.onload = function () { + xhr.onload = () => { localStorage.setItem('tracker_data', this.responseText); localStorage.setItem('tracker_lastfetch', Date.now()); update_tracker(); @@ -116,7 +108,7 @@ $(function(){ sidebar.insertBefore(tracker, sidebar.firstChild); $('#tracker_stop').on('click', stop_tracking); $('#track_user').on('click', function() { stop_tracking(); start_tracking(this.dataset.user) }); - $('#submitform').on('submit', function() { localStorage.removeItem('tracker_lastfetch') }); + $('#submitform').on('submit', () => localStorage.removeItem('tracker_lastfetch')); if(localStorage.getItem('tracker_username')) start_tracking(localStorage.getItem('tracker_username')); diff --git a/js/95-login.js b/js/95-login.js index 3173895..bf16d31 100644 --- a/js/95-login.js +++ b/js/95-login.js @@ -3,10 +3,10 @@ $(function(){ var login = document.createElement('div'); login.innerHTML = 'Log in'; sidebar.insertBefore(login, sidebar.firstChild); - $('#login').on('click', function(e) { + $('#login').on('click', e => { var xhr = new XMLHttpRequest(); xhr.open('GET', '/login'); - xhr.onload = function () { + xhr.onload = () => { if(this.status != 200) return; var username = this.responseText; diff --git a/make_static.PL b/make_static.PL index 44ef19e..0d8d10b 100644 --- a/make_static.PL +++ b/make_static.PL @@ -30,7 +30,7 @@ sub make_css { } sub make_js { - system java => -jar => 'compiler.jar', qw,-O SIMPLE --create_source_map static/js/js.map --js_output_file static/js/all.js --language_in ECMASCRIPT5_STRICT --source_map_location_mapping js/|/static/js/,, ; + system java => -jar => 'compiler.jar', qw,-O SIMPLE --create_source_map static/js/js.map --js_output_file static/js/all.js --language_in ECMASCRIPT6_STRICT --language_out ECMASCRIPT5_STRICT --source_map_location_mapping js/|/static/js/,, ; my $js = read_file 'static/js/all.js'; write_file 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js; system 'cp', '-rp', 'js', 'static/'; -- 2.39.2