kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
9.2 KiB
274 lines
9.2 KiB
9 years ago
|
// -- copyright
|
||
10 years ago
|
// OpenProject is a project management system.
|
||
10 years ago
|
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
|
||
10 years ago
|
//
|
||
|
// This program is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU General Public License version 3.
|
||
|
//
|
||
|
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
// Copyright (C) 2006-2013 Jean-Philippe Lang
|
||
|
// Copyright (C) 2010-2013 the ChiliProject Team
|
||
|
//
|
||
|
// This program is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU General Public License
|
||
|
// as published by the Free Software Foundation; either version 2
|
||
|
// 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 General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
//
|
||
|
// See doc/COPYRIGHT.rdoc for more details.
|
||
9 years ago
|
// ++
|
||
11 years ago
|
|
||
9 years ago
|
function hasDropdownMenu($rootScope, $injector, $window, FocusHelper) {
|
||
11 years ago
|
function getCssPositionProperties(dropdown, trigger) {
|
||
|
var hOffset = 0,
|
||
|
vOffset = 0;
|
||
10 years ago
|
if (dropdown.hasClass('dropdown-anchor-top')) {
|
||
9 years ago
|
vOffset = -dropdown.outerHeight() - trigger.outerHeight() + parseInt(trigger.css('margin-top'), 10);
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
|
// Styling logic taken from jQuery-dropdown plugin: https://github.com/plapier/jquery-dropdown
|
||
|
// (dual MIT/GPL-Licensed)
|
||
|
|
||
|
// Position the dropdown relative-to-parent or relative-to-document
|
||
|
if (dropdown.hasClass('dropdown-relative')) {
|
||
|
return {
|
||
|
left: dropdown.hasClass('dropdown-anchor-right') ?
|
||
9 years ago
|
trigger.position().left -
|
||
|
(dropdown.outerWidth(true) - trigger.outerWidth(true)) -
|
||
|
parseInt(trigger.css('margin-right'), 10) + hOffset :
|
||
|
trigger.position().left + parseInt(trigger.css('margin-left'), 10) + hOffset,
|
||
10 years ago
|
top: trigger.position().top +
|
||
9 years ago
|
trigger.outerHeight(true) -
|
||
|
parseInt(trigger.css('margin-top'), 10) + vOffset
|
||
11 years ago
|
};
|
||
9 years ago
|
}
|
||
|
else {
|
||
11 years ago
|
return {
|
||
|
left: dropdown.hasClass('dropdown-anchor-right') ?
|
||
9 years ago
|
trigger.offset().left - (dropdown.outerWidth() - trigger.outerWidth()) + hOffset : trigger.offset().left + hOffset,
|
||
11 years ago
|
top: trigger.offset().top + trigger.outerHeight() + vOffset
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
function getPositionPropertiesOfEvent(event) {
|
||
9 years ago
|
var position = {};
|
||
10 years ago
|
|
||
|
if (event.pageX && event.pageY) {
|
||
|
position.top = Math.max(event.pageY, 0);
|
||
|
position.left = Math.max(event.pageX, 0);
|
||
9 years ago
|
}
|
||
|
else {
|
||
10 years ago
|
var bounding = angular.element(event.target)[0].getBoundingClientRect();
|
||
|
|
||
|
position.top = Math.max(bounding.bottom, 0);
|
||
|
position.left = Math.max(bounding.left, 0);
|
||
|
}
|
||
|
|
||
|
return position;
|
||
|
}
|
||
|
|
||
|
function getCssPositionPropertiesOfEvent(event) {
|
||
|
var position = getPositionPropertiesOfEvent(event);
|
||
|
|
||
|
position.top += 'px';
|
||
|
position.left += 'px';
|
||
|
|
||
|
return position;
|
||
|
}
|
||
|
|
||
11 years ago
|
return {
|
||
|
restrict: 'A',
|
||
9 years ago
|
controller: [function () {
|
||
11 years ago
|
var dropDownMenuOpened = false;
|
||
|
|
||
9 years ago
|
this.open = function () {
|
||
11 years ago
|
dropDownMenuOpened = true;
|
||
|
};
|
||
9 years ago
|
this.close = function () {
|
||
11 years ago
|
dropDownMenuOpened = false;
|
||
|
};
|
||
9 years ago
|
this.opened = function () {
|
||
11 years ago
|
return dropDownMenuOpened;
|
||
|
};
|
||
|
}],
|
||
9 years ago
|
link: function (scope, element, attrs, ctrl) {
|
||
11 years ago
|
var contextMenu = $injector.get(attrs.target),
|
||
|
locals = {},
|
||
10 years ago
|
pointerPosition,
|
||
|
pointerCssPosition,
|
||
11 years ago
|
win = angular.element($window),
|
||
|
menuElement,
|
||
10 years ago
|
afterFocusOn = attrs.afterFocusOn,
|
||
10 years ago
|
positionRelativeTo = attrs.positionRelativeTo,
|
||
10 years ago
|
triggerOnEvent = (attrs.triggerOnEvent || 'click') + '.dropdown.openproject';
|
||
11 years ago
|
|
||
|
/* contextMenu is a mandatory attribute and used to bind a specific context
|
||
9 years ago
|
menu to the trigger event
|
||
|
triggerOnEvent allows for binding the event for opening the menu to "click" */
|
||
11 years ago
|
|
||
|
|
||
10 years ago
|
function toggle(event) {
|
||
9 years ago
|
if (active()) {
|
||
|
close();
|
||
|
} else {
|
||
|
open(event);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
function active() {
|
||
11 years ago
|
return contextMenu.active() && ctrl.opened();
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
function open(event) {
|
||
10 years ago
|
var ignoreFocusOpener = true;
|
||
|
pointerPosition = getPositionPropertiesOfEvent(event);
|
||
|
pointerCssPosition = getCssPositionPropertiesOfEvent(event);
|
||
|
$rootScope.$broadcast('openproject.dropdown.closeDropdowns', ignoreFocusOpener);
|
||
10 years ago
|
// prepare locals, these define properties to be passed on to the context menu scope
|
||
9 years ago
|
var localKeys = (attrs.locals || '').split(',').map(function (local) {
|
||
10 years ago
|
return local.trim();
|
||
|
});
|
||
9 years ago
|
angular.forEach(localKeys, function (key) {
|
||
10 years ago
|
locals[key] = scope[key];
|
||
|
});
|
||
|
|
||
11 years ago
|
ctrl.open();
|
||
11 years ago
|
|
||
10 years ago
|
contextMenu.open(element, locals)
|
||
9 years ago
|
.then(function (element) {
|
||
11 years ago
|
menuElement = element;
|
||
10 years ago
|
menuElement.trap();
|
||
|
positionDropdown();
|
||
9 years ago
|
menuElement.on('click', function (e) {
|
||
10 years ago
|
// allow inputs to be clickable
|
||
|
// without closing the dropdown
|
||
|
if (angular.element(e.target).is(':input')) {
|
||
|
e.stopPropagation();
|
||
|
}
|
||
|
});
|
||
11 years ago
|
});
|
||
|
}
|
||
|
|
||
10 years ago
|
function close(ignoreFocusOpener) {
|
||
11 years ago
|
ctrl.close();
|
||
10 years ago
|
var disableFocus = ignoreFocusOpener;
|
||
9 years ago
|
contextMenu.close(disableFocus).then(function () {
|
||
10 years ago
|
if (!ignoreFocusOpener) {
|
||
|
FocusHelper.focusElement(afterFocusOn ? element.find(afterFocusOn) : element);
|
||
|
}
|
||
|
});
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
function positionDropdown() {
|
||
10 years ago
|
var positionRelativeToElement = positionRelativeTo ?
|
||
|
element.find(positionRelativeTo) : element;
|
||
9 years ago
|
if (attrs.triggerOnEvent === 'contextmenu') {
|
||
10 years ago
|
menuElement.css(pointerCssPosition);
|
||
|
adjustPosition(menuElement, pointerPosition);
|
||
|
} else {
|
||
|
menuElement.css(getCssPositionProperties(menuElement, positionRelativeToElement));
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
function adjustPosition($element, pointerPosition) {
|
||
|
var viewport = {
|
||
9 years ago
|
top: win.scrollTop(),
|
||
|
left: win.scrollLeft()
|
||
10 years ago
|
};
|
||
|
|
||
|
viewport.right = viewport.left + win.width();
|
||
|
viewport.bottom = viewport.top + win.height();
|
||
|
var bounds = $element.offset();
|
||
|
bounds.right = bounds.left + $element.outerWidth();
|
||
|
bounds.bottom = bounds.top + $element.outerHeight();
|
||
|
if (viewport.right < bounds.right) {
|
||
10 years ago
|
$element.css('left', pointerPosition.left - $element.outerWidth());
|
||
10 years ago
|
}
|
||
|
if (viewport.bottom < bounds.bottom) {
|
||
10 years ago
|
$element.css('top', pointerPosition.top - $element.outerHeight());
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
element.bind(triggerOnEvent, function (event) {
|
||
11 years ago
|
event.preventDefault();
|
||
|
event.stopPropagation();
|
||
9 years ago
|
scope.$apply(function () {
|
||
10 years ago
|
toggle(event);
|
||
11 years ago
|
});
|
||
|
|
||
9 years ago
|
menuElement.find('.menu-item').first().focus();
|
||
|
|
||
11 years ago
|
// set css position parameters after the digest has been completed
|
||
|
if (contextMenu.active()) positionDropdown();
|
||
11 years ago
|
});
|
||
|
|
||
9 years ago
|
scope.$on('openproject.dropdown.closeDropdowns', function (event, ignoreFocusOpener) {
|
||
10 years ago
|
if (!ctrl.opened()) {
|
||
|
return;
|
||
11 years ago
|
}
|
||
10 years ago
|
close(ignoreFocusOpener);
|
||
11 years ago
|
});
|
||
|
|
||
9 years ago
|
scope.$on('openproject.dropdown.reposition', function () {
|
||
10 years ago
|
if (contextMenu.active() && menuElement && ctrl.opened()) {
|
||
10 years ago
|
positionDropdown();
|
||
|
}
|
||
|
});
|
||
11 years ago
|
|
||
10 years ago
|
var elementKeyUpString = 'keyup.contextmenu.dropdown.openproject';
|
||
|
element
|
||
|
.off(elementKeyUpString)
|
||
9 years ago
|
.on(elementKeyUpString, function (event) {
|
||
|
// Alt + Shift + F10
|
||
|
if (event.keyCode === 121 && event.shiftKey && event.altKey && !contextMenu.active()) {
|
||
10 years ago
|
open(event);
|
||
|
}
|
||
9 years ago
|
});
|
||
11 years ago
|
|
||
10 years ago
|
|
||
|
// We need the off/on stuff in order to not have a new listener
|
||
|
// for every linking. It's not only not efficient, if causes bugs
|
||
|
// because of closures
|
||
|
// we also add an event namespace to avoid off'ing unrelated listeners
|
||
|
// We can leave it like this
|
||
|
// or move to the compile function of the directive
|
||
|
// or move to a service and make sure it's called only once
|
||
|
|
||
|
var keyUpEventString = 'keyup.dropdown.openproject';
|
||
|
win
|
||
9 years ago
|
.off(keyUpEventString).on(keyUpEventString, function (event) {
|
||
10 years ago
|
if (event.keyCode === 27) {
|
||
|
$rootScope.$broadcast('openproject.dropdown.closeDropdowns');
|
||
11 years ago
|
}
|
||
10 years ago
|
});
|
||
|
|
||
10 years ago
|
function handleWindowClickEvent() {
|
||
10 years ago
|
$rootScope.$broadcast('openproject.dropdown.closeDropdowns');
|
||
11 years ago
|
}
|
||
|
|
||
|
// Firefox treats a right-click as a click and a contextmenu event while other browsers
|
||
|
// just treat it as a contextmenu event
|
||
10 years ago
|
var clickEventString = 'click.dropdown.openproject';
|
||
|
win
|
||
|
.off(clickEventString)
|
||
|
.on(clickEventString, handleWindowClickEvent);
|
||
|
win
|
||
|
.off(triggerOnEvent)
|
||
|
.on(triggerOnEvent, handleWindowClickEvent);
|
||
11 years ago
|
}
|
||
|
};
|
||
9 years ago
|
}
|
||
|
|
||
|
angular
|
||
|
.module('openproject.uiComponents')
|
||
|
.directive('hasDropdownMenu', hasDropdownMenu);
|