From 6949f919a1ae05bb7dd28044e6d6f85ff1242014 Mon Sep 17 00:00:00 2001 From: Tor Hveem Date: Sat, 5 Oct 2013 16:05:16 +0200 Subject: [PATCH] Save the connection paramaters into localStorage using a simple angular module for localstorage Note: it also saves the password into localStorage, this should be further improved to present a simple checkbox to the user asking if they want to save the password or not --- index.html | 1 + js/localstorage.js | 113 +++++++++++++++++++++++++++++++++++++++++++++ js/websockets.js | 13 +++--- 3 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 js/localstorage.js diff --git a/index.html b/index.html index ac8644a..2f906d4 100644 --- a/index.html +++ b/index.html @@ -6,6 +6,7 @@ + diff --git a/js/localstorage.js b/js/localstorage.js new file mode 100644 index 0000000..a77db29 --- /dev/null +++ b/js/localstorage.js @@ -0,0 +1,113 @@ + +var ls = angular.module('localStorage',[]); + +ls.factory("$store",function($parse){ + /** + * Global Vars + */ + var storage = (typeof window.localStorage === 'undefined') ? undefined : window.localStorage, + supported = !(typeof storage == 'undefined' || typeof window.JSON == 'undefined'); + + var privateMethods = { + /** + * Pass any type of a string from the localStorage to be parsed so it returns a usable version (like an Object) + * @param res - a string that will be parsed for type + * @returns {*} - whatever the real type of stored value was + */ + parseValue: function(res) { + var val; + try { + val = JSON.parse(res); + if (typeof val == 'undefined'){ + val = res; + } + if (val == 'true'){ + val = true; + } + if (val == 'false'){ + val = false; + } + if (parseFloat(val) == val && !angular.isObject(val) ){ + val = parseFloat(val); + } + } catch(e){ + val = res; + } + return val; + } + }; + var publicMethods = { + /** + * Set - let's you set a new localStorage key pair set + * @param key - a string that will be used as the accessor for the pair + * @param value - the value of the localStorage item + * @returns {*} - will return whatever it is you've stored in the local storage + */ + set: function(key,value){ + if (!supported){ + try { + $.cookie(key, value); + return value; + } catch(e){ + console.log('Local Storage not supported, make sure you have the $.cookie supported.'); + } + } + var saver = JSON.stringify(value); + storage.setItem(key, saver); + return privateMethods.parseValue(saver); + }, + /** + * Get - let's you get the value of any pair you've stored + * @param key - the string that you set as accessor for the pair + * @returns {*} - Object,String,Float,Boolean depending on what you stored + */ + get: function(key){ + if (!supported){ + try { + return privateMethods.parseValue($.cookie(key)); + } catch(e){ + return null; + } + } + var item = storage.getItem(key); + return privateMethods.parseValue(item); + }, + /** + * Remove - let's you nuke a value from localStorage + * @param key - the accessor value + * @returns {boolean} - if everything went as planned + */ + remove: function(key) { + if (!supported){ + try { + $.cookie(key, null); + return true; + } catch(e){ + return false; + } + } + storage.removeItem(key); + return true; + }, + /** + * Bind - let's you directly bind a localStorage value to a $scope variable + * @param $scope - the current scope you want the variable available in + * @param key - the name of the variable you are binding + * @param def - the default value (OPTIONAL) + * @returns {*} - returns whatever the stored value is + */ + bind: function ($scope, key, def) { + def = def || ''; + if (!publicMethods.get(key)) { + publicMethods.set(key, def); + } + $parse(key).assign($scope, publicMethods.get(key)); + $scope.$watch(key, function (val) { + publicMethods.set(key, val); + }, true); + return publicMethods.get(key); + } + }; + return publicMethods; +}); + diff --git a/js/websockets.js b/js/websockets.js index 6287893..fa2475e 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -1,4 +1,4 @@ -var weechat = angular.module('weechat', []); +var weechat = angular.module('weechat', ['localStorage']); weechat.factory('colors', [function($scope) { @@ -371,16 +371,17 @@ weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', funct } }]); -weechat.controller('WeechatCtrl', ['$rootScope', '$scope', 'connection', function ($rootScope, $scope, connection) { +weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', 'connection', function ($rootScope, $scope, $store, connection) { $rootScope.commands = [] $rootScope.buffer = [] $rootScope.buffers = {} $rootScope.activeBuffer = null; - $scope.hostport = "localhost:9001" - $scope.proto = "weechat" - $scope.password = "" - + $store.bind($scope, "hostport", "localhost:9001"); + $store.bind($scope, "proto", "weechat"); + $store.bind($scope, "password", ""); + // TODO checkbox for saving password or not? + // $scope.password = ""; $rootScope.closeBuffer = function(buffer_pointer) { delete($rootScope.buffers[buffer_pointer]);