1/** 2 * Copyright © Magento, Inc. All rights reserved. 3 * See COPYING.txt for license details. 4 */ 5 6/** 7 * @api 8 */ 9define([ 10 'jquery', 11 'underscore', 12 'ko', 13 'jquery/jquery-storageapi' 14], function ($, _, ko) { 15 'use strict'; 16 17 var options = { 18 cacheTtl: 0, 19 sectionLoadUrl: '' 20 }, 21 selectors = { 22 productIdSelector: '#product_addtocart_form [name="product"]' 23 }, 24 storage = $.initNamespaceStorage('mage-banners-cache-storage').localStorage, 25 productId = $(selectors.productIdSelector).val(), 26 27 /** 28 * Cache invalidation. Banner cache ttl is 30 sec by default. 29 */ 30 invalidateCacheBySessionTimeOut = function () { 31 var cacheEol = new Date($.localStorage.get('mage-banners-cache-timeout')), 32 dateTo = new Date(Date.now() + options.cacheTtl), 33 cartDataId = null, 34 globalStoreId = $.cookieStorage.get('store') || 'default'; 35 36 if ($.localStorage.get('mage-banners-storeId') === null) { 37 $.localStorage.set('mage-banners-storeId', globalStoreId); 38 } 39 40 if ($.localStorage.get('mage-cache-storage') !== null && 41 $.localStorage.get('mage-cache-storage').hasOwnProperty('cart')) { 42 cartDataId = $.localStorage.get('mage-cache-storage').cart['data_id']; 43 } 44 45 if (cacheEol < new Date() || 46 $.localStorage.get('mage-banners-storeId') !== globalStoreId || 47 $.localStorage.get('mage-banners-cartDataId') !== cartDataId) { 48 storage.removeAll(); 49 $.localStorage.set('mage-banners-cache-timeout', dateTo); 50 $.localStorage.set('mage-banners-storeId', globalStoreId); 51 52 if (cartDataId) { 53 $.localStorage.set('mage-banners-cartDataId', cartDataId); 54 } 55 } 56 }, 57 dataProvider = { 58 59 /** 60 * Request data from storage 61 * 62 * @param {Array} sectionNames 63 * @returns {Object} 64 */ 65 getFromStorage: function (sectionNames) { 66 var result = {}; 67 68 _.each(sectionNames, function (sectionName) { 69 result[sectionName] = storage.get(sectionName); 70 }); 71 72 return result; 73 }, 74 75 /** 76 * Request data from server 77 * 78 * @param {Array} sectionNames 79 * @returns {Object} 80 */ 81 getFromServer: function (sectionNames) { 82 var parameters = { 83 'requesting_page_url': window.location.href 84 }; 85 86 if (productId) { 87 parameters.data = { 88 'product_id': productId 89 }; 90 } 91 92 if (_.isArray(sectionNames)) { 93 parameters.sections = sectionNames.join(','); 94 } 95 96 return $.getJSON(options.sectionLoadUrl, parameters).fail(function (jqXHR) { 97 throw new Error(jqXHR); 98 }); 99 } 100 }, 101 buffer = { 102 data: {}, 103 104 /** 105 * Binding parameter 106 * 107 * @param {String} sectionName 108 */ 109 bind: function (sectionName) { 110 this.data[sectionName] = ko.observable({}); 111 }, 112 113 /** 114 * 115 * @param {String} sectionName 116 * @returns {Object} 117 */ 118 get: function (sectionName) { 119 if (!this.data[sectionName]) { 120 this.bind(sectionName); 121 } 122 123 return this.data[sectionName]; 124 }, 125 126 /** 127 * Get keys 128 * 129 * @returns {Array} 130 */ 131 keys: function () { 132 return _.keys(this.data); 133 }, 134 135 /** 136 * Notify storage 137 * 138 * @param {String} sectionName 139 * @param {Object} sectionData 140 */ 141 notify: function (sectionName, sectionData) { 142 if (!this.data[sectionName]) { 143 this.bind(sectionName); 144 } 145 this.data[sectionName](sectionData); 146 }, 147 148 /** 149 * Update sections 150 * 151 * @param {Array} sections 152 */ 153 update: function (sections) { 154 _.each(sections, function (sectionData, sectionName) { 155 storage.set(sectionName, sectionData); 156 buffer.notify(sectionName, sectionData); 157 }); 158 } 159 }, 160 banner = { 161 162 /** 163 * Initialization 164 */ 165 init: function () { 166 var dataSectionName; 167
168 dataSectionName = this.getSectionName('data'); 169 170 if (_.isEmpty(storage.keys()) || !storage.get(dataSectionName)) { 171 this.reload([]); 172 } else { 173 _.each(dataProvider.getFromStorage(storage.keys()), function (sectionData, sectionName) { 174 buffer.notify(sectionName, sectionData); 175 }); 176 } 177 }, 178 179 /** 180 * Get data 181 * 182 * @param {String} sectionName 183 * @returns {*|Object} 184 */ 185 get: function (sectionName) { 186 sectionName = this.getSectionName(sectionName); 187 188 return buffer.get(sectionName); 189 }, 190 191 /** 192 * Set data 193 * 194 * @param {String} sectionName 195 * @param {Object} sectionData 196 */ 197 set: function (sectionName, sectionData) { 198 var data = {}; 199 200 sectionName = this.getSectionName(sectionName); 201 data[sectionName] = sectionData; 202 buffer.update(data); 203 }, 204 205 /** 206 * Reloading from storage or server 207 * 208 * @param {Array} sectionNames 209 * @returns {Object} 210 */ 211 reload: function (sectionNames) { 212 return dataProvider.getFromServer(sectionNames).done(function (sections) { 213 _.each(sections, function (sectionData, sectionName) { 214 this.set(sectionName, sectionData); 215 }.bind(this)); 216 }.bind(this)); 217 }, 218 219 /** 220 * Get section name 221 * 222 * @returns {String} 223 */ 224 getSectionName: function (sectionName) { 225 var result; 226 227 result = productId && sectionName === 'data' ? 'data_product_' + productId : sectionName; 228 229 return result; 230 }, 231 232 /** 233 * Init helper 234 * 235 * @param {Array} settings 236 */ 237 'Magento_Banner/js/model/banner': function (settings) { 238 options = _.extend(options, settings); 239 invalidateCacheBySessionTimeOut(settings); 240 banner.init(); 241 } 242 }; 243 244 //TODO: remove global change, in this case made for initNamespaceStorage 245 $.cookieStorage.setConf({ 246 path: '/' 247 }); 248 249 return banner; 250});
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.