Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(uiSref): Bind ui-sref to DOM events #3343

Merged
merged 1 commit into from
Feb 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/directives/stateDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ng as angular } from "../angular";
import { IAugmentedJQuery, ITimeoutService, IScope, IInterpolateService } from "angular";

import {
Obj, extend, forEach, tail, isString, isObject, parse, noop, unnestR, identity, uniqR, inArray, removeFrom,
Obj, extend, forEach, tail, isString, isObject, isArray, parse, noop, unnestR, identity, uniqR, inArray, removeFrom,
RawParams, PathNode, StateOrName, StateService, StateDeclaration, UIRouter
} from "ui-router-core";
import { UIViewData } from "./viewDirective";
Expand Down Expand Up @@ -96,6 +96,31 @@ function defaultOpts(el: IAugmentedJQuery, $state: StateService) {
};
}

/** @hidden */
function bindEvents(element: IAugmentedJQuery, scope: IScope, hookFn: (e: JQueryMouseEventObject) => void, uiStateOpts: any): void {
let events;

if (uiStateOpts) {
events = uiStateOpts.event;
}

if (!isArray(events)) {
events = ['click'];
}

let on = element.on ? 'on' : 'bind';
for (let event of events) {
element[on](event, hookFn);
}

scope.$on('$destroy', function() {
let off = element.off ? 'off' : 'unbind';
for (let event of events) {
element[off](event, hookFn);
}
});
}

