apps.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * ownCloud - core
  3. *
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later. See the COPYING file.
  6. *
  7. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  8. * @copyright Bernhard Posselt 2014
  9. */
  10. (function (document, $, exports) {
  11. 'use strict';
  12. var dynamicSlideToggleEnabled = false;
  13. exports.Apps = {
  14. enableDynamicSlideToggle: function () {
  15. dynamicSlideToggleEnabled = true;
  16. }
  17. };
  18. /**
  19. * Shows the #app-sidebar and add .with-app-sidebar to subsequent siblings
  20. *
  21. * @param {Object} [$el] sidebar element to show, defaults to $('#app-sidebar')
  22. */
  23. exports.Apps.showAppSidebar = function($el) {
  24. var $appSidebar = $el || $('#app-sidebar');
  25. $appSidebar.removeClass('disappear')
  26. .show('slide', { direction: 'right' }, 300);
  27. $('#app-content').trigger(new $.Event('appresized'));
  28. };
  29. /**
  30. * Shows the #app-sidebar and removes .with-app-sidebar from subsequent
  31. * siblings
  32. *
  33. * @param {Object} [$el] sidebar element to hide, defaults to $('#app-sidebar')
  34. */
  35. exports.Apps.hideAppSidebar = function($el) {
  36. var $appSidebar = $el || $('#app-sidebar');
  37. $appSidebar.hide('slide', { direction: 'right' }, 300,
  38. function() {
  39. $appSidebar.addClass('disappear');
  40. });
  41. $('#app-content').trigger(new $.Event('appresized'));
  42. };
  43. /**
  44. * Provides a way to slide down a target area through a button and slide it
  45. * up if the user clicks somewhere else. Used for the news app settings and
  46. * add new field.
  47. *
  48. * Usage:
  49. * <button data-apps-slide-toggle=".slide-area">slide</button>
  50. * <div class=".slide-area" class="hidden">I'm sliding up</div>
  51. */
  52. var registerAppsSlideToggle = function () {
  53. var buttons = $('[data-apps-slide-toggle]');
  54. if (buttons.length === 0) {
  55. $('#app-navigation').addClass('without-app-settings');
  56. }
  57. $(document).click(function (event) {
  58. if (dynamicSlideToggleEnabled) {
  59. buttons = $('[data-apps-slide-toggle]');
  60. }
  61. buttons.each(function (index, button) {
  62. var areaSelector = $(button).data('apps-slide-toggle');
  63. var area = $(areaSelector);
  64. function hideArea() {
  65. area.slideUp(OC.menuSpeed*4, function() {
  66. area.trigger(new $.Event('hide'));
  67. });
  68. area.removeClass('opened');
  69. $(button).removeClass('opened');
  70. }
  71. function showArea() {
  72. area.slideDown(OC.menuSpeed*4, function() {
  73. area.trigger(new $.Event('show'));
  74. });
  75. area.addClass('opened');
  76. $(button).addClass('opened');
  77. var input = $(areaSelector + ' [autofocus]');
  78. if (input.length === 1) {
  79. input.focus();
  80. }
  81. }
  82. // do nothing if the area is animated
  83. if (!area.is(':animated')) {
  84. // button toggles the area
  85. if ($(button).is($(event.target).closest('[data-apps-slide-toggle]'))) {
  86. if (area.is(':visible')) {
  87. hideArea();
  88. } else {
  89. showArea();
  90. }
  91. // all other areas that have not been clicked but are open
  92. // should be slid up
  93. } else {
  94. var closest = $(event.target).closest(areaSelector);
  95. if (area.is(':visible') && closest[0] !== area[0]) {
  96. hideArea();
  97. }
  98. }
  99. }
  100. });
  101. });
  102. };
  103. $(document).ready(function () {
  104. registerAppsSlideToggle();
  105. });
  106. }(document, jQuery, OC));