diff --git a/src/definitions/modules/dropdown.js b/src/definitions/modules/dropdown.js index 878982931..b67b7fb4a 100644 --- a/src/definitions/modules/dropdown.js +++ b/src/definitions/modules/dropdown.js @@ -161,7 +161,34 @@ $.fn.dropdown = function(parameters) { elementNamespace = '.' + id; module.verbose('Creating unique id for element', id); }, - userLabels: function() { + userChoice: function(values) { + var + $userChoices, + $userChoice, + html + ; + values = values || module.get.userValues(); + values = $.isArray(values) + ? values + : [values] + ; + module.debug('Creating user choices for each value', values); + $.each(values, function(index, value) { + html = settings.templates.addition(value); + $userChoice = $('
') + .html(html) + .data(metadata.value, value) + .addClass(className.addition) + .addClass(className.item) + ; + $userChoices = ($userChoices === undefined) + ? $userChoice + : $userChoices.add($userChoice) + ; + }); + return $userChoices; + }, + userLabels: function(value) { var userValues = module.get.userValues() ; @@ -532,7 +559,7 @@ $.fn.dropdown = function(parameters) { module.remove.message(); } if(settings.allowAdditions) { - module.add.userChoice(query); + module.add.userSuggestion(query); } if(module.is.searchSelection() && module.can.show() && module.is.focusedOnSearch() ) { module.show(); @@ -2001,7 +2028,8 @@ $.fn.dropdown = function(parameters) { }, selected: function(value, $selectedItem) { var - isMultiple = module.is.multiple() + isMultiple = module.is.multiple(), + $userSelectedItem ; $selectedItem = $selectedItem || module.get.item(value); if(!$selectedItem) { @@ -2015,6 +2043,11 @@ $.fn.dropdown = function(parameters) { else if(settings.useLabels) { module.remove.selectedItem(); } + if(settings.allowAdditions) { + $userSelectedItem = module.create.userChoice(value); + $selectedItem = $selectedItem.add($userSelectedItem); + } + // select each item $selectedItem .each(function() { var @@ -2144,10 +2177,11 @@ $.fn.dropdown = function(parameters) { }); } }, - userChoice: function(value) { + userSuggestion: function(value) { var - alreadyHasValue = module.get.item(value), - $addition = $menu.children(selector.addition), + $addition = $menu.children(selector.addition), + alreadyHasValue = module.get.item(value), + hasUserSuggestion = $addition.length > 0, html ; if(module.has.maxSelections()) { @@ -2157,27 +2191,26 @@ $.fn.dropdown = function(parameters) { $addition.remove(); return; } - html = settings.templates.addition(value); $item .removeClass(className.selected) ; - if($addition.length > 0) { + if(hasUserSuggestion) { + html = settings.templates.addition(value); $addition .html(html) .data(metadata.value, value) .removeClass(className.filtered) .addClass(className.selected) ; + module.verbose('Replacing user suggestion with new value', $addition); } else { - $addition = $('') - .html(html) - .data(metadata.value, value) - .addClass(className.addition) - .addClass(className.item) + $addition = module.create.userChoice(value); + $addition .prependTo($menu) .addClass(className.selected) ; + module.verbose('Adding item choice to menu corresponding with user choice addition', $addition); } }, variables: function(message) { @@ -2189,6 +2222,7 @@ $.fn.dropdown = function(parameters) { count, query ; + module.verbose('Adding templated variables to message', message); if(hasCount) { count = module.get.selectionCount(); message = message.replace('{count}', count);