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.

apps.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @copyright Bernhard Posselt 2014
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license AGPL-3.0-or-later
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. import $ from 'jquery'
  24. let dynamicSlideToggleEnabled = false
  25. const Apps = {
  26. enableDynamicSlideToggle() {
  27. dynamicSlideToggleEnabled = true
  28. },
  29. }
  30. /**
  31. * Shows the #app-sidebar and add .with-app-sidebar to subsequent siblings
  32. *
  33. * @param {object} [$el] sidebar element to show, defaults to $('#app-sidebar')
  34. */
  35. Apps.showAppSidebar = function($el) {
  36. const $appSidebar = $el || $('#app-sidebar')
  37. $appSidebar.removeClass('disappear').show()
  38. $('#app-content').trigger(new $.Event('appresized'))
  39. }
  40. /**
  41. * Shows the #app-sidebar and removes .with-app-sidebar from subsequent
  42. * siblings
  43. *
  44. * @param {object} [$el] sidebar element to hide, defaults to $('#app-sidebar')
  45. */
  46. Apps.hideAppSidebar = function($el) {
  47. const $appSidebar = $el || $('#app-sidebar')
  48. $appSidebar.hide().addClass('disappear')
  49. $('#app-content').trigger(new $.Event('appresized'))
  50. }
  51. /**
  52. * Provides a way to slide down a target area through a button and slide it
  53. * up if the user clicks somewhere else. Used for the news app settings and
  54. * add new field.
  55. *
  56. * Usage:
  57. * <button data-apps-slide-toggle=".slide-area">slide</button>
  58. * <div class=".slide-area" class="hidden">I'm sliding up</div>
  59. */
  60. export const registerAppsSlideToggle = () => {
  61. let buttons = $('[data-apps-slide-toggle]')
  62. if (buttons.length === 0) {
  63. $('#app-navigation').addClass('without-app-settings')
  64. }
  65. $(document).click(function(event) {
  66. if (dynamicSlideToggleEnabled) {
  67. buttons = $('[data-apps-slide-toggle]')
  68. }
  69. buttons.each(function(index, button) {
  70. const areaSelector = $(button).data('apps-slide-toggle')
  71. const area = $(areaSelector)
  72. /**
  73. *
  74. */
  75. function hideArea() {
  76. area.slideUp(OC.menuSpeed * 4, function() {
  77. area.trigger(new $.Event('hide'))
  78. })
  79. area.removeClass('opened')
  80. $(button).removeClass('opened')
  81. }
  82. /**
  83. *
  84. */
  85. function showArea() {
  86. area.slideDown(OC.menuSpeed * 4, function() {
  87. area.trigger(new $.Event('show'))
  88. })
  89. area.addClass('opened')
  90. $(button).addClass('opened')
  91. const input = $(areaSelector + ' [autofocus]')
  92. if (input.length === 1) {
  93. input.focus()
  94. }
  95. }
  96. // do nothing if the area is animated
  97. if (!area.is(':animated')) {
  98. // button toggles the area
  99. if ($(button).is($(event.target).closest('[data-apps-slide-toggle]'))) {
  100. if (area.is(':visible')) {
  101. hideArea()
  102. } else {
  103. showArea()
  104. }
  105. // all other areas that have not been clicked but are open
  106. // should be slid up
  107. } else {
  108. const closest = $(event.target).closest(areaSelector)
  109. if (area.is(':visible') && closest[0] !== area[0]) {
  110. hideArea()
  111. }
  112. }
  113. }
  114. })
  115. })
  116. }
  117. export default Apps