From 02bbdd25752b07ddbc9f7c0b7f3ba8897a355b92 Mon Sep 17 00:00:00 2001 From: Adrian Lee Date: Fri, 4 Apr 2014 13:34:56 -0700 Subject: [PATCH] feature(macAutocomplete): Added option to update mac-menu classes Closes #166 --- src/directives/autocomplete.coffee | 6 +++-- test/unit/autocomplete.spec.coffee | 41 +++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/directives/autocomplete.coffee b/src/directives/autocomplete.coffee index f1705af..aa2dc35 100644 --- a/src/directives/autocomplete.coffee +++ b/src/directives/autocomplete.coffee @@ -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", [ @@ -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)" diff --git a/test/unit/autocomplete.spec.coffee b/test/unit/autocomplete.spec.coffee index ee6633c..466e22d 100644 --- a/test/unit/autocomplete.spec.coffee +++ b/test/unit/autocomplete.spec.coffee @@ -2,6 +2,7 @@ describe "Mac autocomplete", -> data = {} $compile = null $rootScope = null + $timeout = null keys = null $sniffer = null changeInputValue = null @@ -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"] @@ -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 @@ -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("") $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("") $rootScope + $rootScope.$digest() + + changeInputValue element, "f" + $rootScope.$digest() + + $timeout.flush() + + menuEl = $(".mac-menu") + expect(menuEl.attr("ng-class")).toBeFalsy()