]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/util/Cookie.js
removing old opac images and css
[working/Evergreen.git] / Open-ILS / src / javascript / util / Cookie.js
1 /*
2
3 DISCLAIMER: THESE JAVASCRIPT FUNCTIONS ARE SUPPLIED 'AS IS', WITH 
4 NO WARRANTY EXPRESSED OR IMPLIED. YOU USE THEM AT YOUR OWN RISK. 
5 PAUL STEPHENS DOES NOT ACCEPT ANY LIABILITY FOR 
6 ANY LOSS OR DAMAGE RESULTING FROM THEIR USE, HOWEVER CAUSED. 
7
8 Paul Stephens' cookie-handling object library
9
10 Version 2.1
11 2.0 - Introduces field names
12 2.1 - Fixes bug where undefined embedded fields[] elements weren't written to disk
13
14 www.paulspages.co.uk 
15
16 TO USE THIS LIBRARY, INSERT ITS CONTENTS IN THE <HEAD> SECTION 
17 OF YOUR WEB PAGE SOURCE, BEFORE ANY OTHER JAVASCRIPT ROUTINES.
18
19 (C) Paul Stephens, 2001-2003. Feel free to use this code, but please leave this comment block in. This code must not be sold, either alone or as part of an application, without the consent of the author.
20 */
21
22 function cookieObject(name, expires, accessPath) {
23 var i, j
24 this.name = name
25 this.fieldSeparator = "#"
26 this.found = false
27 this.expires = expires
28 this.accessPath = accessPath
29 this.rawValue = ""
30 this.fields = new Array()
31 this.fieldnames = new Array() 
32 if (arguments.length > 3) { // field name(s) specified
33   j = 0
34   for (i = 3; i < arguments.length; i++) {
35     this.fieldnames[j] = arguments[i]    
36     j++
37   }
38   this.fields.length = this.fieldnames.length 
39 }
40 this.read = ucRead
41
42 this.write = ucWrite
43
44 this.remove = ucDelete
45 this.get = ucFieldGet
46 this.put = ucFieldPut
47 this.namepos = ucNamePos
48 this.read()
49 }
50
51
52 function ucFieldGet(fieldname) {
53 var i = this.namepos(fieldname)
54 if (i >=0) {
55   return this.fields[i]
56 } else {
57   return "BadFieldName!"
58 }
59 }
60
61 function ucFieldPut (fieldname, fieldval) {
62 var i = this.namepos(fieldname)
63
64 if(i < 0) {
65         i = this.fieldnames.length;
66         this.fieldnames[i] = fieldname;
67 }
68
69 this.fields[i] = fieldval
70 return true
71 }
72
73 function ucNamePos(fieldname) {
74 var i 
75 for (i = 0; i < this.fieldnames.length; i++) {
76   if (fieldname == this.fieldnames[i]) {
77     return i
78   }
79 }
80 return -1
81 }
82
83
84 function ucWrite() {      
85   var cookietext = this.name + "=" 
86
87 // concatenate array elements into cookie string
88
89 // Special case - single-field cookie, so write without # terminator
90 if (this.fields.length == 1) {
91   cookietext += escape(this.fields[0])
92   } else { // multi-field cookie
93     for (i= 0; i < this.fields.length; i++) {
94       cookietext += escape(this.fields[i]) + this.fieldSeparator }
95   }
96
97
98 // Set expiry parameter, if specified
99     if (this.expires != null) {  
100       if (typeof(this.expires) == "number") { // Expiry period in days specified  
101         var today=new Date()     
102         var expiredate = new Date()      
103         expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires)
104         cookietext += "; expires=" + expiredate.toGMTString()
105       } else { // assume it's a date object
106         cookietext +=  "; expires=" + this.expires.toGMTString()
107       } // end of typeof(this.expires) if
108     } // end of this.expires != null if 
109    
110 // add path, if specified
111    if (this.accessPath != null) {
112    cookietext += "; PATH="+this.accessPath }
113
114 // write cookie
115    // alert("writing "+cookietext)
116    document.cookie = cookietext 
117    return null  
118 }
119
120
121 function ucRead() {
122   var search = this.name + "="                       
123   var CookieString = document.cookie            
124   if(CookieString == null) CookieString = "";
125   this.rawValue = null
126   this.found = false     
127   if (CookieString.length > 0) {                
128     offset = CookieString.indexOf(search)       
129     if (offset != -1) {                         
130       offset += search.length                   
131       end = CookieString.indexOf(";", offset)   
132       if (end == -1) {  // cookie is last item in the string, so no terminator                        
133        end = CookieString.length }              
134       this.rawValue = CookieString.substring(offset, end)                                   
135       this.found = true 
136       } 
137     }
138    
139 if (this.rawValue != null) { // unpack into fields
140
141   var sl = this.rawValue.length
142   var startidx = 0
143   var endidx = 0
144   var i = 0
145
146 // Special case - single-field cookies written by other functions,
147 // so without a '#' terminator
148
149 if (this.rawValue.substr(sl-1, 1) != this.fieldSeparator) {
150   this.fields[0] = unescape(this.rawValue)
151   } else { // separate fields
152
153   do  
154   {
155    endidx = this.rawValue.indexOf(this.fieldSeparator, startidx)
156    if (endidx !=-1) {
157      this.fields[i] = unescape(this.rawValue.substring(startidx, endidx))
158      i++
159      startidx = endidx + 1}
160   }
161   while (endidx !=-1 & endidx != (this.rawValue.length -1));
162 }
163 } // end of unpack into fields if block
164   return this.found
165 } // end of function
166
167
168 function ucDelete() {
169   this.expires = -10
170   this.write()
171   return this.read()
172 }
173
174
175
176 /*
177 *********** IT'S OK TO REMOVE THE CODE BELOW HERE IF YOUR PAGE 
178 DOESN'T USE cookieList() OBJECTS OR THE findCookieObject() FUNCTION.
179 */
180
181
182
183
184 function findCookieObject(cName, cObjArray) {
185 /* 
186 This function finds a named cookie among the objects
187 pointed to by a cookieList array (see below).
188
189 Parameters are the cookie name to search for (a string), and an array created with 
190 the new cookieList() constructor (see below)
191
192 NOTE - if you're only dealing with a specific, named cookie, then it's
193 more efficient to ceate a single cookieObject directly with that name,
194 and check its .found property to see if it already exists on this client.
195
196 This function is for when you've created an all-cookies array anyway,
197 and now want to check whether a specific cookie is present.
198
199 It returns a pointer to the cookieObject if found, or null if not found.
200 */
201
202 var cpointer = null, i
203 for (i in cObjArray) {
204   if (cName == cObjArray[i].name) {
205     cpointer = cObjArray[i]
206   }
207 }
208 return cpointer
209 }
210
211
212 function cookieList() {
213 /* 
214 This constructor function creates a cookieObject object (see below) 
215 for each cookie in document.cookie,
216 and returns an array of pointers to the objects.
217
218 You can use it to load all the cookies available to a page, then walk through them.
219
220 Example usage:
221
222 cookList = new cookieList()
223 for (i in cookList) {
224  document.write(cookList[i].name + " " + cookList[i].fields[0] + "
225 ")
226 }
227
228 */
229
230 var i = 0, rawstring, offset = 0, start, newname
231 cpointers = new Array()
232 rawstring = document.cookie
233 if (rawstring.length > 0) {
234   do {
235    start = rawstring.indexOf("=", offset)
236    if (start != -1) { // another cookie found in string
237      // get cookie string up to end of current cookie name
238      newname = rawstring.substring(0, start) 
239      if (offset > 0) { 
240        // if not first cookie in string, remove previous cookie data from substring
241        // subsequent cookie names have a space before them (just a little browser foible!)
242        newname = newname.substring(newname.lastIndexOf(";")+2, start)
243      }     
244      cpointers[i] = new cookieObject(newname)
245      offset = start + 1
246      i++
247    }
248   } while (start != -1)
249 } // end rawstring.length > 0
250 return cpointers
251 } //end function
252