etherpad-lite/static/js/pad_cookie.js

123 lines
3 KiB
JavaScript
Raw Normal View History

2011-03-26 14:10:41 +01:00
/**
* Copyright 2009 Google Inc.
2011-07-07 19:59:34 +02:00
*
2011-03-26 14:10:41 +01:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2011-07-07 19:59:34 +02:00
*
2011-03-26 14:10:41 +01:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-07-07 19:59:34 +02:00
*
2011-03-26 14:10:41 +01:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2011-07-07 19:59:34 +02:00
var padcookie = (function()
{
function getRawCookie()
{
2011-03-26 14:10:41 +01:00
// returns null if can't get cookie text
2011-07-07 19:59:34 +02:00
if (!document.cookie)
{
2011-03-26 14:10:41 +01:00
return null;
}
// look for (start of string OR semicolon) followed by whitespace followed by prefs=(something);
var regexResult = document.cookie.match(/(?:^|;)\s*prefs=([^;]*)(?:;|$)/);
2011-07-07 19:59:34 +02:00
if ((!regexResult) || (!regexResult[1]))
{
2011-03-26 14:10:41 +01:00
return null;
}
return regexResult[1];
}
2011-07-07 19:59:34 +02:00
function setRawCookie(safeText)
{
2011-03-26 14:10:41 +01:00
var expiresDate = new Date();
expiresDate.setFullYear(3000);
2011-07-07 19:59:34 +02:00
document.cookie = ('prefs=' + safeText + ';expires=' + expiresDate.toGMTString());
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
function parseCookie(text)
{
// returns null if can't parse cookie.
try
{
2011-03-26 14:10:41 +01:00
var cookieData = JSON.parse(unescape(text));
return cookieData;
}
2011-07-07 19:59:34 +02:00
catch (e)
{
2011-03-26 14:10:41 +01:00
return null;
}
}
2011-07-07 19:59:34 +02:00
function stringifyCookie(data)
{
2011-03-26 14:10:41 +01:00
return escape(JSON.stringify(data));
}
2011-07-07 19:59:34 +02:00
function saveCookie()
{
if (!inited)
{
2011-03-26 14:10:41 +01:00
return;
}
setRawCookie(stringifyCookie(cookieData));
2011-07-07 19:59:34 +02:00
if (pad.getIsProPad() && (!getRawCookie()) && (!alreadyWarnedAboutNoCookies))
{
alert("Warning: it appears that your browser does not have cookies enabled." + " EtherPad uses cookies to keep track of unique users for the purpose" + " of putting a quota on the number of active users. Using EtherPad without " + " cookies may fill up your server's user quota faster than expected.");
2011-03-26 14:10:41 +01:00
alreadyWarnedAboutNoCookies = true;
}
}
var wasNoCookie = true;
var cookieData = {};
var alreadyWarnedAboutNoCookies = false;
var inited = false;
var self = {
2011-07-07 19:59:34 +02:00
init: function(prefsToSet)
{
2011-03-26 14:10:41 +01:00
var rawCookie = getRawCookie();
2011-07-07 19:59:34 +02:00
if (rawCookie)
{
2011-03-26 14:10:41 +01:00
var cookieObj = parseCookie(rawCookie);
2011-07-07 19:59:34 +02:00
if (cookieObj)
{
2011-03-26 14:10:41 +01:00
wasNoCookie = false; // there was a cookie
delete cookieObj.userId;
delete cookieObj.name;
delete cookieObj.colorId;
cookieData = cookieObj;
}
}
2011-07-07 19:59:34 +02:00
for (var k in prefsToSet)
{
2011-03-26 14:10:41 +01:00
cookieData[k] = prefsToSet[k];
}
inited = true;
saveCookie();
},
2011-07-07 19:59:34 +02:00
wasNoCookie: function()
{
return wasNoCookie;
},
getPref: function(prefName)
{
2011-03-26 14:10:41 +01:00
return cookieData[prefName];
},
2011-07-07 19:59:34 +02:00
setPref: function(prefName, value)
{
2011-03-26 14:10:41 +01:00
cookieData[prefName] = value;
saveCookie();
}
};
return self;
2011-07-07 19:59:34 +02:00
}());