From f803a6adfbdb5d4306b1c7f91819344278dbebb2 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Thu, 12 Nov 2015 12:28:06 +0100 Subject: [PATCH] Remove AJAX requests when adding a movie I do not like how it is currently implemented and it is not necessary. --- lib/librarix/public/application.js | 39 +++--------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) diff --git a/lib/librarix/public/application.js b/lib/librarix/public/application.js index b69cf2f..90065bf 100644 --- a/lib/librarix/public/application.js +++ b/lib/librarix/public/application.js @@ -8,8 +8,8 @@ function request(method, url, data, loadevent) { req.send(data) } -function initButtons() { - var buttons = document.querySelectorAll('button[data-action=remove-movie]'); +function initButtons(element) { + var buttons = element.querySelectorAll('button[data-action=remove-movie]'); for (var i = 0; i < buttons.length; i++) { buttons[i].addEventListener('click', function(event) { event.preventDefault(); @@ -17,7 +17,7 @@ function initButtons() { }); } - var view_buttons = document.querySelectorAll('button[data-action=view-movie]'); + var view_buttons = element.querySelectorAll('button[data-action=view-movie]'); for (var i = 0; i < view_buttons.length; i++) { view_buttons[i].addEventListener('click', function(event) { event.preventDefault(); @@ -26,33 +26,6 @@ function initButtons() { } } -function initSearch() { - var typing_timer; - var typing_interval = 300; - - document.querySelector('#search > input[type="text"]').addEventListener('input', function(event) { - var value = this.value; - - clearTimeout(typing_timer); - typing_timer = setTimeout(function() { search(value); }, typing_interval); - }); - document.querySelector('#search > input[type="submit"]').style.display = 'none'; - - window.addEventListener('popstate', function(event) { - document.querySelector('#search > input[type="text"]').value = event.state.value; - search(event.state.value); - }); -} - -function search(value) { - var url = '/search?search=' + encodeURIComponent(value) - window.history.pushState({value: value}, "", url); - - request('GET', url, null, function(e) { - document.getElementById('movies').parentNode.innerHTML = this.responseText; - }); -} - function removeMovie(id) { var data = new FormData(); data.append('id', id); @@ -72,9 +45,5 @@ function viewMovie(id) { } document.addEventListener('DOMContentLoaded', function(event) { - initButtons(); - - if(document.querySelector('#search') != null) { - initSearch(); - } + initButtons(document); });