apps.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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' }, 200);
  27. $('#app-content').addClass('with-app-sidebar', 200).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' }, 100,
  38. function() {
  39. $appSidebar.addClass('disappear');
  40. });
  41. $('#app-content').removeClass('with-app-sidebar', 100).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. }
  69. function showArea() {
  70. area.slideDown(OC.menuSpeed*4, function() {
  71. area.trigger(new $.Event('show'));
  72. });
  73. var input = $(areaSelector + ' [autofocus]');
  74. if (input.length === 1) {
  75. input.focus();
  76. }
  77. }
  78. // do nothing if the area is animated
  79. if (!area.is(':animated')) {
  80. // button toggles the area
  81. if ($(button).is($(event.target).closest('[data-apps-slide-toggle]'))) {
  82. if (area.is(':visible')) {
  83. hideArea();
  84. } else {
  85. showArea();
  86. }
  87. // all other areas that have not been clicked but are open
  88. // should be slid up
  89. } else {
  90. var closest = $(event.target).closest(areaSelector);
  91. if (area.is(':visible') && closest[0] !== area[0]) {
  92. hideArea();
  93. }
  94. }
  95. }
  96. });
  97. });
  98. };
  99. $(document).ready(function () {
  100. registerAppsSlideToggle();
  101. });
  102. }(document, jQuery, OC));