1// Improved Options / РаÑÑиÑеннÑе опÑии 2// Support: [email protected] / ÐоддеÑжка: [email protected] 3 4 5function getIOProduct(io_init_params) { 6 7 var io_product = { 8 9 proxied : false, 10 custom_methods : {}, 11 params : { 12 option_prefix : 'option', 13 theme_name : '', 14 options : {}, // io_options 15 product : {}, // product model, sku, upc, reward, product_id 16 search_string : {}, // io_search 17 settings : {}, 18 $container : $('body'), // $('body') 19 infos : { 20 pov_model: 'model', 21 pov_sku: 'sku', 22 pov_upc: 'upc', 23 pov_reward: 'reward', 24 }, 25 codes : { 26 pov_model: 'model', 27 pov_sku: 'sku', 28 pov_upc: 'upc', 29 }, 30 poip_ov : false, // can be set if POIP exists 31 defaults : {}, 32 }, 33 vars : { 34 improvedoption_po_ids : {}, 35 improvedoption_to_select : {}, 36 timer_update_info : 0, 37 }, 38 39 init : function(params) { 40 41 io_product.params = $.extend({}, io_product.params, params); 42 43 if ($('#mijoshop').length && extension.getElement('[name^="option_oc["]').length) { // 44 io_product.params.option_prefix = "option_oc"; 45 } else if ( $('.body-oc').length && $('[name^="option_oc["]').length ) { // jcart 46 io_product.params.option_prefix = "option_oc"; 47 } 48 49 if ( io_product.getSetting('pov_default') ) { 50 51 var improvedoption_default = io_product.params.defaults; 52 var improvedoption_first_values = io_product.getFirstValuesOfOptions(); 53 var improvedoption_to_select = {}; 54 io_product.vars.improvedoption_po_ids = io_product.getProductOptionIds(); 55 56 if ( io_product.getSetting('pov_default') == 1 ) { // autoselect first values 57 improvedoption_to_select = improvedoption_first_values; 58 } else if ( io_product.getSetting('pov_default') == 2) { // autoselect first values if specific default values are not set 59 60 improvedoption_to_select = improvedoption_default; 61 62 // adding first values for the default values is enough 63 io_common.each(improvedoption_first_values, function(product_option_value, product_option_id){ 64 65 if ( !improvedoption_to_select[product_option_id] ) { 66 improvedoption_to_select[product_option_id] = product_option_value; 67 68 } else if (improvedoption_to_select[product_option_id].values && $.inArray(product_option_value.values[0],improvedoption_to_select[product_option_id].values) == -1 ) { 69 improvedoption_to_select[product_option_id].values.push(product_option_value.values[0]); 70 } 71 }); 72 73 // autoselect default values only 74 } else if ( io_product.getSetting('pov_default') == 3 ) { 75 improvedoption_to_select = improvedoption_default; 76 } 77 io_product.vars.improvedoption_to_select = improvedoption_to_select; 78 } 79 80 $(document).on('external_option_change.liveopencart_io', function(){ 81 io_product.externalOptionChange(); 82 }); 83 84 io_product.getContainer().on('change', io_product.getOptionSelector(), function() { 85 io_product.eventOptionChange(this); 86 }); 87 88 // custom-select 89 if ( io_product.getElement('.custom-select') ) { 90 io_product.getElement('.custom-select').on('click', '.select-items div', function(){ 91 setTimeout(function(){ 92 io_product.eventOptionChange(this); 93 }, 1); 94 }); 95 } 96 io_product.updateProductInfo(10); 97 98 $().ready(function(){ 99 //setTimeout( function(){ 100 101 var product_option_value_id_set_by_search = 0; 102 if ( io_product.params.search_string ) {
103 product_option_value_id_set_by_search = io_product.setSelectedPOVBySearch(); 104 } 105 106 io_product.setDefaults( (product_option_value_id_set_by_search ? [product_option_value_id_set_by_search] : [] ) ); 107 108 //}, 100); 109 }); 110 io_product.getContainer().data('io_product', io_product); 111 112 }, 113 114 getOptionSelector : function(){ 115 return 'select[name^="'+io_product.params.option_prefix+'["], input[name^="'+io_product.params.option_prefix+'["]'; 116 }, 117 118 externalOptionChange : function(){ 119 io_product.getElement(io_product.getOptionSelector()).each(function(){ 120 io_product.eventOptionChange(this); 121 }); 122 }, 123 124 eventOptionChange : function(element) { 125 io_product.updateDescription(element); 126 io_product.updateProductInfo(10); 127 }, 128 129 getSetting : function(setting_name){ 130 if ( io_product.params.settings[setting_name] ) { 131 if ( io_product.params.settings[setting_name] == '0' ) { 132 return false; 133 } else { 134 return io_product.params.settings[setting_name]; 135 } 136 } else { 137 return false; 138 } 139 }, 140 141 getElement : function(selector) { 142 return io_product.getContainer().find(selector); 143 }, 144 145 getContainer : function() { 146 return io_product.params.$container; 147 }, 148 149 displayProductInfo : function(info) { // improved_options_update_html 150 151 io_common.each(io_product.params.infos, function(info_key, info_setting_name){ 152 if ( io_product.getSetting(info_setting_name) ) { 153 if ( typeof(info[info_key]) != 'undefined' ) { 154 io_product.getElement('[data-io="'+info_key+'"]').html(info[info_key]); 155 } 156 } 157 }); 158 }, 159 160 getROField : function(field_name) { // improvedoptions_get_ro_field 161 162 // ro get fields ? 163 164 // ?? for oc 3 ?? 165 if ( typeof(liveopencart) != 'undefined' && liveopencart.related_options_instances && liveopencart.related_options_instances.length == 1 && liveopencart.related_options_instances[0].ro_settings ) { 166 167 var ro_extension = liveopencart.related_options_instances[0]; 168 var ro_settings = ro_extension.ro_settings; 169 var options_values = ro_extension.getOptionValues([]); 170 var ro_ids = ro_extension.getCurrentROIds(options_values); 171 var ro_data = ro_extension.ro_data; 172 173 var field_value = ''; 174 if ( typeof(ro_data) != 'undefined' && typeof(ro_settings) != 'undefined' ) { 175 176 var product_value = io_product.params.product[field_name]; 177 178 if (ro_ids.length) { 179 io_common.each(ro_datam, function(ro_dt){ 180 //for (var i in ro_data) { 181 // var ro_dt = ro_data[i]; 182 io_common.each(ro_ids, function(ro_id){ 183 //for (var j in ro_ids) { 184 // var ro_id = ro_ids[j]; 185 if (ro_dt.ro[ro_id] && ro_dt.ro[ro_id][field_name]) { 186 if (ro_settings['spec_'+field_name] == 1) { 187 field_value = ro_dt.ro[ro_id][field_name]; 188 } else if (ro_settings['spec_'+field_name] == 2 || ro_settings['spec_'+field_name] == 3) { 189 if ( field_name == 'model' && ro_settings.spec_model_delimiter_ro && field_value) { 190 field_value+= ro_settings.spec_model_delimiter_ro; 191 } 192 field_value+= ro_dt.ro[ro_id][field_name]; 193 } 194 } 195 }); 196 }); 197 } 198 199 if (field_value) { 200 if (ro_settings['spec_'+field_name] == 3) { 201 if ( field_name == 'model' && ro_settings.spec_model_delimiter_product) { 202 field_value = product_value + ro_settings.spec_model_delimiter_product + field_value; 203 } else { 204 field_value = product_value + field_value; 205 } 206 } 207 } else { 208 field_value = product_value; 209 } 210 } 211 return field_value; 212 } 213 }, 214 215 //var io_show_sku_model_timer = 0; 216 updateProductInfo : function (io_delay) { // improvedoptions_show_sku_model 217 218 clearTimeout(io_product.vars.timer_update_info); 219 if ( io_delay ) { 220 io_product.vars.timer_update_info = setTimeout(function(){ 221 io_product.updateProductInfo(); 222 }, io_delay); 223 return; 224 } 225 226 if ( io_product.params.options ) { 227 /* 228 var ro_model = io_product.getROField('model'); 229 var ro_sku = io_product.getROField('sku'); 230 var ro_upc = io_product.getROField('upc'); 231 */ 232 233 var code_items = {}; 234 io_common.each(io_product.params.codes, function(code_name, setting_name){ 235 if ( io_product.getSetting(setting_name) ) { 236
237 let code_value = io_product.getROField(code_name); 238 if ( !code_value ) { 239 code_value = io_product.params.product[code_name]; 240 } 241 242 code_items[code_name] = { 243 setting_name: setting_name, 244 setting_value: io_product.getSetting(setting_name), 245 value: code_value, 246 is_set: false, 247 last_from_product: true, 248 }; 249 } 250 }); 251 252 if ( !$.isEmptyObject(code_items) ) { 253 254 let io_reward = false; 255 if ( io_product.getSetting('pov_reward') ) { 256 io_reward = parseInt(io_product.params.product.reward) || 0; 257 } 258 259 var io_selected_values = []; 260 io_product.getElement('select[name^="'+io_product.params.option_prefix+'["], input[name^="'+io_product.params.option_prefix+'["]:radio:checked, input[name^="'+io_product.params.option_prefix+'["]:checkbox:checked').each(function(){ 261 if ( $(this).val() ) { 262 io_selected_values.push($(this).val()); 263 } 264 }); 265 266 267 io_common.each(io_product.params.options, function(io_option){ 268 269 if ( $.inArray(io_option.product_option_value_id, io_selected_values) != -1 ) { 270 271 io_common.each(code_items, function(code_item, code_name){ 272 if ( io_option[code_name] ) { 273 if ( code_item.setting_value == 1 ) { 274 if ( !code_item.is_set && io_option[code_name] ) { 275 code_item.value = io_option[code_name]; 276 code_item.is_set = true; 277 } 278 } else if ( code_item.setting_value == 2 ) { 279 var delimiter_product = io_product.getSetting(code_item.setting_name+'_delimiter_product'); 280 var delimiter_option = io_product.getSetting(code_item.setting_name+'_delimiter_option'); 281 if ( code_item.last_from_product && delimiter_product ) { 282 code_item.value+= delimiter_product; 283 } else if ( !code_item.last_from_product && delimiter_option ) { 284 code_item.value+= delimiter_option; 285 } 286 code_item.value+= io_option[code_name]; 287 code_item.last_from_product = false; 288 } 289 } 290 }); 291 292 if ( io_product.getSetting('pov_reward') ) { 293 if ( io_option.reward_prefix == '+' ) { 294 io_reward+= parseInt(io_option.reward) || 0; 295 } else if ( io_option.reward_prefix == '-' ) { 296 io_reward-= parseInt(io_option.reward) || 0; 297 } else if ( io_option.reward_prefix == '=' && ( parseInt(io_option.reward) || 0 ) ) { 298 io_reward= parseInt(io_option.reward) || 0; 299 } 300 } 301 } 302 }); 303 304 var info = {}; 305 io_common.each(code_items, function(code_item, code_name){ 306 info[code_name] = code_item.value; 307 }); 308 309 if ( io_reward !== false ) { 310 info.reward = io_reward; 311 } 312 313 io_product.displayProductInfo(info); 314 } 315 } 316 }, 317 318 getPOVDescription : function(pov_id) { 319 320 var description = ''; 321 322 io_common.each(io_product.params.options, function(option){ 323 if ( option.product_option_value_id == pov_id ) { 324 description = option.description; 325 return false; 326 } 327 }); 328 329 return description; 330 }, 331 332 updateDescription : function(elem) { // improvedoptions_show_description 333 334 if ( io_product.getSetting('pov_description') ) { 335 336 var $element = $(elem); 337 338 var description = ''; 339 if ( $element.val() && ( $element.is('select') || $element.is(':checked') ) ) { 340 description = io_product.getPOVDescription($element.val()); 341 } 342 343 io_product.displayDescription($element, description); 344 } 345 }, 346 347 displayDescription : function($element, description){ 348 349 let po_id = io_product.getPOIdByName($element.attr('name')); 350 351 if ( description && ($element.val() && ( ($element.is('select')) || ($element.is('input') && $element.is(':checked')) )) ) { 352 353 if ( $element.is(':radio') ) { 354 io_product.removeOptionDescriptionContainersByPoId(po_id); 355 } 356 357 // show description 358 io_product.getOptionDescriptionContainer($element).html(description); 359 360 } else { 361 // remove description 362 io_product.getOptionDescriptionContainer($element).remove(); 363 //$element.siblings('[data-io="description"]').remove(); 364 } 365 }, 366 367 getOptionDescriptionContainer : function($element) { 368 369 let po_id = io_product.getPOIdByName($element.attr('name')); 370 371 if ( !$element.siblings('[data-io="description"]').length ) { 372 $element.parent().append('<div data-io="description" data-io-po-id="'+po_id+'"></div>'); 373 } 374 return $element.siblings('[data-io="description"]'); 375 }, 376 377 removeOptionDescriptionContainersByPoId : function(po_id) { 378 io_product.getElement('[data-io="description"][data-io-po-id="'+po_id+'"]').remove(); 379 }, 380 381 getPOIdByName : function(name) { // improvedoptions_get_product_option_id_from_name 382 var option_prefix_length = io_product.params.option_prefix.length; 383 return name.substr(option_prefix_length+1, name.indexOf(']')-(option_prefix_length+1) ); 384 }, 385 386 getPOIdByPOVId : function(product_option_value_id) { // improvedoptions_get_product_option_id_from_value_id 387 388 var name = ''; 389 var $value_element = io_product.getElement('select option, :radio, :checkbox').filter('[value="'+product_option_value_id+'"]'); 390 if ( $value_element.is('option') ) { 391 name = $value_element.parent().attr('name'); 392 } else { 393 name = $value_element.attr('name'); 394 } 395
396 if ( name ) { 397 return io_product.getPOIdByName(name); 398 } else { 399 return 0; 400 } 401 }, 402 403 getFirstValuesOfOptions : function() { // improvedoptions_get_first_values 404 405 var values = []; 406 var product_option_ids = []; 407 io_product.getElement('input[type=radio][name^="'+io_product.params.option_prefix+'["]').each(function () { 408 product_option_id = io_product.getPOIdByName($(this).attr('name')); 409 if ($.inArray(product_option_id,product_option_ids) == -1 && $(this).val()) { 410 values[product_option_id] = {values:[$(this).val()], product_option_id:product_option_id, type:'radio'}; 411 product_option_ids.push(product_option_id); 412 } 413 }); 414 415 io_product.getElement('select[name^="'+io_product.params.option_prefix+'["]').find('option').each(function () { 416 product_option_id = io_product.getPOIdByName($(this).parent().attr('name')); 417 if ($.inArray(product_option_id,product_option_ids) == -1 && $(this).val()) { 418 values[product_option_id] = {values:[$(this).val()], product_option_id:product_option_id, type:'select'}; 419 product_option_ids.push(product_option_id); 420 } 421 }); 422 423 return values; 424 425 }, 426 427 getProductOptionIds : function () { // improvedoptions_get_product_options_ids 428 var po_ids = []; 429 io_product.getElement('input:radio, input:checkbox, select').filter('[name^="'+io_product.params.option_prefix+'"]').each(function(){ 430 var po_id = io_product.getPOIdByName($(this).attr('name')); 431 if ($.inArray(po_id, po_ids)==-1) { 432 po_ids.push(po_id); 433 } 434 }); 435 return po_ids; 436 }, 437 438 setDefaults : function (pov_ids_to_skip, po_id_only) { // improvedoptions_set_defaults 439 440 if ( io_product.getSetting('pov_default') ) { 441 var po_ids_to_skip = []; 442 443 if ( io_product.params.poip_ov ) { 444 po_ids_to_skip.push( io_product.getPOIdByPOVId(io_product.params.poip_ov) ); 445 } 446 447 if ( pov_ids_to_skip ){ 448 io_common.each(pov_ids_to_skip, function(pov_id_to_skip){ 449 if ( pov_id_to_skip ) {
450 po_ids_to_skip.push( io_product.getPOIdByPOVId(pov_id_to_skip) ); 451 } 452 }); 453 } 454 455 456 if ( io_product.vars.improvedoption_to_select ) { 457 var io_to_select = io_product.vars.improvedoption_to_select; 458 459 // to apply in order of html elements 460 io_common.each(io_product.vars.improvedoption_po_ids, function(product_option_id){ 461 462 if ( typeof(po_id_only) != 'undefined' && po_id_only != product_option_id ) { 463 return; 464 } 465 if ( po_ids_to_skip.length && $.inArray(product_option_id, po_ids_to_skip) != -1 ) { 466 return; 467 } 468 if (typeof(io_to_select[product_option_id])=='undefined') { 469 return; 470 } 471 472 var product_option = io_to_select[product_option_id]; 473 var product_option_type = product_option.type; 474 475 if (product_option.values ) { 476 io_common.each(product_option.values, function(product_option_value_id){ 477 478 var pov_set_selected = io_product.setSelectedPOV(product_option_value_id); 479 if (product_option_type != 'checkbox') { 480 if ( pov_set_selected == product_option_value_id ) { 481 return false; 482 } 483 } 484 485 }); 486 } 487 488 }); 489 490 } 491 } 492 }, 493 494 495 setSelectedPOV : function(product_option_value_id) { // improvedoptions_set_selected_value 496 497 var select_option = io_product.getElement('select[name^="'+io_product.params.option_prefix+'"] option[value="'+product_option_value_id+'"]'); 498 499 if ( select_option.length ) { 500 select_option.parent().val(product_option_value_id); 501 select_option.change(); 502 503 return select_option.val(); 504 } else { 505 506 var input_option = io_product.getElement('input[name^="'+io_product.params.option_prefix+'"][value="'+product_option_value_id+'"]').filter(':checkbox, :radio'); 507 if ( input_option.length ) { 508 input_option.prop('checked', true); 509 input_option.change(); 510 511 return input_option.val(); 512 513 } 514 515 } 516 517 }, 518 519 setSelectedPOVBySearch : function() { // improvedoptions_select_option_by_search 520 521 var po_id_to_skip = ( io_product.params.poip_ov ? io_product.getPOIdByPOVId(io_product.params.poip_ov) : 0 );
522 var io_search = io_product.params.search_string; 523 524 var pov_id = io_product.findPOVByEnabledCode('model', io_search, po_id_to_skip, false); 525 if ( !pov_id ) { 526 pov_id = io_product.findPOVByEnabledCode('sku', io_search, po_id_to_skip, false); 527 } 528 if ( !pov_id ) { 529 pov_id = io_product.findPOVByEnabledCode('model', io_search, po_id_to_skip, true); 530 } 531 if ( !pov_id ) { 532 pov_id = io_product.findPOVByEnabledCode('sku', io_search, po_id_to_skip, true); 533 } 534 535 if ( pov_id ) { 536 io_product.setSelectedPOV(pov_id); 537 } 538 539 return pov_id; 540 }, 541 542 findPOVByEnabledCode : function(code_key, code_value, po_id_to_skip, to_lower_case) { 543 544 var pov_id = 0; 545 546 if ( io_product.getSetting('pov_'+code_key) ) { 547 548 io_common.each(io_product.params.options, function(io_option){ 549 550 if ( (!to_lower_case && io_option[code_key] == code_value) || (to_lower_case && io_option[code_key].toLowerCase() == code_value.toLowerCase()) ) { 551 if ( !po_id_to_skip || io_product.getPOIdByPOVId(io_option.product_option_value_id) != po_id_to_skip ) { 552 pov_id = io_option.product_option_value_id; 553 return false; 554 } 555 } 556 }); 557 } 558 return pov_id; 559 }, 560 561 }; 562 563 if ( typeof(setIOProductCustomMethods) == 'function' ) { 564 setIOProductCustomMethods(io_product); 565 } 566 567 io_common.initObject(io_product, io_init_params); 568 569 return io_product; 570}
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.