PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/assets/javascripts/action_menu.js

https://gitlab.com/MichelZuniga/openproject
JavaScript | 94 lines | 39 code | 7 blank | 48 comment | 12 complexity | 8feca7b07824f5718fdaa14d9c625f6d MD5 | raw file
  1. //-- copyright
  2. // OpenProject is a project management system.
  3. // Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License version 3.
  7. //
  8. // OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
  9. // Copyright (C) 2006-2013 Jean-Philippe Lang
  10. // Copyright (C) 2010-2013 the ChiliProject Team
  11. //
  12. // This program is free software; you can redistribute it and/or
  13. // modify it under the terms of the GNU General Public License
  14. // as published by the Free Software Foundation; either version 2
  15. // of the License, or (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program; if not, write to the Free Software
  24. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. //
  26. // See doc/COPYRIGHT.rdoc for more details.
  27. //++
  28. /*
  29. The action menu is a menu that usually belongs to an OpenProject entity (like an Issue, WikiPage, Meeting, ..).
  30. Most likely it looks like this:
  31. <ul class="action_menu_main">
  32. <li><a>Menu item text</a></li>
  33. <li><a>Menu item text</a></li>
  34. <li class="drop-down">
  35. <a class="icon icon-more" href="javascript:">More functions</a>
  36. <ul style="display:none;" class="legacy-actions-more">
  37. <li><a>Menu item text</a></li>
  38. </ul>
  39. </li>
  40. </ul>
  41. The following code is responsible to open and close the "more functions" submenu.
  42. */
  43. jQuery(function ($) {
  44. var animationSpeed = 100; // ms
  45. function menu_top_position(menu) {
  46. // if an h2 tag follows the submenu should unfold out at the border
  47. var menu_start_position;
  48. if (menu.next().get(0) != undefined && (menu.next().get(0).tagName == 'H2')){
  49. menu_start_position = menu.next().innerHeight() + menu.next().position().top;
  50. }
  51. else if(menu.next().hasClass("wiki-content") && menu.next().children().next().first().get(0) != undefined && menu.next().children().next().first().get(0).tagName == 'H1'){
  52. var wiki_heading = menu.next().children().next().first();
  53. menu_start_position = wiki_heading.innerHeight() + wiki_heading.position().top;
  54. }
  55. return menu_start_position;
  56. }
  57. function close_menu(event) {
  58. var menu = $(event.data.menu);
  59. // do not close the menu, if the user accidentally clicked next to a menu item (but still within the menu)
  60. if ( event.target !== menu.find(" > li.drop-down.open > ul").get(0)) {
  61. menu.find(" > li.drop-down.open").removeClass("open").find("> ul").slideUp(animationSpeed);
  62. // no need to watch for clicks, when the menu is already closed
  63. $('html').off('click', close_menu);
  64. }
  65. }
  66. function open_menu(menu) {
  67. var drop_down = menu.find(" > li.drop-down");
  68. // do not open a menu, which is already open
  69. if ( !drop_down.hasClass('open') ) {
  70. drop_down.find('> ul').slideDown(animationSpeed, function(){
  71. drop_down.find('li > a:first').focus();
  72. // when clicking on something, which is not the menu, close the menu
  73. $('html').on('click', {menu: menu.get(0)}, close_menu);
  74. });
  75. drop_down.addClass('open');
  76. }
  77. }
  78. // open the given submenu when clicking on it
  79. function install_menu_logic(menu) {
  80. menu.find(" > li.drop-down").click(function(event) {
  81. open_menu(menu);
  82. });
  83. }
  84. $('.legacy-actions-main, .legacy-actions-specific').each(function(idx, menu){
  85. install_menu_logic($(menu));
  86. });
  87. });