UNPKG

@cision/react-container-query

Version:
1,468 lines (1,225 loc) 77.3 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("react"), require("react-dom")); else if(typeof define === 'function' && define.amd) define(["react", "react-dom"], factory); else if(typeof exports === 'object') exports["ReactContainerQuery"] = factory(require("react"), require("react-dom")); else root["ReactContainerQuery"] = factory(root["React"], root["ReactDOM"]); })(typeof self !== 'undefined' ? self : this, function(__WEBPACK_EXTERNAL_MODULE_0__, __WEBPACK_EXTERNAL_MODULE_5__) { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 4); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE_0__; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function matchQueries(rules) { var entries = []; for (var _i = 0, _a = Object.keys(rules); _i < _a.length; _i++) { var className = _a[_i]; var rule = rules[className]; entries.push({ minWidth: rule.minWidth != null ? rule.minWidth : 0, maxWidth: rule.maxWidth != null ? rule.maxWidth : Infinity, minHeight: rule.minHeight != null ? rule.minHeight : 0, maxHeight: rule.maxHeight != null ? rule.maxHeight : Infinity, className: className, }); } return function (_a) { var height = _a.height, width = _a.width; var classNameMap = {}; for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) { var _b = entries_1[_i], className = _b.className, minWidth = _b.minWidth, maxWidth = _b.maxWidth, minHeight = _b.minHeight, maxHeight = _b.maxHeight; if (height != null && width != null) { classNameMap[className] = (minWidth <= width && width <= maxWidth && minHeight <= height && height <= maxHeight); } else if (height == null && width != null) { classNameMap[className] = minWidth <= width && width <= maxWidth; } else if (height != null && width == null) { classNameMap[className] = minHeight <= height && height <= maxHeight; } else { classNameMap[className] = true; } } return classNameMap; }; } exports.default = matchQueries; //# sourceMappingURL=matchQueries.js.map /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var utils = module.exports = {}; /** * Loops through the collection and calls the callback for each element. if the callback returns truthy, the loop is broken and returns the same value. * @public * @param {*} collection The collection to loop through. Needs to have a length property set and have indices set from 0 to length - 1. * @param {function} callback The callback to be called for each element. The element will be given as a parameter to the callback. If this callback returns truthy, the loop is broken and the same value is returned. * @returns {*} The value that a callback has returned (if truthy). Otherwise nothing. */ utils.forEach = function(collection, callback) { for(var i = 0; i < collection.length; i++) { var result = callback(collection[i]); if(result) { return result; } } }; /***/ }), /* 3 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var detector = module.exports = {}; detector.isIE = function(version) { function isAnyIeVersion() { var agent = navigator.userAgent.toLowerCase(); return agent.indexOf("msie") !== -1 || agent.indexOf("trident") !== -1 || agent.indexOf(" edge/") !== -1; } if(!isAnyIeVersion()) { return false; } if(!version) { return true; } //Shamelessly stolen from https://gist.github.com/padolsey/527683 var ieVersion = (function(){ var undef, v = 3, div = document.createElement("div"), all = div.getElementsByTagName("i"); do { div.innerHTML = "<!--[if gt IE " + (++v) + "]><i></i><![endif]-->"; } while (all[0]); return v > 4 ? v : undef; }()); return version === ieVersion; }; detector.isLegacyOpera = function() { return !!window.opera; }; /***/ }), /* 4 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var __extends = undefined && undefined.__extends || function () { var _extendStatics = function extendStatics(d, b) { _extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; } || function (d, b) { for (var p in b) { if (b.hasOwnProperty(p)) d[p] = b[p]; } }; return _extendStatics(d, b); }; return function (d, b) { _extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; }(); var __assign = undefined && undefined.__assign || function () { __assign = Object.assign || function (t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var React = __webpack_require__(0); var react_1 = __webpack_require__(0); var ReactDOM = __webpack_require__(5); var matchQueries_1 = __webpack_require__(1); var ContainerQueryCore_1 = __webpack_require__(6); /** * <ContainerQuery query={query} initialSize={{width: 123, height: 456}}> * {(params) => { * <div className={classname(params)}></div> * }} * </ContainerQuery> */ exports.ContainerQuery = function (_a) { var renderContents = _a.children, query = _a.query, initialSize = _a.initialSize; var _b = react_1.useState(initialSize ? matchQueries_1.default(query)(initialSize) : {}), params = _b[0], setParams = _b[1]; var _c = react_1.useState(null), prevQuery = _c[0], setPrevQuery = _c[1]; var elementRef = react_1.useRef(null); var cqCoreRef = react_1.useRef(null); react_1.useEffect(function () { cqCoreRef.current = new ContainerQueryCore_1.default(query, function (params) { if (elementRef.current) { setParams(params); } }); return function () { cqCoreRef.current = null; }; }, [elementRef, query]); react_1.useEffect(function () { if (elementRef.current && query !== prevQuery) { cqCoreRef.current.observe(elementRef.current); setPrevQuery(query); } return function () {}; }, [elementRef, prevQuery, query]); return renderContents(params, elementRef); }; ; function applyContainerQuery(Component, query, initialSize) { var _a; return _a = /** @class */function (_super) { __extends(ContainerQuery, _super); function ContainerQuery(props) { var _this = _super.call(this, props) || this; _this.cqCore = null; _this.state = { params: initialSize ? matchQueries_1.default(query)(initialSize) : {} }; return _this; } ContainerQuery.prototype.componentDidMount = function () { var _this = this; this.cqCore = new ContainerQueryCore_1.default(query, function (params) { _this.setState({ params: params }); }); this.cqCore.observe(ReactDOM.findDOMNode(this)); }; ContainerQuery.prototype.componentDidUpdate = function () { this.cqCore.observe(ReactDOM.findDOMNode(this)); }; ContainerQuery.prototype.componentWillUnmount = function () { this.cqCore.disconnect(); this.cqCore = null; }; ContainerQuery.prototype.render = function () { return React.createElement(Component, __assign({}, this.props, { containerQuery: this.state.params })); }; return ContainerQuery; }(React.Component), _a.displayName = Component.displayName ? "ContainerQuery(" + Component.displayName + ")" : 'ContainerQuery', _a; } exports.applyContainerQuery = applyContainerQuery; //# sourceMappingURL=index.js.map /***/ }), /* 5 */ /***/ (function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE_5__; /***/ }), /* 6 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var resize_observer_lite_1 = __webpack_require__(7); var matchQueries_1 = __webpack_require__(1); var isShallowEqual_1 = __webpack_require__(19); var ContainerQueryCore = /** @class */function () { function ContainerQueryCore(query, callback) { var _this = this; this.result = {}; this.rol = new resize_observer_lite_1.default(function (size) { var result = matchQueries_1.default(query)(size); if (!isShallowEqual_1.default(_this.result, result)) { callback(result); _this.result = result; } }); } ContainerQueryCore.prototype.observe = function (element) { this.rol.observe(element); }; ContainerQueryCore.prototype.disconnect = function () { this.rol.disconnect(); }; return ContainerQueryCore; }(); exports.default = ContainerQueryCore; //# sourceMappingURL=ContainerQueryCore.js.map /***/ }), /* 7 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var elementResizeDetectorMaker = __webpack_require__(8); var ResizeObserverLite = /** @class */ (function () { function ResizeObserverLite(handler) { var _this = this; this.handler = handler; this.listenedElement = null; this.hasResizeObserver = typeof window.ResizeObserver !== 'undefined'; if (this.hasResizeObserver) { this.rz = new ResizeObserver(function (entries) { _this.handler(getSize(entries[0].target)); }); } else { this.erd = elementResizeDetectorMaker({ strategy: 'scroll' }); } } ResizeObserverLite.prototype.observe = function (element) { var _this = this; if (this.listenedElement !== element) { if (this.listenedElement) { this.disconnect(); } if (element) { if (this.hasResizeObserver) { this.rz.observe(element); } else { this.erd.listenTo(element, function (element) { _this.handler(getSize(element)); }); } } this.listenedElement = element; } }; ResizeObserverLite.prototype.disconnect = function () { if (this.listenedElement) { if (this.hasResizeObserver) { this.rz.disconnect(); } else { this.erd.uninstall(this.listenedElement); } this.listenedElement = null; } }; return ResizeObserverLite; }()); exports.default = ResizeObserverLite; function getSize(element) { return { width: getNumber(window.getComputedStyle(element)['width']), height: getNumber(window.getComputedStyle(element)['height']) }; } function getNumber(str) { var m = /^([0-9\.]+)px$/.exec(str); return m ? parseFloat(m[1]) : 0; } //# sourceMappingURL=index.js.map /***/ }), /* 8 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var forEach = __webpack_require__(2).forEach; var elementUtilsMaker = __webpack_require__(9); var listenerHandlerMaker = __webpack_require__(10); var idGeneratorMaker = __webpack_require__(11); var idHandlerMaker = __webpack_require__(12); var reporterMaker = __webpack_require__(13); var browserDetector = __webpack_require__(3); var batchProcessorMaker = __webpack_require__(14); var stateHandler = __webpack_require__(16); //Detection strategies. var objectStrategyMaker = __webpack_require__(17); var scrollStrategyMaker = __webpack_require__(18); function isCollection(obj) { return Array.isArray(obj) || obj.length !== undefined; } function toArray(collection) { if (!Array.isArray(collection)) { var array = []; forEach(collection, function (obj) { array.push(obj); }); return array; } else { return collection; } } function isElement(obj) { return obj && obj.nodeType === 1; } /** * @typedef idHandler * @type {object} * @property {function} get Gets the resize detector id of the element. * @property {function} set Generate and sets the resize detector id of the element. */ /** * @typedef Options * @type {object} * @property {boolean} callOnAdd Determines if listeners should be called when they are getting added. Default is true. If true, the listener is guaranteed to be called when it has been added. If false, the listener will not be guarenteed to be called when it has been added (does not prevent it from being called). * @property {idHandler} idHandler A custom id handler that is responsible for generating, setting and retrieving id's for elements. If not provided, a default id handler will be used. * @property {reporter} reporter A custom reporter that handles reporting logs, warnings and errors. If not provided, a default id handler will be used. If set to false, then nothing will be reported. * @property {boolean} debug If set to true, the the system will report debug messages as default for the listenTo method. */ /** * Creates an element resize detector instance. * @public * @param {Options?} options Optional global options object that will decide how this instance will work. */ module.exports = function(options) { options = options || {}; //idHandler is currently not an option to the listenTo function, so it should not be added to globalOptions. var idHandler; if (options.idHandler) { // To maintain compatability with idHandler.get(element, readonly), make sure to wrap the given idHandler // so that readonly flag always is true when it's used here. This may be removed next major version bump. idHandler = { get: function (element) { return options.idHandler.get(element, true); }, set: options.idHandler.set }; } else { var idGenerator = idGeneratorMaker(); var defaultIdHandler = idHandlerMaker({ idGenerator: idGenerator, stateHandler: stateHandler }); idHandler = defaultIdHandler; } //reporter is currently not an option to the listenTo function, so it should not be added to globalOptions. var reporter = options.reporter; if(!reporter) { //If options.reporter is false, then the reporter should be quiet. var quiet = reporter === false; reporter = reporterMaker(quiet); } //batchProcessor is currently not an option to the listenTo function, so it should not be added to globalOptions. var batchProcessor = getOption(options, "batchProcessor", batchProcessorMaker({ reporter: reporter })); //Options to be used as default for the listenTo function. var globalOptions = {}; globalOptions.callOnAdd = !!getOption(options, "callOnAdd", true); globalOptions.debug = !!getOption(options, "debug", false); var eventListenerHandler = listenerHandlerMaker(idHandler); var elementUtils = elementUtilsMaker({ stateHandler: stateHandler }); //The detection strategy to be used. var detectionStrategy; var desiredStrategy = getOption(options, "strategy", "object"); var strategyOptions = { reporter: reporter, batchProcessor: batchProcessor, stateHandler: stateHandler, idHandler: idHandler }; if(desiredStrategy === "scroll") { if (browserDetector.isLegacyOpera()) { reporter.warn("Scroll strategy is not supported on legacy Opera. Changing to object strategy."); desiredStrategy = "object"; } else if (browserDetector.isIE(9)) { reporter.warn("Scroll strategy is not supported on IE9. Changing to object strategy."); desiredStrategy = "object"; } } if(desiredStrategy === "scroll") { detectionStrategy = scrollStrategyMaker(strategyOptions); } else if(desiredStrategy === "object") { detectionStrategy = objectStrategyMaker(strategyOptions); } else { throw new Error("Invalid strategy name: " + desiredStrategy); } //Calls can be made to listenTo with elements that are still being installed. //Also, same elements can occur in the elements list in the listenTo function. //With this map, the ready callbacks can be synchronized between the calls //so that the ready callback can always be called when an element is ready - even if //it wasn't installed from the function itself. var onReadyCallbacks = {}; /** * Makes the given elements resize-detectable and starts listening to resize events on the elements. Calls the event callback for each event for each element. * @public * @param {Options?} options Optional options object. These options will override the global options. Some options may not be overriden, such as idHandler. * @param {element[]|element} elements The given array of elements to detect resize events of. Single element is also valid. * @param {function} listener The callback to be executed for each resize event for each element. */ function listenTo(options, elements, listener) { function onResizeCallback(element) { var listeners = eventListenerHandler.get(element); forEach(listeners, function callListenerProxy(listener) { listener(element); }); } function addListener(callOnAdd, element, listener) { eventListenerHandler.add(element, listener); if(callOnAdd) { listener(element); } } //Options object may be omitted. if(!listener) { listener = elements; elements = options; options = {}; } if(!elements) { throw new Error("At least one element required."); } if(!listener) { throw new Error("Listener required."); } if (isElement(elements)) { // A single element has been passed in. elements = [elements]; } else if (isCollection(elements)) { // Convert collection to array for plugins. // TODO: May want to check so that all the elements in the collection are valid elements. elements = toArray(elements); } else { return reporter.error("Invalid arguments. Must be a DOM element or a collection of DOM elements."); } var elementsReady = 0; var callOnAdd = getOption(options, "callOnAdd", globalOptions.callOnAdd); var onReadyCallback = getOption(options, "onReady", function noop() {}); var debug = getOption(options, "debug", globalOptions.debug); forEach(elements, function attachListenerToElement(element) { if (!stateHandler.getState(element)) { stateHandler.initState(element); idHandler.set(element); } var id = idHandler.get(element); debug && reporter.log("Attaching listener to element", id, element); if(!elementUtils.isDetectable(element)) { debug && reporter.log(id, "Not detectable."); if(elementUtils.isBusy(element)) { debug && reporter.log(id, "System busy making it detectable"); //The element is being prepared to be detectable. Do not make it detectable. //Just add the listener, because the element will soon be detectable. addListener(callOnAdd, element, listener); onReadyCallbacks[id] = onReadyCallbacks[id] || []; onReadyCallbacks[id].push(function onReady() { elementsReady++; if(elementsReady === elements.length) { onReadyCallback(); } }); return; } debug && reporter.log(id, "Making detectable..."); //The element is not prepared to be detectable, so do prepare it and add a listener to it. elementUtils.markBusy(element, true); return detectionStrategy.makeDetectable({ debug: debug }, element, function onElementDetectable(element) { debug && reporter.log(id, "onElementDetectable"); if (stateHandler.getState(element)) { elementUtils.markAsDetectable(element); elementUtils.markBusy(element, false); detectionStrategy.addListener(element, onResizeCallback); addListener(callOnAdd, element, listener); // Since the element size might have changed since the call to "listenTo", we need to check for this change, // so that a resize event may be emitted. // Having the startSize object is optional (since it does not make sense in some cases such as unrendered elements), so check for its existance before. // Also, check the state existance before since the element may have been uninstalled in the installation process. var state = stateHandler.getState(element); if (state && state.startSize) { var width = element.offsetWidth; var height = element.offsetHeight; if (state.startSize.width !== width || state.startSize.height !== height) { onResizeCallback(element); } } if(onReadyCallbacks[id]) { forEach(onReadyCallbacks[id], function(callback) { callback(); }); } } else { // The element has been unisntalled before being detectable. debug && reporter.log(id, "Element uninstalled before being detectable."); } delete onReadyCallbacks[id]; elementsReady++; if(elementsReady === elements.length) { onReadyCallback(); } }); } debug && reporter.log(id, "Already detecable, adding listener."); //The element has been prepared to be detectable and is ready to be listened to. addListener(callOnAdd, element, listener); elementsReady++; }); if(elementsReady === elements.length) { onReadyCallback(); } } function uninstall(elements) { if(!elements) { return reporter.error("At least one element is required."); } if (isElement(elements)) { // A single element has been passed in. elements = [elements]; } else if (isCollection(elements)) { // Convert collection to array for plugins. // TODO: May want to check so that all the elements in the collection are valid elements. elements = toArray(elements); } else { return reporter.error("Invalid arguments. Must be a DOM element or a collection of DOM elements."); } forEach(elements, function (element) { eventListenerHandler.removeAllListeners(element); detectionStrategy.uninstall(element); stateHandler.cleanState(element); }); } return { listenTo: listenTo, removeListener: eventListenerHandler.removeListener, removeAllListeners: eventListenerHandler.removeAllListeners, uninstall: uninstall }; }; function getOption(options, name, defaultValue) { var value = options[name]; if((value === undefined || value === null) && defaultValue !== undefined) { return defaultValue; } return value; } /***/ }), /* 9 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; module.exports = function(options) { var getState = options.stateHandler.getState; /** * Tells if the element has been made detectable and ready to be listened for resize events. * @public * @param {element} The element to check. * @returns {boolean} True or false depending on if the element is detectable or not. */ function isDetectable(element) { var state = getState(element); return state && !!state.isDetectable; } /** * Marks the element that it has been made detectable and ready to be listened for resize events. * @public * @param {element} The element to mark. */ function markAsDetectable(element) { getState(element).isDetectable = true; } /** * Tells if the element is busy or not. * @public * @param {element} The element to check. * @returns {boolean} True or false depending on if the element is busy or not. */ function isBusy(element) { return !!getState(element).busy; } /** * Marks the object is busy and should not be made detectable. * @public * @param {element} element The element to mark. * @param {boolean} busy If the element is busy or not. */ function markBusy(element, busy) { getState(element).busy = !!busy; } return { isDetectable: isDetectable, markAsDetectable: markAsDetectable, isBusy: isBusy, markBusy: markBusy }; }; /***/ }), /* 10 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; module.exports = function(idHandler) { var eventListeners = {}; /** * Gets all listeners for the given element. * @public * @param {element} element The element to get all listeners for. * @returns All listeners for the given element. */ function getListeners(element) { var id = idHandler.get(element); if (id === undefined) { return []; } return eventListeners[id] || []; } /** * Stores the given listener for the given element. Will not actually add the listener to the element. * @public * @param {element} element The element that should have the listener added. * @param {function} listener The callback that the element has added. */ function addListener(element, listener) { var id = idHandler.get(element); if(!eventListeners[id]) { eventListeners[id] = []; } eventListeners[id].push(listener); } function removeListener(element, listener) { var listeners = getListeners(element); for (var i = 0, len = listeners.length; i < len; ++i) { if (listeners[i] === listener) { listeners.splice(i, 1); break; } } } function removeAllListeners(element) { var listeners = getListeners(element); if (!listeners) { return; } listeners.length = 0; } return { get: getListeners, add: addListener, removeListener: removeListener, removeAllListeners: removeAllListeners }; }; /***/ }), /* 11 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; module.exports = function() { var idCount = 1; /** * Generates a new unique id in the context. * @public * @returns {number} A unique id in the context. */ function generate() { return idCount++; } return { generate: generate }; }; /***/ }), /* 12 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; module.exports = function(options) { var idGenerator = options.idGenerator; var getState = options.stateHandler.getState; /** * Gets the resize detector id of the element. * @public * @param {element} element The target element to get the id of. * @returns {string|number|null} The id of the element. Null if it has no id. */ function getId(element) { var state = getState(element); if (state && state.id !== undefined) { return state.id; } return null; } /** * Sets the resize detector id of the element. Requires the element to have a resize detector state initialized. * @public * @param {element} element The target element to set the id of. * @returns {string|number|null} The id of the element. */ function setId(element) { var state = getState(element); if (!state) { throw new Error("setId required the element to have a resize detection state."); } var id = idGenerator.generate(); state.id = id; return id; } return { get: getId, set: setId }; }; /***/ }), /* 13 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /* global console: false */ /** * Reporter that handles the reporting of logs, warnings and errors. * @public * @param {boolean} quiet Tells if the reporter should be quiet or not. */ module.exports = function(quiet) { function noop() { //Does nothing. } var reporter = { log: noop, warn: noop, error: noop }; if(!quiet && window.console) { var attachFunction = function(reporter, name) { //The proxy is needed to be able to call the method with the console context, //since we cannot use bind. reporter[name] = function reporterProxy() { var f = console[name]; if (f.apply) { //IE9 does not support console.log.apply :) f.apply(console, arguments); } else { for (var i = 0; i < arguments.length; i++) { f(arguments[i]); } } }; }; attachFunction(reporter, "log"); attachFunction(reporter, "warn"); attachFunction(reporter, "error"); } return reporter; }; /***/ }), /* 14 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var utils = __webpack_require__(15); module.exports = function batchProcessorMaker(options) { options = options || {}; var reporter = options.reporter; var asyncProcess = utils.getOption(options, "async", true); var autoProcess = utils.getOption(options, "auto", true); if(autoProcess && !asyncProcess) { reporter && reporter.warn("Invalid options combination. auto=true and async=false is invalid. Setting async=true."); asyncProcess = true; } var batch = Batch(); var asyncFrameHandler; var isProcessing = false; function addFunction(level, fn) { if(!isProcessing && autoProcess && asyncProcess && batch.size() === 0) { // Since this is async, it is guaranteed to be executed after that the fn is added to the batch. // This needs to be done before, since we're checking the size of the batch to be 0. processBatchAsync(); } batch.add(level, fn); } function processBatch() { // Save the current batch, and create a new batch so that incoming functions are not added into the currently processing batch. // Continue processing until the top-level batch is empty (functions may be added to the new batch while processing, and so on). isProcessing = true; while (batch.size()) { var processingBatch = batch; batch = Batch(); processingBatch.process(); } isProcessing = false; } function forceProcessBatch(localAsyncProcess) { if (isProcessing) { return; } if(localAsyncProcess === undefined) { localAsyncProcess = asyncProcess; } if(asyncFrameHandler) { cancelFrame(asyncFrameHandler); asyncFrameHandler = null; } if(localAsyncProcess) { processBatchAsync(); } else { processBatch(); } } function processBatchAsync() { asyncFrameHandler = requestFrame(processBatch); } function clearBatch() { batch = {}; batchSize = 0; topLevel = 0; bottomLevel = 0; } function cancelFrame(listener) { // var cancel = window.cancelAnimationFrame || window.mozCancelAnimationFrame || window.webkitCancelAnimationFrame || window.clearTimeout; var cancel = clearTimeout; return cancel(listener); } function requestFrame(callback) { // var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || function(fn) { return window.setTimeout(fn, 20); }; var raf = function(fn) { return setTimeout(fn, 0); }; return raf(callback); } return { add: addFunction, force: forceProcessBatch }; }; function Batch() { var batch = {}; var size = 0; var topLevel = 0; var bottomLevel = 0; function add(level, fn) { if(!fn) { fn = level; level = 0; } if(level > topLevel) { topLevel = level; } else if(level < bottomLevel) { bottomLevel = level; } if(!batch[level]) { batch[level] = []; } batch[level].push(fn); size++; } function process() { for(var level = bottomLevel; level <= topLevel; level++) { var fns = batch[level]; for(var i = 0; i < fns.length; i++) { var fn = fns[i]; fn(); } } } function getSize() { return size; } return { add: add, process: process, size: getSize }; } /***/ }), /* 15 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var utils = module.exports = {}; utils.getOption = getOption; function getOption(options, name, defaultValue) { var value = options[name]; if((value === undefined || value === null) && defaultValue !== undefined) { return defaultValue; } return value; } /***/ }), /* 16 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var prop = "_erd"; function initState(element) { element[prop] = {}; return getState(element); } function getState(element) { return element[prop]; } function cleanState(element) { delete element[prop]; } module.exports = { initState: initState, getState: getState, cleanState: cleanState }; /***/ }), /* 17 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Resize detection strategy that injects objects to elements in order to detect resize events. * Heavily inspired by: http://www.backalleycoder.com/2013/03/18/cross-browser-event-based-element-resize-detection/ */ var browserDetector = __webpack_require__(3); module.exports = function(options) { options = options || {}; var reporter = options.reporter; var batchProcessor = options.batchProcessor; var getState = options.stateHandler.getState; if(!reporter) { throw new Error("Missing required dependency: reporter."); } /** * Adds a resize event listener to the element. * @public * @param {element} element The element that should have the listener added. * @param {function} listener The listener callback to be called for each resize event of the element. The element will be given as a parameter to the listener callback. */ function addListener(element, listener) { if(!getObject(element)) { throw new Error("Element is not detectable by this strategy."); } function listenerProxy() { listener(element); } if(browserDetector.isIE(8)) { //IE 8 does not support object, but supports the resize event directly on elements. getState(element).object = { proxy: listenerProxy }; element.attachEvent("onresize", listenerProxy); } else { var object = getObject(element); object.contentDocument.defaultView.addEventListener("resize", listenerProxy); } } /** * Makes an element detectable and ready to be listened for resize events. Will call the callback when the element is ready to be listened for resize changes. * @private * @param {object} options Optional options object. * @param {element} element The element to make detectable * @param {function} callback The callback to be called when the element is ready to be listened for resize changes. Will be called with the element as first parameter. */ function makeDetectable(options, element, callback) { if (!callback) { callback = element; element = options; options = null; } options = options || {}; var debug = options.debug; function injectObject(element, callback) { var OBJECT_STYLE = "display: block; position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: none; padding: 0; margin: 0; opacity: 0; z-index: -1000; pointer-events: none;"; //The target element needs to be positioned (everything except static) so the absolute positioned object will be positioned relative to the target element. // Position altering may be performed directly or on object load, depending on if style resolution is possible directly or not. var positionCheckPerformed = false; // The element may not yet be attached to the DOM, and therefore the style object may be empty in some browsers. // Since the style object is a reference, it will be updated as soon as the element is attached to the DOM. var style = window.getComputedStyle(element); var width = element.offsetWidth; var height = element.offsetHeight; getState(element).startSize = { width: width, height: height }; function mutateDom() { function alterPositionStyles() { if(style.position === "static") { element.style.position = "relative"; var removeRelativeStyles = function(reporter, element, style, property) { function getNumericalValue(value) { return value.replace(/[^-\d\.]/g, ""); } var value = style[property]; if(value !== "auto" && getNumericalValue(value) !== "0") { reporter.warn("An element that is positioned static has style." + property + "=" + value + " which is ignored due to the static positioning. The element will need to be positioned relative, so the style." + property + " will be set to 0. Element: ", element); element.style[property] = 0; } }; //Check so that there are no accidental styles that will make the element styled differently now that is is relative. //If there are any, set them to 0 (this should be okay with the user since the style properties did nothing before [since the element was positioned static] anyway). removeRelativeStyles(reporter, element, style, "top"); removeRelativeStyles(reporter, element, style, "right"); removeRelativeStyles(reporter, element, style, "bottom"); removeRelativeStyles(reporter, element, style, "left"); } } function onObjectLoad() { // The object has been loaded, which means that the element now is guaranteed to be attached to the DOM. if (!positionCheckPerformed) { alterPositionStyles(); } /*jshint validthis: true */ function getDocument(element, callback) { //Opera 12 seem to call the object.onload before the actual document has been created. //So if it is not present, poll it with an timeout until it is present. //TODO: Could maybe be handled better with object.onreadystatechange or similar. if(!element.contentDocument) { setTimeout(function checkForObjectDocument() { getDocument(element, callback); }, 100); return; } callback(element.contentDocument); } //Mutating the object element here seems to fire another load event. //Mutating the inner document of the object element is fine though. var objectElement = this; //Create the style element to be added to the object. getDocument(objectElement, function onObjectDocumentReady(objectDocument) { //Notify that the element is ready to be listened to. callback(element); }); } // The element may be detached from the DOM, and some browsers does not support style resolving of detached elements. // The alterPositionStyles needs to be delayed until we know the element has been attached to the DOM (which we are sure of when the onObjectLoad has been fired), if style resolution is not possible. if (style.position !== "") { alterPositionStyles(style); positionCheckPerformed = true; } //Add an object element as a child to the target element that will be listened to for resize events. var object = document.createElement("object"); object.style.cssText = OBJECT_STYLE; object.tabIndex = -1; object.type = "text/html"; object.onload = onObjectLoad; //Safari: This must occur before adding the object to the DOM. //IE: Does not like that this happens before, even if it is also added after. if(!browserDetector.isIE()) { object.data = "about:blank"; } element.appendChild(object); getState(element).object = object; //IE: This must occur after adding the object to the DOM. if(browserDetector.isIE()) { object.data = "about:blank"; } } if(batchProcessor) { batchProcessor.add(mutateDom); } else { mutateDom(); } } if(browserDetector.isIE(8)) { //IE 8 does not support objects properly. Luckily they do support the resize event. //So do not inject the object and notify that the element is already ready to be listened to. //The event handler for the resize event is attached in the utils.addListener instead. callback(element); } else { injectObject(element, callback); } } /** * Returns the child object of the target element. * @private * @param {element} element The target element. * @returns The object element of the target. */ function getObject(element) { return getState(element).object; } function uninstall(element) { if(browserDetector.isIE(8)) { element.detachEvent("onresize", getState(element).object.proxy); } else { element.removeChild(getObject(element)); } delete getState(element).object; } return { makeDetectable: makeDetectable, addListener: addListener, uninstall: uninstall }; }; /***/ }), /* 18 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Resize detection strategy that injects divs to elements in order to detect resize events on scroll events. * Heavily inspired by: https://github.com/marcj/css-element-queries/blob/master/src/ResizeSensor.js */ var forEach = __webpack_require__(2).forEach; module.exports = function(options) { options = options || {}; var reporter = options.reporter; var batchProcessor = options.batchProcessor; var getState = options.stateHandler.getState; var hasState = options.stateHandler.hasState; var idHandler = options.idHandler; if (!batchProcessor) { throw new Error("Missing required dependency: batchProcessor"); } if (!reporter) { throw new Error("Missing required dependency: reporter."); } //TODO: Could this perhaps be done at installation time? var scrollbarSizes = getScrollbarSizes(); // Inject the scrollbar styling that prevents them from appearing sometimes in Chrome. // The injected container needs to have a class, so that it may be styled with CSS (pseudo elements). var styleId = "erd_scroll_detection_scrollbar_style"; var detectionContainerClass = "erd_scroll_detection_container"; injectScrollStyle(styleId, detectionContainerClass); function getScrollbarSizes() { var width = 500; var height = 500; var child = document.createElement("div"); child.style.cssText = "position: absol