Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function startApp() {
- sessionStorage.clear();
- showHideMenuLinks();
- showView('viewHome');
- // Bind the navigation menu links
- $('#linkHome').click(showHomeView);
- $('#linkLogin').click(showLoginView);
- $('#linkRegister').click(showRegisterView);
- $('#linkListAds').click(showCollectionView);
- $('#linkCreateAd').click(showCreateView);
- $('#linkLogout').click(logout);
- // Bind the form submit buttons
- $("#formLogin").submit(login);
- $("#formRegister").submit(register);
- $("#buttonCreateAd").click(create);
- $("#buttonEditAd").click(edit);
- // Bind the info / error boxes: hide on click
- $("#infoBox, #errorBox").click(function() {
- $(this).fadeOut();
- });
- $(document).on({
- ajaxStart: function() { $("#loadingBox").show() },
- ajaxStop: function() { $("#loadingBox").hide() }
- });
- // Show or hide the links in the navigation bar
- function showHideMenuLinks() {
- $("#linkHome").show();
- if (sessionStorage.getItem('authToken')) {
- // We have logged in user
- $("#linkLogin").hide();
- $("#linkRegister").hide();
- $("#linkListAds").show();
- $("#linkCreateAd").show();
- $("#linkLogout").show();
- } else {
- // No logged in user
- $("#linkLogin").show();
- $("#linkRegister").show();
- $("#linkListAds").hide();
- $("#linkCreateAd").hide();
- $("#linkLogout").hide();
- }
- }
- // Hide all views and show the selected view only
- function showView(viewName) {
- $('main > section').hide();
- $('#' + viewName).show(); // viewName is the id of the given section
- }
- //-----Views----------------------------------------------------------------------//
- function showHomeView() {
- showView('viewHome');
- }
- //---------------------------
- function showLoginView() {
- showView('viewLogin');
- $('#formLogin').trigger('reset');
- }
- //----------------------------
- function showRegisterView() {
- showView('viewRegister');
- $('#formRegister').trigger('reset');
- }
- //----------------------------
- function showCollectionView() {
- $('#ads').empty();
- showView('viewAds');
- KinveyRequester.findAllEntities()
- .then(loadSuccess)
- .catch(handleAjaxError);
- function loadSuccess(response) {
- showInfo('Entities loaded.');
- if (response.length == 0) {
- $('#ads').text('The collection is empty.');
- }
- else {
- let table = $('<table>')
- .append($('<tr>').append(
- '<th>Title</th><th>Body</th><th>Author</th>,' +
- '<th>Date</th><th>Actions</th>'));
- for (let entity of response) {
- let links = [];
- // action links
- if (entity._acl.creator == sessionStorage['userID']) {
- let deleteLink = $('<a href="#">[Delete]</a>')
- .click(function () {
- deleteIt(entity)
- });
- let editLink = $('<a href="#">[Edit]</a>')
- .click(function () {
- loadForEdit(entity)
- });
- links = [deleteLink, ' ', editLink];
- }
- table.append($('<tr>').append(
- $('<td>').text(entity.title),
- $('<td>').text(entity.body),
- $('<td>').text(entity.author),
- $('<td>').text(entity.date),
- $('<td>').append(links)
- ));
- }
- $('#ads').append(table);
- }
- }
- }
- //-------------------------
- function showCreateView() {
- $('#formCreateAd').trigger('reset');
- showView('viewCreateAd');
- }
- //--------------------------
- function loadForEdit(entity) {
- KinveyRequester.findEntityById(entity)
- .then(loadForEditSuccess)
- .catch(handleAjaxError);
- function loadForEditSuccess(entity) {
- $('#formEditAd input[name=id]').val(entity._id);
- $('#formEditAd input[name=title]').val(entity.title);
- $('#formEditAd textarea[name=description]').val(entity.body);
- $('#formEditAd input[name=datePublished]').val(entity.date);
- showView('viewEditAd');
- }
- }
- //-----Actions without a view------------------------------------------------------//
- function deleteIt(entity) {
- KinveyRequester.deleteEntity(entity)
- .then(deleteSuccess)
- .catch(handleAjaxError);
- function deleteSuccess() {
- showCollectionView();
- showInfo('Entity deleted.');
- }
- }
- //----------------------
- function logout() {
- sessionStorage.clear();
- $('#loggedInUser').text("");
- showHideMenuLinks();
- showView('viewHome');
- showInfo('Logout successful.');
- }
- //-----Input forms-----------------------------------------------------------------//
- function login() {
- event.preventDefault();
- let username = $('#formLogin input[name=username]').val();
- let password = $('#formLogin input[name=passwd]').val();
- KinveyRequester.loginUser(username, password)
- .then(loginSuccess)
- .catch(handleAjaxError);
- function loginSuccess(userInfo) {
- saveAuthInSession(userInfo);
- showHideMenuLinks();
- showCollectionView();
- showInfo('Login successful.');
- }
- }
- //-------------------
- function register() {
- event.preventDefault();
- let username = $('#formRegister input[name=username]').val();
- let password = $('#formRegister input[name=passwd]').val();
- KinveyRequester.registerUser(username, password)
- .then(registerSuccess)
- .catch(handleAjaxError);
- function registerSuccess(userInfo) {
- saveAuthInSession(userInfo);
- showHideMenuLinks();
- showCollectionView();
- showInfo('User registration successful.');
- }
- }
- //------------------
- function create() {
- let title = $('#formCreateAd input[name=title]').val();
- let body = $('#formCreateAd textarea[name=description]').val();
- let author = sessionStorage.username;
- let date = $('#formCreateAd input[name=datePublished]').val();
- KinveyRequester.createEntity(title, body, author, date)
- .then(createEntitySuccess)
- .catch(handleAjaxError);
- function createEntitySuccess() {
- showCollectionView();
- showInfo('Entity created.');
- }
- }
- //-----------------
- function edit() {
- let entity = $('#formEditAd input[name=id]').val();
- console.log(entity);
- let title = $('#formEditAd input[name=title]').val();
- console.log(title);
- let body = $('#formEditAd textarea[name=description]').val();
- console.log(body);
- let author = sessionStorage.username;
- let date = $('#formEditAd input[name=datePublished]').val();
- KinveyRequester.editEntity(entity, title, body, author, date)
- .then(editEntitySuccess)
- .catch(handleAjaxError);
- function editEntitySuccess() {
- showCollectionView();
- showInfo('Entity edited.');
- }
- }
- //-----Save data in sessionStorage--------------------------------------------------//
- function saveAuthInSession(userInfo) {
- let userAuth = userInfo._kmd.authtoken;
- sessionStorage.setItem('authToken', userAuth);
- let userId = userInfo._id;
- sessionStorage.setItem('userID', userId);
- let username = userInfo.username;
- sessionStorage.setItem('username', username);
- $('#loggedInUser').text("Welcome, " + username + "!");
- $('#loggedInUser').show();
- }
- //-----Error and information messages--------------------------------------------------//
- function showInfo(message) {
- $('#infoBox').text(message);
- $('#infoBox').show();
- setTimeout(function() {
- $('#infoBox').fadeOut();
- }, 3000);
- }
- function handleAjaxError(response) {
- let errorMsg = JSON.stringify(response);
- if (response.readyState === 0)
- errorMsg = "Cannot connect due to network error.";
- if (response.responseJSON &&
- response.responseJSON.description)
- errorMsg = response.responseJSON.description;
- showError(errorMsg);
- }
- function showError(errorMsg) {
- $('#errorBox').text("Error: " + errorMsg);
- $('#errorBox').show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement