Skip to content

Instantly share code, notes, and snippets.

@timruffles
Created July 10, 2014 12:16
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 timruffles/b325a89adcfd51a888de to your computer and use it in GitHub Desktop.
Save timruffles/b325a89adcfd51a888de to your computer and use it in GitHub Desktop.
toggled input for angular
(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