From 5a02fdef10e5b9c4804827afc494eb5a0246bf9c Mon Sep 17 00:00:00 2001 From: lusabo Date: Wed, 30 Jul 2014 08:58:30 -0300 Subject: [PATCH] Melhorias na estrutura do javascript. --- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/bookmark-edit.html | 3 ++- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/bookmark-list.html | 19 ++++++++++--------- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/home.html | 4 +++- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-edit.js | 37 ++++++++----------------------------- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-list.js | 30 ++++-------------------------- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/home.js | 8 -------- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js | 8 +++++--- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/menu.js | 34 ++++++++++++++-------------------- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js | 29 +++++++++++++++++++++++++++++ archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/auth.js | 66 +++++++++++++++++++++++++++++------------------------------------- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/bookmark.js | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------- archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/login.html | 4 +++- 12 files changed, 167 insertions(+), 197 deletions(-) create mode 100644 archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/bookmark-edit.html b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/bookmark-edit.html index f0af565..f4294a0 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/bookmark-edit.html +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/bookmark-edit.html @@ -38,8 +38,9 @@ + - + \ No newline at end of file diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/bookmark-list.html b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/bookmark-list.html index c55455c..83179de 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/bookmark-list.html +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/bookmark-list.html @@ -27,14 +27,15 @@

Aplicação de exemplo do Demoiselle 2.5.0

- - - - - - - - - + + + + + + + + + + \ No newline at end of file diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/home.html b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/home.html index 89b1fe6..ae1f73c 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/home.html +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/home.html @@ -21,8 +21,10 @@

Aplicação de exemplo do Demoiselle 2.5.0

