From a60c2a979516187a12f1af6f0636bbde9a6497aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Sun, 22 Feb 2015 14:49:55 +0100 Subject: [PATCH] Made the app now fully functional --- index.html | 5 ---- js/app.js | 58 +++++++++++++++++++++++++++++++++---- js/storelists.js | 75 +++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 117 insertions(+), 21 deletions(-) diff --git a/index.html b/index.html index 8461ffa..0f89393 100644 --- a/index.html +++ b/index.html @@ -59,11 +59,6 @@

Coming soon...

--> - diff --git a/js/app.js b/js/app.js index 0fd6e65..971accc 100644 --- a/js/app.js +++ b/js/app.js @@ -11,44 +11,83 @@ function openList(elementClicked) { var numElements = getListItemCount(listID); if((numElements) && (numElements > 0)) { var text; + var identification; for(var count = 0; count < numElements; count++) { text = getItemText(listID, count); + identification = listID + '_' + count; if(text) { var list_display = $('#new_elem_text'); if (isItemChecked(listID, count)) { - list_display.before('
  • ' + text + '
  • '); + list_display.before('
  • ' + text + '
  • '); } else { - list_display.before('
  • ' + text + '
  • '); + list_display.before('
  • ' + text + '
  • '); } + $('#check_' + identification).data('listID', listID); + $('#check_' + identification).data('itemID', count); + $('#check_' + identification).click(function () { + itemClicked($(this)); + }); } } } $('#add_new_item').data('currentlist', listID); + $('#delete_list').data('currentlist', listID); $.afui.loadContent('#list_panel', false, false, "slide"); $.afui.setTitle(listTitle); } +function itemClicked(elementClicked) { + var listID = elementClicked.data('listID'); + var itemID = elementClicked.data('itemID'); + + var identification = listID + '_' + itemID; + if(!isItemChecked(listID, itemID)) { + checkItem(listID, itemID); + $('#check_' + identification).removeClass('minimize'); + $('#check_' + identification).addClass('check'); + } else { + uncheckItem(listID, itemID); + $('#check_' + identification).removeClass('check'); + $('#check_' + identification).addClass('minimize'); + } +} + function addItemToListClicked(listID, text) { - insertItem(listID, text); + var itemID = insertItem(listID, text); var list_display = $('#new_elem_text'); - list_display.before('
  • ' + text + '
  • '); + var identification = listID + '_' + itemID; + list_display.before('
  • ' + text + '
  • '); + $('#check_' + identification).data('listID', listID); + $('#check_' + identification).data('itemID', itemID); + $('#check_' + identification).click(function () { + itemClicked($(this)); + }); } function addNewListClicked(newListName) { var listID = createList(newListName); $('li#add_list_last_elem').before('
  • ' + newListName + '
  • '); - $('a#list_' + listID).click(function () { openList('a#list_' + listID); }); - $('a#list_' + count).data('listID', listID); + $('a#list_' + listID).data('listID', listID); + $('a#list_' + listID).click(function () { openList($(this)); }); } +function deleteListClicked(listID) { + deleteList(listID); + + $('a#list_' + listID).parent().remove(); + $('a#list_' + listID).remove(); + $.afui.goBack(); +} + // Loads the lists in the Store element function loadLists() { console.log('Recovering data'); var listData = getAllLists(); + console.log("Lists: " + listData.length); listData.forEach(function(list, count) { console.log('Adding list item ' + count); @@ -106,6 +145,13 @@ window.addEventListener('DOMContentLoaded', function() { addItemToListClicked(listID, text); } + $('#new_elem_text').val(''); + + }); + + $('#delete_list').click(function() { + var listID = $('#delete_list').data('currentlist'); + deleteListClicked(listID); }); } }); diff --git a/js/storelists.js b/js/storelists.js index bb5df39..720ab91 100644 --- a/js/storelists.js +++ b/js/storelists.js @@ -10,11 +10,12 @@ function getListTitle(listID) { } function getListItemCount(listID) { - return localStorage.getItem(PREFIX + listID + '_element_count'); + return parseInt(localStorage.getItem(PREFIX + listID + '_element_count')); } function getAllLists() { - var maxLists = localStorage.getItem('ToDoList_LastID'); + var maxLists = parseInt(localStorage.getItem('ToDoList_LastID')); + console.log('LastID: ' + maxLists); var allLists = []; for(var count = 0; count < maxLists; count++) { @@ -47,30 +48,37 @@ function itemExists(listID, itemID) { // INSERTs and SETs function createList(text) { var lastID = 0; - if(localStorage.getItem('ToDoList_LastID')) { - lastID = localStorage.getItem('ToDoList_LastID'); + if(localStorage.getItem(PREFIX + 'LastID')) { + lastID = parseInt(localStorage.getItem(PREFIX + 'LastID')); + } else { + localStorage.setItem(PREFIX + 'LastID', 0); } var finalID = 0; - if(lastID !== 0) { + if(lastID != 0) { var count = 0; - while((localStorage.getItem('ToDoList_list' + count)) && (count < lastID)) { + while((localStorage.getItem(PREFIX + 'list' + count)) && (count < lastID)) { count++; } if(count < lastID) { - finalID = lastID; + finalID = count; } else { finalID = lastID; lastID++; + localStorage.setItem(PREFIX + 'LastID', lastID); } + } else { + finalID = 0; + lastID = 1; + localStorage.setItem(PREFIX + 'LastID', lastID); } - localStorage.setItem('ToDoList_list' + finalID, newListName); - localStorage.setItem('ToDoList_LastID', lastID); + localStorage.setItem(PREFIX + 'list' + finalID, text); return finalID; } + function insertItem(listID, text) { var numElements = getListItemCount(listID); var elementPosition = 0; @@ -93,7 +101,54 @@ function insertItem(listID, text) { } localStorage.setItem(PREFIX + listID + '_element_' + elementPosition, text); - localStorage.setItem(PREFIX + listID + '_element_' + count + '_checked', 'false'); + localStorage.setItem(PREFIX + listID + '_element_' + elementPosition + '_checked', 'false'); localStorage.setItem(PREFIX + listID + '_element_count', numElements); + + return elementPosition; +} + +function checkItem(listID, itemID) { + if(itemExists(listID, itemID)) { + localStorage.setItem(PREFIX + listID + '_element_' + itemID + '_checked', 'true'); + } +} + +function uncheckItem(listID, itemID) { + if(itemExists(listID, itemID)) { + localStorage.setItem(PREFIX + listID + '_element_' + itemID + '_checked', 'false'); + } +} + +// Delete functions +function deleteItem(listID, itemID) { + var maxElementID = getListItemCount(listID); + + if(itemExists(listID, itemID)) { + localStorage.removeItem(PREFIX + listID + '_element_' + itemID); + localStorage.removeItem(PREFIX + listID + '_element_' + itemID + '_checked'); + + if(itemID == (maxElementID - 1)) { + localStorage.setItem(PREFIX + listID + '_element_count', itemID); + } + } +} + +function deleteList(listID) { + var maxItemID = getListItemCount(listID); + + for(var count = 0; count < maxItemID; count++) { + deleteItem(listID, count); + } + + localStorage.removeItem(PREFIX + listID + '_element_count'); + localStorage.removeItem(PREFIX + 'list' + listID); + + if (listID == (localStorage.getItem(PREFIX + 'LastID') - 1)) { + localStorage.setItem(PREFIX + 'LastID', listID); + } + + if (listID == 0) { + localStorage.removeItem(PREFIX + 'LastID'); + } } \ No newline at end of file