1/** 2 * 3 * @param {jQuery} $owner 4 * @param {CisPaymentSettings} settings 5 * @constructor 6 */ 7var CisPaymentManager = function ($owner, settings) { 8 var namespace = "cisPaymentManager"; 9 10 this._settings = settings; 11 this.$owner = $owner; 12 this.$merchant = $(settings.getElement("merchant"), $owner); 13 this.$currency = $(settings.getElement("currency"), $owner); 14 this.$recipient = $(settings.getElement("recipient"), $owner); 15 this.$merchantList = $(settings.getElement("merchantsList"), $owner); 16 this.$recipientList = $(settings.getElement("recipientsList"), $owner); 17 this.$currencyList = $(settings.getElement("currenciesList"), $owner); 18 this.$autoSignCheckbox = $(settings.getElement("autoSignCheckbox"), $owner); 19 this.$paymentTtl = $(settings.getElement("paymentTtl"), $owner); 20 21 if (0 === this.$currencyList.length) { 22 throw "Can't find currency list for widget"; 23 } 24 25 /** @type {MerchantData[]} */ 26 this.merchantsData = []; 27 /** @type {Object.<string, MerchantData>} */ 28 this.merchantsDataByName = {}; 29 /** @type {CurrencyData[]} */ 30 this.servicesData = []; 31 /** @type {Object.<number, CurrencyData>} */ 32 this.servicesDataById = {}; 33 34 _initHandlers(this); 35 36 /** 37 * @this CisPaymentManager 38 * @param {string|boolean} [initValue] 39 */ 40 CisPaymentManager.prototype.updateMerchantsList = function (initValue) { 41 if (0 === this.$merchantList.length) { 42 this.$owner.trigger("merchantChange", [this.merchantsData[0]]); 43 44 return; 45 } 46 initValue = undefined !== initValue ? initValue : true; 47 if (true === initValue) { 48 initValue = this.$merchant.val(); 49 } 50 this.$merchantList 51 .empty() 52 .closest(this._settings.getElement("widgetRow")) 53 .toggle(this.merchantsData.length > 1); 54 this.merchantsData.forEach(function (/** MerchantData */ merchant) { 55 var $option = $("<option>", {text: merchant.title, val: merchant.name}); 56 if (initValue === merchant.name) { 57 $option.prop("selected", true); 58 } 59 $option.appendTo(this.$merchantList); 60 }, this); 61 this.$merchantList 62 .trigger("change") 63 .trigger("chosen:updated"); 64 }; 65 66 /** 67 * @this CisPaymentManager 68 * @param {MerchantData} merchantData 69 * @param {int|string|boolean} [initValue] 70 */ 71 CisPaymentManager.prototype.updateCurrenciesList = function (merchantData, initValue) { 72 initValue = undefined !== initValue ? initValue : true; 73 if (true === initValue) { 74 initValue = this.$currency.val(); 75 } 76 if ("string" === typeof initValue) { 77 initValue = parseInt(initValue, 10); 78 } 79 this.$currencyList 80 .empty() 81 .closest(this._settings.getElement("widgetRow")) 82 .toggle(merchantData.currencies.length > 1); 83 merchantData.currencies.forEach(function (/** MerchantCurrency */ currency) { 84 var $option = $("<option>", {text: currency.iso, val: currency.id}); 85 if (initValue === currency.id) { 86 $option.prop("selected", true); 87 } 88 $option.appendTo(this.$currencyList); 89 }, this); 90 this.$currencyList 91 .trigger("change") 92 .trigger("chosen:updated"); 93 }; 94 95 /** 96 * @this CisPaymentManager 97 * @param {MerchantData|boolean} merchantData 98 */ 99 CisPaymentManager.prototype.updateRecipientsList = function (merchantData) { 100 var recipientId = true === merchantData ? this.$recipient.val() : merchantData.recipientId; 101 if ("" === recipientId) { 102 recipientId = null; 103 } 104 if (this.$recipientList.length > 0) { 105 this.$recipientList 106 .val([recipientId]) 107 .trigger("change") 108 .trigger("chosen:updated") 109 .closest(this._settings.getElement("widgetRow")) 110 .toggle(null !== recipientId); 111 } else if (true !== merchantData) { 112 this.$recipient.val(recipientId); 113 } 114 }; 115 116 /** 117 * @this CisPaymentManager 118 * @param {MerchantData} merchantData 119 */ 120 CisPaymentManager.prototype.updateAutoSignCheckbox = function (merchantData) { 121 this.$autoSignCheckbox 122 .closest(this._settings.getElement("widgetRow")) 123 .toggle(merchantData.canUserChangeAutoSign) 124 // ÐÑбиÑаем вмеÑÑе Ñо ÑкÑÑÑÑм полем 125 .find("input") 126 .prop("disabled", !merchantData.canUserChangeAutoSign); 127 }; 128 129 /** 130 * @this CisPaymentManager 131 * @param {MerchantData} merchantData 132 */ 133 CisPaymentManager.prototype.updatePaymentTtl = function (merchantData) { 134 /** @type {string} */ 135 var template = this._settings.getOption("merchantPaymentTtlTemplate"); 136 if (undefined === template) { 137 return; 138 } 139 140 this.$paymentTtl 141 .html(template.replace("{paymentTtl}", merchantData.paymentTtl || "")) 142 .closest(this._settings.getElement("widgetRow")) 143 .toggle(null !== merchantData.paymentTtl); 144 }; 145 146 /** 147 * @this CisPaymentManager 148 * @param {string} merchantName 149 * @param {?boolean} value 150 */ 151 CisPaymentManager.prototype.toggleEnableMerchantForm = function (merchantName, value) { 152 if (!_isMerchantEnabled(this, merchantName) || null === this.merchantsDataByName[merchantName].$form) { 153 return; 154 } 155 this.merchantsDataByName[merchantName].$form 156 .find(":input") 157 .prop("disabled", !(undefined !== value ? value : true)); 158 }; 159 160 /** 161 * @this CisPaymentManager 162 * @param {string} merchantName 163 * @param {?boolean} value 164 */ 165 CisPaymentManager.prototype.toggleMerchantForm = function (merchantName, value) { 166 if (!_isMerchantEnabled(this, merchantName) || null === this.merchantsDataByName[merchantName].$form) { 167 return; 168 } 169 this.toggleEnableMerchantForm(merchantName, value); 170 this.merchantsDataByName[merchantName].$form.toggle(undefined !== value ? value : true); 171 }; 172 173 /** 174 * @this CisPaymentManager 175 * @param {MerchantData} merchantData 176 */ 177 CisPaymentManager.prototype.updateMerchantForms = function (merchantData) { 178 var self = this; 179 Object.keys(this.merchantsDataByName) 180 .filter(function (name) { 181 return null !== self.merchantsDataByName[name].$form; 182 }) 183 .forEach(function (name) { 184 self.toggleMerchantForm(name, name === merchantData.name); 185 }); 186 }; 187 188 /** 189 * @this CisPaymentManager 190 * @return {MerchantData|null} 191 */ 192 CisPaymentManager.prototype.getActualMerchant = function () { 193 var merchantName = this.$merchant.val(); 194 195 return _isMerchantEnabled(this, merchantName) ? this.merchantsDataByName[merchantName] : null; 196 }; 197 198 /** 199 * @this CisPaymentManager 200 * @return {CurrencyData|null} 201 */ 202 CisPaymentManager.prototype.getActualCurrency = function () { 203 var currencyId = parseInt(this.$currency.val(), 10); 204 205 return _isCurrencyEnabled(this, currencyId) ? this.servicesDataById[currencyId] : null; 206 }; 207 208 /** 209 * @this CisPaymentManager 210 */ 211 CisPaymentManager.prototype.updateMerchantsAndServices = function () { 212 var self = this, 213 s = this._settings, 214 excludedServices = s.getOption("excludedServices", []), 215 allowServicesWithoutTariffs = s.getOption("allowServicesWithoutTariffs", false); 216 this.merchantsData = []; 217 this.merchantsDataByName = {}; 218 this.servicesData = []; 219 this.servicesDataById = {}; 220 $.each(s.getOption("servicesData", []), function (/** number */ index, /** CurrencyData */ value) { 221 /** @type {CurrencyData}
221 */ 222 var data = $.extend(true, {}, value); 223 var isAllExcluded = true; 224 for (var serviceName in data.services) { 225 /** @type {boolean|ServiceData} */ 226 var service = data.services[serviceName]; 227 if (false === service) { 228 continue; 229 } 230 if (-1 !== excludedServices.indexOf(service.id)) { 231 data.services[serviceName] = false; 232 } else { 233 isAllExcluded = false; 234 } 235 } 236 if (allowServicesWithoutTariffs || !isAllExcluded) { 237 self.servicesData.push(data); 238 self.servicesDataById[data.id] = data; 239 } 240 }); 241 /** @type {string} */ 242 var merchantFormSelector = s.getOption("el.merchantForm"); 243 $.each(s.getOption("merchantsData", []), function (/** number */ index, /** MerchantData */ value) { 244 /** @type {MerchantData} */ 245 var data = $.extend(true, {}, value); 246 if (!allowServicesWithoutTariffs) { 247 data.currencies = data.currencies.filter(function (/** MerchantCurrency */ item) { 248 return _isCurrencyEnabled(self, item.id); 249 }); 250 } 251 if (0 !== data.currencies.length) { 252 /** @type {jQuery} */ 253 var $merchantForm = self.$owner.find("[" + merchantFormSelector + "='" + data.name + "']"); 254 data.$form = 1 === $merchantForm.length ? $merchantForm : null; 255 self.merchantsData.push(data); 256 self.merchantsDataByName[data.name] = data; 257 } 258 }); 259 if (0 === this.merchantsData.length) { 260 throw "Empty merchants list"; 261 } 262 }; 263 264 /** 265 * @param {CisPaymentManager} manager 266 * @param {string} name 267 * @return {boolean} 268 */ 269 function _isMerchantEnabled(manager, name) { 270 return manager.merchantsDataByName.hasOwnProperty(name); 271 } 272 273 /** 274 * @param {CisPaymentManager} manager 275 * @param {number} id 276 * @return {boolean} 277 */ 278 function _isCurrencyEnabled(manager, id) { 279 return !Number.isNaN(id) && manager.servicesDataById.hasOwnProperty(id.toString()); 280 } 281 282 /** 283 * @param {CisPaymentManager} manager 284 */ 285 function _initHandlers(manager) { 286 manager.$owner 287 .on("change." + namespace, manager._settings.getElement("merchantsList"), function () { 288 var $this = $(this), 289 $option = $this.find("option:selected"); 290 if (0 === $option.length) { 291 var $options = $this.find("option"); 292 if (0 === $options.length) { 293 throw "Empty merchants list"; 294 } 295 $($options[0]).prop("selected", true); 296 $this 297 .trigger("change") 298 .trigger("chosen:updated"); 299 300 return false; 301 } 302 var merchantName = $option.val(), 303 merchantData = manager.merchantsDataByName[merchantName]; 304 manager.$owner.trigger("merchantChange", [merchantData]); 305 }) 306 .on("change." + namespace, manager._settings.getElement("currenciesList"), function () { 307 var $this = $(this), 308 $option = $this.find("option:selected"), 309 currencyId = parseInt($option.val(), 10); 310 if ($option.length > 0 && _isCurrencyEnabled(manager, currencyId)) { 311 manager.$currency.val(currencyId); 312 manager.$owner.trigger("currencyChange", [manager.servicesDataById[currencyId]]); 313 } 314 }) 315 .on("change." + namespace, manager._settings.getElement("recipientsList"), function () { 316 var $this = $(this), 317 $option = $this.find("option:selected"); 318 manager.$recipient.val($option.length > 0 ? parseInt($option.val(), 10) : null); 319 }) 320 .on("merchantChange." + namespace, function (ev, /** MerchantData */ merchantData) { 321 manager.$merchant.val(merchantData.name); 322 manager.updateCurrenciesList(merchantData); 323 manager.updateMerchantForms(merchantData); 324 manager.updateRecipientsList(merchantData); 325 manager.updateAutoSignCheckbox(merchantData); 326 manager.updatePaymentTtl(merchantData); 327 }); 328 } 329};
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.