1/*'use strict';*/ 2/* 3 dependencies : jquery, jquery.cookie 4 */ 5(function($){ 6 /*@todo: BO to set id/class*/ 7 var bookmarkElementId = 'browserBookmark'; 8 var bookmarkElementClass = '0'; 9 10 var cookieName = 'cg974'; 11 var cookiePath = '/'; 12 var expireInDays = '365'; 13 /*store cookie as json? has to be boolean*/ 14 $.cookie.json = true; 15 16 /*on page load, retrieve favorites/bookmarks. false if cookie does not exist*/ 17 var favorites = $.cookie(cookieName); 18 //configure new favorite 19 var url = window.location.href; 20 var title = $('title').text(); 21 22 /** 23 * get today's date. 24 * ex: 31st of January 2020 will give 31-1-2020 25 * */ 26 function getDateToday(){ 27 var date = new Date(); 28 var m = date.getMonth()+1; 29 var d = date.getDate(); 30 var y = date.getFullYear(); 31 var dateSeparator = '-'; 32 return /*(($(d).length)<2 ? '0':'') +*/ d + dateSeparator 33 /*+ (($(m).length)<2 ? '0':'')*/ + m + dateSeparator 34 + y; 35 } 36 37 var category = categoryIndex = ''; 38 /** 39 * retrieve category of content by using breadcrumb. 40 * @see CDR-372 41 * */ 42 function getCategory(){ 43 var category = ''; 44 if($('.breadcrumb li').length) { 45 var categoryIndex = $('.breadcrumb li').last().index(); 46 switch(categoryIndex){ 47 case -1: 48 case 0: 49 category = 'Autres'; 50 break; 51 case 1: 52 category = 'Listes'; 53 break; 54 default: 55 categoryIndex--; 56 category = $('.breadcrumb li')[categoryIndex].children[0]; 57 category = $(category).text(); 58 break; 59 } 60 } 61 return category; 62 } 63 64 /** 65 * if content has 'mots clefs', 66 * retrieve them 67 * */ 68 function getKeywords(){ 69 var keywords = new Array(); 70 if($('ul.tags>li a').length){ 71 $('ul.tags>li a').each(function(){ 72 keywords.push($(this)[0]); 73 }); 74 } 75 return keywords; 76 } 77 78 /** 79 * define the properties of the new favorite/bookmark 80 * based on content's values 81 * */ 82 var favorite = { 83 'url': url, 84 'title': title, 85 'category': getCategory(), 86 'keywords': getKeywords(), 87 'date': getDateToday() 88 }; 89 90 /** 91 * check if the current/new favorite/bookmark has already been stored. 92 * returns true if already stored, false otherwise 93 * */ 94 function isBookmarked(bookmarks, bookmark, bookmarked){ 95 if(bookmarked === undefined) { 96 bookmarked = false; 97 } 98 var maxIndex = $(bookmarks).length; 99 /*content's title, url and category should be enough for 'uniqueness' comparison*/ 100 for(var i=0; i<maxIndex; i++){ 101 if(!bookmarked 102 && bookmarks[i].title == bookmark.title 103 && bookmarks[i].url == bookmark.url 104 && bookmarks[i].category == bookmark.category 105 ) { 106 bookmarked = true; 107 break; 108 } 109 } 110 return bookmarked; 111 } 112 113 /** 114 * Get the array index at which the current favorite/bookmark is. 115 * Useful for operations such as delete current favorite from all favorites 116 * */ 117 function getBookmarkedIndex(bookmarks, bookmark){ 118 var maxIndex = $(bookmarks).length; 119 for(var i=0; i<maxIndex; i++){ 120 if(!bookmarked 121 && bookmarks[i].title == bookmark.title 122 && bookmarks[i].url == bookmark.url 123 && bookmarks[i].category == bookmark.category 124 ) { 125 return i; 126 } 127 } 128 } 129 130 /** 131 * Update the button/link for 'add to favorites' 132 * Changes text to 'added to favorites' 133 * */ 134 function toggleBookmarkElement(bookmarked, selector) { 135 if(bookmarked === undefined) { 136 return; 137 } 138 if (selector === undefined || selector == false) { 139 selector = '#'+bookmarkElementId; 140 } 141 if (bookmarked) { 142 $(selector).text('Page ajoutée à mes favoris').addClass('added'); 143 /*setTimeout(function(){ 144 $(selector).parents('.js-add-bookmark').removeClass('active'); 145 }, 1000);*/ 146 } 147 } 148 149 /** 150 * Only show link to 'all favorites/bookmarks' page when 151 * there is a favorite/bookmark stored 152 * */ 153 function toggleSeeAllLink(){ 154 var flag = $(favorites).length /*? isBookmarked(favorites, favorite, false) : false*/; 155 if(flag){ 156 $('.blkFav li').last().attr('style', ''); 157 }else{ 158 $('.blkFav li').last().attr('style', 'display:none'); 159 } 160 } 161 162 /*(on document ready)check if the current content has already been bookmarked/favorited and update UI*/ 163 var bookmarked = isBookmarked(favorites, favorite, false) || $('#'+bookmarkElementId).hasClass('bookmarked'); 164 toggleBookmarkElement(bookmarked, '#'+bookmarkElementId); 165 toggleSeeAllLink(); 166 167 /** 168 * Add this content to favorites/bookmark if it is not already bookmarked/favorited 169 * */ 170 function createBookmark(){ 171 favorites = $.cookie(cookieName); 172 if(favorites !== undefined) { 173 favorites.push(favorite); 174 } else{ 175 favorites = new Array(favorite); 176 } 177 if(!bookmarked){ 178 $.cookie(cookieName, favorites, {expire: expireInDays, path:cookiePath}); 179 bookmarked = true; 180 } 181 toggleBookmarkElement(bookmarked, '#'+bookmarkElementId); 182 } 183 184 /** 185 * Remove this content from favorites/bookmarks (if it exists in favorites/bookmarks) 186 * */ 187 function removeBookmark(){ 188 favorites = $.cookie(cookieName); 189 if(favorites !== undefined){ 190 switch($(favorites).length){ 191 case 0: 192 /*will never happen*/ 193 break; 194 case 1: 195 /*if only one entry, remove whole cookie*/ 196 var bookmarkRemoved = $.removeCookie(cookieName, {path: cookiePath}); 197 bookmarked = !bookmarkRemoved; 198 break; 199 default: 200 /*if many entries, remove only this entry*/ 201 var test = favorites.splice(getBookmarkedIndex(favorites, favorite)); 202 $.cookie(cookieName, favorites, {expire: expireInDays, path:cookiePath}); 203 bookmarked = false;
204 break; 205 } 206 } 207 /*update UI*/ 208 toggleBookmarkElement(bookmarked, '#'+bookmarkElementId); 209 } 210 211 212 /*check if html element for bookmarking and jquery cookie library available*/ 213 if(($('#'+bookmarkElementId).length || $('.'+bookmarkElementClass).length) && $.cookie() !== undefined){ 214 $('#'+bookmarkElementId).on('click', function(e){ 215 /*just in case element is a link*/ 216 e.preventDefault(); 217 /*get current content's bookmarked status: true if bookmarked, false otherwise*/ 218 bookmarked = isBookmarked(favorites, favorite, false); 219 220 if(bookmarked) { 221 /*my bad! not part of specs!*/ 222 // removeBookmark(); 223 }else{ 224 createBookmark(); 225 } 226 227 /*update app data based on new cookie state*/ 228 favorites = $.cookie(cookieName); 229 230 /** 231 * update ui 232 * Important: 233 * bookmark element itself already updated in create and remove functions 234 * */ 235 toggleSeeAllLink(); 236 }); 237 } 238 /*if html elements exist but cookie library does not exist*/ 239 else if( ($('#'+bookmarkElementId).length || $('.'+bookmarkElementClass).length) && $.cookie() === undefined){ 240 /*log message in drupal : cookie library not found/loaded*/ 241 } 242 /*explicit check: when not properly configured*/ 243 else if(!($('#'+bookmarkElementId).length) || !($('.'+bookmarkElementClass).length) || $.cookie() === undefined ){ 244 /*log message in drupal : script has not been properly configured*/ 245 } 246 247 var pageFavoritesId = 'liste-favoris'; 248 if($('#'+pageFavoritesId).length) { 249 /*@todo : BO*/ 250 var cookieName = 'cg974'; 251 var cookiePath = '/'; 252 253 var cookieFav = $.cookie(cookieName); 254 if($(cookieFav).length){ 255 $('#'+pageFavoritesId).html("<li>Merci de patienter un moment pendant qu'on réactualise votre liste de favoris...</li>"); 256 }else{ 257 $('#'+pageFavoritesId).html("<li>Vous n'avez pas de favoris. <a href='/'>Retour à la page d'accueil</a></li>"); 258 } 259 260 /*Sort order by date: most recent first*/ 261 cookieFav.sort(function(a, b) { 262 aDates = a.date.split('-'); 263 bDates = b.date.split('-'); 264 return new Date( aDates[2], aDates[1], aDates[0] ) < new Date( bDates[2], bDates[1], bDates[0] ); 265 }); 266 267 /*Retrieve content/page categories from cookie*/ 268 function populatePageCategories(){ 269 if($(cookieFav).length){ 270 var cat = new Array(); 271 $(cookieFav).each(function(){ 272 if($.inArray($(this)[0].category, cat) == -1){ 273 cat.push($(this)[0].category); 274 } 275 }); 276 return cat; 277 } 278 } 279 var pageCategories = populatePageCategories(); 280 281 /*will store final list*/ 282 var lists = new Array(); 283 284 /*used to remove site name prefix/suffix*/ 285 var pageTitleSuffix = ' | CDR'; 286 var pageTitleSuffixReplacement = ''; 287 288 /*generate list of contents based on cookie values and groups them by category*/ 289 function generateListItems(pageCategories, lists){ 290 /*When storing data in cookie, all contents have been given a category*/ 291 if(pageCategories === undefined){ 292 lists.push("<li>Vous n'avez pas de favoris. <a href='/'>Retour à la page d'accueil</a></li>"); 293 return; 294 } 295 296 if($(cookieFav).length) { 297 var pageLoopMax = $(pageCategories).length; 298 var cookieLoopMax = $(cookieFav).length; 299 300 /*loop over all categories*/ 301 for(var p=0; p<pageLoopMax; p++){ 302 var pageCategory = pageCategories[p]; 303 /*row stores current list element*/ 304 var row = '<li><h2>' + pageCategory + '</h2><p>'; 305 /*'redundant' variable: used so that the last item in row is not followed by ',' or ';'*/ 306 var links = []; 307 /*loop over all bookmarks. if bookmark's category matches current category, add it to links*/ 308 for(var c=0; c<cookieLoopMax; c++){ 309 if($(cookieFav)[c].category == pageCategory){ 310 var a = '<a href="' 311 + $(cookieFav)[c].url 312 +'" target="_blank">' 313 + $(cookieFav)[c].title.replace(pageTitleSuffix, pageTitleSuffixReplacement) 314 +'</a>'; 315 links.push(a); 316 } 317 } 318 /*join all links: separate each of them by ',' or ';'*/ 319 row += links.join(' ; '); 320 row += '</p></li>'; 321 lists.push(row); 322 } 323 } 324 } 325 generateListItems(pageCategories, lists); 326 /*update UI with generated list items*/ 327 $('#'+pageFavoritesId).html(lists); 328 } 329})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.