From 34edba3adfb2c15543273d3de4ddc74abf896e8c Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Thu, 26 Jan 2012 22:42:58 -0800 Subject: [PATCH] Share `createCookie` and `readCookie` methods. --- static/js/pad.js | 30 ++---------------------------- static/js/pad_utils.js | 31 +++++++++++++++++++++++++++++++ static/js/timeslider.js | 24 ++---------------------- 3 files changed, 35 insertions(+), 50 deletions(-) diff --git a/static/js/pad.js b/static/js/pad.js index 17c2c800..0b292174 100644 --- a/static/js/pad.js +++ b/static/js/pad.js @@ -46,34 +46,8 @@ var padsavedrevs = require('/pad_savedrevs').padsavedrevs; var paduserlist = require('/pad_userlist').paduserlist; var padutils = require('/pad_utils').padutils; -function createCookie(name, value, days, path) -{ - if (days) - { - var date = new Date(); - date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); - var expires = "; expires=" + date.toGMTString(); - } - else var expires = ""; - - if(!path) - path = "/"; - - document.cookie = name + "=" + value + expires + "; path=" + path; -} - -function readCookie(name) -{ - var nameEQ = name + "="; - var ca = document.cookie.split(';'); - for (var i = 0; i < ca.length; i++) - { - var c = ca[i]; - while (c.charAt(0) == ' ') c = c.substring(1, c.length); - if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); - } - return null; -} +var createCookie = require('/pad_utils').createCookie; +var readCookie = require('/pad_utils').readCookie; function randomString() { diff --git a/static/js/pad_utils.js b/static/js/pad_utils.js index 071185a8..ddfe4664 100644 --- a/static/js/pad_utils.js +++ b/static/js/pad_utils.js @@ -20,6 +20,35 @@ * limitations under the License. */ +function createCookie(name, value, days, path) +{ + if (days) + { + var date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + var expires = "; expires=" + date.toGMTString(); + } + else var expires = ""; + + if(!path) + path = "/"; + + document.cookie = name + "=" + value + expires + "; path=" + path; +} + +function readCookie(name) +{ + var nameEQ = name + "="; + var ca = document.cookie.split(';'); + for (var i = 0; i < ca.length; i++) + { + var c = ca[i]; + while (c.charAt(0) == ' ') c = c.substring(1, c.length); + if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); + } + return null; +} + var padutils = { escapeHtml: function(x) { @@ -481,4 +510,6 @@ padutils.setupGlobalExceptionHandler = setupGlobalExceptionHandler; padutils.binarySearch = require('/ace2_common').binarySearch; +exports.createCookie = createCookie; +exports.readCookie = readCookie; exports.padutils = padutils; diff --git a/static/js/timeslider.js b/static/js/timeslider.js index d2fce8fd..11eeddd7 100644 --- a/static/js/timeslider.js +++ b/static/js/timeslider.js @@ -26,28 +26,8 @@ require('/jquery'); JSON = require('/json2'); require('/undo-xpopup'); -function createCookie(name,value,days) -{ - if (days) { - var date = new Date(); - date.setTime(date.getTime()+(days*24*60*60*1000)); - var expires = "; expires="+date.toGMTString(); - } - else var expires = ""; - document.cookie = name+"="+value+expires+"; path=/"; -} - -function readCookie(name) -{ - var nameEQ = name + "="; - var ca = document.cookie.split(';'); - for(var i=0;i < ca.length;i++) { - var c = ca[i]; - while (c.charAt(0)==' ') c = c.substring(1,c.length); - if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); - } - return null; -} +var createCookie = require('/pad_utils').createCookie; +var readCookie = require('/pad_utils').readCookie; function randomString() { var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";