var sd = this; sd.config = { 'url': 'https://discord-bots.com' } sd.loadCache = function(id) { var cache = JSON.parse(localStorage.getItem("user-lists")); if (cache == null) { cache = [{'name': 'mine', 'products': []},{'name': 'want', 'products': []}]; } return cache; }; sd.createCollection = function(id) { var cache = sd.loadCache(); cache.push({'name': id, 'products': []}); localStorage.setItem("user-lists", JSON.stringify(cache)); $('#createModal').modal('toggle'); location.reload(); }; sd.removeCollection = function(collection) { var cache = sd.loadCache(); for (var i = cache.length - 1; i >= 0; i--) { if (cache[i].name === collection) { cache.splice(i, 1); } } localStorage.setItem("user-lists", JSON.stringify(cache)); $('#' + collection).remove() }; sd.addToCollection = function(id, collection) { var cache = sd.loadCache(); cache.forEach(function (a) { if(a.name == collection) { a.products.push(id); } }); localStorage.setItem("user-lists", JSON.stringify(cache)); $('#collectionModal').modal('toggle'); }; sd.removeFromCollection = function(id, collection) { var cache = sd.loadCache(); cache.forEach(function (a) { if(a.name == collection) { for (var i = a.products.length - 1; i >= 0; i--) { if (a.products[i] === id) { a.products.splice(i, 1); } } } }); localStorage.setItem("user-lists", JSON.stringify(cache)); $('#' + collection + '-' + id).remove() }; sd.loadCollectionsPage = function() { var cache = sd.loadCache(); cache.forEach(function (a) { var html = ''; html+='
' + a.name + '

' + a.products.length + ' Squishables in this collection

'; a.products.forEach(function (p) { html+='
'; }); html+='
'; $('#collections').append(html); }); }; sd.loadCollectionsModal = function() { var cache = sd.loadCache(); var html = ''; $('#collections-select').append(html); }; // Events $( document ).ready(function() { $('.sd-create-list').click(function() { sd.createCollection($('#sd-collection-name').val()); }); $('.sd-delete-list').click(function() { sd.removeCollection($(this).data('id')); }); $('.sd-add-list').click(function() { sd.addToCollection($(this).data('id'), $('#sd-collection-select').val()); console.log('pp'); }); $('.sd-remove-list').click(function() { sd.removeFromCollection($(this).data('id'), $(this).data('collection')); console.log('pp'); }); });