~mrooney/etherpad/ubuntu

« back to all changes in this revision

Viewing changes to trunk/trunk/etherpad/src/static/js/pad_cookie.js

  • Committer: Aaron Iba
  • Date: 2009-12-18 07:40:23 UTC
  • Revision ID: hg-v1:a9f8774a2e00cc15b35857471fecea17f649e3c9
initial code push

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2009 Google Inc.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS-IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
 
 
18
var padcookie = (function(){
 
19
  function getRawCookie() {
 
20
    // returns null if can't get cookie text
 
21
    if (! document.cookie) {
 
22
      return null;
 
23
    }
 
24
    // look for (start of string OR semicolon) followed by whitespace followed by prefs=(something);
 
25
    var regexResult = document.cookie.match(/(?:^|;)\s*prefs=([^;]*)(?:;|$)/);
 
26
    if ((! regexResult) || (! regexResult[1])) {
 
27
      return null;
 
28
    }
 
29
    return regexResult[1];
 
30
  }
 
31
  function setRawCookie(safeText) {
 
32
    var expiresDate = new Date();
 
33
    expiresDate.setFullYear(3000);
 
34
    document.cookie = ('prefs='+safeText+';expires='+expiresDate.toGMTString());
 
35
  }
 
36
  function parseCookie(text) {
 
37
    // returns null if can't parse cookie.
 
38
 
 
39
    try {
 
40
      var cookieData = JSON.parse(unescape(text));
 
41
      return cookieData;
 
42
    }
 
43
    catch (e) {
 
44
      return null;
 
45
    }
 
46
  }
 
47
  function stringifyCookie(data) {
 
48
    return escape(JSON.stringify(data));
 
49
  }
 
50
  function saveCookie() {
 
51
    if (! inited) {
 
52
      return;
 
53
    }
 
54
    setRawCookie(stringifyCookie(cookieData));
 
55
 
 
56
    if (pad.getIsProPad() && (! getRawCookie()) && (! alreadyWarnedAboutNoCookies)) {
 
57
      alert("Warning: it appears that your browser does not have cookies enabled."+
 
58
            " EtherPad uses cookies to keep track of unique users for the purpose"+
 
59
            " of putting a quota on the number of active users.  Using EtherPad without "+
 
60
            " cookies may fill up your server's user quota faster than expected.");
 
61
      alreadyWarnedAboutNoCookies = true;
 
62
    }
 
63
  }
 
64
 
 
65
  var wasNoCookie = true;
 
66
  var cookieData = {};
 
67
  var alreadyWarnedAboutNoCookies = false;
 
68
  var inited = false;
 
69
 
 
70
  var self = {
 
71
    init: function(prefsToSet) {
 
72
      var rawCookie = getRawCookie();
 
73
      if (rawCookie) {
 
74
        var cookieObj = parseCookie(rawCookie);
 
75
        if (cookieObj) {
 
76
          wasNoCookie = false; // there was a cookie
 
77
          delete cookieObj.userId;
 
78
          delete cookieObj.name;
 
79
          delete cookieObj.colorId;
 
80
          cookieData = cookieObj;
 
81
        }
 
82
      }
 
83
 
 
84
      for(var k in prefsToSet) {
 
85
        cookieData[k] = prefsToSet[k];
 
86
      }
 
87
 
 
88
      inited = true;
 
89
      saveCookie();
 
90
    },
 
91
    wasNoCookie: function() { return wasNoCookie; },
 
92
    getPref: function(prefName) {
 
93
      return cookieData[prefName];
 
94
    },
 
95
    setPref: function(prefName, value) {
 
96
      cookieData[prefName] = value;
 
97
      saveCookie();
 
98
    }
 
99
  };
 
100
  return self;
 
101
}());
 
 
b'\\ No newline at end of file'