Browse Source

Complete first pass of API integration with dropdown

pull/2300/head
jlukic 9 years ago
parent
commit
071d101215
2 changed files with 41 additions and 64 deletions
  1. 104
      src/definitions/modules/dropdown.js
  2. 1
      src/definitions/modules/dropdown.less

104
src/definitions/modules/dropdown.js

@ -194,12 +194,6 @@ $.fn.dropdown = function(parameters) {
; ;
module.verbose('Searching for query', query); module.verbose('Searching for query', query);
module.filter(query); module.filter(query);
if(settings.allowAdditions) {
module.add.userChoice(query);
}
if(module.is.searchSelection() && module.can.show() ) {
module.show();
}
}, },
select: { select: {
@ -231,18 +225,6 @@ $.fn.dropdown = function(parameters) {
}, },
setup: { setup: {
api: function() {
var
apiSettings = {
debug : settings.debug,
on : false,
action : 'select'
},
searchHTML
;
module.verbose('First request, initializing API');
$module.api(apiSettings);
},
layout: function() { layout: function() {
if( $module.is('select') ) { if( $module.is('select') ) {
module.setup.select(); module.setup.select();
@ -257,6 +239,12 @@ $.fn.dropdown = function(parameters) {
if(settings.allowTab) { if(settings.allowTab) {
module.set.tabbable(); module.set.tabbable();
} }
if($menu.length === 0) {
$menu = $('<div />')
.addClass(className.menu)
.appendTo($module)
;
}
}, },
select: function() { select: function() {
var var
@ -294,13 +282,8 @@ $.fn.dropdown = function(parameters) {
module.refresh(); module.refresh();
}, },
menu: function(values) { menu: function(values) {
if($menu.length === 0) {
$menu = $('<div />')
.addClass(className.menu)
.appendTo($module)
;
}
$menu.html( templates.menu( values )); $menu.html( templates.menu( values ));
$item = $menu.find(selector.item);
}, },
reference: function() { reference: function() {
var var
@ -548,19 +531,23 @@ $.fn.dropdown = function(parameters) {
else { else {
module.remove.message(); module.remove.message();
} }
if(settings.allowAdditions) {
module.add.userChoice(query);
}
if(module.is.searchSelection() && module.can.show() ) {
module.show();
}
} }
; ;
if(module.has.maxSelections()) { if(module.has.maxSelections()) {
return; return;
} }
if(true) {
// async
if(settings.apiSettings) {
module.queryRemote(searchTerm, function() { module.queryRemote(searchTerm, function() {
afterFiltered(); afterFiltered();
}); });
} }
else { else {
// sync
module.filterItems(searchTerm); module.filterItems(searchTerm);
afterFiltered(); afterFiltered();
} }
@ -569,52 +556,40 @@ $.fn.dropdown = function(parameters) {
queryRemote: function(query, callback) { queryRemote: function(query, callback) {
var var
apiSettings = { apiSettings = {
mockResponseAsync: function(settings, callback) {
setTimeout(function() {
callback({
success: true,
results: [
{
name : 'Apple',
value : 'apple'
},
{
name : 'Bear',
value : 'bear'
},
{
name : 'Pineapple',
value : 'pineapple'
}
]
});
}, 300);
debug : settings.debug,
on : false,
errorDuration : false,
urlData: {
query: query
},
onError: function() {
module.add.message(message.serverError);
},
onFailure: function() {
module.add.message(message.serverError);
}, },
onSuccess : function(response) { onSuccess : function(response) {
module.remove.message();
module.setup.menu({ module.setup.menu({
values: response.results values: response.results
}); });
callback(); callback();
},
urlData: {
query: query
} }
} }
; ;
if( !$module.api('get request') ) {
module.setup.api();
}
$.extend(true, apiSettings, settings.apiSettings);
module.debug('Executing remote search', apiSettings);
apiSettings = $.extend(true, {}, apiSettings, settings.apiSettings);
$module $module
.api(apiSettings)
.api('abort') .api('abort')
.api('setting', apiSettings)
.api('query') .api('query')
; ;
}, },
filterItems: function(searchTerm) {
filterItems: function(query) {
var var
searchTerm = (query !== undefined)
? query
: module.get.query(),
$results = $(), $results = $(),
escapedTerm = module.escape.regExp(searchTerm), escapedTerm = module.escape.regExp(searchTerm),
beginsWithRegExp = new RegExp('^' + escapedTerm, 'igm') beginsWithRegExp = new RegExp('^' + escapedTerm, 'igm')
@ -766,6 +741,9 @@ $.fn.dropdown = function(parameters) {
if(settings.showOnFocus) { if(settings.showOnFocus) {
module.show(); module.show();
} }
if(module.is.searchSelection()) {
module.search();
}
}, },
blur: function(event) { blur: function(event) {
var var
@ -1028,7 +1006,6 @@ $.fn.dropdown = function(parameters) {
module.verbose('Removing last label on input backspace'); module.verbose('Removing last label on input backspace');
$activeLabel = $label.last().addClass(className.active); $activeLabel = $label.last().addClass(className.active);
module.remove.labels($activeLabel); module.remove.labels($activeLabel);
event.preventDefault();
} }
} }
else { else {
@ -1568,7 +1545,7 @@ $.fn.dropdown = function(parameters) {
module.remove.message(); module.remove.message();
module.remove.filteredItem(); module.remove.filteredItem();
if(module.is.searchSelection()) { if(module.is.searchSelection()) {
module.filter();
module.filterItems();
} }
return false; return false;
} }
@ -2738,7 +2715,7 @@ $.fn.dropdown.settings = {
on : 'click', // what event should show menu action on item selection on : 'click', // what event should show menu action on item selection
action : 'activate', // action on item selection (nothing, activate, select, combo, hide, function(){}) action : 'activate', // action on item selection (nothing, activate, select, combo, hide, function(){})
apiSettings : {},
apiSettings : false,
keepOnScreen : true, // Whether dropdown should check whether it is on screen before showing keepOnScreen : true, // Whether dropdown should check whether it is on screen before showing
@ -2776,7 +2753,7 @@ $.fn.dropdown.settings = {
delay : { delay : {
hide : 300, hide : 300,
show : 200, show : 200,
search : 0,
search : 20,
touch : 50 touch : 50
}, },
@ -2796,7 +2773,8 @@ $.fn.dropdown.settings = {
addResult : 'Add <b>{term}</b>', addResult : 'Add <b>{term}</b>',
count : '{count} selected', count : '{count} selected',
maxSelections : 'Max {maxCount} selections', maxSelections : 'Max {maxCount} selections',
noResults : 'No results found.'
noResults : 'No results found.',
serverError : 'There was an error contacting the server'
}, },
error : { error : {
@ -2914,4 +2892,4 @@ $.fn.dropdown.settings.templates = {
}; };
})( jQuery, window , document );
})( jQuery, window , document );

1
src/definitions/modules/dropdown.less

@ -873,7 +873,6 @@ select.ui.dropdown {
color: @errorItemTextColor; color: @errorItemTextColor;
} }
.ui.multiple.selection.error.dropdown > .label { .ui.multiple.selection.error.dropdown > .label {
background: @errorBackgroundColor;
border-color: @errorBorderColor; border-color: @errorBorderColor;
} }

Loading…
Cancel
Save