1/** 2 * RCRiotAvailPricingGrouper RiotJS Mixin 3 */ 4(function($) { 5 var opts = {}; 6 var tag = {}; 7 var RCRiotAvailPricingGrouper = { 8 /** 9 * Process newly attached pricing into offer groups. 10 */ 11 processPricing: function(pricing, tagContext) { 12 if (!pricing || !pricing.prices) { 13 return null; 14 } 15 16 tag = tagContext; 17 opts = tag.opts; 18 var self = this; 19 var decoratedPrices = pricing.prices.map(function decoratePrices(price) { 20 price.has_package = self.getPriceHasPackage(price); 21 price.has_coupon = self.getPriceHasCoupon(price); 22 price.has_savings = self.getPriceHasSavings(price); 23 price.is_alternate = self.getPriceIsAlternate(price); 24 25 if (price.has_savings) { 26 self.has_savings = true; 27 } 28 29 if (price.is_alternate) { 30 self.has_alternates = true; 31 } 32 33 return price; 34 }); 35 36 self.has_package = self.getItemHasPackage(self.pricing.eid); 37 self.has_pricing = true; 38 39 switch (opts.groupby) { 40 case "name": 41 return decoratedPrices.reduce( 42 propGrouper("n", propNamer("n"), nullFeaturer), 43 {} 44 ); 45 case "begin": 46 return decoratedPrices.reduce( 47 propGrouper("b", beginNamer, flexFeaturer), 48 {} 49 ); 50 default: 51 return decoratedPrices.reduce( 52 noGrouper("b", noNamer, nullFeaturer), 53 {} 54 ); 55 } 56 }, 57 58 /** 59 * Get only featured offer groups. 60 */ 61 featuredOfferGroups: function() { 62 var groups = []; 63 for (key in this.offer_groups) { 64 if ( 65 this.offer_groups[key] instanceof OfferGroup && 66 this.offer_groups[key].featured 67 ) { 68 groups.push(this.offer_groups[key]); 69 } 70 } 71 return groups; 72 }, 73 74 /** 75 * Get only non-featured offer groups. 76 */ 77 nonFeaturedOfferGroups: function() { 78 var groups = []; 79 for (key in this.offer_groups) { 80 if ( 81 this.offer_groups[key] instanceof OfferGroup && 82 !this.offer_groups[key].featured 83 ) { 84 groups.push(this.offer_groups[key]); 85 } 86 } 87 return groups; 88 } 89 }; 90 riot.mixin("RCRiotAvailPricingGrouper", RCRiotAvailPricingGrouper); 91 92 // 93 // TYPES 94 // 95 96 /** 97 * OfferGroup represents a group of prices 98 * 99 * @param name {string} 100 * @param offers {array} 101 * @param offerby {string} 102 * @param featured {bool} 103 */ 104 function OfferGroup(name, offers, offerby, featured) { 105 this.name = name; 106 this.offers = offers; 107 this.offerby = offerby; 108 this.featured = featured; 109 } 110 111 // 112 // GROUPERS 113 // 114 115 /** 116 * Get a price grouper (reducer) that creates groups based on the given property of the price object. 117 * 118 * @param prop {string} - The property of a price object on which to group. 119 * @param namer {function} - The group namer function. 120 * @param featurer {function} - The group featurer function. 121 * @return function 122 */ 123 function propGrouper(prop, namer, featurer) { 124 return function(groups, price) { 125 var value = price[prop]; 126 127 if (!groups.hasOwnProperty(value)) { 128 groups[value] = new OfferGroup( 129 prefix(namer(price)), 130 [], 131 tag.offerby, 132 featurer(price) 133 ); 134 } 135 136 groups[value].offers.push(price); 137 return groups; 138 }; 139 } 140 141 /** 142 * Get a price grouper (reducer) that creates only a single group. 143 * 144 * @param prop {string} - The property of a price object on which to group. 145 * @param namer {function} - The group namer method. 146 * @param featurer {function} - The group featurer function. 147 * @return function 148 */ 149 function noGrouper(prop, namer, featurer) { 150 return function(groups, price) { 151 var value = "nogroup"; 152 153 if (!groups.hasOwnProperty(value)) { 154 groups[value] = new OfferGroup( 155 prefix(namer(value)), 156 [], 157 tag.offerby, 158 featurer(price) 159 ); 160 } 161 162 groups[value].offers.push(price); 163 return groups; 164 }; 165 } 166 167 // 168 // NAMERS 169 // 170 171 /** 172 * Name an offer group based on a property of the price object. 173 * 174 * @param prop {object} - The propery of the price object with which to name the group. 175 * @return string 176 */ 177 function propNamer(prop) { 178 return function(price) { 179 return price[prop]; 180 }; 181 } 182 183 /** 184 * Name an offer group based on the begin date. 185 * 186 * @param price {object} - A price object 187 * @return string 188 */ 189 function beginNamer(price) { 190 var groupDate = new Date(price.bt); 191 if (opts.groupby_date_dayname) { 192 return (
193 groupDate.toLocaleString("en-us", { weekday: "long" }) + 194 " " + 195 price.qp.rcav.begin 196 ); 197 } 198 199 return price.qp.rcav.begin; 200 } 201 202 /** 203 * Name an offer group with an empty string. 204 * 205 * @param price {object} - A price object 206 * @return string 207 */ 208 function noNamer(price) { 209 return ""; 210 } 211 212 /** 213 * Optionally prefix a group name based on the text setting. 214 * 215 * @param name 216 */ 217 function prefix(name) { 218 return opts.txt.pricing_group_prefix 219 ? opts.txt.pricing_group_prefix + " " + name 220 : name; 221 } 222 223 // 224 // FEATURERS 225 // 226 227 /** 228 * Feature all groups. 229 */ 230 function nullFeaturer(price) { 231 return true; 232 } 233 234 /** 235 * Feature non-alternate groups. 236 */ 237 function flexFeaturer(price) { 238 return !price.is_alternate; 239 } 240})(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.