Skip to content

Commit

Permalink
fix nested groups
Browse files Browse the repository at this point in the history
  • Loading branch information
aurovrata committed Aug 16, 2021
1 parent e9c1d5a commit 7326769
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
wget --post-data="input=`cat hybrid-dropdown.js`" --output-document=min/hybrid-dropdown.min.js https://javascript-minifier.com/raw
# wget --post-data="input=`cat hybrid-dropdown.js`" --output-document=min/hybrid-dropdown.min.js https://javascript-minifier.com/raw
#css
wget --post-data="input=`cat hybrid-dropdown.css`" --output-document=min/hybrid-dropdown.min.css https://cssminifier.com/raw
4 changes: 3 additions & 1 deletion hybrid-dropdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
cursor: pointer;
background:inherit;
}

.hybrid-dropdown:focus {box-shadow: 1px 1px 3px}
.hybriddd-option::before, .hybriddd-group::before {
display: none !important;
content: none;
Expand Down Expand Up @@ -67,6 +67,8 @@
cursor: pointer;
min-width: 100%;
max-width: inherit;
display: inline-block;
z-index: 2;
}

.hybriddd-selected::after {
Expand Down
33 changes: 23 additions & 10 deletions hybrid-dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,19 @@ class HybridDDError extends Error {
break;
default:
_.isDS = true;// dateset source.
try{
cnfg['dataSet'] = JSON.parse(elm.querySelector('script').innerHTML);
}catch(se){
console.log(se.name+":"+se.message);
console.log("Unable to Hybridise element, missing or malformed json dataset");
if(!settings['dataSet']){ //json object parsed or in HTML
let is = elm.querySelector('script');
if(is){
try{
cnfg['dataSet'] = JSON.parse(elm.querySelector('script').innerHTML);
}catch(se){
console.log(se.name+":"+se.message);
console.log("HybridDropdown ERROR: missing or malformed json dataset");
}
}else{
console.log("HybridDropdown ERROR: missing json dataset");
cnfg['dataSet'] = null;
}
}
if(!cnfg['id'] && cnfg['fieldName']) cnfg['id'] = cnfg['fieldName'];
elm.classList.add('hybriddd-custom'); //flag the element as custom.
Expand All @@ -94,6 +102,7 @@ class HybridDDError extends Error {
elm._hybriddd = _;
_.el = elm; //keep original element reference.
_.el.classList.add('hybridddised'); //flag the element as converted.
// let pl = _.el.closest('label');
// merge user setting with defaults
Object.keys(cnfg).forEach(k=>{
switch(k){
Expand Down Expand Up @@ -140,19 +149,19 @@ class HybridDDError extends Error {
);
//nake sure we have proper functions
['selectedLabel','optionLabel'].forEach(s=>{
if(!_.opt[s] || !(_.opt[s] instanceof Function) && 1==_.opt[s].length){
if(!_.opt[s] || !(_.opt[s] instanceof Function) || 1!=_.opt[s].length){
throw new HybridDDError(`${s} setting must be a function with 1 argument.`);
}
});
if(_.opt.listOption ){ //check we have a function.
if( !(_.opt.listOption instanceof Function && 2==_.opt.listOption.length) ){
if( !(_.opt.listOption instanceof Function) || 2!=_.opt.listOption.length ){
throw new HybridDDError("listOption setting must be a function with 2 arguments.");
}
}
if(_.opt.treeView && 1==_.opt.limitSelection) _.opt.limitSelection=-1; //by default
_.multi = (_.opt.limitSelection !=1); //flag multi select field.
//check if we have a field name.
if(!_.opt.fieldName) _.opt.fieldName = _.opt.fieldId; //try to set it to id.
if(!_.opt.fieldId) _.opt.fieldId = _.opt.fieldName;
if(_.multi && _.opt.fieldName && _.opt.fieldName.indexOf('[]')<0) _.opt.fieldName +='[]';

//initialise the hybrid-dd.
Expand All @@ -177,6 +186,8 @@ class HybridDDError extends Error {
_.hdd.setAttribute('tabindex',_.opt.tabIndex);
}
_.hdd.setAttribute('id',_.opt.fieldId);
let pl = _.el.closest('label');
if(pl) pl.setAttribute('for',_.opt.fieldId);
_.hdd.setAttribute('class',_.opt.fieldClass);
_.hdd.classList.add('hybrid-dropdown');
// _.hdd.setAttribute('aria-hidden', true);//hide from readers.
Expand Down Expand Up @@ -373,11 +384,13 @@ class HybridDDError extends Error {
case 0===p && o[1] instanceof Array:
hasChildren = isGroup = true;
if(o[1]['label']){
isGroup = false;
val = o[0];
icl = 'hybridddis';
lbl = _.opt.optionLabel(o[1]['label']);
kids = Object.entries(o[1]).slice(1);
isGroup = false;
kids = Object.entries(o[1]);
kids.splice(Object.keys(o[1]).indexOf('label'));
hasChildren = (kids.length>0);
}else kids = Object.entries(o[1]);
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion min/hybrid-dropdown.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7326769

Please sign in to comment.