Pony.fm/public/scripts/app/directives/popup.coffee

74 lines
1.7 KiB
CoffeeScript
Raw Normal View History

2013-07-27 05:00:45 +02:00
angular.module('ponyfm').directive 'pfmPopup', () ->
(scope, element, attrs) ->
$popup = $ '#' + attrs.pfmPopup
$element = $ element
2013-07-28 08:07:25 +02:00
$positionParent = null
2013-07-27 05:00:45 +02:00
open = false
2013-07-28 08:07:25 +02:00
$popup.parents().each () ->
$this = $ this
$positionParent = $this if $positionParent == null && ($this.css('position') == 'relative' || $this.is 'body')
2013-07-27 05:00:45 +02:00
documentClickHandler = () ->
return if !open
$popup.removeClass 'open'
open = false
2013-07-28 08:07:25 +02:00
calculatePosition = ->
position = $element.offset()
parentPosition = $positionParent.offset()
left = position.left
right = left + $popup.width()
windowWidth = $(window).width() - 15
if right > windowWidth
left -= right - windowWidth
height = 'auto'
top = position.top + $element.height() + 10
bottom = top + $popup.height()
windowHeight = $(window).height()
if bottom > windowHeight
height = windowHeight - top;
return {
left: left - parentPosition.left - 2
top: top - parentPosition.top,
height: height}
2013-07-27 05:00:45 +02:00
2013-07-28 08:07:25 +02:00
windowResizeHandler = () ->
return if !open
$popup.css 'height', 'auto'
position = calculatePosition()
$popup.css
left: position.left
top: position.top
height: position.height
$(document.body).bind 'click', documentClickHandler
$(window).bind 'resize', windowResizeHandler
2013-07-27 05:00:45 +02:00
$(element).click (e) ->
e.preventDefault()
e.stopPropagation()
if open
open = false
$popup.removeClass 'open'
return
$popup.addClass 'open'
2013-07-28 06:37:32 +02:00
2013-07-28 08:07:25 +02:00
$popup.css 'height', 'auto'
position = calculatePosition()
2013-07-27 05:00:45 +02:00
$popup.css
2013-07-28 08:07:25 +02:00
left: position.left
top: position.top
height: position.height
2013-07-27 05:00:45 +02:00
open = true
scope.$on '$destroy', () ->
$(document.body).unbind 'click', documentClickHandler
2013-07-28 08:07:25 +02:00
$(window).unbind 'click', windowResizeHandler