Created
February 22, 2014 15:37
-
-
Save athlan/9156734 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCookies() { | |
var c = document.cookie, v = 0, cookies = {}; | |
if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) { | |
c = RegExp.$1; | |
v = 1; | |
} | |
if (v === 0) { | |
c.split(/[,;]/).map(function(cookie) { | |
var parts = cookie.split(/=/, 2), | |
name = decodeURIComponent(parts[0].trimLeft()), | |
value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null; | |
cookies[name] = value; | |
}); | |
} else { | |
c.match(/(?:^|\s+)([!#$%&'*+\-.0-9A-Z^`a-z|~]+)=([!#$%&'*+\-.0-9A-Z^`a-z|~]*|"(?:[\x20-\x7E\x80\xFF]|\\[\x00-\x7F])*")(?=\s*[,;]|$)/g).map(function($0, $1) { | |
var name = $0, | |
value = $1.charAt(0) === '"' | |
? $1.substr(1, -1).replace(/\\(.)/g, "$1") | |
: $1; | |
cookies[name] = value; | |
}); | |
} | |
return cookies; | |
} | |
function getCookie(name) { | |
return getCookies()[name]; | |
} | |
function setCookie(c_name, value, exdays) { | |
var exdate = new Date(); | |
exdate.setDate(exdate.getDate() + exdays); | |
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()); | |
document.cookie = c_name + "=" + c_value; | |
} | |
function layerToggleStates(states, options) { | |
if(states.length <= 0) | |
throw "No states defined." | |
var that = this; | |
this.states = states; | |
this.stateCurrent = null; // idx | |
this.buttonZoomIn = options.buttonZoomIn || null; | |
this.buttonZoomOut = options.buttonZoomOut || null; | |
this.stateRememberCookie = options.stateRememberCookie || null; | |
this.defaultState = options.defaultState || null; | |
this.buttonStack = [ | |
this.buttonZoomIn, | |
this.buttonZoomOut | |
] | |
/** | |
* Try to discover index | |
*/ | |
if(this.stateCurrent === null && this.stateRememberCookie) { | |
this.stateCurrent = getCookie(this.stateRememberCookie) | |
if(typeof this.stateCurrent == 'undefined' || this.stateCurrent < 0 || this.stateCurrent > (this.states.length-1)) | |
this.stateCurrent = null; | |
} | |
if(this.stateCurrent === null && this.defaultState) { | |
this.stateCurrent = this.defaultState; | |
if(this.stateCurrent < 0 || this.stateCurrent > (this.states.length-1)) | |
this.stateCurrent = null; | |
} | |
if(this.stateCurrent === null) { | |
this.stateCurrent = 0 | |
} | |
if(this.buttonZoomIn) { | |
this.buttonZoomIn.on('click', function() { | |
var btn = $(this); | |
if(btn.hasClass('disabled')) | |
return false; | |
++that.stateCurrent; | |
$.each(that.buttonStack, function() { | |
this.removeClass('disabled'); | |
}) | |
if(that.stateCurrent >= (that.states.length-1)) | |
btn.addClass('disabled'); | |
that.runToggle(); | |
return false; | |
}) | |
} | |
if(this.buttonZoomOut) { | |
this.buttonZoomOut.on('click', function() { | |
var btn = $(this); | |
if(btn.hasClass('disabled')) | |
return false; | |
--that.stateCurrent; | |
$.each(that.buttonStack, function() { | |
this.removeClass('disabled'); | |
}) | |
if(that.stateCurrent <= 0) | |
btn.addClass('disabled'); | |
that.runToggle(); | |
return false; | |
}) | |
} | |
this.runToggle = function() { | |
console.log('Enable state #' + that.stateCurrent); | |
that.states[that.stateCurrent](); | |
if(that.stateRememberCookie) { | |
setCookie(that.stateRememberCookie, that.stateCurrent, 365); | |
} | |
} | |
this.runToggle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment