Created
September 27, 2016 20:41
-
-
Save ryanve/d2d07a83f07d44e7ae7b21de1f4247e7 to your computer and use it in GitHub Desktop.
Angular clone-click directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @ngdoc directive | |
* @restrict A | |
* @description Clone descendent click. Clicking [rad-clone-click] triggers click on associated selector. | |
* @element ANY | |
* @example | |
* <ANY clone-click=".is-clone-click:not(:disabled):not(.is-disabled)"> | |
* <a class="is-clone-click" ui-sref="root.example">example</a> | |
* </ANY> | |
*/ | |
app.directive('cloneClick', function() { | |
var angular = require('angular'); | |
var ignore = 'label, input, textarea, button, select, option, optgroup, [href], [ui-sref], [ng-click], .is-no-clone-click'; | |
return { | |
restrict: 'A', | |
scope: { | |
selector: '@cloneClick' | |
}, | |
link: function (scope, element) { | |
element.click(function(e) { | |
if (e.isTrigger) { | |
return; | |
} | |
var cloned = element.find(scope.selector).first(); | |
var target = angular.element(e.target); | |
if (cloned.length && !cloned.is(target) && !target.is(ignore)) { | |
cloned.click(); | |
} | |
}); | |
element.mouseover(function() { | |
element.toggleClass('is-pointer', !!element.has(scope.selector).length); | |
}); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment