From 5c8e702bbceff1b551d3c7d0251cd7c129682c88 Mon Sep 17 00:00:00 2001 From: Denis Bendrikov Date: Sun, 24 Jul 2016 18:38:23 +0300 Subject: [PATCH] feat(events): expose uis-open-close callback Closes #432 #1153 --- src/uisOpenCloseDirective.js | 19 +++++++++++++++++++ test/select.spec.js | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/uisOpenCloseDirective.js diff --git a/src/uisOpenCloseDirective.js b/src/uisOpenCloseDirective.js new file mode 100644 index 000000000..9b0a00821 --- /dev/null +++ b/src/uisOpenCloseDirective.js @@ -0,0 +1,19 @@ +uis.directive('uisOpenClose', ['$parse', '$timeout', function ($parse, $timeout) { + return { + restrict: 'A', + require: 'uiSelect', + link: function (scope, element, attrs, $select) { + $select.onOpenCloseCallback = $parse(attrs.uisOpenClose); + + scope.$watch('$select.open', function (isOpen, previousState) { + if (isOpen !== previousState) { + $timeout(function () { + $select.onOpenCloseCallback(scope, { + isOpen: isOpen + }); + }); + } + }); + } + }; +}]); diff --git a/test/select.spec.js b/test/select.spec.js index 5f8f31de6..6c335e479 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -1375,6 +1375,32 @@ describe('ui-select tests', function() { expect(scope.$model).toBe(scope.$item); }); + it('should call open-close callback with isOpen state as first argument on open and on close', function () { + + var el = compileTemplate( + ' \ + {{$select.selected.name}} \ + \ +
\ +
\ +
\ +
' + ); + + scope.onOpenCloseFn = function(){}; + spyOn(scope, 'onOpenCloseFn'); + + openDropdown(el); + $timeout.flush(); + expect(scope.onOpenCloseFn).toHaveBeenCalledWith(true); + + closeDropdown(el); + $timeout.flush(); + expect(scope.onOpenCloseFn).toHaveBeenCalledWith(false); + + expect(scope.onOpenCloseFn.calls.count()).toBe(2); + }); + it('should allow creating tag in single select mode with tagging enabled', function() { scope.taggingFunc = function (name) {