-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from AlchemyCMS/fix-variant-select
Fix variant select ingredient
- Loading branch information
Showing
2 changed files
with
71 additions
and
28 deletions.
There are no files selected for viewing
77 changes: 50 additions & 27 deletions
77
app/assets/javascripts/alchemy/solidus/admin/variant_select.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,55 @@ | ||
//= require alchemy/solidus/admin/select2_config | ||
|
||
$.fn.alchemyVariantSelect = function(options) { | ||
var config = Alchemy.Solidus.getSelect2Config(options) | ||
$.fn.alchemyVariantSelect = function (options) { | ||
const config = Alchemy.Solidus.getSelect2Config(options) | ||
|
||
this.select2($.extend(true, config, { | ||
ajax: { | ||
data: function(term, page) { | ||
return { | ||
q: $.extend({ | ||
product_name_or_sku_cont: term | ||
}, options.query_params), | ||
page: page | ||
} | ||
function formatSelection(variant) { | ||
return variant.options_text | ||
? `${variant.name} - ${variant.options_text}` | ||
: variant.name | ||
} | ||
|
||
function formatResult(variant, _el, query) { | ||
const matchTerm = new RegExp(query.term, "gi") | ||
const formatMatch = (match) => `<em>${match}</em>` | ||
const name = variant.name.replace(matchTerm, formatMatch) | ||
const sku = variant.sku.replace(matchTerm, formatMatch) | ||
return ` | ||
<div class="variant-select-result"> | ||
<div> | ||
<span>${name}</span> | ||
</div> | ||
<div> | ||
<span>${variant.options_text}</span> | ||
<span>${sku}</span> | ||
</div> | ||
</div> | ||
` | ||
} | ||
|
||
this.select2( | ||
$.extend(true, config, { | ||
ajax: { | ||
data: function (term, page) { | ||
return { | ||
q: $.extend( | ||
{ | ||
product_name_or_sku_cont: term, | ||
}, | ||
options.query_params | ||
), | ||
page: page, | ||
} | ||
}, | ||
results: function (data, page) { | ||
return { | ||
results: data.variants, | ||
more: page * data.per_page < data.total_count, | ||
} | ||
}, | ||
}, | ||
results: function(data, page) { | ||
return { | ||
results: data.variants.map(function(variant) { | ||
return { | ||
id: variant.id, | ||
text: variant.frontend_display | ||
} | ||
}), | ||
more: page * data.per_page < data.total_count | ||
} | ||
} | ||
}, | ||
formatSelection: function(variant) { | ||
return variant.text || variant.frontend_display | ||
} | ||
})) | ||
formatSelection, | ||
formatResult, | ||
}) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters