1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 22× 2× 2× 2× 2× 2× 2× 1× 1× 1× 1× 1× 1× 1× 1× 2× 3× 3× 3× 3× 3× 3× 3× 3× 1× 1× 1× 1× 1× 3× 2× 2× | 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.ALLOW_ELEMENT_OVERFLOW_TOUCH = exports.EVENT_AFTER_FIXED_REMOVE = exports.EVENT_BEFORE_FIXED_ADD = undefined; var _typeof2 = require('babel-runtime/helpers/typeof'); var _typeof3 = _interopRequireDefault(_typeof2); var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray'); var _slicedToArray3 = _interopRequireDefault(_slicedToArray2); var _assign = require('babel-runtime/core-js/object/assign'); var _assign2 = _interopRequireDefault(_assign); var _from = require('babel-runtime/core-js/array/from'); var _from2 = _interopRequireDefault(_from); var _set = require('babel-runtime/core-js/set'); var _set2 = _interopRequireDefault(_set); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _Settings = require('../util/Settings'); var _Settings2 = _interopRequireDefault(_Settings); var _debounce = require('./debounce'); var _debounce2 = _interopRequireDefault(_debounce); var _Event = require('../util/Event'); var _Event2 = _interopRequireDefault(_Event); var _Util = require('../util/Util'); var _Util2 = _interopRequireDefault(_Util); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var CLS_FIXED_WINDOW = 'fixed-window-open'; var EVENT_BEFORE_FIXED_ADD = exports.EVENT_BEFORE_FIXED_ADD = 'flexcss.fixedWindow.beforeAdd'; var EVENT_AFTER_FIXED_REMOVE = exports.EVENT_AFTER_FIXED_REMOVE = 'flexcss.fixedWindow.afterRemove'; /** * @type {string} */ var ALLOW_ELEMENT_OVERFLOW_TOUCH = exports.ALLOW_ELEMENT_OVERFLOW_TOUCH = 'data-overflow'; /** * @type {FixedWindow} */ var fixedWindowInstance = void 0; var FixedWindow = function () { function FixedWindow() { (0, _classCallCheck3.default)(this, FixedWindow); this.widgets = []; this.currentScrollTop = 0; this.fixedScreenConstraints = []; this.windowWidth = 0; this.isFixedWindowActive = false; this.touchListener = null; } /** * Adds a constraint to detect if the window needs to be changed when the screensize changes * * @param {Function} widget * @param {Function} fixedBreakpointFn gets a width argument, return true to open a window */ (0, _createClass3.default)(FixedWindow, [{ key: 'addScreenConstraint', value: function addScreenConstraint(widget, fixedBreakpointFn) { this.fixedScreenConstraints[widget] = fixedBreakpointFn; } /** * @returns {null|DestroyableWidget} */ }, { key: 'getCurrentWidget', value: function getCurrentWidget() { return this.widgets.length > 0 ? this.widgets[this.widgets.length - 1] : null; } }, { key: 'getLastFixedWidget', value: function getLastFixedWidget() { var length = this.widgets.length; var widget = null; while (length > 0) { var thisWidget = this.widgets[length - 1]; if (thisWidget.fixed) { widget = thisWidget; break; } length--; } return widget; } /** * @private */ }, { key: '_checkFixedNeeded', value: function _checkFixedNeeded() { var _this = this; if (this.widgets.length === 0) { return; } var widgets = new _set2.default(this.widgets); var widgetsThatRequireFixedWindow = (0, _from2.default)(widgets).some(function (el) { var widget = el.widget; var isFixed = _this.fixedScreenConstraints[widget] && _this.fixedScreenConstraints[widget](_this.windowWidth, _this.getCurrentWidget()); var index = _this.widgets.indexOf(el); _this.widgets[index] = (0, _assign2.default)(el, { fixed: isFixed }); return isFixed; }); if (!widgetsThatRequireFixedWindow) { this._removeFixedContainer(); } else { this._addFixedContainer(); } } /** * @private */ }, { key: '_addFixedContainer', value: function _addFixedContainer() { var _this2 = this; if (this.isFixedWindowActive) { return; } _Event2.default.dispatchAndFire(global.document, EVENT_BEFORE_FIXED_ADD, { detail: this.getCurrentWidget() }); // this causes layout and should be optimized // At lest we write in a batch later _Settings2.default.get().scrollbarUpdateNodes.map(function (n) { var foundProperty = 'paddingRight'; var direction = 1; var node = n; if (n instanceof Array) { var _n = (0, _slicedToArray3.default)(n, 3), whatNode = _n[0], property = _n[1], d = _n[2]; foundProperty = property; node = whatNode; direction = d || 1; } return { node: node, property: foundProperty, value: parseInt(global.getComputedStyle(node)[foundProperty], 10) + (!node.__fixedWindowMod__ ? _Settings2.default.getScrollbarWidth() * direction : 0) + 'px' }; }).forEach(function (d) { d.node.__fixedWindowMod__ = true; d.node.style[d.property] = d.value; }); this.touchListener = function (e) { e.preventDefault(); }; var shouldNotMoveUp = false; var shouldNotMoveDown = false; var neverScroll = false; var lastClientY = 0; this.touchStartListener = function (e) { var _getLastFixedWidget = _this2.getLastFixedWidget(), element = _getLastFixedWidget.element; var closestOverflow = _Util2.default.closestCallback(e.target, function (el) { return el instanceof HTMLTextAreaElement || el.hasAttribute && el.hasAttribute(ALLOW_ELEMENT_OVERFLOW_TOUCH); }); if (closestOverflow && closestOverflow.scrollHeight !== closestOverflow.offsetHeight) { element = closestOverflow; } if (_Util2.default.isPartOfNode(e.target, element)) { neverScroll = element.scrollHeight === element.offsetHeight; lastClientY = e.touches[0].clientY; // never allow scrolling when there is nothing to scroll if (neverScroll) { return false; } if (element.scrollTop === 0) { shouldNotMoveUp = true; } else if (element.scrollHeight === element.scrollTop + element.offsetHeight) { shouldNotMoveDown = true; } } }; global.addEventListener('touchmove', this.touchListener); global.document.body.addEventListener('touchstart', this.touchStartListener); this.touchMoveListener = function (e) { var _getLastFixedWidget2 = _this2.getLastFixedWidget(), element = _getLastFixedWidget2.element; if (_Util2.default.isPartOfNode(e.target, element)) { var clientY = e.touches[0].clientY; var isScrollingDown = lastClientY - clientY > 0; lastClientY = clientY; if (neverScroll) { return false; } if (!shouldNotMoveDown && isScrollingDown || shouldNotMoveDown && !isScrollingDown || !shouldNotMoveDown && !shouldNotMoveUp && !isScrollingDown || shouldNotMoveDown && shouldNotMoveUp && isScrollingDown) { e.stopImmediatePropagation(); } } }; this.touchEndListener = function () { neverScroll = false; shouldNotMoveUp = false; shouldNotMoveDown = false; lastClientY = 0; }; global.document.body.addEventListener('touchmove', this.touchMoveListener); global.document.body.addEventListener('touchend', this.touchEndListener); global.document.documentElement.classList.add(CLS_FIXED_WINDOW); this.isFixedWindowActive = true; } /** * @private */ }, { key: '_removeFixedContainer', value: function _removeFixedContainer() { if (this.isFixedWindowActive) { // cleanup event listeners global.removeEventListener('touchmove', this.touchListener); global.document.body.removeEventListener('touchstart', this.touchStartListener); global.document.body.removeEventListener('touchmove', this.touchMoveListener); global.document.body.removeEventListener('touchend', this.touchEndListener); // reset scrollbar nodes _Settings2.default.get().scrollbarUpdateNodes.forEach(function (node) { if (node instanceof Array) { var _node = (0, _slicedToArray3.default)(node, 2), whatNode = _node[0], property = _node[1]; delete whatNode.__fixedWindowMod__; whatNode.style[property] = ''; } else { delete node.__fixedWindowMod__; node.style.paddingRight = ''; } }); global.document.documentElement.classList.remove(CLS_FIXED_WINDOW); _Event2.default.dispatchAndFire(global.document, EVENT_AFTER_FIXED_REMOVE); this.isFixedWindowActive = false; } } /** * Will close a window when no widgets are opened that need one */ }, { key: 'resizeListener', value: function resizeListener() { this.windowWidth = global.innerWidth; this._checkFixedNeeded(); } /** * @returns {FixedWindow} */ }, { key: 'close', /** * Request a close of the fixed window */ value: function close() { this.widgets.pop(); this._checkFixedNeeded(); if (this.widgets.length === 0) { this._removeFixedContainer(); } } /** * Request to open a fixed windows * @param {Object|DestroyableWidget} instance * @param {HTMLElement} element */ }, { key: 'open', value: function open(instance, element) { var fixed = false; Eif ((typeof instance === 'undefined' ? 'undefined' : (0, _typeof3.default)(instance)) === 'object') { var widget = instance.constructor; var fixedWidget = this.fixedScreenConstraints[instance.constructor]; Iif (widget && fixedWidget) { fixed = fixedWidget(this.windowWidth, instance); } this.widgets.push({ widget: widget, element: element, fixed: fixed }); // open a new window if there is no window active Iif (!this.isFixedWindowActive && fixed) { this._addFixedContainer(); } } } }], [{ key: 'getInstance', value: function getInstance() { if (!fixedWindowInstance) { fixedWindowInstance = new FixedWindow(); fixedWindowInstance.windowWidth = global.innerWidth; var eventHandler = fixedWindowInstance.resizeListener.bind(fixedWindowInstance); global.addEventListener('resize', (0, _debounce2.default)(eventHandler, 500)); global.addEventListener('orientationchange', eventHandler); } return fixedWindowInstance; } }]); return FixedWindow; }(); exports.default = FixedWindow; |