1 /** 2 * Copyright (c) 2014, salesforce.com, inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, are permitted provided 6 * that the following conditions are met: 7 * 8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the 9 * following disclaimer. 10 * 11 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 12 * the following disclaimer in the documentation and/or other materials provided with the distribution. 13 * 14 * Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or 15 * promote products derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 19 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 28 (function ($$) { 29 30 "use strict"; 31 32 var module = (function() { 33 34 function isSecure() 35 { 36 return window.location.protocol === 'https:'; 37 } 38 39 /** 40 * @description Create a cookie 41 * @param {String} name Cookie name 42 * @param {String} value Cookie value 43 * @param {Integer} [days] Number of days for the cookie to remain active. 44 If not provided, the cookie never expires 45 */ 46 function set(name, value, days) { 47 var expires = "", date; 48 if (days) { 49 date = new Date(); 50 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 51 expires = "; expires=" + date.toGMTString(); 52 } 53 else { 54 expires = ""; 55 } 56 document.cookie = name + "=" + value + expires + "; path=/" + ((isSecure() === true) ? "; secure" : ""); 57 } 58 59 /** 60 * @description Get the cookie with the specified name 61 * @param {String} name The name of the cookie to retrieve 62 * @returns The value of the cookie if the name is found, otherwise null 63 */ 64 function get(name) { 65 var nameEQ, ca, c, i; 66 67 if ($$.isUndefined(name)) { 68 return document.cookie.split(';'); 69 } 70 71 nameEQ = name + "="; 72 ca = document.cookie.split(';'); 73 for (i = 0; i < ca.length; i += 1) { 74 c = ca[i]; 75 while (c.charAt(0) === ' ') {c = c.substring(1, c.length);} 76 if (c.indexOf(nameEQ) === 0) { 77 return c.substring(nameEQ.length, c.length); 78 } 79 } 80 return null; 81 } 82 83 /** 84 * @description Remove the specified cookie by setting the expiry date to one day ago 85 * @param {String} name The name of the cookie to remove. 86 */ 87 function remove(name) { 88 set(name, "", -1); 89 } 90 91 return { 92 set : set, 93 get : get, 94 remove : remove 95 }; 96 }()); 97 98 99 $$.module('Sfdc.canvas.cookies', module); 100 101 }(Sfdc.canvas));