Skip to content

Instantly share code, notes, and snippets.

@gdyrrahitis
Created September 2, 2017 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdyrrahitis/b90ec9626c08a1a174475e7f528d6485 to your computer and use it in GitHub Desktop.
Save gdyrrahitis/b90ec9626c08a1a174475e7f528d6485 to your computer and use it in GitHub Desktop.
let uiSref: ng1_directive;
uiSref = ['$uiRouter', '$timeout',
function $StateRefDirective($uiRouter: UIRouter, $timeout: ITimeoutService) {
let $state = $uiRouter.stateService;
return {
restrict: 'A',
require: ['?^uiSrefActive', '?^uiSrefActiveEq'],
link: function (scope: IScope, element: IAugmentedJQuery, attrs: any, uiSrefActive: any) {
let type = getTypeInfo(element);
let active = uiSrefActive[1] || uiSrefActive[0];
let unlinkInfoFn: Function = null;
let hookFn;
let rawDef = {} as Def;
let getDef = () => processedDef($state, element, rawDef);
let ref = parseStateRef(attrs.uiSref);
rawDef.uiState = ref.state;
rawDef.uiStateOpts = attrs.uiSrefOpts ? scope.$eval(attrs.uiSrefOpts) : {};
function update() {
let def = getDef();
if (unlinkInfoFn) unlinkInfoFn();
if (active) unlinkInfoFn = active.$$addStateInfo(def.uiState, def.uiStateParams);
if (def.href != null) attrs.$set(type.attr, def.href);
}
if (ref.paramExpr) {
scope.$watch(ref.paramExpr, function (val) {
rawDef.uiStateParams = extend({}, val);
update();
}, true);
rawDef.uiStateParams = extend({}, scope.$eval(ref.paramExpr));
}
update();
scope.$on('$destroy', <any> $uiRouter.stateRegistry.onStatesChanged(update));
scope.$on('$destroy', <any> $uiRouter.transitionService.onSuccess({}, update));
if (!type.clickable) return;
hookFn = clickHook(element, $state, $timeout, type, getDef);
bindEvents(element, scope, hookFn, rawDef.uiStateOpts);
}
};
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment