[pLog-svn] r5934 - plog/trunk/js/ui

oscar at devel.lifetype.net oscar at devel.lifetype.net
Sat Sep 15 07:45:13 EDT 2007


Author: oscar
Date: 2007-09-15 07:45:13 -0400 (Sat, 15 Sep 2007)
New Revision: 5934

Added:
   plog/trunk/js/ui/uisettings.js
Log:
The new UISettings class allows javascript classes to persist settings in a brower cookie, so that they are available accross requests. I will also soon implement access to this from within smarty and the core, as data is saved inside the cookie in a simple but specific format (key1:value1|key2:value2|key3:value3|...)


Added: plog/trunk/js/ui/uisettings.js
===================================================================
--- plog/trunk/js/ui/uisettings.js	                        (rev 0)
+++ plog/trunk/js/ui/uisettings.js	2007-09-15 11:45:13 UTC (rev 5934)
@@ -0,0 +1,87 @@
+/**
+ * This class allows Javascript code to easily persist some values that are required
+ * accross sessions in a cookie. For example we can store whether a block of data
+ * is hidden or shown so that next time we can show or hide the data right away
+ */
+Lifetype.UISettings = function() {}
+
+/**
+ * Name and duration of the cookie that is used for the settings
+ */
+Lifetype.UISettings.cookieName = "LT_UISettings";
+Lifetype.UISettings.cookieDays = 365;
+
+/**
+ * Regular expressions used to break up the cookie into
+ * name-values pairs
+ */
+Lifetype.UISettings.nameValueSeparator =':';
+Lifetype.UISettings.valuesSeparator = '|';
+
+/**
+ * @param key
+ * @param value
+ */
+Lifetype.UISettings.setValue = function( key, value )
+{
+	var cookie = getCookie( Lifetype.UISettings.cookieName );
+	
+	if( cookie == null ) {
+		// the cookie wasn't there
+		setCookie( Lifetype.UISettings.cookieName, key+Lifetype.UISettings.nameValueSeparator+value, Lifetype.UISettings.cookieDays );
+	}
+	else {
+		// the cookie was there, we need to process its values and see whether we need to replace
+		// one that is already there or add is as a new one
+		if( Lifetype.UISettings.getValue( key ) == null ) {
+			// new value
+			var cookieValue = getCookie( Lifetype.UISettings.cookieName );
+			cookieValue += Lifetype.UISettings.valuesSeparator + key + Lifetype.UISettings.nameValueSeparator + value;
+		}
+		else {
+			// old value
+			var cookieValue = getCookie( Lifetype.UISettings.cookieName );
+			var values = cookieValue.split( Lifetype.UISettings.valuesSeparator );
+			var newCookieValue = "";
+			
+			for (var i = 0; i < values.length; i++ ) {
+				var valueData = values[i].split( Lifetype.UISettings.nameValueSeparator );
+				if( valueData[0] == key ) {
+					valueData[1] = value;
+				}
+				
+				// rebuild the cookie
+				newCookieValue += valueData[0] + Lifetype.UISettings.nameValueSeparator + valueData[1] + Lifetype.UISettings.valuesSeparator;
+			}
+			
+			// remove the last character if it's the separator
+			if( newCookieValue[newCookieValue.length-1] == Lifetype.UISettings.valuesSeparator )
+				newCookieValue = newCookieValue.substring( 0, newCookieValue.length - 1 );
+			
+			cookieValue = newCookieValue;
+		}
+		
+		setCookie( Lifetype.UISettings.cookieName, cookieValue, Lifetype.UISettings.cookieDays );
+	}
+}
+
+/**
+ * @param key
+ */
+Lifetype.UISettings.getValue = function( key )
+{
+	var cookieValue = getCookie( Lifetype.UISettings.cookieName );
+	
+	// the cookie doesn't exist
+	if( cookieValue == null )
+		return( null );
+		
+	var values = cookieValue.split( Lifetype.UISettings.valuesSeparator );
+	
+	for (var i = 0; i < values.length; i++ ) {
+		var valueData = values[i].split( Lifetype.UISettings.nameValueSeparator );
+		if( valueData[0] == key ) return valueData[1];
+	}
+	
+	return null;
+}
\ No newline at end of file



More information about the pLog-svn mailing list