No more CookieManager since it's dependent on Prototype

This commit is contained in:
Eric Allen 2009-09-02 11:04:28 -04:00
parent e57e1445b4
commit fac5e7ca83
6 changed files with 106 additions and 131 deletions

View file

@ -11,12 +11,12 @@
if (Element.visible(toggleTarget)) if (Element.visible(toggleTarget))
{ {
todoItems.collapseNextActionListing(this, toggleTarget); todoItems.collapseNextActionListing(this, toggleTarget);
todoItems.contextCollapseCookieManager.setCookie(todoItems.buildCookieName(containerElem), true) $.cookie(todoItems.buildCookieName(containerElem), true);
} }
else else
{ {
todoItems.expandNextActionListing(this, toggleTarget); todoItems.expandNextActionListing(this, toggleTarget);
todoItems.contextCollapseCookieManager.clearCookie(todoItems.buildCookieName(containerElem)) $.cookie(todoItems.buildCookieName(containerElem), null);
} }
} }
" "

View file

@ -11,6 +11,7 @@
<%= stylesheet_link_tag "print", :media => "print" %> <%= stylesheet_link_tag "print", :media => "print" %>
<% bundle :name => "jquery" do %> <% bundle :name => "jquery" do %>
<%= javascript_include_tag :defaults %> <%= javascript_include_tag :defaults %>
<%= javascript_include_tag 'jquery.cookie' %>
<% end %> <% end %>
<% bundle :name => "tracks_js" do %> <% bundle :name => "tracks_js" do %>
<%= javascript_include_tag *%w[ <%= javascript_include_tag *%w[

View file

@ -61,7 +61,7 @@
<script type="text/javascript"> <script type="text/javascript">
function showPreferredAuth() { function showPreferredAuth() {
var preferredAuth = new CookieManager().getCookie('preferred_auth'); var preferredAuth = $.cookie('preferred_auth');
var databaseEnabled = <%= show_database_form ? 'true' : 'false' %>; var databaseEnabled = <%= show_database_form ? 'true' : 'false' %>;
var openidEnabled = <%= show_openid_form ? 'true' : 'false' %>; var openidEnabled = <%= show_openid_form ? 'true' : 'false' %>;
if (preferredAuth && preferredAuth == 'openid' && openidEnabled) { if (preferredAuth && preferredAuth == 'openid' && openidEnabled) {

View file

@ -6,7 +6,7 @@ var Login = {
if ($('alternate_auth_database')) $('alternate_auth_database').show(); if ($('alternate_auth_database')) $('alternate_auth_database').show();
if ($('openid_url')) $('openid_url').focus(); if ($('openid_url')) $('openid_url').focus();
if ($('openid_url')) $('openid_url').select(); if ($('openid_url')) $('openid_url').select();
new CookieManager().setCookie('preferred_auth', 'openid'); $.cookie('preferred_auth', 'openid');
}, },
showDatabase: function(container) { showDatabase: function(container) {
@ -16,7 +16,7 @@ var Login = {
if ($('alternate_auth_openid')) $('alternate_auth_openid').show(); if ($('alternate_auth_openid')) $('alternate_auth_openid').show();
if ($('user_login')) $('user_login').focus(); if ($('user_login')) $('user_login').focus();
if ($('user_login')) $('user_login').select(); if ($('user_login')) $('user_login').select();
new CookieManager().setCookie('preferred_auth', 'database'); $.cookie('preferred_auth', 'database');
} }
} }
@ -110,124 +110,3 @@ Event.observe(window, 'load', function() {
}); });
}); });
/**
* Provides a simple interface for creating, retrieving and clearing cookies.
* Adapted from Jonathan Buchanan's code at http://insin.woaf.net/code/javascript/cookiemanager.html
*/
CookieManager = Class.create();
CookieManager.prototype =
{
BROWSER_IS_IE:
(document.all
&& window.ActiveXObject
&& navigator.userAgent.toLowerCase().indexOf("msie") > -1
&& navigator.userAgent.toLowerCase().indexOf("opera") == -1),
/**
* I hate navigator string based browser detection too, but when Opera alone
* chokes on cookies containing double quotes...
*/
BROWSER_IS_OPERA:
(navigator.userAgent.toLowerCase().indexOf("opera") != -1),
initialize: function(options)
{
this.options = Object.extend({
shelfLife: 365,
userData: false
}, options || {});
this.cookieShelfLife = this.options.shelfLife;
this.userDataForIE = this.options.userData;
// Internet Explorer has a cookie handling bug - if the *combined size*
// of all cookies stored for a given domain is greater than 4096 bytes,
// document.cookie will return an empty string. Until this is fixed, we
// can fall back on IE's proprietary userData behaviour if necessary.
if (this.BROWSER_IS_IE && this.userDataForIE)
{
this.IE_CACHE_NAME = "storage";
if ($(this.IE_CACHE_NAME) == null)
{
var div = document.createElement("DIV");
div.id = this.IE_CACHE_NAME;
document.body.appendChild(div);
}
this.store = $(this.IE_CACHE_NAME);
this.store.style.behavior = "url('#default#userData')";
}
},
/**
* Returns the value of a cookie with the given name, or <code>null</code>
* if no such cookie exists.
*/
getCookie: function(aCookieName)
{
var result = null;
if (this.BROWSER_IS_IE && this.userDataForIE)
{
this.store.load(this.IE_CACHE_NAME);
result = this.store.getAttribute(aCookieName);
}
else
{
for (var i = 0; i < document.cookie.split('; ').length; i++)
{
var crumb = document.cookie.split('; ')[i].split('=');
if (crumb[0] == aCookieName && crumb[1] != null)
{
result = crumb[1];
break;
}
}
}
if (this.BROWSER_IS_OPERA && result != null)
{
result = result.replace(/%22/g, '"');
}
return result;
},
/**
* Sets a cookie with the given name and value.
*/
setCookie: function(aCookieName, aCookieValue)
{
if (this.BROWSER_IS_IE && this.userDataForIE)
{
this.store.setAttribute(aCookieName, aCookieValue);
this.store.save(this.IE_CACHE_NAME);
}
else
{
if (this.BROWSER_IS_OPERA)
{
aCookieValue = aCookieValue.replace(/"/g, "%22");
}
var date = new Date();
date.setTime(date.getTime() + (this.cookieShelfLife * 24*60*60*1000));
var expires = '; expires=' + date.toGMTString();
document.cookie = aCookieName + '=' + aCookieValue + expires + '; path=/';
}
},
/**
* Clears the cookie with the given name.
*/
clearCookie: function(aCookieName)
{
if (this.BROWSER_IS_IE && this.userDataForIE)
{
this.store.load(this.IE_CACHE_NAME);
this.store.removeAttribute(aCookieName);
this.store.save(this.IE_CACHE_NAME);
}
else
{
document.cookie =
aCookieName + '=;expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';
}
}
}

View file

@ -0,0 +1,96 @@
/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
/**
* Create a cookie with the given name and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
* used when the cookie was set.
*
* @param String name The name of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
/**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};

View file

@ -15,7 +15,6 @@ ToDoItems.prototype = {
/* keep track of last effect so you can check if the animation has finised */ /* keep track of last effect so you can check if the animation has finised */
this.lastEffect= null; this.lastEffect= null;
this.initialized = true; this.initialized = true;
this.contextCollapseCookieManager = new CookieManager();
this.toggleItemsMap = {}; this.toggleItemsMap = {};
this.toggleContainerMap = {}; this.toggleContainerMap = {};
this.containerItemsMap = {}; this.containerItemsMap = {};
@ -41,7 +40,7 @@ ToDoItems.prototype = {
{ {
toggleElem = this.containerToggles[i]; toggleElem = this.containerToggles[i];
containerElem = this.toggleContainerMap[toggleElem.id]; containerElem = this.toggleContainerMap[toggleElem.id];
collapsedCookie = this.contextCollapseCookieManager.getCookie(this.buildCookieName(containerElem)); collapsedCookie = $.cookie(this.buildCookieName(containerElem));
itemsElem = this.toggleItemsMap[toggleElem.id]; itemsElem = this.toggleItemsMap[toggleElem.id];
isExpanded = Element.visible(itemsElem); isExpanded = Element.visible(itemsElem);
if (collapsedCookie && isExpanded) if (collapsedCookie && isExpanded)
@ -93,12 +92,12 @@ ToDoItems.prototype = {
if (Element.visible(itemsElem)) if (Element.visible(itemsElem))
{ {
this.collapseNextActionListing(toggleElem, itemsElem); this.collapseNextActionListing(toggleElem, itemsElem);
this.contextCollapseCookieManager.setCookie(this.buildCookieName(containerElem), true) $.cookie(this.buildCookieName(containerElem), true);
} }
else else
{ {
this.expandNextActionListing(toggleElem, itemsElem); this.expandNextActionListing(toggleElem, itemsElem);
this.contextCollapseCookieManager.clearCookie(this.buildCookieName(containerElem)) $.cookie(this.buildCookieName(containerElem), null);
} }
}, },
findToggleElemForContext : function(contextElem) findToggleElemForContext : function(contextElem)
@ -148,7 +147,7 @@ ToDoItems.prototype = {
}, },
buildCookieName: function(containerElem) buildCookieName: function(containerElem)
{ {
tracks_login = this.contextCollapseCookieManager.getCookie('tracks_login'); tracks_login = $.cookie('tracks_login');
return 'tracks_'+tracks_login+'_context_' + containerElem.id + '_collapsed'; return 'tracks_'+tracks_login+'_context_' + containerElem.id + '_collapsed';
}, },