function setCookie(label_name, value) { /** * Create a cookie for ten years? */ var today = new Date(); var expires = new Date(); expires.setTime(today.getTime() + (10*365*24*60*60*1000)); document.cookie = label_name + "=" + value + ";expires=" + expires.toGMTString(); } function getCookie(label_name) { /** * Retrieve the value of a cookie due to it's label_name. */ var regex = new RegExp("(?:; )?" + label_name + "=([^;]*);?"); if (regex.test(document.cookie)) return (RegExp["$1"]); else return null; } function labels_tab_to_str(labels) { /** * Transform tab of labels to string. * ex : [{'id_label': 1, 'label_name': "Learn C++", 'father_id': 6}] * = 1, Learn C++, 6|... */ var result = ""; var inter = [] for (var i = 0; labels[i]; i++) inter.push([labels[i]['id'], labels[i]['label_name'], labels[i]['father_id']]); result = inter.join('|'); return (result); } function str_to_labels_tab(str) { /** * Transform string to 2D tab. * Ex : "42, test, 5|56, salut, 3" => * [[42, 'test', 5], [56, 'salut', 3]] */ var result = []; var split = str.split('|'); for (var i = 0; split[i]; i++) { var inter = split[i].split(',') if (inter.length == 2) result.push({'id': inter[0], 'label_name': inter[1]}); else result.push({'id': inter[0], 'label_name': inter[1], 'father_id': inter[2]}); } return (result); } function save_session() { /** * Save mandatory_labels and unselect_tab in Cookies. */ setCookie("cookies_mandatory_labels", labels_tab_to_str(hierarchy.mandatory_labels)); setCookie("cookies_forbiden_labels", labels_tab_to_str(hierarchy.forbiden_labels)); } function setup_session() { /** * Return 0 no cookies are found return 0. * Else return 1 if old_tab cookie are found => the initialization of tutos * and labels is done from the labels contained in old_tab. * need to be initialise (return 0). */ var success = 0; if (getCookie('cookies_mandatory_labels') != null && getCookie('cookies_mandatory_labels').length != 0) hierarchy.mandatory_labels = str_to_labels_tab(getCookie('cookies_mandatory_labels')); if (getCookie('cookies_forbiden_labels') != null && getCookie('cookies_forbiden_labels').length != 0) hierarchy.forbiden_labels = str_to_labels_tab(getCookie('cookies_forbiden_labels')); if (getCookie('cookies_mandatory_labels') != null && getCookie('cookies_mandatory_labels').length != 0) { update_elements_and_labels(); success = 1; } else if (getCookie('cookies_forbiden_labels') != null && getCookie('cookies_forbiden_labels').length != 0) { update_elements_and_labels(); success = 1; } return (success); }