sonata_admin_js_sticky_13.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*!
  2. Waypoints Sticky Element Shortcut - 4.0.0
  3. Copyright © 2011-2015 Caleb Troughton
  4. Licensed under the MIT license.
  5. https://github.com/imakewebthings/waypoints/blog/master/licenses.txt
  6. */
  7. (function() {
  8. 'use strict'
  9. var $ = window.jQuery
  10. var Waypoint = window.Waypoint
  11. /* http://imakewebthings.com/waypoints/shortcuts/sticky-elements */
  12. function Sticky(options) {
  13. this.options = $.extend({}, Waypoint.defaults, Sticky.defaults, options)
  14. this.element = this.options.element
  15. this.$element = $(this.element)
  16. this.createWrapper()
  17. this.createWaypoint()
  18. }
  19. /* Private */
  20. Sticky.prototype.createWaypoint = function() {
  21. var originalHandler = this.options.handler
  22. this.waypoint = new Waypoint($.extend({}, this.options, {
  23. element: this.wrapper,
  24. handler: $.proxy(function(direction) {
  25. var shouldBeStuck = this.options.direction.indexOf(direction) > -1
  26. var wrapperHeight = shouldBeStuck ? this.$element.outerHeight(true) : ''
  27. this.$wrapper.height(wrapperHeight)
  28. this.$element.toggleClass(this.options.stuckClass, shouldBeStuck)
  29. if (originalHandler) {
  30. originalHandler.call(this, direction)
  31. }
  32. }, this)
  33. }))
  34. }
  35. /* Private */
  36. Sticky.prototype.createWrapper = function() {
  37. if (this.options.wrapper) {
  38. this.$element.wrap(this.options.wrapper)
  39. }
  40. this.$wrapper = this.$element.parent()
  41. this.wrapper = this.$wrapper[0]
  42. }
  43. /* Public */
  44. Sticky.prototype.destroy = function() {
  45. if (this.$element.parent()[0] === this.wrapper) {
  46. this.waypoint.destroy()
  47. this.$element.removeClass(this.options.stuckClass)
  48. if (this.options.wrapper) {
  49. this.$element.unwrap()
  50. }
  51. }
  52. }
  53. Sticky.defaults = {
  54. wrapper: '<div class="sticky-wrapper" />',
  55. stuckClass: 'stuck',
  56. direction: 'down right'
  57. }
  58. Waypoint.Sticky = Sticky
  59. }())
  60. ;