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

Commit

Permalink
feature(macAutocomplete): Added option to update mac-menu classes
Browse files Browse the repository at this point in the history
Closes #166
  • Loading branch information
adrianlee44 committed Apr 4, 2014
1 parent 1f9b2a0 commit 02bbdd2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/directives/autocomplete.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Source support multiple types:
@param {String} mac-autocomplete-label The label to display to the users (default "name")
@param {String} mac-autocomplete-query The query parameter on GET command (default "q")
@param {Integer} mac-autocomplete-delay Delay on fetching autocomplete data after keyup (default 800)
@param {Expr} mac-menu-class Classes for mac-menu used by mac-autocomplete. For more info, check [ngClass](http://docs.angularjs.org/api/ng/directive/ngClass)
###

angular.module("Mac").directive "macAutocomplete", [
Expand Down Expand Up @@ -95,13 +97,13 @@ angular.module("Mac").directive "macAutocomplete", [
# is selected in the menu
onSelectBool = false

# NOTE: An isolate scope is created to prevent rootScope pollution
$menuScope = $rootScope.$new(true)
$menuScope = $scope.$new()
$menuScope.items = []
$menuScope.index = 0

menuEl = angular.element(document.createElement("mac-menu"))
menuEl.attr
"ng-class": attrs.macMenuClass or null
"mac-menu-items": "items"
"mac-menu-style": "style"
"mac-menu-select": "select(index)"
Expand Down
41 changes: 35 additions & 6 deletions test/unit/autocomplete.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ describe "Mac autocomplete", ->
data = {}
$compile = null
$rootScope = null
$timeout = null
keys = null
$sniffer = null
changeInputValue = null
Expand All @@ -10,11 +11,12 @@ describe "Mac autocomplete", ->
beforeEach module("template/autocomplete.html")
beforeEach module("template/menu.html")

beforeEach inject (_$compile_, _$rootScope_, _keys_, _$sniffer_) ->
beforeEach inject (_$compile_, _$rootScope_, _keys_, _$sniffer_, _$timeout_) ->
$compile = _$compile_
$rootScope = _$rootScope_
keys = _keys_
$sniffer = _$sniffer_
$timeout = _$timeout_

data = ["foo", "world", "bar"]

Expand Down Expand Up @@ -192,11 +194,6 @@ describe "Mac autocomplete", ->
expect(items[0].value).toBe "foo"

describe "options", ->
$timeout = null

beforeEach inject (_$timeout_) ->
$timeout = _$timeout_

it "should use default delay - 800ms", ->
$rootScope.source = data

Expand Down Expand Up @@ -337,3 +334,35 @@ describe "Mac autocomplete", ->
$rootScope.$digest()

expect(element.val()).toBe "hello"

describe "mac-menu class", ->
it "should set classes", ->
$rootScope.source = data
$rootScope.classes = {test: true, hello: false}

element = $compile("<mac-autocomplete ng-model='test' mac-autocomplete-source='source' mac-menu-class='classes'></mac-autocomplete>") $rootScope
$rootScope.$digest()

changeInputValue element, "f"
$rootScope.$digest()

$timeout.flush()

menuEl = $(".mac-menu")

expect(menuEl.hasClass "test").toBeTruthy()
expect(menuEl.hasClass "hello").toBeFalsy()

it "should not have ng-class on menu element", ->
$rootScope.source = data

element = $compile("<mac-autocomplete ng-model='test' mac-autocomplete-source='source'></mac-autocomplete>") $rootScope
$rootScope.$digest()

changeInputValue element, "f"
$rootScope.$digest()

$timeout.flush()

menuEl = $(".mac-menu")
expect(menuEl.attr("ng-class")).toBeFalsy()

0 comments on commit 02bbdd2

Please sign in to comment.