Magento 2 - Auto select option if only one is awailable - magento2

I want to achieve, that if configurable product has only one option (swatch or option in dropdown), to auto select that option. Can you help me please ?
Thanks

Solution:
$(document).on('swatch.initialized', function() {
var swatchLength = $('.swatch-attribute.color').length;
var onlyOneColor = $('.swatch-attribute.color .swatch-option').length;
if(swatchLength == 1 && onlyOneColor == 1){
$('.swatch-option').first().trigger('click');
}
});

Related

find the count of checked checkboxes using protractor

I want to do protractor testing in my AngularJS application where my need is to find the count of checked checkboxes. There are about 44 checkboxes in a page out of which only 4 are checked.I wrote below code where the console prints
true,false,false,true,false,false,true,false,false,true; etc.
How to find how many true values are there?
var rows = element.all(by.model("accordoptionGroup[optgroup.id][objopt.id].value"));
rows.isSelected().then(function(isSelect){
console.log(isSelect);
});
There is a better way - use filter():
var rows = element.all(by.model("accordoptionGroup[optgroup.id][objopt.id].value"));
var count = rows.filter(function (row) {
return row.isSelected().then(function (isSelected) {
return isSelected;
});
}).count();
expect(count).toEqual(4);
You could do something like this -
var rows = element.all(by.model("accordoptionGroup[optgroup.id][objopt.id].value"));
rows.isSelected().then(function(selected){
count =0;
for(i=0;i<rows.length<i++) {
if(selected) {
count++;
}
}
expect(count).toEqual(4); // to check 4 checkboxes are checked
});
Then you could try giving a for loop and iterating it if selected it returns true and you can get the count!

Cannot trigger click on class when selector change

I really appreciate if i got help this problem. I lost almost a week to find but i can't solve this
I knew i am not allowed to post link but if can please make it nofollow.
http://giayattom.com/san-pham/giay-sandal-da-ca-sau-cao-cap-at15-01
My target is when i click on number 1 on images also link to 1 on tab select (1 -- 1 ; 2--2; 3--3) (I did it thanks to this
$('ul.tabs-content a').click(function() {
var node = $(this).closest('#content');
var this_value = $(this).attr('title');
node.find('#pa_color').val(this_value).trigger('change');
return false;
})
But when i click on tab select it wont trigger click on images as i expect. Please help me solve this problem. Many thanks.
I used this
$("#pa_color").change(function () {
var this_class = $(this).val();
console.log(this_class);
$('ul.tabs-content a.'+this_class).trigger('click');
})
change $('ul.tabs-content a.'+this_class).trigger('click');
to $('ul.tabs-content a.'+this_class)[0].click();

JSX/Photoshop: Toggling non selected layer visibility by name?

I'm using this piece for hide/show selected layer:
app.activeDocument.activeLayer.visible = !app.activeDocument.activeLayer.visible;
I wonder if there exist a way of toggling a non selected layer by it's name.
Many thanks
Update:
I got it working with this thing (I know, it must be cleaned):
function toggleLayer() {
for( var i = 0; i < app.activeDocument.artLayers.length; i++) {
if (app.activeDocument.artLayers[i].name == "theLayer"){
app.activeDocument.artLayers[i].allLocked = false;
app.activeDocument.artLayers[i].visible = !app.activeDocument.artLayers[i].visible;
}
}
}
I'd like to know if we can do the same without the loop.
Thanks
Here is the solution I did write. Unexpectedly it worked :P
function toggleLayer() {
var tl = app.activeDocument.layers["theLayer"];
tl.visible = !tl.visible;
}
toggleLayer();
Now, I have another doubt: Whats the difference between "layers" and "artLayers"?
Cheers

In TinyMCE 4.x how do you attach a click event handler to content?

I have been trying many things to attach a click event handler to a selection box in tinymce 4.0.2 content with no success. Does anyone know how to do this in a custom plugin? The following is what I have tried but it is not functioning.
ctr++;
var id = 'vnetforms_elem_'+ctr;
editor.insertContent('<select id="'+id+'"><option>X</option</select>');
tinymce.dom.DOMUtils.bind(tinymce.activeEditor.dom.select('#'+id)[0],'click',function() {
alert('click!');
});
Using jQuery this may help:
$(ed.getBody()).find('#'+id).bind('click', function() {
alert('click!');
});
I have solved my own problem.
It turns out that this was indeed a bug in firefox. When a select element in firefox is marked as editable it doesn't fire events. I was able to resolve this with the following.
ctr++;
var id = 'vnetforms_elem_'+ctr;
editor.insertContent('<select id="'+id+'"></select>');
tinymce.activeEditor.dom.select('#'+id)[0].contentEditable = 'false';
addEvent(tinymce.activeEditor.dom.select('#'+id)[0],'click',function() {
alert('MyClick');
});
Where addEvent is defined in the custom plugin as
var addEvent = function(node,eventName,func){
if ("undefined" == typeof node || null == node) {
} else {
if (!node.ownerDocument.addEventListener && node.ownerDocument.attachEvent) {
node.attachEvent('on' + eventName, func);
} else node.addEventListener(eventName,func,false);
}
}; this.addEvent = addEvent;

how to determine if the browser supports selectionStart if there is no text selected

This code will return true if the browser supports selectionStart and some text is selected, but if no text is selected it returns false (even on browsers that support it):
if (el.selectionStart) {
}
How do you determine if the property is available regardless of whether text happens to be selected?
Thanks
Further googling revealed the answer:
if (el.selectionStart != undefined) {
}
Hopefully this will help you. I tested it on an old Android 4.2 (which returns false) and Chrome (which returns true).
function selectionSupport() {
var input = document.createElement("input");
input.setAttribute('value', '111');
input.selectionStart = 1;
input.selectionEnd = 2;
return (input.selectionStart === 1 && input.selectionEnd === 2);
}
var selectionIsSupported = selectionSupport();