diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 7493713fd..914222276 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -1343,7 +1343,8 @@ class Server(object): "js/lib/md5.min.js", "js/lib/bootstrap-slider-knockout-binding.js", "js/lib/loglevel.min.js", - "js/lib/sockjs.js" + "js/lib/sockjs.js", + "js/lib/ResizeSensor.js" ] js_client = [ "js/app/client/base.js", diff --git a/src/octoprint/static/js/app/bindings/copywidth.js b/src/octoprint/static/js/app/bindings/copywidth.js index 6a04a86b4..ef98a53d7 100644 --- a/src/octoprint/static/js/app/bindings/copywidth.js +++ b/src/octoprint/static/js/app/bindings/copywidth.js @@ -2,15 +2,17 @@ ko.bindingHandlers.copyWidth = { init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { var node = ko.bindingHandlers.copyWidth._getReferenceNode(element, valueAccessor); ko.bindingHandlers.copyWidth._setWidth(node, element); + ko.bindingHandlers.copyWidth._addResizeSensor(node, element); }, update: function(element, valueAccessor, allBindings, viewModel, bindingContext) { var node = ko.bindingHandlers.copyWidth._getReferenceNode(element, valueAccessor); ko.bindingHandlers.copyWidth._setWidth(node, element); + ko.bindingHandlers.copyWidth._addResizeSensor(node, element); }, _setWidth: function(node, element) { var width = node.width(); if (!width) return; - if ($(element).width() == width) return; + if ($(element).width() === width) return; element.style.width = width + "px"; }, _getReferenceNode: function(element, valueAccessor) { @@ -21,7 +23,7 @@ ko.bindingHandlers.copyWidth = { var node = $(element); while (parts.length > 0) { var part = parts.shift(); - if (part == ":parent") { + if (part === ":parent") { node = node.parent(); } else { var selector = part; @@ -33,6 +35,16 @@ ko.bindingHandlers.copyWidth = { } } return node; + }, + _addResizeSensor: function(node, element) { + if (element.copyWidthSensor) { + element.copyWidthSensor.detach(); + delete element.copyWidthSensor; + } + element.copyWidthSensor = new ResizeSensor(node, function() { + log.info("size of", node, "changed, width is now", node.width(), "this is", this); + ko.bindingHandlers.copyWidth._setWidth(node, element); + }); } }; diff --git a/src/octoprint/static/js/lib/ResizeSensor.js b/src/octoprint/static/js/lib/ResizeSensor.js new file mode 100644 index 000000000..2ee0d5fa4 --- /dev/null +++ b/src/octoprint/static/js/lib/ResizeSensor.js @@ -0,0 +1,263 @@ +'use strict'; + +/** + * Copyright Marc J. Schmidt. See the LICENSE file at the top-level + * directory of this distribution and at + * https://github.com/marcj/css-element-queries/blob/master/LICENSE. + */ +(function (root, factory) { + if (typeof define === "function" && define.amd) { + define(factory); + } else if (typeof exports === "object") { + module.exports = factory(); + } else { + root.ResizeSensor = factory(); + } +}(typeof window !== 'undefined' ? window : this, function () { + + // Make sure it does not throw in a SSR (Server Side Rendering) situation + if (typeof window === "undefined") { + return null; + } + // Only used for the dirty checking, so the event callback count is limited to max 1 call per fps per sensor. + // In combination with the event based resize sensor this saves cpu time, because the sensor is too fast and + // would generate too many unnecessary events. + var requestAnimationFrame = window.requestAnimationFrame || + window.mozRequestAnimationFrame || + window.webkitRequestAnimationFrame || + function (fn) { + return window.setTimeout(fn, 20); + }; + + /** + * Iterate over each of the provided element(s). + * + * @param {HTMLElement|HTMLElement[]} elements + * @param {Function} callback + */ + function forEachElement(elements, callback){ + var elementsType = Object.prototype.toString.call(elements); + var isCollectionTyped = ('[object Array]' === elementsType + || ('[object NodeList]' === elementsType) + || ('[object HTMLCollection]' === elementsType) + || ('[object Object]' === elementsType) + || ('undefined' !== typeof jQuery && elements instanceof jQuery) //jquery + || ('undefined' !== typeof Elements && elements instanceof Elements) //mootools + ); + var i = 0, j = elements.length; + if (isCollectionTyped) { + for (; i < j; i++) { + callback(elements[i]); + } + } else { + callback(elements); + } + } + + /** + * Get element size + * @param {HTMLElement} element + * @returns {Object} {width, height} + */ + function getElementSize(element) { + if (!element.getBoundingClientRect) { + return { + width: element.offsetWidth, + height: element.offsetHeight + } + } + + var rect = element.getBoundingClientRect(); + return { + width: Math.round(rect.width), + height: Math.round(rect.height) + } + } + + /** + * Class for dimension change detection. + * + * @param {Element|Element[]|Elements|jQuery} element + * @param {Function} callback + * + * @constructor + */ + var ResizeSensor = function(element, callback) { + /** + * + * @constructor + */ + function EventQueue() { + var q = []; + this.add = function(ev) { + q.push(ev); + }; + + var i, j; + this.call = function() { + for (i = 0, j = q.length; i < j; i++) { + q[i].call(); + } + }; + + this.remove = function(ev) { + var newQueue = []; + for(i = 0, j = q.length; i < j; i++) { + if(q[i] !== ev) newQueue.push(q[i]); + } + q = newQueue; + }; + + this.length = function() { + return q.length; + } + } + + /** + * + * @param {HTMLElement} element + * @param {Function} resized + */ + function attachResizeEvent(element, resized) { + if (!element) return; + if (element.resizedAttached) { + element.resizedAttached.add(resized); + return; + } + + element.resizedAttached = new EventQueue(); + element.resizedAttached.add(resized); + + element.resizeSensor = document.createElement('div'); + element.resizeSensor.dir = 'ltr'; + element.resizeSensor.className = 'resize-sensor'; + var style = 'position: absolute; left: -10px; top: -10px; right: 0; bottom: 0; overflow: hidden; z-index: -1; visibility: hidden;'; + var styleChild = 'position: absolute; left: 0; top: 0; transition: 0s;'; + + element.resizeSensor.style.cssText = style; + element.resizeSensor.innerHTML = + '
' + + '