2015-10-25 06:17:45 +01:00
|
|
|
# Pony.fm - A community for pony fan music.
|
|
|
|
# Copyright (C) 2015 Peter Deltchev
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-08-31 17:42:21 +02:00
|
|
|
angular.module('ponyfm').directive 'pfmPopup', () ->
|
2015-10-25 03:35:37 +01:00
|
|
|
(scope, element, attrs) ->
|
|
|
|
align = 'left'
|
|
|
|
elementId = attrs.pfmPopup
|
|
|
|
if elementId.indexOf ',' != -1
|
|
|
|
parts = elementId.split ','
|
|
|
|
elementId = parts[0]
|
|
|
|
align = parts[1]
|
|
|
|
|
|
|
|
$popup = $ '#' + attrs.pfmPopup
|
|
|
|
$element = $ element
|
|
|
|
$positionParent = null
|
|
|
|
open = false
|
2016-01-17 07:16:47 +01:00
|
|
|
closeOnClick = attrs.pfmPopupCloseOnClick?
|
2015-10-25 03:35:37 +01:00
|
|
|
|
|
|
|
|
2016-01-17 05:04:23 +01:00
|
|
|
close = () ->
|
2015-10-25 03:35:37 +01:00
|
|
|
$popup.removeClass 'open'
|
|
|
|
open = false
|
2016-01-17 05:04:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
documentClickHandler = (event) ->
|
2016-01-17 07:16:47 +01:00
|
|
|
if !open
|
|
|
|
return
|
|
|
|
|
|
|
|
if (closeOnClick)
|
|
|
|
close()
|
|
|
|
return true
|
|
|
|
|
|
|
|
# Based on: https://stackoverflow.com/a/4660738/3225811
|
|
|
|
else if event.target.id == elementId or $(event.target).parents("##{elementId}").size()
|
2016-01-17 05:04:23 +01:00
|
|
|
return true
|
|
|
|
|
2016-01-17 07:16:47 +01:00
|
|
|
else
|
|
|
|
close()
|
|
|
|
return true
|
2015-10-25 03:35:37 +01:00
|
|
|
|
2016-01-17 05:04:23 +01:00
|
|
|
|
2015-10-25 03:35:37 +01:00
|
|
|
calculatePosition = ->
|
|
|
|
$popup.parents().each () ->
|
|
|
|
$this = $ this
|
2016-01-17 08:11:06 +01:00
|
|
|
|
|
|
|
if $positionParent == null && ($this.css('position') == 'relative' || $this.is 'body')
|
|
|
|
$positionParent = $this
|
|
|
|
return false
|
2015-10-25 03:35:37 +01:00
|
|
|
|
|
|
|
position = $element.offset()
|
2016-01-17 08:11:06 +01:00
|
|
|
parentPosition = $positionParent.offset() + $positionParent.height()
|
2015-10-25 03:35:37 +01:00
|
|
|
|
|
|
|
windowWidth = $(window).width() - 15
|
|
|
|
left = position.left
|
|
|
|
right = left + $popup.width()
|
|
|
|
|
|
|
|
if align == 'left' && right > windowWidth
|
|
|
|
left -= right - windowWidth
|
|
|
|
else if align == 'right'
|
|
|
|
left -= $popup.outerWidth() - $element.outerWidth()
|
|
|
|
|
|
|
|
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
|
|
|
|
top: top - parentPosition.top,
|
|
|
|
height: height - 15}
|
|
|
|
|
2016-01-17 05:04:23 +01:00
|
|
|
|
2015-10-25 03:35:37 +01:00
|
|
|
windowResizeHandler = () ->
|
|
|
|
return if !open
|
|
|
|
$popup.css 'height', 'auto'
|
|
|
|
position = calculatePosition()
|
|
|
|
$popup.css
|
|
|
|
left: position.left
|
|
|
|
top: position.top
|
2016-01-17 08:11:06 +01:00
|
|
|
maxHeight: position.height
|
2015-10-25 03:35:37 +01:00
|
|
|
|
|
|
|
$(document.body).bind 'click', documentClickHandler
|
|
|
|
$(window).bind 'resize', windowResizeHandler
|
|
|
|
|
|
|
|
$(element).click (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
|
2016-01-17 04:22:04 +01:00
|
|
|
if open and not $element.is(':focus')
|
2016-01-17 05:04:23 +01:00
|
|
|
close
|
2015-10-25 03:35:37 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
$popup.addClass 'open'
|
|
|
|
|
|
|
|
$popup.css 'height', 'auto'
|
|
|
|
window.setTimeout (->
|
|
|
|
position = calculatePosition()
|
|
|
|
$popup.css
|
|
|
|
left: position.left
|
|
|
|
top: position.top
|
|
|
|
height: position.height
|
|
|
|
|
|
|
|
open = true
|
|
|
|
), 0
|
|
|
|
|
2016-01-17 05:04:23 +01:00
|
|
|
|
|
|
|
scope.$on '$stateChangeStart', () ->
|
|
|
|
close()
|
|
|
|
|
|
|
|
|
2015-10-25 03:35:37 +01:00
|
|
|
scope.$on '$destroy', () ->
|
|
|
|
$(document.body).unbind 'click', documentClickHandler
|
|
|
|
$(window).unbind 'click', windowResizeHandler
|