Skip to content

Commit

Permalink
fully extracted cookie support from Base
Browse files Browse the repository at this point in the history
  • Loading branch information
andykent committed Apr 12, 2009
1 parent 79272d0 commit a676a69
Showing 1 changed file with 54 additions and 21 deletions.
75 changes: 54 additions & 21 deletions lib/polypage.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
var newState = !!val;
if(this.getState(stateName)==newState) return newState;
this.states[stateName] = newState;
this.setCookie(stateName, newState);
this.refresh();
this.scope.trigger('pp_stateChange', { name:stateName, value:newState });
return newState;
Expand All @@ -89,17 +88,6 @@

toggleStates: function(stateNames) { for(var i in stateNames) this.toggleState(stateNames[i]); },

setCookie: function(state,val) {
if(!$.cookie()||!state.length) return false;
$.cookie(this.prefixed(state), val ? 'yes' : null, {"path":"/"});
return true;
},

getCookie: function(state) {
if(!$.cookie()||!state.length) return false;
$.cookie(this.prefixed(state))!=null;
},

prefixed: function(str){
return this.options.prefix+this.options.separator+str;
},
Expand Down Expand Up @@ -137,13 +125,8 @@
var s = self.extractDataFromClassName($(this).attr('class'));
var states = s.split(new RegExp(self.separated('?not')+'|'+self.separated('or')+'|'+self.separated('and')));
for(var state in states) {
if(self.states[states[state]]==undefined && states[state]!='') {
if($.cookie()){
self.states[states[state]]=self.getCookie(states[state]);
} else {
self.states[states[state]]=false;
}
}
if(self.states[states[state]]==undefined && states[state]!='')
self.states[states[state]]=false;
}
});
return this.states;
Expand Down Expand Up @@ -187,10 +170,16 @@
return eval(str);
},

statesList: function(){
var list = [];
for(var state in this.states) { list.push(state) };
return list;
},

alphabeticalStates: function(){
var alphaStates = [], ret = [];
for(var state in this.states) { alphaStates.push(state) }
alphaStates = alphaStates.sort();
alphaStates = this.statesList();
for(var state in alphaStates) { ret.push({name:alphaStates[state], value:this.states[alphaStates[state]]}); };
return ret
}
Expand Down Expand Up @@ -356,9 +345,53 @@

$.polypage.extension('Cookie',
{

expires: null, // can be a Date object or a integer number of days
namespace:'pp_'
},
{
init: function(){
this.checkForCookieSupport();
this.setInitialStateFromCookie();
this.bindStateChangeListener();
},

checkForCookieSupport: function(){
if($.cookie===undefined) throw "The jQuery Cookie plugin is required to use the ppCookie() extension but was not found at $.cookie()";
},

setInitialStateFromCookie: function(){
var states = this.polypage.statesList();
for(var i in states) {
var stateName = states[i];
if(this.getCookie(stateName)) {
var obj = {}
obj[stateName]=true
this.scope.trigger('pp_setState', obj);

}
}
},

setCookie: function(state,val) {
if(!state.length) return false;
$.cookie(this.prefixed(state), val ? 'yes' : null, {"path":"/", "expires":this.options.expires});
return true;
},

getCookie: function(state) {
if(!state.length) return false;
return $.cookie(this.prefixed(state))!=null;
},

prefixed: function(state) {
return this.options.namespace+state;
},

bindStateChangeListener: function(){
var c = this;
this.polypageScope.bind('pp_stateChange', function(e, state) {
c.setCookie(state.name, state.value);
});
}
});
})(jQuery);

0 comments on commit a676a69

Please sign in to comment.