apps.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * Provides a way to slide down a target area through a button and slide it
  20. * up if the user clicks somewhere else. Used for the news app settings and
  21. * add new field.
  22. *
  23. * Usage:
  24. * <button data-apps-slide-toggle=".slide-area">slide</button>
  25. * <div class=".slide-area" class="hidden">I'm sliding up</div>
  26. */
  27. var registerAppsSlideToggle = function () {
  28. var buttons = $('[data-apps-slide-toggle]');
  29. $(document).click(function (event) {
  30. if (dynamicSlideToggleEnabled) {
  31. buttons = $('[data-apps-slide-toggle]');
  32. }
  33. buttons.each(function (index, button) {
  34. var areaSelector = $(button).data('apps-slide-toggle');
  35. var area = $(areaSelector);
  36. function hideArea() {
  37. area.slideUp(OC.menuSpeed*4, function() {
  38. area.trigger(new $.Event('hide'));
  39. });
  40. }
  41. function showArea() {
  42. area.slideDown(OC.menuSpeed*4, function() {
  43. area.trigger(new $.Event('show'));
  44. });
  45. }
  46. // do nothing if the area is animated
  47. if (!area.is(':animated')) {
  48. // button toggles the area
  49. if ($(button).is($(event.target).closest('[data-apps-slide-toggle]'))) {
  50. if (area.is(':visible')) {
  51. hideArea();
  52. } else {
  53. showArea();
  54. }
  55. // all other areas that have not been clicked but are open
  56. // should be slid up
  57. } else {
  58. var closest = $(event.target).closest(areaSelector);
  59. if (area.is(':visible') && closest[0] !== area[0]) {
  60. hideArea();
  61. }
  62. }
  63. }
  64. });
  65. });
  66. };
  67. $(document).ready(function () {
  68. registerAppsSlideToggle();
  69. });
  70. }(document, jQuery, OC));