From: Marius Gavrilescu Date: Thu, 11 Jun 2015 12:44:00 +0000 (+0300) Subject: Make JS a bit simpler X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=dee46c5bb42f8fdaa70e70d6bdd4da5072a99ecd;p=gruntmaster-page.git Make JS a bit simpler --- diff --git a/js/05-makedom.js b/js/05-makedom.js index 389fe4c..a938d2c 100644 --- a/js/05-makedom.js +++ b/js/05-makedom.js @@ -3,3 +3,7 @@ function m(html) { div.innerHTML = html; return div.firstChild; } + +function q(selector) { + return document.querySelector(selector); +} diff --git a/js/10-modal.js b/js/10-modal.js index 3a8a214..d930f44 100644 --- a/js/10-modal.js +++ b/js/10-modal.js @@ -13,8 +13,8 @@ function hide_modal () { } $(function() { - modal = $('.modal')[0]; - backdrop = $('.backdrop')[0]; + modal = q('.modal'); + backdrop = q('.backdrop'); $(modal).on('transitionend', el => { if(!document.body.classList.contains('modal-open')) modal.classList.add('hidden'); diff --git a/js/80-sidebar.js b/js/80-sidebar.js index 2f6ca8a..5120df5 100644 --- a/js/80-sidebar.js +++ b/js/80-sidebar.js @@ -1,6 +1,6 @@ $(function() { if(!document.getElementById('sidebar')) { - const content = $('main')[0]; + const content = q('main'); content.innerHTML = '
' + content.innerHTML + '
'; content.classList.add('row'); } diff --git a/js/90-divine-format.js b/js/90-divine-format.js index da48856..a842de1 100644 --- a/js/90-divine-format.js +++ b/js/90-divine-format.js @@ -30,11 +30,11 @@ const ext_table = { } function divine_format() { - const filename = $('#prog')[0].value; + const filename = q('#prog').value; const ext = /\.([^.]*)$/.exec(filename)[1]; if(ext_table[ext]) - $('#prog_format')[0].value = ext_table[ext]; + q('#prog_format').value = ext_table[ext]; } $(() => $('#prog').on('change', divine_format)); diff --git a/js/90-form.js b/js/90-form.js index aa5e9ef..a30e7f4 100644 --- a/js/90-form.js +++ b/js/90-form.js @@ -1,5 +1,5 @@ $(function(){ - const result = $('#result')[0]; + const result = q('#result'); $('.jsform > input[type="submit"]').on('click', e => { const form_data = new FormData(this.parentElement); const form = this.parentNode; diff --git a/js/90-mobilenav.js b/js/90-mobilenav.js index 36e7298..bd47c8f 100644 --- a/js/90-mobilenav.js +++ b/js/90-mobilenav.js @@ -1,6 +1,6 @@ $(function(){ - const nav = $('nav')[0]; + const nav = q('nav'); nav.classList.add('hidden-xs'); - $('#title')[0].insertAdjacentHTML('beforebegin', '
Tap title to toggle menu
'); + q('#title').insertAdjacentHTML('beforebegin', '
Tap title to toggle menu
'); $('#title').on('click', () => nav.classList.toggle('hidden-xs')); }); diff --git a/js/90-nav.js b/js/90-nav.js index b4127d6..36aec02 100644 --- a/js/90-nav.js +++ b/js/90-nav.js @@ -12,5 +12,5 @@ $(function(){ const path = location.pathname; for (const nav in NAVS) if(path.match(NAVS[nav])) - $('#nav-' + nav)[0].classList.add('active'); + q('#nav-' + nav).classList.add('active'); }); diff --git a/js/90-themes.js b/js/90-themes.js index a9d9b51..3f761fc 100644 --- a/js/90-themes.js +++ b/js/90-themes.js @@ -4,7 +4,7 @@ function set_style(name, trans){ setTimeout(() => document.body.classList.remove('transition-color'), 1000); } $('link[title]').each(e => e.disabled = true); - $('link[title="' + name + '"]')[0].disabled = false; + q('link[title="' + name + '"]').disabled = false; localStorage.setItem("theme", name); } @@ -13,7 +13,7 @@ $(function() { '
  • Black' + '
  • Grey' + '
  • White'; - const sidebar = $('#sidebar')[0]; + const sidebar = q('#sidebar'); sidebar.insertBefore(m(theme_ul), sidebar.firstChild); $('#themes a').on('click', e => set_style(e.target.dataset.theme, true)); }); diff --git a/js/90-tracker.js b/js/90-tracker.js index b7f5e57..6b73fb3 100644 --- a/js/90-tracker.js +++ b/js/90-tracker.js @@ -31,11 +31,11 @@ function update_tracker(){ data.contests.forEach(e => contests[e.contest] = 1); clean_tracker(); - $('#tracker_userlink')[0].innerHTML = data.name; - $('#tracker_solved')[0].innerHTML = solvednr; - $('#tracker_attempted')[0].innerHTML = attemptednr; - $('#tracker_contests')[0].innerHTML = data.contests.length; - $('#tracker_log')[0].setAttribute('href', '/log/?owner=' + user); + q('#tracker_userlink').innerHTML = data.name; + q('#tracker_solved').innerHTML = solvednr; + q('#tracker_attempted').innerHTML = attemptednr; + q('#tracker_contests').innerHTML = data.contests.length; + q('#tracker_log').setAttribute('href', '/log/?owner=' + user); if(location.pathname == '/pb/') $('table .name a').each(el => { @@ -73,8 +73,8 @@ function update_tracker(){ function start_tracking(user){ localStorage.setItem('tracker_username', user); - $('#tracker_userlink')[0].setAttribute('href', '/us/' + user); - $('#tracker_userlink')[0].innerHTML = user; + q('#tracker_userlink').setAttribute('href', '/us/' + user); + q('#tracker_userlink').innerHTML = user; tracker.classList.remove('hidden'); update_tracker(); const lastfetch = localStorage.getItem('tracker_lastfetch'); @@ -104,7 +104,7 @@ function stop_tracking(){ $(function(){ tracker = m(''); - const sidebar = $('#sidebar')[0]; + const sidebar = q('#sidebar'); sidebar.insertBefore(tracker, sidebar.firstChild); $('#tracker_stop').on('click', stop_tracking); $('#track_user').on('click', function() { stop_tracking(); start_tracking(this.dataset.user) }); diff --git a/js/95-login.js b/js/95-login.js index aad77d3..6036aef 100644 --- a/js/95-login.js +++ b/js/95-login.js @@ -1,5 +1,5 @@ $(function(){ - const sidebar = $('#sidebar')[0]; + const sidebar = q('#sidebar'); const login = document.createElement('div'); login.innerHTML = 'Log in'; sidebar.insertBefore(login, sidebar.firstChild);