/**
* `ui-sref`: A directive for linking to a state
*
Expand Down Expand Up @@ -157,7 +182,7 @@ function defaultOpts(el: IAugmentedJQuery, $state: StateService) {
*
* ### Transition Options
* You can specify [[TransitionOptions]] to pass to [[StateService.go]] by using the `ui-sref-opts` attribute.
* Options are restricted to `location`, `inherit`, and `reload`.
* Options are restricted to `location`, `inherit`, `reload`, and `event`.
*
* #### Example:
* ```html
Expand Down Expand Up @@ -262,10 +287,7 @@ uiSref = ['$uiRouter', '$timeout',

if (!type.clickable) return;
hookFn = clickHook(element, $state, $timeout, type, getDef);
element[element.on ? 'on' : 'bind']("click", hookFn);
scope.$on('$destroy', function () {
element[element.off ? 'off' : 'unbind']("click", hookFn);
});
bindEvents(element, scope, hookFn, rawDef.uiStateOpts);
}
};
}];
Expand Down Expand Up @@ -318,7 +340,7 @@ uiSref = ['$uiRouter', '$timeout',
*
* ### Transition Options
* You can specify [[TransitionOptions]] to pass to [[StateService.go]] by using the `ui-state-opts` attribute.
* Options are restricted to `location`, `inherit`, and `reload`.
* Options are restricted to `location`, `inherit`, `reload`, and `event`.
* The value of the `ui-state-opts` is `$watch`ed and evaluated as an expression.
*
* #### Example:
Expand Down Expand Up @@ -390,10 +412,7 @@ uiState = ['$uiRouter', '$timeout',

if (!type.clickable) return;
hookFn = clickHook(element, $state, $timeout, type, getDef);
element[element.on ? 'on' : 'bind']("click", hookFn);
scope.$on('$destroy', function () {
element[element.off ? 'off' : 'unbind']("click", hookFn);
});
bindEvents(element, scope, hookFn, rawDef.uiStateOpts);
}
};
}];
Expand Down
160 changes: 160 additions & 0 deletions test/stateDirectivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ describe('uiStateRef', function() {
el[0].dispatchEvent(e);
}

function triggerHTMLEvent(name) {
var event = document.createEvent('HTMLEvents');
event.initEvent(name, false, true);
el[0].dispatchEvent(event);
}

function triggerMouseEvent(name) {
var event = document.createEvent('MouseEvents');
event.initEvent(name, true, true);
el[0].dispatchEvent(event);
}

describe('links with promises', function() {

it('should update the href when promises on parameters change before scope is applied', inject(function($rootScope, $compile, $q) {
Expand Down Expand Up @@ -523,6 +535,84 @@ describe('uiStateRef', function() {
expect(transitionOptions.reload).toEqual(true);
expect(transitionOptions.absolute).toBeUndefined();
}));

describe('option event', function() {
it('should bind click event by default', inject(function($compile, $state, $timeout) {
expect($state.current.name).toBe('top');

el = angular.element('<a ui-state="state"></a>');

scope.state = 'contacts';
$compile(el)(scope);
scope.$digest();

triggerClick(el);
$timeout.flush();

expect($state.current.name).toBe('contacts');
}));

it('should bind single HTML events', inject(function($compile, $state, $timeout) {
expect($state.current.name).toEqual('top');

el = angular.element('<input type="text" ui-state="state" ui-state-opts="{ event: [\'change\'] }">');

scope.state = 'contacts';
$compile(el)(scope);
scope.$digest();

triggerHTMLEvent('change');
$timeout.flush();

expect($state.current.name).toEqual('contacts');
}));

it('should bind multiple HTML events', inject(function($compile, $state, $timeout) {
expect($state.current.name).toEqual('top');

el = angular.element('<input type="text" ui-state="state" ui-state-opts="{ event: [\'change\', \'blur\'] }">');

scope.state = 'contacts';
$compile(el)(scope);
scope.$digest();

triggerHTMLEvent('change');
$timeout.flush();
expect($state.current.name).toEqual('contacts');

$state.go('top');
scope.$digest();

expect($state.current.name).toEqual('top');

triggerHTMLEvent('blur');
$timeout.flush();
expect($state.current.name).toEqual('contacts');
}));

it('should bind multiple Mouse events', inject(function($compile, $state, $timeout) {
expect($state.current.name).toEqual('top');

el = angular.element('<a ui-state="state" ui-state-opts="{ event: [\'mouseover\', \'mousedown\'] }">');

scope.state = 'contacts';
$compile(el)(scope);
scope.$digest();

triggerMouseEvent('mouseover');
$timeout.flush();
expect($state.current.name).toEqual('contacts');

$state.go('top');
scope.$digest();

expect($state.current.name).toEqual('top');

triggerMouseEvent('mousedown');
$timeout.flush();
expect($state.current.name).toEqual('contacts');
}));
});
});

describe('forms', function() {
Expand Down Expand Up @@ -597,6 +687,76 @@ describe('uiStateRef', function() {
expect($state.$current.name).toBe("contacts");
}));
});

describe('option event', function() {
it('should bind click event by default', inject(function($rootScope, $compile, $state, $timeout) {
el = angular.element('<a ui-sref="contacts"></a>');
$compile(el)($rootScope);
$rootScope.$digest();

expect($state.current.name).toEqual('top');

triggerClick(el);
$timeout.flush();

expect($state.current.name).toEqual('contacts');
}));

it('should bind single HTML events', inject(function($rootScope, $compile, $state, $timeout) {
el = angular.element('<input type="text" ui-sref="contacts" ui-sref-opts="{ event: [\'change\'] }">');
$compile(el)($rootScope);
$rootScope.$digest();

expect($state.current.name).toEqual('top');

triggerHTMLEvent('change');
$timeout.flush();

expect($state.current.name).toEqual('contacts');
}));

it('should bind multiple HTML events', inject(function($rootScope, $compile, $state, $timeout) {
el = angular.element('<input type="text" ui-sref="contacts" ui-sref-opts="{ event: [\'change\', \'blur\'] }">');
$compile(el)($rootScope);
$rootScope.$digest();

expect($state.current.name).toEqual('top');

triggerHTMLEvent('change');
$timeout.flush();
expect($state.current.name).toEqual('contacts');

$state.go('top');
$rootScope.$digest();

expect($state.current.name).toEqual('top');

triggerHTMLEvent('blur');
$timeout.flush();
expect($state.current.name).toEqual('contacts');
}));

it('should bind multiple Mouse events', inject(function($rootScope, $compile, $state, $timeout) {
el = angular.element('<a ui-sref="contacts" ui-sref-opts="{ event: [\'mouseover\', \'mousedown\'] }">');
$compile(el)($rootScope);
$rootScope.$digest();

expect($state.current.name).toEqual('top');

triggerMouseEvent('mouseover');
$timeout.flush();
expect($state.current.name).toEqual('contacts');

$state.go('top');
$rootScope.$digest();

expect($state.current.name).toEqual('top');

triggerMouseEvent('mousedown');
$timeout.flush();
expect($state.current.name).toEqual('contacts');
}));
});
});

describe('uiSrefActive', function() {
Expand Down