assets/javascripts/semantic_ui/definitions/modules/dropdown.js in less-rails-semantic_ui-1.6.2.0 vs assets/javascripts/semantic_ui/definitions/modules/dropdown.js in less-rails-semantic_ui-1.7.0.0

- old
+ new

@@ -49,11 +49,11 @@ $module = $(this), $text = $module.find(selector.text), $search = $module.find(selector.search), $input = $module.find(selector.input), - $combo = ($module.prev().find(selector.text).size() > 0) + $combo = ($module.prev().find(selector.text).length > 0) ? $module.prev().find(selector.text) : $module.prev(), $menu = $module.children(selector.menu), $item = $menu.find(selector.item), @@ -136,16 +136,13 @@ query ; query = $search.val(); module.verbose('Searching for query', query); - - if(module.is.searchSelection()) { - module.filter(query); - if( module.can.show() ) { - module.show(); - } + module.filter(query); + if(module.is.searchSelection() && module.can.show() ) { + module.show(); } }, setup: { @@ -168,14 +165,14 @@ selectValues = module.get.selectValues() ; module.debug('Dropdown initialized on a select', selectValues); // see if select exists inside a dropdown $input = $module; - if($input.parents(selector.dropdown).size() > 0) { + if($input.parents(selector.dropdown).length > 0) { module.debug('Creating dropdown menu only from template'); $module = $input.closest(selector.dropdown); - if($module.find('.' + className.dropdown).size() === 0) { + if($module.find('.' + className.dropdown).length === 0) { $('<div />') .addClass(className.menu) .html( settings.templates.menu( selectValues )) .appendTo($module) ; @@ -220,20 +217,20 @@ show: function(callback) { callback = $.isFunction(callback) ? callback : function(){} ; - if( !module.is.active() && !module.is.allFiltered() ) { + if( module.can.show() && !module.is.active() && !module.is.allFiltered() ) { module.debug('Showing dropdown'); module.animate.show(function() { if( module.can.click() ) { module.bind.intent(); } module.set.visible(); - $.proxy(callback, element)(); + callback.call(element); }); - $.proxy(settings.onShow, element)(); + settings.onShow.call(element); } }, hide: function(callback) { callback = $.isFunction(callback) @@ -242,13 +239,13 @@ ; if( module.is.active() ) { module.debug('Hiding dropdown'); module.animate.hide(function() { module.remove.visible(); - $.proxy(callback, element)(); + callback.call(element); }); - $.proxy(settings.onHide, element)(); + settings.onHide.call(element); } }, hideOthers: function() { module.verbose('Finding other dropdowns to hide'); @@ -300,10 +297,11 @@ .on('mousedown' + eventNamespace, selector.menu, module.event.menu.activate) .on('mouseup' + eventNamespace, selector.menu, module.event.menu.deactivate) .on('click' + eventNamespace, selector.search, module.show) .on('focus' + eventNamespace, selector.search, module.event.searchFocus) .on('blur' + eventNamespace, selector.search, module.event.searchBlur) + .on('click' + eventNamespace, selector.text, module.event.searchTextFocus) ; } else { if(settings.on == 'click') { $module @@ -364,21 +362,22 @@ }, filter: function(searchTerm) { var $results = $(), - exactRegExp = new RegExp('^' + searchTerm, 'igm'), - fullTextRegExp = new RegExp(searchTerm, 'ig'), + escapedTerm = module.escape.regExp(searchTerm), + exactRegExp = new RegExp('^' + escapedTerm, 'igm'), + fullTextRegExp = new RegExp(escapedTerm, 'ig'), allItemsFiltered ; module.verbose('Searching for matching values'); $item .each(function(){ var $choice = $(this), - text = module.get.choiceText($choice, false), - value = module.get.choiceValue($choice, text) + text = String(module.get.choiceText($choice, false)), + value = String(module.get.choiceValue($choice, text)) ; if( text.match(exactRegExp) || value.match(exactRegExp) ) { $results = $results.add($choice); } else if(settings.fullTextSearch) { @@ -403,12 +402,14 @@ .eq(0) .addClass(className.selected) ; if( module.is.allFiltered() ) { module.debug('All items filtered, hiding dropdown', searchTerm); - module.hide(); - $.proxy(settings.onNoResults, element)(searchTerm); + if(module.is.searchSelection()) { + module.hide(); + } + settings.onNoResults.call(element, searchTerm); } }, focusSearch: function() { if( module.is.search() ) { @@ -449,12 +450,18 @@ ; if(!itemActivated && !pageLostFocus) { module.hide(); } }, + searchTextFocus: function(event) { + activated = true; + $search.focus(); + }, input: function(event) { - module.set.filtered(); + if(module.is.searchSelection()) { + module.set.filtered(); + } clearTimeout(module.timer); module.timer = setTimeout(module.search, settings.delay.search); }, keydown: function(event) { var @@ -467,18 +474,18 @@ upArrow : 38, downArrow : 40 }, selectedClass = className.selected, currentIndex = $visibleItems.index( $selectedItem ), - hasSelectedItem = ($selectedItem.size() > 0), + hasSelectedItem = ($selectedItem.length > 0), $nextItem, newIndex ; // default to activated choice if no selection present if(!hasSelectedItem) { $selectedItem = $item.filter('.' + className.active).eq(0); - hasSelectedItem = ($selectedItem.size() > 0); + hasSelectedItem = ($selectedItem.length > 0); } // close shortcuts if(pressedKey == keys.escape) { module.verbose('Escape key pressed, closing dropdown'); module.hide(); @@ -490,11 +497,11 @@ } // result shortcuts if(module.is.visible()) { if(pressedKey == keys.enter && hasSelectedItem) { module.verbose('Enter key pressed, choosing selected item'); - $.proxy(module.event.item.click, $selectedItem)(event); + module.event.item.click.call($selectedItem, event); event.preventDefault(); return false; } else if(pressedKey == keys.upArrow) { if(!hasSelectedItem) { @@ -520,11 +527,11 @@ $nextItem = $visibleItems.eq(0); } else { $nextItem = $selectedItem.nextAll(selector.item + ':not(.' + className.filtered + ')').eq(0); } - if(currentIndex + 1 < $visibleItems.size() ) { + if(currentIndex + 1 < $visibleItems.length ) { module.verbose('Down key pressed, changing active item'); $item .removeClass(selectedClass) ; $nextItem @@ -575,11 +582,11 @@ mouseenter: function(event) { var $currentMenu = $(this).children(selector.menu), $otherMenus = $(this).siblings(selector.item).children(selector.menu) ; - if( $currentMenu.size() > 0 ) { + if( $currentMenu.length > 0 ) { clearTimeout(module.itemTimer); module.itemTimer = setTimeout(function() { $.each($otherMenus, function() { module.animate.hide(false, $(this)); }); @@ -592,11 +599,11 @@ mouseleave: function(event) { var $currentMenu = $(this).children(selector.menu) ; - if($currentMenu.size() > 0) { + if($currentMenu.length > 0) { clearTimeout(module.itemTimer); module.itemTimer = setTimeout(function() { module.verbose('Hiding sub-menu', $currentMenu); module.animate.hide(false, $currentMenu); }, settings.delay.hide); @@ -612,12 +619,12 @@ value = module.get.choiceValue($choice, text), callback = function() { module.remove.searchTerm(); module.determine.selectAction(text, value); }, - openingSubMenu = ($subMenu.size() > 0), - isSubItem = ($subMenu.find($target).size() > 0) + openingSubMenu = ($subMenu.length > 0), + isSubItem = ($subMenu.find($target).length > 0) ; if(isSubItem) { return false; } if(!openingSubMenu || settings.allowCategorySelection) { @@ -651,11 +658,11 @@ eventInModule: function(event, callback) { callback = $.isFunction(callback) ? callback : function(){} ; - if( $(event.target).closest($module).size() === 0 ) { + if( $(event.target).closest($module).length === 0 ) { module.verbose('Triggering event', callback); callback(); return true; } else { @@ -666,11 +673,11 @@ eventInMenu: function(event, callback) { callback = $.isFunction(callback) ? callback : function(){} ; - if( $(event.target).closest($menu).size() === 0 ) { + if( $(event.target).closest($menu).length === 0 ) { module.verbose('Triggering event', callback); callback(); return true; } else { @@ -731,42 +738,42 @@ get: { text: function() { return $text.text(); }, value: function() { - return ($input.size() > 0) + return ($input.length > 0) ? $input.val() : $module.data(metadata.value) ; }, choiceText: function($choice, preserveHTML) { preserveHTML = (preserveHTML !== undefined) ? preserveHTML : settings.preserveHTML ; if($choice !== undefined) { - if($choice.find(selector.menu).size() > 0) { + if($choice.find(selector.menu).length > 0) { module.verbose('Retreiving text of element with sub-menu'); $choice = $choice.clone(); $choice.find(selector.menu).remove(); $choice.find(selector.menuIcon).remove(); } return ($choice.data(metadata.text) !== undefined) ? $choice.data(metadata.text) : (preserveHTML) - ? $choice.html() - : $choice.text() + ? $choice.html().trim() + : $choice.text().trim() ; } }, choiceValue: function($choice, choiceText) { choiceText = choiceText || module.get.choiceText($text); return ($choice.data(metadata.value) !== undefined) ? $choice.data(metadata.value) : (typeof choiceText === 'string') - ? choiceText.toLowerCase() - : choiceText + ? choiceText.toLowerCase().trim() + : choiceText.trim() ; }, inputEvent: function() { var input = $search[0] @@ -891,19 +898,26 @@ var defaultText = $module.data(metadata.defaultText) ; module.debug('Restoring default text', defaultText); module.set.text(defaultText); + $text.addClass(settings.className.placeholder); }, defaultValue: function() { var defaultValue = $module.data(metadata.defaultValue) ; if(defaultValue !== undefined) { module.debug('Restoring default value', defaultValue); - module.set.selected(defaultValue); - module.set.value(defaultValue); + if(defaultValue.length) { + module.set.selected(defaultValue); + module.set.value(defaultValue); + } + else { + module.remove.activeItem(); + module.remove.selectedItem(); + } } } }, save: { @@ -968,11 +982,11 @@ abovePage, belowPage ; $item = $item || module.get.activeItem(); - hasActive = ($item && $item.size() > 0); + hasActive = ($item && $item.length > 0); forceScroll = (forceScroll !== undefined) ? forceScroll : false ; @@ -1023,11 +1037,11 @@ } } }, value: function(value) { module.debug('Adding selected value to hidden input', value, $input); - if($input.size() > 0) { + if($input.length > 0) { $input .val(value) .trigger('change') ; } @@ -1058,11 +1072,11 @@ .addClass(className.selected) ; selectedText = module.get.choiceText($selectedItem); module.set.text(selectedText); - $.proxy(settings.onChange, element)(value, selectedText, $selectedItem); + settings.onChange.call(element, value, selectedText, $selectedItem); } } }, remove: { @@ -1115,11 +1129,11 @@ ? $subMenu.is(':animated') || $subMenu.transition && $subMenu.transition('is animating') : $menu.is(':animated') || $menu.transition && $menu.transition('is animating') ; }, allFiltered: function() { - return ($item.filter('.' + className.filtered).size() === $item.size()); + return ($item.filter('.' + className.filtered).length === $item.length); }, hidden: function($subMenu) { return ($subMenu) ? $subMenu.is(':hidden') : $menu.is(':hidden') @@ -1127,18 +1141,21 @@ }, search: function() { return $module.hasClass(className.search); }, searchable: function() { - return ($search.size() > 0); + return ($search.length > 0); }, searchSelection: function() { return ( module.is.searchable() && $search.parent().is($module) ); }, selection: function() { return $module.hasClass(className.selection); }, + upward: function() { + return $module.hasClass(className.upward); + }, visible: function($subMenu) { return ($subMenu) ? $subMenu.is(':visible') : $menu.is(':visible') ; @@ -1171,12 +1188,20 @@ : function(){} ; module.set.scrollPosition(module.get.activeItem(), true); module.verbose('Doing menu show animation', $currentMenu); if( module.is.hidden($currentMenu) || module.is.animating($currentMenu) ) { + + if(settings.transition == 'auto') { + settings.transition = module.is.upward() + ? 'slide up' + : 'slide down' + ; + } + if(settings.transition == 'none') { - $.proxy(callback, element)(); + callback.call(element); } else if($.fn.transition !== undefined && $module.transition('is supported')) { $currentMenu .transition({ animation : settings.transition + ' in', @@ -1184,11 +1209,11 @@ verbose : settings.verbose, duration : settings.duration, queue : true, onStart : start, onComplete : function() { - $.proxy(callback, element)(); + callback.call(element); } }) ; } else if(settings.transition == 'slide down') { @@ -1203,23 +1228,23 @@ .animate({ opacity : 1 }, settings.duration, 'easeOutQuad', module.event.resetStyle) .end() .slideDown(100, 'easeOutQuad', function() { - $.proxy(module.event.resetStyle, this)(); - $.proxy(callback, element)(); + module.event.resetStyle.call(this); + callback.call(element); }) ; } else if(settings.transition == 'fade') { start(); $currentMenu .hide() .clearQueue() .fadeIn(settings.duration, function() { - $.proxy(module.event.resetStyle, this)(); - $.proxy(callback, element)(); + module.event.resetStyle.call(this); + callback.call(element); }) ; } else { module.error(error.transition, settings.transition); @@ -1247,12 +1272,19 @@ : function(){} ; if( module.is.visible($currentMenu) || module.is.animating($currentMenu) ) { module.verbose('Doing menu hide animation', $currentMenu); + if(settings.transition == 'auto') { + settings.transition = module.is.upward() + ? 'slide up' + : 'slide down' + ; + } + if(settings.transition == 'none') { - $.proxy(callback, element)(); + callback.call(element); } else if($.fn.transition !== undefined && $module.transition('is supported')) { $currentMenu .transition({ animation : settings.transition + ' out', @@ -1260,11 +1292,11 @@ debug : settings.debug, verbose : settings.verbose, queue : true, onStart : start, onComplete : function() { - $.proxy(callback, element)(); + callback.call(element); } }) ; } else if(settings.transition == 'slide down') { @@ -1279,23 +1311,23 @@ opacity : 0 }, 100, 'easeOutQuad', module.event.resetStyle) .end() .delay(50) .slideUp(100, 'easeOutQuad', function() { - $.proxy(module.event.resetStyle, this)(); - $.proxy(callback, element)(); + module.event.resetStyle.call(this); + callback.call(element); }) ; } else if(settings.transition == 'fade') { start(); $currentMenu .show() .clearQueue() .fadeOut(150, function() { - $.proxy(module.event.resetStyle, this)(); - $.proxy(callback, element)(); + module.event.resetStyle.call(this); + callback.call(element); }) ; } else { module.error(error.transition); @@ -1315,10 +1347,17 @@ clearTimeout(module.timer); module.timer = setTimeout(module.hide, settings.delay.hide); } }, + escape: { + regExp: function(text) { + text = String(text); + return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); + } + }, + setting: function(name, value) { module.debug('Changing setting', name, value); if( $.isPlainObject(name) ) { $.extend(true, settings, name); } @@ -1515,11 +1554,11 @@ show : 200, search : 50, touch : 50 }, - transition : 'slide down', + transition : 'auto', duration : 250, /* Callbacks */ onNoResults : function(searchTerm){}, onChange : function(value, text){}, @@ -1564,10 +1603,11 @@ menu : 'menu', placeholder : 'default', search : 'search', selected : 'selected', selection : 'selection', + upward : 'upward', visible : 'visible' } }; @@ -1613,6 +1653,6 @@ return -c *(t/=d)*(t-2) + b; }, }); -})( jQuery, window , document ); \ No newline at end of file +})( jQuery, window , document );