1/* 2 * Copyright 2021 Anyware Services 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 AmetysFlashInfo = { 18 19 isAnonymous: true, 20 21 userItems: [], 22 23 _allItems:[], 24 25 _nextItem: 0, 26 27 _currentItemId: null, 28 29 contextPath: null, 30 31 /** 32 * @property {String} __FLASH_INFO_ITEMS The id of flash info items for local storage 33 */ 34 __FLASH_INFO_ITEMS: "flashInfoItems", 35 36 localFlashInfos: { 37 38 /** 39 * Mark a flash info item as read (local storage only) 40 * @param {String} id the item's id 41 */ 42 markAsRead: function(id) 43 { 44 var items = AmetysFlashInfo.localFlashInfos.getReadItems(); 45 46 id = AmetysFlashInfo._toStorageFormat(id); 47 if ($j.inArray(id, items) == -1) 48 { 49 items.push(id); 50 } 51 52 localStorage.setItem(AmetysFlashInfo.__FLASH_INFO_ITEMS, JSON.stringify(items)); 53 }, 54 55 /** 56 * Get read item stored in local storage 57 * @return {String[]} The read flash infos items 58 */ 59 getReadItems: function() 60 { 61 var items = localStorage.getItem(AmetysFlashInfo.__FLASH_INFO_ITEMS) || '[]'; 62 return items ? JSON.parse(items) : {}; 63 }, 64 65 /** 66 * Empty the read items (local storage only) 67 */ 68 removeAll: function() 69 { 70 localStorage.setItem(AmetysFlashInfo.__FLASH_INFO_ITEMS, '[]'); 71 }, 72 73 /** 74 * Return true if item is in local storage 75 * @param {String} id the item's id 76 * @return {Boolean} true if present in local storage 77 */ 78 hasItem: function(id) 79 { 80 var items = AmetysFlashInfo.localFlashInfos.getReadItems(); 81 return $j.inArray(id, items) != -1; 82 } 83 }, 84 85 /** 86 * Function to call to hide flash info banner or popup 87 */ 88 onHide: function() {}, 89 90 /** 91 * Function to call to show flash info banner or popup 92 */ 93 onShow: function() {}, 94 95 /** 96 * Initialize flash info notifications 97 * @param {Function} onShow the function to call to show popup or banner 98 * @param {Function} onHide the function to call to hide popup or banner 99 * @param {String} ctxPath the context path 100 */ 101 initialize: function(selector, onShow, onHide, ctxPath) 102 { 103 AmetysFlashInfo.onHide = onHide; 104 AmetysFlashInfo.onShow = onShow; 105 AmetysFlashInfo.contextPath = ctxPath; 106 107 var $jbanner = $j(selector); 108 var $contents = $jbanner.find("[data-flashinfo-id]"); 109 $contents.hide(); 110 111 AmetysFlashInfo._allItems = []; 112 AmetysFlashInfo._nextItem = 0; 113 114 $contents.each(function(index, content){ 115 AmetysFlashInfo._allItems.push($j(content).attr('data-flashinfo-id')); 116 }); 117 118 if (AmetysFlashInfo._allItems.length > 0) 119 { 120 AmetysFlashInfo._goToNext(); 121 } 122 else 123 { 124 AmetysFlashInfo.onHide(); 125 } 126 }, 127 128 /**
129 * Mark a item as read then go to next unread flash info 130 */ 131 markAsRead: function() 132 { 133 var me = this; 134 135 me._internalGetItems(function(itemIds){ 136 if (!me.isAnonymous) 137 { 138 me.userItems.push(me._toStorageFormat(me._currentItemId)); 139 me.save(function(data){ 140 me._goToNext(); 141 }); 142 } 143 else 144 { 145 me.localFlashInfos.markAsRead(me._currentItemId); 146 me._goToNext(); 147 } 148 }); 149 }, 150 151 /** 152 * Function to call to go to next unread flash info without marking the current as read 153 */ 154 showLater: function() 155 { 156 this._goToNext(); 157 }, 158 159 /** 160 * Show the next unread flash info 161 */ 162 _goToNext: function() 163 { 164 var me = this; 165 166 if (me._currentItemId) 167 { 168 $j('[data-flashinfo-id="' + me._currentItemId + '"]').hide(); 169 me._currentItemId = null; 170 } 171 172 if (me._nextItem < me._allItems.length) 173 { 174 me.getReadItems(function(readItems){ 175 var nextId = AmetysFlashInfo._allItems[me._nextItem]; 176 me._nextItem++; 177 178 if ($j.inArray(me._toStorageFormat(nextId), readItems) != -1) 179 { 180 me._goToNext(); // already read, go to next flash 181 } 182 else 183 { 184 // Show flash 185 me._currentItemId = nextId; 186 $j('[data-flashinfo-id="' + nextId + '"]').show(); 187 me.onShow(); 188 } 189 }); 190 } 191 else 192 { 193 // No more items 194 me.onHide(); 195 } 196 }, 197 198 _toStorageFormat: function(id) 199 { 200 return id.replace(new RegExp(':', 'g'), ''); 201 }, 202 203 204 /** 205 * Save the read items if a user is currently connected 206 * @param {Function} [callback] the callback function 207 */ 208 save: function(callback) 209 { 210 if (this.isAnonymous) 211 { 212 // No user connected 213 return; 214 } 215 216 var itemIds = this.userItems; 217 218 function success(data) 219 { 220 if (data && data.success) 221 { 222 if (typeof (callback) == 'function') 223 { 224 callback(data); 225 } 226 } 227 } 228 229 function error(jqXHR, textStatus, errorThrown) 230 { 231 console.error("Unable to save read flash infos in user preferences") 232 } 233 234 $j.ajax({ 235 url: AmetysFlashInfo.contextPath + "/_plugins/front-notification/read-items/save", 236 data: { 237 "itemIds": itemIds.join(",") 238 }, 239 dataType: "json", 240 type: 'post', 241 success: success, 242 error: error 243 }); 244 }, 245 246 /** 247 * Get the cart's items 248 * @param {String} lang The current language 249 * @param {String} [view] The view to use for cart's elements. Defaults to 'cart'. 250 * @param {Function} [callback] the callback function to call after getting items. 251 */ 252 getReadItems: function(callback) 253 { 254 var me = this; 255 256 me._internalGetItems(function(itemIds){ 257 258 if (typeof(callback) == "function") 259 { 260 callback(itemIds); 261 } 262 }); 263 }, 264 265 _initialized: false, 266 _isRunning: false, 267 _callbacks: [], 268 269 _internalGetItems: function(callback) 270 { 271 if (this._initialized) 272 { 273 var itemIds = []; 274 if (this.isAnonymous) 275 { 276 // Get items from local storage 277 itemIds = this.localFlashInfos.getReadItems(); 278 } 279 else 280 { 281 itemIds = this.userItems; 282 } 283 284 callback(itemIds); 285 return; 286 } 287 288 this._callbacks.push(callback); 289 290 var me = this; 291 292 function success(data) 293 { 294 me._isRunning = false;
295 296 if (data) 297 { 298 me.isAnonymous = data.isAnonymous; 299 300 var itemIds = []; 301 if (me.isAnonymous) 302 { 303 // Get items from local storage 304 me.userItems = []; 305 itemIds = me.localFlashInfos.getReadItems(); 306 } 307 else 308 { 309 // Merge saved items with localStorage 310 me._mergeUserPrefs(data.items); 311 itemIds = me.userItems; 312 } 313 314 me._initialized = true; 315 316 $j.each(me._callbacks, function(index, cb) { 317 cb(itemIds); 318 }); 319 320 me._callbacks = []; 321 } 322 } 323 324 function handleError(jqXHR, textStatus, errorThrown) 325 { 326 console.error("Unable to get read flash infos in user preferences") 327 me._isRunning = false; 328 } 329 330 if (!this._isRunning) 331 { 332 this._isRunning = true; 333 334 $j.ajax({ 335 url: AmetysFlashInfo.contextPath + "/_plugins/front-notification/read-items/get", 336 dataType: "json", 337 type: 'post', 338 success: success, 339 error: handleError 340 }); 341 } 342 }, 343 344 /** 345 * @private 346 * Merge the user preferences with the local storage 347 * @param {Object[]} readItems The read items stored in user prefs 348 */ 349 _mergeUserPrefs: function(readItems) 350 { 351 var localItems = this.localFlashInfos.getReadItems(); 352 var localSize = localItems.length; 353 354 var me = this; 355 $j.each(readItems, function(index, readItemId) { 356 357 if (!me.localFlashInfos.hasItem(readItemId)) 358 { 359 // Update local storage 360 me.localFlashInfos.markAsRead(readItemId); 361 } 362 else 363 { 364 // The item is already in current cart 365 localSize--; 366 } 367 }); 368 369 this.userItems = this.localFlashInfos.getReadItems(); 370 371 if (localSize > 0) 372 { 373 // at least there is a local element not saved on server side 374 this.save(); 375 } 376 377 // Empty local storage 378 this.localFlashInfos.removeAll(); 379 } 380 }
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.