PC.fe.views=PC.fe.views||{};
!(function($, Views, _){
'use strict';
const announce_message=function(message){
if(PC.fe&&PC.fe.announce){
PC.fe.announce(message);
}};
const format_layer_name=function(layer){
return layer&&layer.get ?(layer.get('name')||''):'';
};
wp.hooks.addAction('PC.fe.layer.beforeRenderChoices', 'mkl/product_configurator/multiple_choice', function(layer_view){
if('multiple'===layer_view.layer_type){
layer_view.choices=new Views.multiple_choices({ content: PC.fe.getLayerContent(layer_view.model.id), model: layer_view.model })
}});
const count_items=function(selected_choices, choice){
let count_selected_choices=0;
_.each(selected_choices, function(item){
count_selected_choices +=wp.hooks.applyFilters('PC.fe.multiple_choice.item_quantity',
1,
item,
choice
);
});
return wp.hooks.applyFilters('PC.fe.multiple_choice.items_selected_count', count_selected_choices, selected_choices, false);
};
wp.hooks.addAction('PC.fe.save_data.parse_choices.after', 'mkl/product_configurator/multiple_choice', function(layer, s){
if('multiple'!==layer.get('type') ) return;
var min_selection=layer.get('min_selection') ? parseInt(layer.get('min_selection') ):0;
var choices=PC.fe.getLayerContent(layer.id);
if(! choices) return;
var model_data=wp.hooks.applyFilters('PC.fe.configurator.layer_data', layer.attributes);
var selected_choices=choices.filter(function(item){
return item.get('active')&&false!==item.get('cshow')&&! item.get('is_group');
});
const count_selected_choices=count_items(selected_choices, false);
if(min_selection > count_selected_choices){
var min_layer_message=PC_Multiple_Choice_Config.min_items_required.replace('%s', model_data.name).replace('%i', min_selection);
announce_message(min_layer_message + '. Selected ' + count_selected_choices + '.');
PC.fe.errors.push({
choice: false,
layer: layer,
message: min_layer_message
});
}
var groups_with_min=choices.filter(function(item){
var min=item.get('min_selection');
return !! min;
});
if(groups_with_min.length){
_.each(groups_with_min, function(group){
var min_selection_in_group=group.get('min_selection');
var selected_choices_for_group=choices.filter(function(item){
return item.get('active')&&false!==item.get('cshow')&&! item.get('is_group')&&group.id===item.get('parent');
});
var count_selected_choices_in_group=count_items(selected_choices_for_group, false);
if(PC.conditionalLogic&&PC.conditionalLogic.item_is_hidden(group) ) return;
if(min_selection_in_group > count_selected_choices_in_group){
var separator_in=(PC_config.lang&&PC_config.lang.validation_separator_in) ? PC_config.lang.validation_separator_in:', ';
var min_group_message=PC_Multiple_Choice_Config.min_items_required.replace('%s', model_data.name + separator_in + group.get_name()).replace('%i', min_selection_in_group);
announce_message(min_group_message + '. Selected ' + count_selected_choices_in_group + '.');
PC.fe.errors.push({
choice: false,
layer: layer,
message: min_group_message
});
}});
}});
wp.hooks.addFilter('PC.choices.canSelectChoice', 'mkl/product_configurator/multiple_choice', function(canSelect, choice, choices){
if(! canSelect) return canSelect;
if(choices.layer.get('include_in_max_calculation')&&! check_max_items()){
var total_layer_name=format_layer_name(choices.layer);
var total_max_message=total_layer_name ?(total_layer_name + ': ' + PC_Multiple_Choice_Config.total_max_items_reached):PC_Multiple_Choice_Config.total_max_items_reached;
announce_message(total_max_message);
return false;
}
var max_selection=choices.layer.get('max_selection') ? parseInt(choices.layer.get('max_selection') ):0;
var min_selection=choices.layer.get('min_selection') ? parseInt(choices.layer.get('min_selection') ):0;
if(choice.get('parent') ){
var parent=choices.get(choice.get('parent') );
if(parent&&parent.get('max_selection') ){
var parent_max_selection=parent.get('max_selection');
var items=choices.where({ active: true, parent: choice.get('parent') });
var children_selected=count_items(items, choice);
if(children_selected >=parent_max_selection){
if(parent.get('auto_switch') ){
if(items[0]){
if(items[0].cid===choice.cid) return false;
choices.selectChoice(items[0], false);
items[0].set('active', false);
return true;
}}
if(parent&&parent.get_name){
announce_message(parent.get_name() + ': maximum reached (' + children_selected + '/' + parent_max_selection + ')');
}else{
announce_message(PC_Multiple_Choice_Config.max_items_reached);
}
return false;
}}
}
if(max_selection){
var items=choices.where({ active: true });
var number_selected=count_items(items, choice);
if(number_selected < max_selection){
return true;
}else{
if(choices.layer.get('auto_switch') ){
if(items[0]){
if(items[0].cid===choice.cid) return false;
choices.selectChoice(items[0], false);
return true;
}}
var layer_name=format_layer_name(choices.layer);
var max_message=layer_name ?(layer_name + ': maximum reached (' + number_selected + '/' + max_selection + ')'):PC_Multiple_Choice_Config.max_items_reached;
announce_message(max_message);
return false;
}}
return true;
});
function check_max_items(){
var total_selectable_options=PC.fe.currentProductData.product_info.total_max_items||PC_Multiple_Choice_Config.default_total_max_items;
if(! total_selectable_options) return true;
var selected_items_count=0;
var layers=PC.fe.layers.filter(function(model){
if(false===model.get('cshow') ) return false;
return true==model.get('include_in_max_calculation')
});
_.each(layers, function(layer){
var layer_content=PC.fe.getLayerContent(layer.id);
if(! layer_content) return;
var selected_items=layer_content.filter(function(model){
if(false===model.get('cshow') ) return false;
return true==model.get('active');
});
if(selected_items) selected_items_count +=selected_items.length;
});
return selected_items_count < total_selectable_options;
}
Views.multiple_choices=Views.choices.extend({
render: function(){
this.$el.append(this.template(this.model.attributes) );
this.$el.addClass(this.model.get('type') );
if(this.model.get('class_name') ) this.$el.addClass(this.model.get('class_name') );
if(this.model.get('parent') ) this.$el.addClass('is-child-layer');
if(this.model.get('columns') ) this.$el.addClass('columns-' + this.model.get('columns') );
if(this.model.get('color_swatch_size') ) this.$el.addClass('swatches-size--' + this.model.get('color_swatch_size') );
this.$list=this.$el.find('.choices-list ul');
this.set_a11y_attributes();
var min=parseInt(this.model.get('min_selection'), 10)||0;
var max=parseInt(this.model.get('max_selection'), 10)||0;
if(min||max){
var instruction=[];
if(min) instruction.push('Select at least ' + min + ' option' +(min > 1 ? 's':'') + '.');
if(max) instruction.push('Select up to ' + max + ' option' +(max > 1 ? 's':'') + '.');
var instruction_id='mkl-pc-multiple-choice-help-' + this.model.id;
this.$('#' + instruction_id).remove();
this.$el.append('<p id="' + instruction_id + '" class="screen-reader-text">' + instruction.join(' ') + '</p>');
this.$list.attr('aria-describedby', instruction_id);
}
this.add_all(this.options.content);
return this.$el;
},
add_one: function(model){
if(!wp.hooks.applyFilters('PC.fe.choices.add_one', true, model) ) return;
if(model.get('is_group') ){
var new_choice=new PC.fe.views.choiceGroup({ model: model, multiple: false, parent: this });
}else{
var new_choice=new PC.fe.views.choice({ model: model, multiple: true, parent: this });
}
if(model.get('parent')&&this.$('ul[data-item-id=' + model.get('parent') + ']').length){
this.$('ul[data-item-id=' + model.get('parent') + ']').append(new_choice.render());
}else{
this.$list.append(new_choice.render());
}
wp.hooks.doAction('PC.fe.choices.add_one.after', this, new_choice);
},
});
})(jQuery, PC.fe.views, PC._us||window._);