-
-
Save adardesign/4198111 to your computer and use it in GitHub Desktop.
Shop More functionality for mobile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Begin Adrma storage utility */ | |
adrma = window.adrma || {}; | |
adrma.storage = { | |
init: function () { | |
}, | |
supported: function () { | |
try { | |
return 'localStorage' in window && window['localStorage'] !== null; | |
} catch (e) { | |
return false; | |
} | |
}, | |
get: function (type, key, obj) { | |
var item = window[type + "Storage"].getItem(key); | |
if (item && obj) { | |
return JSON.parse(item); | |
} | |
return item; | |
}, | |
set: function (type, key, val) { | |
if ($.isPlainObject(val) || $.isArray(val)) { | |
val = JSON.stringify(val); | |
} | |
try { | |
window[type + "Storage"].setItem(key, val); | |
return true; | |
} catch (err) { | |
//Handle errors here | |
return false; | |
} | |
}, | |
add: function (options) { // need to add sopport for string concat | |
var defaults = { | |
type: "local", | |
key: "", | |
val: "", | |
isObj: false, | |
propsAreObjs: false, | |
comparePropName: "", | |
limit: null, | |
addDirection: "push" // or unshift | |
}; | |
options = $.extend({}, defaults, options); | |
var storageedKey = adrma.storage.get(options.type, options.key, options.isObj), | |
isInArray; | |
if (storageedKey) { | |
if ($.isArray(storageedKey)) { | |
if (options.propsAreObjs) { | |
var matchedArray = $.grep(storageedKey, function (ele) { | |
return (options.val[options.comparePropName] === ele[options.comparePropName]); | |
}); | |
if (matchedArray.length) { | |
return false; | |
} | |
} else { | |
isInArray = $.inArray(options.val, storageedKey); | |
if (isInArray !== -1) { | |
return false; | |
} | |
} | |
if (options.limit && storageedKey.length >= options.limit) { | |
// just do the opposite of addDirection | |
//storageedKey[options.addDirection === "unshift" ? "unshift" : "push"](); | |
storageedKey["pop"](); | |
} | |
storageedKey[options.addDirection](options.val); | |
options.val = storageedKey; | |
} | |
} else { | |
options.val = [options.val]; | |
} | |
// either way add/update | |
return adrma.storage.set(options.type, options.key, options.val, options.isObj); | |
}, | |
remove: function (type, key, val) { | |
var storageedKey = adrma.storage.get(key, true), | |
isInArray; | |
if (storageedKey) { | |
if ($.isArray(storageedKey)) { | |
// check if its already there. | |
//isInArray returns the index of the found result in the aaray. | |
isInArray = $.inArray(val, storageedKey); | |
adrma.noop = isInArray !== -1 && storageedKey.splice(isInArray, 1); | |
val = storageedKey; | |
//console.log(storageedKey); | |
adrma.storage.set(key, val); | |
} | |
} | |
// either way add/update | |
}, | |
"delete": function (key) { | |
localStorage.removeItem(key); | |
}, | |
sync: function (key, url, direction) { | |
// sync key, and optional direction... | |
//adrma.fetchData({}); | |
}, | |
compare: function (key, url) { | |
} | |
}; | |
/* End Adrma storage utility */ | |
/* Begin ShopMoreFunctionality */ | |
ShopMoreFunctionality = { | |
AddLinkToAdoramaStorage: function (data) { | |
adrma.storage.add({ | |
type: "local", // this lets you set either localStorage to sessionStorage | |
key: "pageVisitLog", // key is the name you will store this. | |
val: { | |
// here you pass a object that will be added to the array | |
title: data.title, | |
url: data.url | |
}, | |
isObj: true, // this tells the adrma.storage.set and adrma.storage.get (which are called internally) that we are setting/getting a obj, not a string | |
comparePropName: 'title', // this tell the compare engine (inside this method) where to look for already same existing objects | |
propsAreObjs: true, // here we tell the script that the array properties are objects | |
limit: 5, // simply a limit | |
addDirection: "unshift" // where to "add" the new value, by default it is "push" which adds it as the last position, "unshift" adds it in front. | |
}); | |
}, | |
GetLinksFromAdoramaStorage: function () { | |
// Get all data from localStorage | |
var storageKeys = adrma.storage.get("local", "pageVisitLog", true); | |
var arrayShopMoreLinks = []; | |
for (var numKey = 0; numKey < storageKeys.length; numKey++) { | |
arrayShopMoreLinks.push("<a href=" + storageKeys[numKey].url + ">" + storageKeys[numKey].title + "</a>"); | |
} | |
return arrayShopMoreLinks; | |
} | |
} | |
/* End ShopMoreFunctionality */ | |
/* Adding new link to Adrma storage */ | |
ShopMoreFunctionality.AddLinkToAdoramaStorage( | |
{ | |
title: 'title of page', | |
url: 'url of page' | |
}); | |
/* Getting and displaying link from Adrma storage */ | |
var arrayShopMoreLinks = ShopMoreFunctionality.GetLinksFromAdoramaStorage(); | |
for (var numLink = 0; numLink < arrayShopMoreLinks.length; numLink++) { | |
$("...").append(arrayShopMoreLinks[numLink]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great!