Browse Source

Adds #1669, Dropdown will now recreate menus when option values change, dropdown will now produce errors when methods invoked on <select> and not ui dropdown

pull/1698/merge
jlukic 9 years ago
parent
commit
4046863e37
1 changed files with 55 additions and 16 deletions
  1. 71
      src/definitions/modules/dropdown.js

71
src/definitions/modules/dropdown.js

@ -73,7 +73,14 @@ $.fn.dropdown = function(parameters) {
initialize: function() { initialize: function() {
module.debug('Initializing dropdown', settings); module.debug('Initializing dropdown', settings);
module.setup.layout();
if( module.is.alreadySetup() ) {
module.error(error.alreadySetup);
}
else {
module.setup.layout();
}
module.save.defaults(); module.save.defaults();
module.set.selected(); module.set.selected();
@ -104,6 +111,9 @@ $.fn.dropdown = function(parameters) {
.off(eventNamespace) .off(eventNamespace)
.removeData(moduleNamespace) .removeData(moduleNamespace)
; ;
$menu
.off(eventNamespace)
;
$document $document
.off(elementNamespace) .off(elementNamespace)
; ;
@ -112,8 +122,14 @@ $.fn.dropdown = function(parameters) {
observeChanges: function() { observeChanges: function() {
if('MutationObserver' in window) { if('MutationObserver' in window) {
observer = new MutationObserver(function(mutations) { observer = new MutationObserver(function(mutations) {
module.debug('DOM tree modified, updating selector cache');
module.refresh();
if( module.is.selectMutation(mutations) ) {
module.debug('<select> modified, recreating menu');
module.setup.select();
}
else {
module.debug('DOM tree modified, updating selector cache');
module.refresh();
}
}); });
observer.observe(element, { observer.observe(element, {
childList : true, childList : true,
@ -162,24 +178,27 @@ $.fn.dropdown = function(parameters) {
}, },
select: function() { select: function() {
var var
selectValues = module.get.selectValues()
selectValues = module.get.selectValues()
; ;
module.debug('Dropdown initialized on a select', selectValues); module.debug('Dropdown initialized on a select', selectValues);
// see if select exists inside a dropdown
$input = $module;
if($input.parents(selector.dropdown).length > 0) {
module.debug('Creating dropdown menu only from template');
if( $module.is('select') ) {
$input = $module;
}
// see if select is placed correctly already
if($input.parent(selector.dropdown).length > 0) {
module.debug('UI dropdown already exists. Creating dropdown menu only');
$module = $input.closest(selector.dropdown); $module = $input.closest(selector.dropdown);
if($module.find('.' + className.dropdown).length === 0) {
$('<div />')
$menu = $module.children(selector.menu);
if($menu.length === 0) {
$menu = $('<div />')
.addClass(className.menu) .addClass(className.menu)
.html( settings.templates.menu( selectValues ))
.appendTo($module) .appendTo($module)
; ;
} }
$menu.html( settings.templates.menu( selectValues ));
} }
else { else {
module.debug('Creating entire dropdown from template');
module.debug('Creating entire dropdown from select');
$module = $('<div />') $module = $('<div />')
.attr('class', $input.attr('class') ) .attr('class', $input.attr('class') )
.addClass(className.selection) .addClass(className.selection)
@ -197,9 +216,14 @@ $.fn.dropdown = function(parameters) {
}, },
refresh: function() { refresh: function() {
module.verbose('Refreshing selector cache');
$text = $module.find(selector.text); $text = $module.find(selector.text);
$search = $module.find(selector.search); $search = $module.find(selector.search);
$input = $module.find(selector.input); $input = $module.find(selector.input);
$combo = ($module.prev().find(selector.text).length > 0)
? $module.prev().find(selector.text)
: $module.prev()
;
$menu = $module.children(selector.menu); $menu = $module.children(selector.menu);
$item = $menu.find(selector.item); $item = $menu.find(selector.item);
}, },
@ -294,7 +318,6 @@ $.fn.dropdown = function(parameters) {
}, },
mouseEvents: function() { mouseEvents: function() {
module.verbose('Mouse detected binding mouse events'); module.verbose('Mouse detected binding mouse events');
if( module.is.searchSelection() ) { if( module.is.searchSelection() ) {
$module $module
.on('mousedown' + eventNamespace, selector.menu, module.event.menu.activate) .on('mousedown' + eventNamespace, selector.menu, module.event.menu.activate)
@ -1169,6 +1192,9 @@ $.fn.dropdown = function(parameters) {
active: function() { active: function() {
return $module.hasClass(className.active); return $module.hasClass(className.active);
}, },
alreadySetup: function() {
return ($module.is('select') && $module.parent(selector.dropdown).length > 0);
},
animating: function($subMenu) { animating: function($subMenu) {
return ($subMenu) return ($subMenu)
? $subMenu.is(':animated') || $subMenu.transition && $subMenu.transition('is animating') ? $subMenu.is(':animated') || $subMenu.transition && $subMenu.transition('is animating')
@ -1184,6 +1210,18 @@ $.fn.dropdown = function(parameters) {
: $menu.is(':hidden') : $menu.is(':hidden')
; ;
}, },
selectMutation: function(mutations) {
var
selectChanged = false
;
$.each(mutations, function(index, mutation) {
if(mutation.target && $(mutation.target).is('select')) {
selectChanged = true;
return true;
}
});
return selectChanged;
},
search: function() { search: function() {
return $module.hasClass(className.search); return $module.hasClass(className.search);
}, },
@ -1616,9 +1654,10 @@ $.fn.dropdown.settings = {
namespace : 'dropdown', namespace : 'dropdown',
error : { error : {
action : 'You called a dropdown action that was not defined',
method : 'The method you called is not defined.',
transition : 'The requested transition was not found'
action : 'You called a dropdown action that was not defined',
alreadySetup : 'Once a select has been initialized behaviors must be called on the created ui dropdown',
method : 'The method you called is not defined.',
transition : 'The requested transition was not found'
}, },
metadata: { metadata: {

Loading…
Cancel
Save