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.

183 lines
4.9 KiB

  1. /*!
  2. * jquery-drawer v3.2.2
  3. * Flexible drawer menu using jQuery, iScroll and CSS.
  4. * http://git.blivesta.com/drawer
  5. * License : MIT
  6. * Author : blivesta <design@blivesta.com> (http://blivesta.com/)
  7. */
  8. ;(function umd(factory) {
  9. 'use strict';
  10. if (typeof define === 'function' && define.amd) {
  11. define(['jquery'], factory);
  12. } else if (typeof exports === 'object') {
  13. module.exports = factory(require('jquery'));
  14. } else {
  15. factory(jQuery);
  16. }
  17. }(function Drawer($) {
  18. 'use strict';
  19. var namespace = 'drawer';
  20. var touches = typeof document.ontouchstart != 'undefined';
  21. var __ = {
  22. init: function init(options) {
  23. options = $.extend({
  24. iscroll: {
  25. mouseWheel: true,
  26. preventDefault: false
  27. },
  28. showOverlay: true
  29. }, options);
  30. __.settings = {
  31. state: false,
  32. events: {
  33. opened: 'drawer.opened',
  34. closed: 'drawer.closed'
  35. },
  36. dropdownEvents: {
  37. opened: 'shown.bs.dropdown',
  38. closed: 'hidden.bs.dropdown'
  39. }
  40. };
  41. __.settings.class = $.extend({
  42. nav: 'drawer-nav',
  43. toggle: 'drawer-toggle',
  44. overlay: 'drawer-overlay',
  45. open: 'drawer-open',
  46. close: 'drawer-close',
  47. dropdown: 'drawer-dropdown'
  48. }, options.class);
  49. return this.each(function instantiateDrawer() {
  50. var _this = this;
  51. var $this = $(this);
  52. var data = $this.data(namespace);
  53. if (!data) {
  54. options = $.extend({}, options);
  55. $this.data(namespace, { options: options });
  56. __.refresh.call(_this);
  57. if (options.showOverlay) {
  58. __.addOverlay.call(_this);
  59. }
  60. $('.' + __.settings.class.toggle).on('click.' + namespace, function toggle() {
  61. __.toggle.call(_this);
  62. return _this.iScroll.refresh();
  63. });
  64. $(window).resize(function close() {
  65. __.close.call(_this);
  66. return _this.iScroll.refresh();
  67. });
  68. $('.' + __.settings.class.dropdown)
  69. .on(__.settings.dropdownEvents.opened + ' ' + __.settings.dropdownEvents.closed, function onOpenedOrClosed() {
  70. return _this.iScroll.refresh();
  71. });
  72. }
  73. }); // end each
  74. },
  75. refresh: function refresh() {
  76. this.iScroll = new IScroll(
  77. '.' + __.settings.class.nav,
  78. $(this).data(namespace).options.iscroll
  79. );
  80. },
  81. addOverlay: function addOverlay() {
  82. var _this = this;
  83. var $this = $(this);
  84. var $overlay = $('<div>').addClass(__.settings.class.overlay + ' ' + __.settings.class.toggle);
  85. return $this.append($overlay);
  86. },
  87. toggle: function toggle() {
  88. var _this = this;
  89. if (__.settings.state) {
  90. return __.close.call(_this);
  91. } else {
  92. return __.open.call(_this);
  93. }
  94. },
  95. open: function open() {
  96. var $this = $(this);
  97. if (touches) {
  98. $this.on('touchmove.' + namespace, function disableTouch(event) {
  99. event.preventDefault();
  100. });
  101. }
  102. return $this
  103. .removeClass(__.settings.class.close)
  104. .addClass(__.settings.class.open)
  105. // XXX: local patch waiting for:
  106. // https://github.com/blivesta/drawer/pull/36
  107. //.css({ 'overflow': 'hidden' })
  108. // end local patch
  109. .drawerCallback(function triggerOpenedListeners() {
  110. __.settings.state = true;
  111. $this.trigger(__.settings.events.opened);
  112. });
  113. },
  114. close: function close() {
  115. var $this = $(this);
  116. if (touches) $this.off('touchmove.' + namespace);
  117. return $this
  118. .removeClass(__.settings.class.open)
  119. .addClass(__.settings.class.close)
  120. // XXX: local patch waiting for:
  121. // https://github.com/blivesta/drawer/pull/36
  122. //.css("overflow", "auto")
  123. // end local patch
  124. .drawerCallback(function triggerClosedListeners() {
  125. __.settings.state = false;
  126. $this.trigger(__.settings.events.closed);
  127. });
  128. },
  129. destroy: function destroy() {
  130. return this.each(function destroyEach() {
  131. var $this = $(this);
  132. $(window).off('.' + namespace);
  133. $this.removeData(namespace);
  134. });
  135. }
  136. };
  137. $.fn.drawerCallback = function drawerCallback(callback) {
  138. var end = 'transitionend webkitTransitionEnd';
  139. return this.each(function setAnimationEndHandler() {
  140. var $this = $(this);
  141. $this.on(end, function invokeCallbackOnAnimationEnd() {
  142. $this.off(end);
  143. return callback.call(this);
  144. });
  145. });
  146. };
  147. $.fn.drawer = function drawer(method) {
  148. if (__[method]) {
  149. return __[method].apply(this, Array.prototype.slice.call(arguments, 1));
  150. } else if (typeof method === 'object' || !method) {
  151. return __.init.apply(this, arguments);
  152. } else {
  153. $.error('Method ' + method + ' does not exist on jQuery.' + namespace);
  154. }
  155. };
  156. }));