jquery.zmd.hierarchical-display.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* ========================================================================
  2. * Zavoloklom Material Design: jquery.zmd.hierarchical-display.js
  3. * http://zavoloklom.github.io/material-design-hierarchical-display/
  4. * ========================================================================
  5. * Copyright 2014 Zavoloklom.
  6. * Licensed under MIT (https://github.com/zavoloklom/material-design-hierarchical-display/blob/master/LICENSE)
  7. * ======================================================================== */
  8. (function ($) {
  9. 'use strict';
  10. // CLASS DEFINITION
  11. // ======================
  12. var HDisplay = function (element, options) {
  13. this.$element = $(element);
  14. this.$children = this.$element.children();
  15. this.options = $.extend({}, HDisplay.DEFAULTS, options);
  16. this._time = HDisplay.TRANSITION_DURATION * this.options.speed;
  17. this.init();
  18. if (this.options.debug === true) this._debug();
  19. };
  20. HDisplay.VERSION = '1.0.1';
  21. HDisplay.TRANSITION_DURATION = 300;
  22. HDisplay.DEFAULTS = {
  23. action: 'show',
  24. speed: 5,
  25. animationIn: 'zoomedIn',
  26. animationOut: 'zoomedOut',
  27. debug: false
  28. };
  29. HDisplay.prototype.init = function () {
  30. var self = this;
  31. var parentElement = this.$element;
  32. var children = this.$children;
  33. var options = this.options;
  34. var time = this._time;
  35. var elementOffset, calculatedOffset, elemDelay;
  36. parentElement.addClass('zmd-hierarchical-display');
  37. children.each(function () {
  38. elementOffset = $(this).position();
  39. calculatedOffset = elementOffset.left * 0.8 + elementOffset.top;
  40. elemDelay = parseFloat(calculatedOffset / time).toFixed(2);
  41. $(this)
  42. .css("-webkit-animation-delay", elemDelay + 's')
  43. .css("animation-delay", elemDelay + 's');
  44. });
  45. this._delay = elemDelay;
  46. // Call complete function after animation on last children element ends
  47. children.last().on('webkitAnimationEnd animationend', function(){
  48. if ($(this).hasClass(options.animationOut)) {self._complete('hidden');}
  49. if ($(this).hasClass(options.animationIn)) {self._complete('shown');}
  50. });
  51. };
  52. HDisplay.prototype.show = function () {
  53. var parentElement = this.$element;
  54. var children = this.$children;
  55. var options = this.options;
  56. if (parentElement.hasClass('in') || parentElement.hasClass('zmd-hierarchical-displaying')) return;
  57. this._removeAnimations();
  58. parentElement.trigger($.Event('show.zmd.hierarchicalDisplay'));
  59. this._addAnimation(options.animationIn);
  60. };
  61. HDisplay.prototype.hide = function () {
  62. var parentElement = this.$element;
  63. var children = this.$children;
  64. var options = this.options;
  65. if (parentElement.css('visibility') === 'hidden' || parentElement.hasClass('zmd-hierarchical-displaying')) return;
  66. this._removeAnimations();
  67. parentElement.trigger($.Event('hide.zmd.hierarchicalDisplay'));
  68. this._addAnimation(options.animationOut);
  69. };
  70. HDisplay.prototype.toggle = function () {
  71. if (this.$element.hasClass('in')) {return this.hide();}
  72. return this.show();
  73. };
  74. HDisplay.prototype._removeAnimations = function () {
  75. var options = this.options;
  76. this.$children.each(function () {
  77. $(this)
  78. .removeClass(options.animationIn)
  79. .removeClass(options.animationOut);
  80. });
  81. };
  82. HDisplay.prototype._addAnimation = function (animation) {
  83. this.$element.addClass('zmd-hierarchical-displaying');
  84. this.$children.each(function () {
  85. $(this)
  86. .addClass(animation)
  87. .addClass('animation');
  88. });
  89. };
  90. HDisplay.prototype._complete = function (eventName) {
  91. this.$element
  92. .removeClass('zmd-hierarchical-displaying')
  93. .toggleClass('in')
  94. .trigger($.Event(eventName+'.zmd.hierarchicalDisplay'));
  95. };
  96. HDisplay.prototype._debug = function () {
  97. $(document)
  98. .on('show.zmd.hierarchicalDisplay', function (e) {
  99. console.log('Event "show.zmd.hierarchicalDisplay". For more information see:');
  100. console.log(e);
  101. })
  102. .on('shown.zmd.hierarchicalDisplay', function (e) {
  103. console.log('Event "shown.zmd.hierarchicalDisplay". For more information see:');
  104. console.log(e);
  105. })
  106. .on('hide.zmd.hierarchicalDisplay', function (e) {
  107. console.log('Event "hide.zmd.hierarchicalDisplay". For more information see:');
  108. console.log(e);
  109. })
  110. .on('hidden.zmd.hierarchicalDisplay', function (e) {
  111. console.log('Event "hidden.zmd.hierarchicalDisplay". For more information see:');
  112. console.log(e);
  113. });
  114. };
  115. // PLUGIN DEFINITION
  116. // =======================
  117. function Plugin(settings) {
  118. return this.each(function () {
  119. var $this = $(this);
  120. var data = $this.data('zmd.hierarchicalDisplay');
  121. var options = $.extend({}, HDisplay.DEFAULTS, $this.data(), typeof settings === 'object' && settings);
  122. if (!data) {$this.data('zmd.hierarchicalDisplay', (data = new HDisplay(this, options)));}
  123. if (typeof settings === 'string') {return data[settings]();}
  124. if (options.action in data) {return data[options.action]();}
  125. });
  126. }
  127. $.fn.hierarchicalDisplay = Plugin;
  128. $.fn.hierarchicalDisplay.Constructor = HDisplay;
  129. // DATA-API
  130. // ==============
  131. $(document).on('ready', function () {
  132. $('[data-animation="hierarchical-display"]').each(function () {
  133. Plugin.call($(this));
  134. });
  135. });
  136. $(document).on('click', '[data-toggle="hierarchical-display"]', function (e) {
  137. var $this = $(this);
  138. var $target = $($this.attr('data-target') || $this.attr('href'));
  139. if ($this.is('a')) e.preventDefault();
  140. Plugin.call($target, 'toggle');
  141. });
  142. })(jQuery);