+ + - + \ No newline at end of file diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-edit.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-edit.js index 0a71a13..1e3d0c7 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-edit.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-edit.js @@ -1,19 +1,12 @@ $(function() { - $("#menu").load("menu.html", function() { - AuthProxy.getUser(getUserOk, getUserFailed); - $("#logout").on("click", function() { - sessionStorage.removeItem('credentials'); - location.href = "home.html"; - }); - }); - $("#delete").hide(); + $("#description").focus(); $(document).ready(function() { if (id = $.url().param('id')) { - BookmarkProxy.load(id, loadOk, loadFailed); + BookmarkProxy.load(id).done(loadOk).fail(loadFailed); } }); @@ -28,16 +21,16 @@ $(function() { }; if (id = $("#id").val()) { - BookmarkProxy.update(id, form, saveOk, saveFailed); + BookmarkProxy.update(id, form).done(saveOk).fail(saveFailed); } else { - BookmarkProxy.insert(form, saveOk, saveFailed); + BookmarkProxy.insert(form).done(saveOk).fail(saveFailed); } }); $("#delete").click(function() { bootbox.confirm("Tem certeza que deseja apagar?", function(result) { if(result) { - BookmarkProxy.remove([$("#id").val()], removeOk, removeFailed); + BookmarkProxy.remove([$("#id").val()]).done(removeOk); } }); }); @@ -59,8 +52,9 @@ function loadOk(data) { function loadFailed(request) { switch (request.status) { case 404: - alert('Você está tentando acessar um registro inexistente.\r\nVocê será redirecionado.') - location.href = "bookmark-list.html"; + bootbox.alert("Você está tentando acessar um registro inexistente!", function(){ + location.href = "bookmark-list.html"; + }); break; default: @@ -74,10 +68,6 @@ function saveOk(data) { function saveFailed(request) { switch (request.status) { - case 401: - alert('Você não está autenticado.'); - break; - case 412: $($("form input").get().reverse()).each(function() { var id = $(this).attr('id'); @@ -106,15 +96,4 @@ function saveFailed(request) { function removeOk(data) { location.href = 'bookmark-list.html'; -} - -function removeFailed(request) { - switch (request.status) { - case 401: - alert('Você não está autenticado.'); - break; - - default: - break; - } } \ No newline at end of file diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-list.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-list.js index 9b88341..211437b 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-list.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-list.js @@ -1,17 +1,9 @@ $(function() { - $("#menu").load("menu.html", function() { - AuthProxy.getUser(getUserOk, getUserFailed); - $("#logout").on("click", function() { - sessionStorage.removeItem('credentials'); - location.href = "home.html"; - }); - }); - $("#new").focus(); $(document).ready(function() { - BookmarkProxy.findAll(findAllOk); + BookmarkProxy.findAll().done(findAllOk); }); $("form").submit(function(event) { @@ -33,19 +25,16 @@ $(function() { bootbox.alert({message: "Nenhum registro selecionado"}); } else { bootbox.confirm("Tem certeza que deseja apagar?", function(result) { - console.log(result); if(result) { - BookmarkProxy.remove(ids, removeOk, removeFailed); + BookmarkProxy.remove(ids).done(removeOk); } }); } }); }); -var oTable; - function findAllOk(data) { - oTable = $('#resultList').dataTable({ + var oTable = $('#resultList').dataTable({ "aoColumns" : [ { "aTargets" : [ 0 ], "mDataProp" : "id", @@ -86,16 +75,5 @@ function findAllOk(data) { } function removeOk() { - BookmarkProxy.findAll(findAllOk); -} - -function removeFailed(request) { - switch (request.status) { - case 401: - alert('Você não está autenticado.'); - break; - - default: - break; - } + BookmarkProxy.findAll().done(findAllOk); } \ No newline at end of file diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/home.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/home.js index 810ac25..eae289a 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/home.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/home.js @@ -1,11 +1,3 @@ $(function() { - $("#menu").load("menu.html", function() { - AuthProxy.getUser(getUserOk, getUserFailed); - $("#logout").on("click", function() { - sessionStorage.removeItem('credentials'); - location.href = "home.html"; - }); - }); - }); diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js index 1802956..8d46ca2 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js @@ -3,12 +3,15 @@ $(function() { $("form").submit(function(event) { event.preventDefault(); + $("[id$='-message']").hide(); + var form = { 'username' : $("#username").val().trim(), 'password' : $("#password").val().trim() }; - AuthProxy.login(form, loginOk, loginFail); + + AuthProxy.login(form).done(loginOk).fail(loginFail); }); }); @@ -19,12 +22,11 @@ function make_base_auth(user, password) { } function loginOk(data) { - sessionStorage.setItem('credentials', make_base_auth($("#username").val().trim(), $("#password").val().trim())); + App.setToken(make_base_auth($("#username").val().trim(), $("#password").val().trim())); location.href = "home.html"; } function loginFail(request) { - sessionStorage.clear(); switch (request.status) { case 401: $("#global-message").html("Usuário ou senha inválidos.").show(); diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/menu.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/menu.js index 688630f..04f3e8e 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/menu.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/menu.js @@ -1,24 +1,18 @@ -function getUserOk(data) { - $("#username").html(data.username); -} +$(function() { + + $("#menu").load("menu.html", function() { + + AuthProxy.getUser().done(getUserOk); -function getUserFailed(request) { - switch (request.status) { - case 401: + $("#logout").on("click", function() { + App.removeToken(); location.href = "login.html"; - break; - default: - console.log(request.statusText); - break; - } -} + }); -function logoutOk(data) { - sessionStorage.removeItem('credentials'); - location.href = "home.html"; -} + }); -function logoutFailed(data) { - console.log('Falha no logout'); - console.log(data); -} +}); + +function getUserOk(data) { + $("#username").html(data.username); +} \ No newline at end of file diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js new file mode 100644 index 0000000..66a9760 --- /dev/null +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js @@ -0,0 +1,29 @@ +$.ajaxSetup({ + error : function(request) { + switch (request.status) { + case 401: + bootbox.alert("Você não está autenticado!", function(){ + location.href = "login.html"; + }); + break; + } + } +}); + +var App = { + + tokenKey : "credentials", + + getToken : function() { + return sessionStorage.getItem(this.tokenKey); + }, + + setToken : function(token) { + sessionStorage.setItem(this.tokenKey, token); + }, + + removeToken : function() { + sessionStorage.removeItem(this.tokenKey); + } + +} diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/auth.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/auth.js index 7c6677f..fc94e1c 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/auth.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/auth.js @@ -1,43 +1,35 @@ -var AuthProxy = {}; +var AuthProxy = { -AuthProxy.url = "api/auth"; + url : "api/auth", -AuthProxy.login = function($credentials, $success, $error) { - $.ajax({ - url : this.url, - type : "POST", - data : JSON.stringify($credentials), - contentType : "application/json", - success : $success, - error : $error - }); -}; + login : function($credentials) { + return $.ajax({ + url : this.url, + type : "POST", + data : JSON.stringify($credentials), + contentType : "application/json", + error: function(){ } + }); + }, -AuthProxy.logout = function($success, $error) { - $.ajax({ - url : this.url, - type : "DELETE", - success : $success, - error : $error, - beforeSend : function(xhr) { - console.log(AuthProxy.getCredentials()); - xhr.setRequestHeader("Authorization", AuthProxy.getCredentials()); - } - }); -}; + logout : function() { + return $.ajax({ + url : this.url, + type : "DELETE", + beforeSend : function(request) { + request.setRequestHeader("Authorization", App.getToken()); + } + }); + }, -AuthProxy.getUser = function($success, $error) { - $.ajax({ - url : this.url, - type : "GET", - success : $success, - error : $error, - beforeSend : function(xhr) { - xhr.setRequestHeader("Authorization", AuthProxy.getCredentials()); - } - }); -}; + getUser : function() { + return $.ajax({ + url : this.url, + type : "GET", + beforeSend : function(request) { + request.setRequestHeader("Authorization", App.getToken()); + } + }); + } -AuthProxy.getCredentials = function() { - return sessionStorage.getItem('credentials'); } \ No newline at end of file diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/bookmark.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/bookmark.js index 673b3f5..32d0068 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/bookmark.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/proxy/bookmark.js @@ -1,63 +1,61 @@ -var BookmarkProxy = {}; +var BookmarkProxy = { + + url : "api/bookmark", + + findAll : function() { + return $.ajax({ + type : "GET", + url : this.url, + beforeSend : function(request) { + request.setRequestHeader("Authorization", App.getToken()); + } + }); + }, + + load : function($id) { + return $.ajax({ + type : "GET", + url : this.url + "/" + $id, + beforeSend : function(request) { + request.setRequestHeader("Authorization", App.getToken()); + } + }); + }, + + insert : function($form) { + return $.ajax({ + type : "POST", + url : this.url, + data : JSON.stringify($form), + contentType : "application/json", + beforeSend : function(request) { + request.setRequestHeader("Authorization", App.getToken()); + } + }); + }, -BookmarkProxy.url = "api/bookmark"; - -BookmarkProxy.findAll = function($success, $error) { - $.ajax({ - type : "GET", - url : this.url, - success : $success, - error : $error - }); -}; - -BookmarkProxy.load = function($id, $success, $error) { - $.ajax({ - type : "GET", - url : this.url + "/" + $id, - success : $success, - error : $error - }); -}; - -BookmarkProxy.insert = function($form, $success, $error) { - $.ajax({ - type : "POST", - url : this.url, - data : JSON.stringify($form), - contentType : "application/json", - success : $success, - error : $error, - beforeSend : function(xhr) { - xhr.setRequestHeader("Authorization", AuthProxy.getCredentials()); - } - }); -}; - -BookmarkProxy.update = function($id, $form, $success, $error) { - $.ajax({ - type : "PUT", - url : this.url + "/" + $id, - data : JSON.stringify($form), - contentType : "application/json", - success : $success, - error : $error, - beforeSend : function(xhr) { - xhr.setRequestHeader("Authorization", AuthProxy.getCredentials()); - } - }); -}; - -BookmarkProxy.remove = function($ids, $success, $error) { - $.ajax({ - type : "DELETE", - url : this.url, - data : JSON.stringify($ids), - contentType : "application/json", - success : $success, - error : $error, - beforeSend : function(xhr) { - xhr.setRequestHeader("Authorization", AuthProxy.getCredentials()); - } - }); -}; + update : function($id, $form) { + return $.ajax({ + type : "PUT", + url : this.url + "/" + $id, + data : JSON.stringify($form), + contentType : "application/json", + beforeSend : function(request) { + request.setRequestHeader("Authorization", App.getToken()); + } + }); + }, + + remove : function($ids) { + return $.ajax({ + type : "DELETE", + url : this.url, + data : JSON.stringify($ids), + contentType : "application/json", + beforeSend : function(request) { + request.setRequestHeader("Authorization", App.getToken()); + } + }); + } + +} \ No newline at end of file diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/login.html b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/login.html index 27e3170..d0c64be 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/login.html +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/login.html @@ -41,7 +41,9 @@

Aplicação de exemplo do Demoiselle 2.5.0

- + + + \ No newline at end of file -- libgit2 0.21.2