Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
fix(macAutocomplete): Fixed not able to select item using mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlee44 committed Jun 27, 2014
1 parent 5ea9ab0 commit f644f3f
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions src/directives/autocomplete.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,45 @@ angular.module("Mac").directive "macAutocomplete", [

isMenuAppended = false

# NOTE: onSelectBool is used to prevent parser from firing when an item
# NOTE: preventParser is used to prevent parser from firing when an item
# is selected in the menu
onSelectBool = false
preventParser = false

# NOTE: preventBlur is used to prevent blur even from firing when user click
# on the menu
preventBlur = false

$menuScope = $scope.$new()
$menuScope.items = []
$menuScope.index = 0

$menuScope.select = (index) ->
selected = currentAutocomplete[index]
onSelect $scope, {selected}

label = $menuScope.items[index].label or ""
preventParser = true

if attrs.ngModel?
ctrl.$setViewValue label
ctrl.$render()

reset()

$menuScope.onMousedown = ($event) ->
# prevent moving focus out of text field
$event.preventDefault()

preventBlur = true
$timeout ->
preventBlur = false
, 0, false


menuEl = angular.element(document.createElement("mac-menu"))
menuEl.attr
"ng-class": attrs.macMenuClass or null
"ng-mousedown": "onMousedown($event)"
"mac-menu-items": "items"
"mac-menu-select": "select(index)"
"mac-menu-index": "index"
Expand All @@ -129,7 +157,7 @@ angular.module("Mac").directive "macAutocomplete", [
ctrl.$parsers.push (value) ->
# NOTE: If value is more than an empty string,
# autocomplete is enabled and not 'onSelect' cycle
if value and not disabled($scope) and not onSelectBool
if value and not disabled($scope) and not preventParser
$timeout.cancel timeoutId if timeoutId?
if delay > 0
timeoutId = $timeout ->
Expand All @@ -142,18 +170,23 @@ angular.module("Mac").directive "macAutocomplete", [
else
reset()

onSelectBool = false
preventParser = false

return value

###
@name clickHandler
@name blurHandler
@description
Create a click handler function to make sure directive is unbinding
Create a blur handler function to make sure directive is unbinding
the correct handler
###
clickHandler = ->
$scope.$apply -> hide()
blurHandler = ->
if preventBlur
preventBlur = false
return

$scope.$apply ->
reset()

###
@function
Expand All @@ -164,7 +197,7 @@ angular.module("Mac").directive "macAutocomplete", [
###
appendMenu = (callback) ->
unless isMenuAppended
element.bind "blur", clickHandler
element.bind "blur", blurHandler

isMenuAppended = true

Expand All @@ -180,21 +213,17 @@ angular.module("Mac").directive "macAutocomplete", [
Resetting autocomplete
###
reset = ->
$menuScope.items.length = 0

hide()

hide = ->
$animate.leave menuEl, ->
$menuScope.index = 0
$menuScope.index = 0
$menuScope.items.length = 0

# Clear menu element inline style
menuEl[0].style.top = ""
menuEl[0].style.left = ""

isMenuAppended = false

element.unbind "blur", clickHandler
element.unbind "blur", blurHandler

return

Expand Down Expand Up @@ -228,8 +257,6 @@ angular.module("Mac").directive "macAutocomplete", [
@param {Array} data Array of data
###
updateItem = (data = []) ->
$menuScope.items.length = 0

if data.length > 0
currentAutocomplete = data

Expand Down Expand Up @@ -294,19 +321,6 @@ angular.module("Mac").directive "macAutocomplete", [
else if angular.isFunction(sourceData)
sourceData query, updateItem

$menuScope.select = (index) ->
selected = currentAutocomplete[index]
onSelect $scope, {selected}

label = $menuScope.items[index].label or ""
onSelectBool = true

if attrs.ngModel?
ctrl.$setViewValue label
ctrl.$render()

reset()

element.bind "keydown", (event) ->
# No action when menu is not showing
return true if $menuScope.items.length is 0
Expand Down Expand Up @@ -334,7 +348,7 @@ angular.module("Mac").directive "macAutocomplete", [

when keys.ESCAPE
$scope.$apply ->
hide()
reset()

event.preventDefault()

Expand Down

0 comments on commit f644f3f

Please sign in to comment.