Made the app now fully functional

This commit is contained in:
José Carlos Cuevas 2015-02-22 14:49:55 +01:00
parent 32128eb7f4
commit a60c2a9795
3 changed files with 117 additions and 21 deletions

View file

@ -59,11 +59,6 @@
<p>Coming soon...</p>
</div> -->
</div>
<!-- <footer>
<a href="#main" id="navbar_home" class="icon home" data-l10n-id="main_list">Lists</a>
<a href="#new_list" id="navbar_settings" class="icon settings" data-l10n-id="settings">Settings</a>
<a href="#about" id="navbar_about" class="icon info" data-l10n-id="about">About</a>
</footer> -->
</div>
</div>
</body>

View file

@ -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('<li id="element_' + listID + '_' + count +'"><span class="icon check"></span>' + text + '</li>');
list_display.before('<li id="element_' + identification +'"><span id="check_' + identification + '" class="icon check"></span>' + text + '</li>');
} else {
list_display.before('<li id="element_' + listID + '_' + count +'"><span class="icon minimize"></span>' + text + '</li>');
list_display.before('<li id="element_' + identification +'"><span id="check_' + identification + '" class="icon minimize"></span>' + text + '</li>');
}
$('#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('<li id="element_' + listID + '_' + count +'"><span class="icon minimize"></span>' + text + '</li>');
var identification = listID + '_' + itemID;
list_display.before('<li id="element_' + identification +'"><span id="check_' + identification + '" class="icon minimize"></span>' + text + '</li>');
$('#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('<li><a id="list_' + listID + '">' + newListName + '</li>');
$('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);
});
}
});

View file

@ -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');
}
}