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 = { onOpen: undefined, onClose: undefined }; /** * @class * */ var TapTarget = function (_Component) { _inherits(TapTarget, _Component); /** * Construct TapTarget instance * @constructor * @param {Element} el * @param {Object} options */ function TapTarget(el, options) { _classCallCheck(this, TapTarget); var _this = _possibleConstructorReturn(this, (TapTarget.__proto__ || Object.getPrototypeOf(TapTarget)).call(this, TapTarget, el, options)); _this.el.M_TapTarget = _this; /** * Options for the select * @member TapTarget#options * @prop {Function} onOpen - Callback function called when feature discovery is opened * @prop {Function} onClose - Callback function called when feature discovery is closed */ _this.options = $.extend({}, TapTarget.defaults, options); _this.isOpen = false; // setup _this.$origin = $('#' + _this.$el.attr('data-target')); _this._setup(); _this._calculatePositioning(); _this._setupEventHandlers(); return _this; } _createClass(TapTarget, [{ key: 'destroy', /** * Teardown component */ value: function destroy() { this._removeEventHandlers(); this.el.TapTarget = undefined; } /** * Setup Event Handlers */ }, { key: '_setupEventHandlers', value: function _setupEventHandlers() { this._handleDocumentClickBound = this._handleDocumentClick.bind(this); this._handleTargetClickBound = this._handleTargetClick.bind(this); this._handleOriginClickBound = this._handleOriginClick.bind(this); this.el.addEventListener('click', this._handleTargetClickBound); this.originEl.addEventListener('click', this._handleOriginClickBound); // Resize var throttledResize = M.throttle(this._handleResize, 200); this._handleThrottledResizeBound = throttledResize.bind(this); window.addEventListener('resize', this._handleThrottledResizeBound); } /** * Remove Event Handlers */ }, { key: '_removeEventHandlers', value: function _removeEventHandlers() { this.el.removeEventListener('click', this._handleTargetClickBound); this.originEl.removeEventListener('click', this._handleOriginClickBound); window.removeEventListener('resize', this._handleThrottledResizeBound); } /** * Handle Target Click * @param {Event} e */ }, { key: '_handleTargetClick', value: function _handleTargetClick(e) { this.open(); } /** * Handle Origin Click * @param {Event} e */ }, { key: '_handleOriginClick', value: function _handleOriginClick(e) { this.close(); } /** * Handle Resize * @param {Event} e */ }, { key: '_handleResize', value: function _handleResize(e) { this._calculatePositioning(); } /** * Handle Resize * @param {Event} e */ }, { key: '_handleDocumentClick', value: function _handleDocumentClick(e) { if (!$(e.target).closest('.tap-target-wrapper').length) { this.close(); e.preventDefault(); e.stopPropagation(); } } /** * Setup Tap Target */ }, { key: '_setup', value: function _setup() { // Creating tap target this.wrapper = this.$el.parent()[0]; this.waveEl = $(this.wrapper).find('.tap-target-wave')[0]; this.originEl = $(this.wrapper).find('.tap-target-origin')[0]; this.contentEl = this.$el.find('.tap-target-content')[0]; // Creating wrapper if (!$(this.wrapper).hasClass('.tap-target-wrapper')) { this.wrapper = document.createElement('div'); this.wrapper.classList.add('tap-target-wrapper'); this.$el.before($(this.wrapper)); this.wrapper.append(this.el); } // Creating content if (!this.contentEl) { this.contentEl = document.createElement('div'); this.contentEl.classList.add('tap-target-content'); this.$el.append(this.contentEl); } // Creating foreground wave if (!this.waveEl) { this.waveEl = document.createElement('div'); this.waveEl.classList.add('tap-target-wave'); // Creating origin if (!this.originEl) { this.originEl = this.$origin.clone(true, true); this.originEl.addClass('tap-target-origin'); this.originEl.removeAttr('id'); this.originEl.removeAttr('style'); this.originEl = this.originEl[0]; this.waveEl.append(this.originEl); } this.wrapper.append(this.waveEl); } } /** * Calculate positioning */ }, { key: '_calculatePositioning', value: function _calculatePositioning() { // Element or parent is fixed position? var isFixed = this.$origin.css('position') === 'fixed'; if (!isFixed) { var parents = this.$origin.parents(); for (var i = 0; i < parents.length; i++) { isFixed = $(parents[i]).css('position') == 'fixed'; if (isFixed) { break; } } } // Calculating origin var originWidth = this.$origin.outerWidth(); var originHeight = this.$origin.outerHeight(); var originTop = isFixed ? this.$origin.offset().top - M.getDocumentScrollTop() : this.$origin.offset().top; var originLeft = isFixed ? this.$origin.offset().left - M.getDocumentScrollLeft() : this.$origin.offset().left; // Calculating screen var windowWidth = window.innerWidth; var windowHeight = window.innerHeight; var centerX = windowWidth / 2; var centerY = windowHeight / 2; var isLeft = originLeft <= centerX; var isRight = originLeft > centerX; var isTop = originTop <= centerY; var isBottom = originTop > centerY; var isCenterX = originLeft >= windowWidth * 0.25 && originLeft <= windowWidth * 0.75; // Calculating tap target var tapTargetWidth = this.$el.outerWidth(); var tapTargetHeight = this.$el.outerHeight(); var tapTargetTop = originTop + originHeight / 2 - tapTargetHeight / 2; var tapTargetLeft = originLeft + originWidth / 2 - tapTargetWidth / 2; var tapTargetPosition = isFixed ? 'fixed' : 'absolute'; // Calculating content var tapTargetTextWidth = isCenterX ? tapTargetWidth : tapTargetWidth / 2 + originWidth; var tapTargetTextHeight = tapTargetHeight / 2; var tapTargetTextTop = isTop ? tapTargetHeight / 2 : 0; var tapTargetTextBottom = 0; var tapTargetTextLeft = isLeft && !isCenterX ? tapTargetWidth / 2 - originWidth : 0; var tapTargetTextRight = 0; var tapTargetTextPadding = originWidth; var tapTargetTextAlign = isBottom ? 'bottom' : 'top'; // Calculating wave var tapTargetWaveWidth = originWidth > originHeight ? originWidth * 2 : originWidth * 2; var tapTargetWaveHeight = tapTargetWaveWidth; var tapTargetWaveTop = tapTargetHeight / 2 - tapTargetWaveHeight / 2; var tapTargetWaveLeft = tapTargetWidth / 2 - tapTargetWaveWidth / 2; // Setting tap target var tapTargetWrapperCssObj = {}; tapTargetWrapperCssObj.top = isTop ? tapTargetTop + 'px' : ''; tapTargetWrapperCssObj.right = isRight ? windowWidth - tapTargetLeft - tapTargetWidth + 'px' : ''; tapTargetWrapperCssObj.bottom = isBottom ? windowHeight - tapTargetTop - tapTargetHeight + 'px' : ''; tapTargetWrapperCssObj.left = isLeft ? tapTargetLeft + 'px' : ''; tapTargetWrapperCssObj.position = tapTargetPosition; $(this.wrapper).css(tapTargetWrapperCssObj); // Setting content $(this.contentEl).css({ width: tapTargetTextWidth + 'px', height: tapTargetTextHeight + 'px', top: tapTargetTextTop + 'px', right: tapTargetTextRight + 'px', bottom: tapTargetTextBottom + 'px', left: tapTargetTextLeft + 'px', padding: tapTargetTextPadding + 'px', verticalAlign: tapTargetTextAlign }); // Setting wave $(this.waveEl).css({ top: tapTargetWaveTop + 'px', left: tapTargetWaveLeft + 'px', width: tapTargetWaveWidth + 'px', height: tapTargetWaveHeight + 'px' }); } /** * Open TapTarget */ }, { key: 'open', value: function open() { if (this.isOpen) { return; } // onOpen callback if (typeof this.options.onOpen === 'function') { this.options.onOpen.call(this, this.$origin[0]); } this.isOpen = true; this.wrapper.classList.add('open'); document.body.addEventListener('click', this._handleDocumentClickBound, true); document.body.addEventListener('touchend', this._handleDocumentClickBound); } /** * Close Tap Target */ }, { key: 'close', value: function close() { if (!this.isOpen) { return; } // onClose callback if (typeof this.options.onClose === 'function') { this.options.onClose.call(this, this.$origin[0]); } this.isOpen = false; this.wrapper.classList.remove('open'); document.body.removeEventListener('click', this._handleDocumentClickBound, true); document.body.removeEventListener('touchend', this._handleDocumentClickBound); } }], [{ key: 'init', value: function init(els, options) { return _get(TapTarget.__proto__ || Object.getPrototypeOf(TapTarget), '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_TapTarget; } }, { key: 'defaults', get: function () { return _defaults; } }]); return TapTarget; }(Component); M.TapTarget = TapTarget; if (M.jQueryLoaded) { M.initializeJqueryWrapper(TapTarget, 'tapTarget', 'M_TapTarget'); } })(cash);