summaryrefslogtreecommitdiff
path: root/js/options.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/options.js')
-rw-r--r--js/options.js113
1 files changed, 113 insertions, 0 deletions
diff --git a/js/options.js b/js/options.js
new file mode 100644
index 0000000..331b05e
--- /dev/null
+++ b/js/options.js
@@ -0,0 +1,113 @@
+/**
+ * Filter Storage to assist in storing the filter to localStorage
+ * and retrieving to populate back
+ *
+ * @returns {{persistFilters: persistFilters, populateFilters: populateFilters}}
+ * @constructor
+ */
+function HubStorage() {
+
+ /**
+ * Returns the storage object if available, otherwise
+ * a polyfill
+ * @returns {*}
+ */
+ var getStorage = function () {
+ if (window.localStorage) {
+ return window.localStorage;
+ }
+
+ return {
+ setItem: function () {},
+ getItem: function () {}
+ }
+ };
+
+ /**
+ * Persists the values of the provided input input selector
+ * @param selector`
+ */
+ var persistFilters = function (selector) {
+ var storage = getStorage();
+
+ $(selector).each(function(index, input){
+ var $input = $(input),
+ name = $input.attr('name');
+
+ storage.setItem(name, $input.val());
+ });
+ };
+
+ /**
+ * Retrieves the values for the provided selector from localstorage
+ * and populates the fields back
+ * @param selector
+ */
+ var populateFilters = function (selector) {
+ var storage = getStorage(),
+ anyPopulated = false;
+
+ $(selector).each(function(index, input){
+ var $input = $(input),
+ name = $input.attr('name'),
+ value = storage.getItem(name);
+
+ if (value) {
+ $input.val(value);
+ anyPopulated = true;
+ }
+ });
+
+ return anyPopulated;
+ };
+
+ return {
+ /**
+ * Persist the fields matching the provided selector to local storage
+ * @param selector
+ */
+ persistFilters: function (selector) {
+ persistFilters(selector);
+ },
+
+ /**
+ * Populates the filters using the values stored in the localstorage
+ * @param selector
+ */
+ populateFilters: function (selector) {
+ return populateFilters(selector);
+ }
+ };
+}
+
+function HubOptions() {
+
+ var hubStorage = new HubStorage();
+
+ function bindUI() {
+ $(document).on('click', '.save-token', function (e) {
+ e.preventDefault();
+
+ hubStorage.persistFilters('.githunt_token');
+ $('.quote-item').html('Woohoo! Token saved, happy hunting.');
+ });
+ }
+
+ return {
+ init: function () {
+ var tokenPopulated = hubStorage.populateFilters('.githunt_token');
+ if (tokenPopulated) {
+ $('.quote-item').html('Token already saved. Better go for the hunt!');
+ }
+
+ bindUI();
+ }
+ };
+}
+
+$(function () {
+ var hubOptions = new HubOptions();
+ hubOptions.init();
+
+ particlesJS.load('particles-js', 'js/particles.json', function () {});
+});