Created
July 10, 2014 12:16
-
-
Save timruffles/b325a89adcfd51a888de to your computer and use it in GitHub Desktop.
toggled input for angular
This file contains 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
(function() { | |
var app = angular.module("toggleInput",[]) | |
app.directive("toggleInput",function() { | |
return { | |
scope: true, | |
controller: function($scope) { | |
$scope.input = {visible: false} | |
}, | |
link: function(scope,el,attrs) { | |
var original = el | |
scope.$broadcast("inputDisplayed",false) | |
} | |
} | |
}) | |
app.directive("toggleTrigger",function() { | |
return { | |
require: "^toggleInput", | |
link: function(scope,el,attrs) { | |
el.on("click",function() { | |
scope.input.visible = true | |
scope.$apply() | |
}) | |
scope.$watch("input.visible",function(val) { | |
el.toggleClass("off",val) | |
}) | |
} | |
} | |
}) | |
app.directive("toggledInput",function($document) { | |
return { | |
require: "^toggleInput", | |
link: function(scope,el,attrs) { | |
var handler | |
function off() { | |
if(handler) $document.off("click",handler) | |
handler = false | |
scope.input.visible = false | |
scope.$apply() | |
} | |
el.on("submit",function() { | |
off() | |
}) | |
scope.$watch("input.visible",function(val) { | |
el.toggleClass("off",!val) | |
if(!val) return | |
el.find("input")[0].focus() | |
// avoid any event | |
setTimeout(function() { | |
$document.on("click",handler = function(event) { | |
if(el.has(event.target).length > 0 || el.is(event.target)) return | |
off() | |
}) | |
}) | |
}) | |
} | |
} | |
}) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment