2016-01-13 17:10:43 -06:00
|
|
|
(function($) {
|
|
|
|
|
|
|
|
function reorder($container) {
|
|
|
|
$container.find("div.nested-fields").each(function(idx, editor) {
|
|
|
|
var $editor = $(editor);
|
|
|
|
$editor.find('input.sort-order').val(idx + 1).trigger("changed");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-14 15:22:15 -06:00
|
|
|
function initializeIngredientEditor($container, ingredientSearchEngine) {
|
|
|
|
$container.find(".ingredient-typeahead").typeahead({
|
|
|
|
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'ingredients',
|
|
|
|
source: ingredientSearchEngine,
|
|
|
|
display: function(datum) {
|
|
|
|
return datum.name;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-13 17:10:43 -06:00
|
|
|
$(document).on("ready page:load", function() {
|
|
|
|
|
2016-01-14 15:22:15 -06:00
|
|
|
var ingredientSearchEngine = new Bloodhound({
|
|
|
|
datumTokenizer: function(datum) {
|
|
|
|
return Bloodhound.tokenizers.whitespace(datum.name);
|
|
|
|
},
|
|
|
|
queryTokenizer: Bloodhound.tokenizers.whitespace,
|
|
|
|
identify: function(datum) { return datum.id; },
|
|
|
|
sorter: function(a, b) {
|
|
|
|
if (a.name < b.name) {
|
|
|
|
return -1;
|
|
|
|
} else if (b.name < a.name) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
prefetch: {
|
|
|
|
url: '/ingredients/prefetch.json',
|
|
|
|
cache: false
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
url: '/ingredients/search.json?query=%QUERY',
|
|
|
|
wildcard: '%QUERY'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-13 17:10:43 -06:00
|
|
|
$("#step-list")
|
|
|
|
.on("cocoon:after-insert", function(e, item) {
|
|
|
|
reorder($(this));
|
|
|
|
})
|
|
|
|
.on("cocoon:after-remove", function(e, item) {
|
|
|
|
reorder($(this));
|
2016-01-14 15:22:15 -06:00
|
|
|
})
|
|
|
|
.on('changed', 'input.sort-order', function() {
|
|
|
|
var $this = $(this);
|
|
|
|
var $span = $this.closest(".nested-fields").find(".sort-order-display");
|
|
|
|
$span.html($this.val());
|
2016-01-13 17:10:43 -06:00
|
|
|
});
|
|
|
|
|
2016-01-14 15:22:15 -06:00
|
|
|
var $ingredientList = $("#ingredient-list");
|
|
|
|
|
|
|
|
initializeIngredientEditor($ingredientList, ingredientSearchEngine);
|
2016-01-13 17:10:43 -06:00
|
|
|
|
2016-01-14 15:22:15 -06:00
|
|
|
$ingredientList
|
2016-01-13 17:10:43 -06:00
|
|
|
.on("cocoon:after-insert", function(e, item) {
|
|
|
|
reorder($(this));
|
2016-01-14 15:22:15 -06:00
|
|
|
initializeIngredientEditor(item, ingredientSearchEngine);
|
2016-01-13 17:10:43 -06:00
|
|
|
})
|
|
|
|
.on("cocoon:after-remove", function(e, item) {
|
|
|
|
reorder($(this));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
})(jQuery);
|