var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } (function ($) { 'use strict'; var _defaults = { top: 0, bottom: Infinity, offset: 0, onPositionChange: null }; /** * @class * */ var Pushpin = function (_Component) { _inherits(Pushpin, _Component); /** * Construct Pushpin instance * @constructor * @param {Element} el * @param {Object} options */ function Pushpin(el, options) { _classCallCheck(this, Pushpin); var _this = _possibleConstructorReturn(this, (Pushpin.__proto__ || Object.getPrototypeOf(Pushpin)).call(this, Pushpin, el, options)); _this.el.M_Pushpin = _this; /** * Options for the modal * @member Pushpin#options */ _this.options = $.extend({}, Pushpin.defaults, options); _this.originalOffset = _this.el.offsetTop; Pushpin._pushpins.push(_this); _this._setupEventHandlers(); _this._updatePosition(); return _this; } _createClass(Pushpin, [{ key: 'destroy', /** * Teardown component */ value: function destroy() { this.el.style.top = null; this._removePinClasses(); // Remove pushpin Inst var index = Pushpin._pushpins.indexOf(this); Pushpin._pushpins.splice(index, 1); if (Pushpin._pushpins.length === 0) { this._removeEventHandlers(); } this.el.M_Pushpin = undefined; } }, { key: '_setupEventHandlers', value: function _setupEventHandlers() { document.addEventListener('scroll', Pushpin._updateElements); } }, { key: '_removeEventHandlers', value: function _removeEventHandlers() { document.removeEventListener('scroll', Pushpin._updateElements); } }, { key: '_updatePosition', value: function _updatePosition() { var scrolled = M.getDocumentScrollTop() + this.options.offset; if (this.options.top <= scrolled && this.options.bottom >= scrolled && !this.el.classList.contains('pinned')) { this._removePinClasses(); this.el.style.top = this.options.offset + 'px'; this.el.classList.add('pinned'); // onPositionChange callback if (typeof this.options.onPositionChange === 'function') { this.options.onPositionChange.call(this, 'pinned'); } } // Add pin-top (when scrolled position is above top) if (scrolled < this.options.top && !this.el.classList.contains('pin-top')) { this._removePinClasses(); this.el.style.top = 0; this.el.classList.add('pin-top'); // onPositionChange callback if (typeof this.options.onPositionChange === 'function') { this.options.onPositionChange.call(this, 'pin-top'); } } // Add pin-bottom (when scrolled position is below bottom) if (scrolled > this.options.bottom && !this.el.classList.contains('pin-bottom')) { this._removePinClasses(); this.el.classList.add('pin-bottom'); this.el.style.top = this.options.bottom - this.originalOffset + 'px'; // onPositionChange callback if (typeof this.options.onPositionChange === 'function') { this.options.onPositionChange.call(this, 'pin-bottom'); } } } }, { key: '_removePinClasses', value: function _removePinClasses() { // IE 11 bug (can't remove multiple classes in one line) this.el.classList.remove('pin-top'); this.el.classList.remove('pinned'); this.el.classList.remove('pin-bottom'); } }], [{ key: 'init', value: function init(els, options) { return _get(Pushpin.__proto__ || Object.getPrototypeOf(Pushpin), 'init', this).call(this, this, els, options); } /** * Get Instance */ }, { key: 'getInstance', value: function getInstance(el) { var domElem = !!el.jquery ? el[0] : el; return domElem.M_Pushpin; } }, { key: '_updateElements', value: function _updateElements() { for (var elIndex in Pushpin._pushpins) { var pInstance = Pushpin._pushpins[elIndex]; pInstance._updatePosition(); } } }, { key: 'defaults', get: function () { return _defaults; } }]); return Pushpin; }(Component); /** * @static * @memberof Pushpin */ Pushpin._pushpins = []; M.Pushpin = Pushpin; if (M.jQueryLoaded) { M.initializeJqueryWrapper(Pushpin, 'pushpin', 'M_Pushpin'); } })(cash);