CSS Fix styles for narrow columns
This commit is contained in:
parent
715c92be3f
commit
0934fbfa91
|
@ -0,0 +1,530 @@
|
|||
'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(['./ResizeSensor.js'], factory);
|
||||
} else if (typeof exports === "object") {
|
||||
module.exports = factory(require('./ResizeSensor.js'));
|
||||
} else {
|
||||
root.ElementQueries = factory(root.ResizeSensor);
|
||||
root.ElementQueries.listen();
|
||||
}
|
||||
}(typeof window !== 'undefined' ? window : this, function (ResizeSensor) {
|
||||
|
||||
/**
|
||||
*
|
||||
* @type {Function}
|
||||
* @constructor
|
||||
*/
|
||||
var ElementQueries = function () {
|
||||
//<style> element with our dynamically created styles
|
||||
var cssStyleElement;
|
||||
|
||||
//all rules found for element queries
|
||||
var allQueries = {};
|
||||
|
||||
//association map to identify which selector belongs to a element from the animationstart event.
|
||||
var idToSelectorMapping = [];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param element
|
||||
* @returns {Number}
|
||||
*/
|
||||
function getEmSize(element) {
|
||||
if (!element) {
|
||||
element = document.documentElement;
|
||||
}
|
||||
var fontSize = window.getComputedStyle(element, null).fontSize;
|
||||
return parseFloat(fontSize) || 16;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @copyright https://github.com/Mr0grog/element-query/blob/master/LICENSE
|
||||
*
|
||||
* @param {HTMLElement} element
|
||||
* @param {*} value
|
||||
* @returns {*}
|
||||
*/
|
||||
function convertToPx(element, value) {
|
||||
var numbers = value.split(/\d/);
|
||||
var units = numbers[numbers.length - 1];
|
||||
value = parseFloat(value);
|
||||
switch (units) {
|
||||
case "px":
|
||||
return value;
|
||||
case "em":
|
||||
return value * getEmSize(element);
|
||||
case "rem":
|
||||
return value * getEmSize();
|
||||
// Viewport units!
|
||||
// According to http://quirksmode.org/mobile/tableViewport.html
|
||||
// documentElement.clientWidth/Height gets us the most reliable info
|
||||
case "vw":
|
||||
return value * document.documentElement.clientWidth / 100;
|
||||
case "vh":
|
||||
return value * document.documentElement.clientHeight / 100;
|
||||
case "vmin":
|
||||
case "vmax":
|
||||
var vw = document.documentElement.clientWidth / 100;
|
||||
var vh = document.documentElement.clientHeight / 100;
|
||||
var chooser = Math[units === "vmin" ? "min" : "max"];
|
||||
return value * chooser(vw, vh);
|
||||
default:
|
||||
return value;
|
||||
// for now, not supporting physical units (since they are just a set number of px)
|
||||
// or ex/ch (getting accurate measurements is hard)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {HTMLElement} element
|
||||
* @param {String} id
|
||||
* @constructor
|
||||
*/
|
||||
function SetupInformation(element, id) {
|
||||
this.element = element;
|
||||
var key, option, elementSize, value, actualValue, attrValues, attrValue, attrName;
|
||||
|
||||
var attributes = ['min-width', 'min-height', 'max-width', 'max-height'];
|
||||
|
||||
/**
|
||||
* Extracts the computed width/height and sets to min/max- attribute.
|
||||
*/
|
||||
this.call = function () {
|
||||
// extract current dimensions
|
||||
elementSize = getElementSize(this.element);
|
||||
|
||||
attrValues = {};
|
||||
|
||||
for (key in allQueries[id]) {
|
||||
if (!allQueries[id].hasOwnProperty(key)) {
|
||||
continue;
|
||||
}
|
||||
option = allQueries[id][key];
|
||||
|
||||
value = convertToPx(this.element, option.value);
|
||||
|
||||
actualValue = option.property === 'width' ? elementSize.width : elementSize.height;
|
||||
attrName = option.mode + '-' + option.property;
|
||||
attrValue = '';
|
||||
|
||||
if (option.mode === 'min' && actualValue >= value) {
|
||||
attrValue += option.value;
|
||||
}
|
||||
|
||||
if (option.mode === 'max' && actualValue <= value) {
|
||||
attrValue += option.value;
|
||||
}
|
||||
|
||||
if (!attrValues[attrName]) attrValues[attrName] = '';
|
||||
if (attrValue && -1 === (' ' + attrValues[attrName] + ' ').indexOf(' ' + attrValue + ' ')) {
|
||||
attrValues[attrName] += ' ' + attrValue;
|
||||
}
|
||||
}
|
||||
|
||||
for (var k in attributes) {
|
||||
if (!attributes.hasOwnProperty(k)) continue;
|
||||
|
||||
if (attrValues[attributes[k]]) {
|
||||
this.element.setAttribute(attributes[k], attrValues[attributes[k]].substr(1));
|
||||
} else {
|
||||
this.element.removeAttribute(attributes[k]);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} element
|
||||
* @param {Object} id
|
||||
*/
|
||||
function setupElement(element, id) {
|
||||
if (!element.elementQueriesSetupInformation) {
|
||||
element.elementQueriesSetupInformation = new SetupInformation(element, id);
|
||||
}
|
||||
|
||||
if (!element.elementQueriesSensor) {
|
||||
element.elementQueriesSensor = new ResizeSensor(element, function () {
|
||||
element.elementQueriesSetupInformation.call();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores rules to the selector that should be applied once resized.
|
||||
*
|
||||
* @param {String} selector
|
||||
* @param {String} mode min|max
|
||||
* @param {String} property width|height
|
||||
* @param {String} value
|
||||
*/
|
||||
function queueQuery(selector, mode, property, value) {
|
||||
if (typeof(allQueries[selector]) === 'undefined') {
|
||||
allQueries[selector] = [];
|
||||
// add animation to trigger animationstart event, so we know exactly when a element appears in the DOM
|
||||
|
||||
var id = idToSelectorMapping.length;
|
||||
cssStyleElement.innerHTML += '\n' + selector + ' {animation: 0.1s element-queries;}';
|
||||
cssStyleElement.innerHTML += '\n' + selector + ' > .resize-sensor {min-width: '+id+'px;}';
|
||||
idToSelectorMapping.push(selector);
|
||||
}
|
||||
|
||||
allQueries[selector].push({
|
||||
mode: mode,
|
||||
property: property,
|
||||
value: value
|
||||
});
|
||||
}
|
||||
|
||||
function getQuery(container) {
|
||||
var query;
|
||||
if (document.querySelectorAll) query = (container) ? container.querySelectorAll.bind(container) : document.querySelectorAll.bind(document);
|
||||
if (!query && 'undefined' !== typeof $$) query = $$;
|
||||
if (!query && 'undefined' !== typeof jQuery) query = jQuery;
|
||||
|
||||
if (!query) {
|
||||
throw 'No document.querySelectorAll, jQuery or Mootools\'s $$ found.';
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
/**
|
||||
* If animationStart didn't catch a new element in the DOM, we can manually search for it
|
||||
*/
|
||||
function findElementQueriesElements(container) {
|
||||
var query = getQuery(container);
|
||||
|
||||
for (var selector in allQueries) if (allQueries.hasOwnProperty(selector)) {
|
||||
// find all elements based on the extract query selector from the element query rule
|
||||
var elements = query(selector, container);
|
||||
|
||||
for (var i = 0, j = elements.length; i < j; i++) {
|
||||
setupElement(elements[i], selector);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {HTMLElement} element
|
||||
*/
|
||||
function attachResponsiveImage(element) {
|
||||
var children = [];
|
||||
var rules = [];
|
||||
var sources = [];
|
||||
var defaultImageId = 0;
|
||||
var lastActiveImage = -1;
|
||||
var loadedImages = [];
|
||||
|
||||
for (var i in element.children) {
|
||||
if (!element.children.hasOwnProperty(i)) continue;
|
||||
|
||||
if (element.children[i].tagName && element.children[i].tagName.toLowerCase() === 'img') {
|
||||
children.push(element.children[i]);
|
||||
|
||||
var minWidth = element.children[i].getAttribute('min-width') || element.children[i].getAttribute('data-min-width');
|
||||
//var minHeight = element.children[i].getAttribute('min-height') || element.children[i].getAttribute('data-min-height');
|
||||
var src = element.children[i].getAttribute('data-src') || element.children[i].getAttribute('url');
|
||||
|
||||
sources.push(src);
|
||||
|
||||
var rule = {
|
||||
minWidth: minWidth
|
||||
};
|
||||
|
||||
rules.push(rule);
|
||||
|
||||
if (!minWidth) {
|
||||
defaultImageId = children.length - 1;
|
||||
element.children[i].style.display = 'block';
|
||||
} else {
|
||||
element.children[i].style.display = 'none';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lastActiveImage = defaultImageId;
|
||||
|
||||
function check() {
|
||||
var imageToDisplay = false, i;
|
||||
|
||||
for (i in children) {
|
||||
if (!children.hasOwnProperty(i)) continue;
|
||||
|
||||
if (rules[i].minWidth) {
|
||||
if (element.offsetWidth > rules[i].minWidth) {
|
||||
imageToDisplay = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!imageToDisplay) {
|
||||
//no rule matched, show default
|
||||
imageToDisplay = defaultImageId;
|
||||
}
|
||||
|
||||
if (lastActiveImage !== imageToDisplay) {
|
||||
//image change
|
||||
|
||||
if (!loadedImages[imageToDisplay]) {
|
||||
//image has not been loaded yet, we need to load the image first in memory to prevent flash of
|
||||
//no content
|
||||
|
||||
var image = new Image();
|
||||
image.onload = function () {
|
||||
children[imageToDisplay].src = sources[imageToDisplay];
|
||||
|
||||
children[lastActiveImage].style.display = 'none';
|
||||
children[imageToDisplay].style.display = 'block';
|
||||
|
||||
loadedImages[imageToDisplay] = true;
|
||||
|
||||
lastActiveImage = imageToDisplay;
|
||||
};
|
||||
|
||||
image.src = sources[imageToDisplay];
|
||||
} else {
|
||||
children[lastActiveImage].style.display = 'none';
|
||||
children[imageToDisplay].style.display = 'block';
|
||||
lastActiveImage = imageToDisplay;
|
||||
}
|
||||
} else {
|
||||
//make sure for initial check call the .src is set correctly
|
||||
children[imageToDisplay].src = sources[imageToDisplay];
|
||||
}
|
||||
}
|
||||
|
||||
element.resizeSensorInstance = new ResizeSensor(element, check);
|
||||
check();
|
||||
}
|
||||
|
||||
function findResponsiveImages() {
|
||||
var query = getQuery();
|
||||
|
||||
var elements = query('[data-responsive-image],[responsive-image]');
|
||||
for (var i = 0, j = elements.length; i < j; i++) {
|
||||
attachResponsiveImage(elements[i]);
|
||||
}
|
||||
}
|
||||
|
||||
var regex = /,?[\s\t]*([^,\n]*?)((?:\[[\s\t]*?(?:min|max)-(?:width|height)[\s\t]*?[~$\^]?=[\s\t]*?"[^"]*?"[\s\t]*?])+)([^,\n\s\{]*)/mgi;
|
||||
var attrRegex = /\[[\s\t]*?(min|max)-(width|height)[\s\t]*?[~$\^]?=[\s\t]*?"([^"]*?)"[\s\t]*?]/mgi;
|
||||
|
||||
/**
|
||||
* @param {String} css
|
||||
*/
|
||||
function extractQuery(css) {
|
||||
var match, smatch, attrs, attrMatch;
|
||||
|
||||
css = css.replace(/'/g, '"');
|
||||
while (null !== (match = regex.exec(css))) {
|
||||
smatch = match[1] + match[3];
|
||||
attrs = match[2];
|
||||
|
||||
while (null !== (attrMatch = attrRegex.exec(attrs))) {
|
||||
queueQuery(smatch, attrMatch[1], attrMatch[2], attrMatch[3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {CssRule[]|String} rules
|
||||
*/
|
||||
function readRules(rules) {
|
||||
var selector = '';
|
||||
|
||||
if (!rules) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ('string' === typeof rules) {
|
||||
rules = rules.toLowerCase();
|
||||
if (-1 !== rules.indexOf('min-width') || -1 !== rules.indexOf('max-width')) {
|
||||
extractQuery(rules);
|
||||
}
|
||||
} else {
|
||||
for (var i = 0, j = rules.length; i < j; i++) {
|
||||
if (1 === rules[i].type) {
|
||||
selector = rules[i].selectorText || rules[i].cssText;
|
||||
if (-1 !== selector.indexOf('min-height') || -1 !== selector.indexOf('max-height')) {
|
||||
extractQuery(selector);
|
||||
} else if (-1 !== selector.indexOf('min-width') || -1 !== selector.indexOf('max-width')) {
|
||||
extractQuery(selector);
|
||||
}
|
||||
} else if (4 === rules[i].type) {
|
||||
readRules(rules[i].cssRules || rules[i].rules);
|
||||
} else if (3 === rules[i].type) {
|
||||
if(rules[i].styleSheet.hasOwnProperty("cssRules")) {
|
||||
readRules(rules[i].styleSheet.cssRules);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var defaultCssInjected = false;
|
||||
|
||||
/**
|
||||
* Searches all css rules and setups the event listener to all elements with element query rules..
|
||||
*/
|
||||
this.init = function () {
|
||||
var animationStart = 'animationstart';
|
||||
if (typeof document.documentElement.style['webkitAnimationName'] !== 'undefined') {
|
||||
animationStart = 'webkitAnimationStart';
|
||||
} else if (typeof document.documentElement.style['MozAnimationName'] !== 'undefined') {
|
||||
animationStart = 'mozanimationstart';
|
||||
} else if (typeof document.documentElement.style['OAnimationName'] !== 'undefined') {
|
||||
animationStart = 'oanimationstart';
|
||||
}
|
||||
|
||||
document.body.addEventListener(animationStart, function (e) {
|
||||
var element = e.target;
|
||||
var styles = element && window.getComputedStyle(element, null);
|
||||
var animationName = styles && styles.getPropertyValue('animation-name');
|
||||
var requiresSetup = animationName && (-1 !== animationName.indexOf('element-queries'));
|
||||
|
||||
if (requiresSetup) {
|
||||
element.elementQueriesSensor = new ResizeSensor(element, function () {
|
||||
if (element.elementQueriesSetupInformation) {
|
||||
element.elementQueriesSetupInformation.call();
|
||||
}
|
||||
});
|
||||
|
||||
var sensorStyles = window.getComputedStyle(element.resizeSensor, null);
|
||||
var id = sensorStyles.getPropertyValue('min-width');
|
||||
id = parseInt(id.replace('px', ''));
|
||||
setupElement(e.target, idToSelectorMapping[id]);
|
||||
}
|
||||
});
|
||||
|
||||
if (!defaultCssInjected) {
|
||||
cssStyleElement = document.createElement('style');
|
||||
cssStyleElement.type = 'text/css';
|
||||
cssStyleElement.innerHTML = '[responsive-image] > img, [data-responsive-image] {overflow: hidden; padding: 0; } [responsive-image] > img, [data-responsive-image] > img {width: 100%;}';
|
||||
|
||||
//safari wants at least one rule in keyframes to start working
|
||||
cssStyleElement.innerHTML += '\n@keyframes element-queries { 0% { visibility: inherit; } }';
|
||||
document.getElementsByTagName('head')[0].appendChild(cssStyleElement);
|
||||
defaultCssInjected = true;
|
||||
}
|
||||
|
||||
for (var i = 0, j = document.styleSheets.length; i < j; i++) {
|
||||
try {
|
||||
if (document.styleSheets[i].href && 0 === document.styleSheets[i].href.indexOf('file://')) {
|
||||
console.warn("CssElementQueries: unable to parse local css files, " + document.styleSheets[i].href);
|
||||
}
|
||||
|
||||
readRules(document.styleSheets[i].cssRules || document.styleSheets[i].rules || document.styleSheets[i].cssText);
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
|
||||
findResponsiveImages();
|
||||
};
|
||||
|
||||
/**
|
||||
* Go through all collected rules (readRules()) and attach the resize-listener.
|
||||
* Not necessary to call it manually, since we detect automatically when new elements
|
||||
* are available in the DOM. However, sometimes handy for dirty DOM modifications.
|
||||
*
|
||||
* @param {HTMLElement} container only elements of the container are considered (document.body if not set)
|
||||
*/
|
||||
this.findElementQueriesElements = function (container) {
|
||||
findElementQueriesElements(container);
|
||||
};
|
||||
|
||||
this.update = function () {
|
||||
this.init();
|
||||
};
|
||||
};
|
||||
|
||||
ElementQueries.update = function () {
|
||||
ElementQueries.instance.update();
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes all sensor and elementquery information from the element.
|
||||
*
|
||||
* @param {HTMLElement} element
|
||||
*/
|
||||
ElementQueries.detach = function (element) {
|
||||
if (element.elementQueriesSetupInformation) {
|
||||
//element queries
|
||||
element.elementQueriesSensor.detach();
|
||||
delete element.elementQueriesSetupInformation;
|
||||
delete element.elementQueriesSensor;
|
||||
|
||||
} else if (element.resizeSensorInstance) {
|
||||
//responsive image
|
||||
|
||||
element.resizeSensorInstance.detach();
|
||||
delete element.resizeSensorInstance;
|
||||
}
|
||||
};
|
||||
|
||||
ElementQueries.init = function () {
|
||||
if (!ElementQueries.instance) {
|
||||
ElementQueries.instance = new ElementQueries();
|
||||
}
|
||||
|
||||
ElementQueries.instance.init();
|
||||
};
|
||||
|
||||
var domLoaded = function (callback) {
|
||||
/* Mozilla, Chrome, Opera */
|
||||
if (document.addEventListener) {
|
||||
document.addEventListener('DOMContentLoaded', callback, false);
|
||||
}
|
||||
/* Safari, iCab, Konqueror */
|
||||
else if (/KHTML|WebKit|iCab/i.test(navigator.userAgent)) {
|
||||
var DOMLoadTimer = setInterval(function () {
|
||||
if (/loaded|complete/i.test(document.readyState)) {
|
||||
callback();
|
||||
clearInterval(DOMLoadTimer);
|
||||
}
|
||||
}, 10);
|
||||
}
|
||||
/* Other web browsers */
|
||||
else window.onload = callback;
|
||||
};
|
||||
|
||||
ElementQueries.findElementQueriesElements = function (container) {
|
||||
ElementQueries.instance.findElementQueriesElements(container);
|
||||
};
|
||||
|
||||
ElementQueries.listen = function () {
|
||||
domLoaded(ElementQueries.init);
|
||||
};
|
||||
|
||||
return ElementQueries;
|
||||
|
||||
}));
|
|
@ -0,0 +1,354 @@
|
|||
'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;
|
||||
}
|
||||
// https://github.com/Semantic-Org/Semantic-UI/issues/3855
|
||||
// https://github.com/marcj/css-element-queries/issues/257
|
||||
var globalWindow = typeof window != 'undefined' && window.Math == Math
|
||||
? window
|
||||
: typeof self != 'undefined' && self.Math == Math
|
||||
? self
|
||||
: Function('return this')();
|
||||
// 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 = globalWindow.requestAnimationFrame ||
|
||||
globalWindow.mozRequestAnimationFrame ||
|
||||
globalWindow.webkitRequestAnimationFrame ||
|
||||
function (fn) {
|
||||
return globalWindow.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)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply CSS styles to element.
|
||||
*
|
||||
* @param {HTMLElement} element
|
||||
* @param {Object} style
|
||||
*/
|
||||
function setStyle(element, style) {
|
||||
Object.keys(style).forEach(function(key) {
|
||||
element.style[key] = style[key];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for dimension change detection.
|
||||
*
|
||||
* @param {Element|Element[]|Elements|jQuery} element
|
||||
* @param {Function} callback
|
||||
*
|
||||
* @constructor
|
||||
*/
|
||||
var ResizeSensor = function(element, callback) {
|
||||
var lastAnimationFrame = 0;
|
||||
|
||||
/**
|
||||
*
|
||||
* @constructor
|
||||
*/
|
||||
function EventQueue() {
|
||||
var q = [];
|
||||
this.add = function(ev) {
|
||||
q.push(ev);
|
||||
};
|
||||
|
||||
var i, j;
|
||||
this.call = function(sizeInfo) {
|
||||
for (i = 0, j = q.length; i < j; i++) {
|
||||
q[i].call(this, sizeInfo);
|
||||
}
|
||||
};
|
||||
|
||||
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 = {
|
||||
pointerEvents: 'none',
|
||||
position: 'absolute',
|
||||
left: '0px',
|
||||
top: '0px',
|
||||
right: '0px',
|
||||
bottom: '0px',
|
||||
overflow: 'hidden',
|
||||
zIndex: '-1',
|
||||
visibility: 'hidden',
|
||||
maxWidth: '100%'
|
||||
};
|
||||
var styleChild = {
|
||||
position: 'absolute',
|
||||
left: '0px',
|
||||
top: '0px',
|
||||
transition: '0s',
|
||||
};
|
||||
|
||||
setStyle(element.resizeSensor, style);
|
||||
|
||||
var expand = document.createElement('div');
|
||||
expand.className = 'resize-sensor-expand';
|
||||
setStyle(expand, style);
|
||||
|
||||
var expandChild = document.createElement('div');
|
||||
setStyle(expandChild, styleChild);
|
||||
expand.appendChild(expandChild);
|
||||
|
||||
var shrink = document.createElement('div');
|
||||
shrink.className = 'resize-sensor-shrink';
|
||||
setStyle(shrink, style);
|
||||
|
||||
var shrinkChild = document.createElement('div');
|
||||
setStyle(shrinkChild, styleChild);
|
||||
setStyle(shrinkChild, { width: '200%', height: '200%' });
|
||||
shrink.appendChild(shrinkChild);
|
||||
|
||||
element.resizeSensor.appendChild(expand);
|
||||
element.resizeSensor.appendChild(shrink);
|
||||
element.appendChild(element.resizeSensor);
|
||||
|
||||
var computedStyle = window.getComputedStyle(element);
|
||||
var position = computedStyle ? computedStyle.getPropertyValue('position') : null;
|
||||
if ('absolute' !== position && 'relative' !== position && 'fixed' !== position && 'sticky' !== position) {
|
||||
element.style.position = 'relative';
|
||||
}
|
||||
|
||||
var dirty, rafId;
|
||||
var size = getElementSize(element);
|
||||
var lastWidth = 0;
|
||||
var lastHeight = 0;
|
||||
var initialHiddenCheck = true;
|
||||
lastAnimationFrame = 0;
|
||||
|
||||
var resetExpandShrink = function () {
|
||||
var width = element.offsetWidth;
|
||||
var height = element.offsetHeight;
|
||||
|
||||
expandChild.style.width = (width + 10) + 'px';
|
||||
expandChild.style.height = (height + 10) + 'px';
|
||||
|
||||
expand.scrollLeft = width + 10;
|
||||
expand.scrollTop = height + 10;
|
||||
|
||||
shrink.scrollLeft = width + 10;
|
||||
shrink.scrollTop = height + 10;
|
||||
};
|
||||
|
||||
var reset = function() {
|
||||
// Check if element is hidden
|
||||
if (initialHiddenCheck) {
|
||||
var invisible = element.offsetWidth === 0 && element.offsetHeight === 0;
|
||||
if (invisible) {
|
||||
// Check in next frame
|
||||
if (!lastAnimationFrame){
|
||||
lastAnimationFrame = requestAnimationFrame(function(){
|
||||
lastAnimationFrame = 0;
|
||||
|
||||
reset();
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
} else {
|
||||
// Stop checking
|
||||
initialHiddenCheck = false;
|
||||
}
|
||||
}
|
||||
|
||||
resetExpandShrink();
|
||||
};
|
||||
element.resizeSensor.resetSensor = reset;
|
||||
|
||||
var onResized = function() {
|
||||
rafId = 0;
|
||||
|
||||
if (!dirty) return;
|
||||
|
||||
lastWidth = size.width;
|
||||
lastHeight = size.height;
|
||||
|
||||
if (element.resizedAttached) {
|
||||
element.resizedAttached.call(size);
|
||||
}
|
||||
};
|
||||
|
||||
var onScroll = function() {
|
||||
size = getElementSize(element);
|
||||
dirty = size.width !== lastWidth || size.height !== lastHeight;
|
||||
|
||||
if (dirty && !rafId) {
|
||||
rafId = requestAnimationFrame(onResized);
|
||||
}
|
||||
|
||||
reset();
|
||||
};
|
||||
|
||||
var addEvent = function(el, name, cb) {
|
||||
if (el.attachEvent) {
|
||||
el.attachEvent('on' + name, cb);
|
||||
} else {
|
||||
el.addEventListener(name, cb);
|
||||
}
|
||||
};
|
||||
|
||||
addEvent(expand, 'scroll', onScroll);
|
||||
addEvent(shrink, 'scroll', onScroll);
|
||||
|
||||
// Fix for custom Elements
|
||||
lastAnimationFrame = requestAnimationFrame(reset);
|
||||
}
|
||||
|
||||
forEachElement(element, function(elem){
|
||||
attachResizeEvent(elem, callback);
|
||||
});
|
||||
|
||||
this.detach = function(ev) {
|
||||
// clean up the unfinished animation frame to prevent a potential endless requestAnimationFrame of reset
|
||||
if (!lastAnimationFrame) {
|
||||
window.cancelAnimationFrame(lastAnimationFrame);
|
||||
lastAnimationFrame = 0;
|
||||
}
|
||||
ResizeSensor.detach(element, ev);
|
||||
};
|
||||
|
||||
this.reset = function() {
|
||||
element.resizeSensor.resetSensor();
|
||||
};
|
||||
};
|
||||
|
||||
ResizeSensor.reset = function(element) {
|
||||
forEachElement(element, function(elem){
|
||||
elem.resizeSensor.resetSensor();
|
||||
});
|
||||
};
|
||||
|
||||
ResizeSensor.detach = function(element, ev) {
|
||||
forEachElement(element, function(elem){
|
||||
if (!elem) return;
|
||||
if(elem.resizedAttached && typeof ev === "function"){
|
||||
elem.resizedAttached.remove(ev);
|
||||
if(elem.resizedAttached.length()) return;
|
||||
}
|
||||
if (elem.resizeSensor) {
|
||||
if (elem.contains(elem.resizeSensor)) {
|
||||
elem.removeChild(elem.resizeSensor);
|
||||
}
|
||||
delete elem.resizeSensor;
|
||||
delete elem.resizedAttached;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if (typeof MutationObserver !== "undefined") {
|
||||
var observer = new MutationObserver(function (mutations) {
|
||||
for (var i in mutations) {
|
||||
if (mutations.hasOwnProperty(i)) {
|
||||
var items = mutations[i].addedNodes;
|
||||
for (var j = 0; j < items.length; j++) {
|
||||
if (items[j].resizeSensor) {
|
||||
ResizeSensor.reset(items[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("DOMContentLoaded", function (event) {
|
||||
observer.observe(document.body, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return ResizeSensor;
|
||||
|
||||
}));
|
|
@ -271,6 +271,9 @@
|
|||
.status-card__description {
|
||||
color: #282c37;
|
||||
}
|
||||
.status__wrapper[max-width~="350px"] .status-card__description {
|
||||
display: none;
|
||||
}
|
||||
.status-card__host {
|
||||
display: block;
|
||||
margin-top: 5px;
|
||||
|
@ -279,6 +282,23 @@
|
|||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.status__wrapper[max-width~="350px"] .status.status-public {
|
||||
padding: 8px 10px 8px 10px;
|
||||
}
|
||||
.status__wrapper[max-width~="350px"] .status__info {
|
||||
font-size: 1.8rem;
|
||||
margin-left: 60px;
|
||||
position: initial;
|
||||
}
|
||||
.status__wrapper[max-width~="350px"] .status__content {
|
||||
margin-top: 2rem;
|
||||
}
|
||||
.status__content .invisible {
|
||||
display: none;
|
||||
}
|
||||
.status__content .ellipsis:after {
|
||||
content: '\2026';
|
||||
}
|
||||
.status__content summary {
|
||||
background: #eee;
|
||||
border-radius: 5px;
|
||||
|
|
|
@ -66,3 +66,17 @@ a.card{
|
|||
font-size: 14px;
|
||||
color: #fff;
|
||||
}
|
||||
.pixelfed[max-width~="400px"] .pixelfed-meta .text-center{
|
||||
display: none;
|
||||
}
|
||||
.pixelfed[max-width~="350px"] .pixelfed-header{
|
||||
display: block!important;
|
||||
}
|
||||
.pixelfed[max-width~="350px"] .pixelfed-account{
|
||||
float: left;
|
||||
clear: both;
|
||||
}
|
||||
.pixelfed[max-width~="350px"] .pixelfed-instance{
|
||||
float: right;
|
||||
clear: both;
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ class FediEmbedi_Mastodon extends WP_Widget {
|
|||
|
||||
//getStatus from remote instance
|
||||
$status = $client->getStatus($only_media, $pinned, $exclude_replies, null, null, null, $number, $exclude_reblogs);
|
||||
$account = $status[0]->account;
|
||||
include(plugin_dir_path(__FILE__) . 'templates/mastodon.tpl.php' );
|
||||
|
||||
echo $args['after_widget'];
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
* Plugin Name: FediEmbedi
|
||||
* Plugin URI: https://git.feneas.org/mediaformat/fediembedi
|
||||
* Github Plugin URI: https://github.com/mediaformat/fediembedi
|
||||
* Description: A widget to show your Mastodon profile timeline
|
||||
* Version: 0.8.4
|
||||
* Description: A widget to show your Fediverse profile timeline
|
||||
* Version: 0.8.5
|
||||
* Author: mediaformat
|
||||
* Author URI: https://mediaformat.org
|
||||
* License: GPLv3
|
||||
|
@ -152,6 +152,11 @@ class FediConfig
|
|||
|
||||
public function enqueue_styles($hook)
|
||||
{
|
||||
if( is_active_widget( false, false, 'mastodon') || is_active_widget( false, false, 'pixelfed') ) {
|
||||
wp_enqueue_script( 'resize-sensor', plugin_dir_url( __FILE__ ) . 'assets/ResizeSensor.js', array(), 'css-element-queries-1.2.2' );
|
||||
wp_enqueue_script( 'element-queries', plugin_dir_url( __FILE__ ) . 'assets/ElementQueries.js', array('resize-sensor'), 'css-element-queries-1.2.2' );
|
||||
|
||||
}
|
||||
if( is_active_widget( false, false, 'mastodon') ) {
|
||||
wp_enqueue_style( 'mastodon', plugin_dir_url( __FILE__ ) . 'assets/mastodon.css', array(), filemtime(plugin_dir_path( __FILE__ ) . 'assets/mastodon.css') );
|
||||
}
|
||||
|
|
11
readme.md
11
readme.md
|
@ -5,10 +5,10 @@
|
|||
FediEmbedi will display your Mastodon, Pleroma, or Pixelfed timeline in a widget, with various display options.
|
||||
|
||||
### Currently supported software
|
||||
* Mastodon
|
||||
* Pleroma
|
||||
* Pixelfed
|
||||
* PeerTube
|
||||
* [Mastodon](http://joinmastodon.org/)
|
||||
* [Pleroma](https://git.pleroma.social/pleroma)
|
||||
* [Pixelfed](https://pixelfed.org/)
|
||||
* [PeerTube](https://joinpeertube.org/)
|
||||
|
||||
|
||||
### Planned supported software
|
||||
|
@ -48,6 +48,9 @@ and redirected to your site with a secure token. Similar to how you would connec
|
|||
|
||||
## Changelog
|
||||
|
||||
### 0.8.5
|
||||
* CSS for small columns
|
||||
|
||||
### 0.8.4
|
||||
* Fix large video player
|
||||
|
||||
|
|
13
readme.txt
13
readme.txt
|
@ -18,10 +18,10 @@ Display your Fediverse timeline in a widget
|
|||
FediEmbedi will display your Mastodon, Pleroma, or Pixelfed timeline in a widget, with various display options.
|
||||
|
||||
= Currently supported software =
|
||||
* Mastodon
|
||||
* Pleroma
|
||||
* Pixelfed
|
||||
* PeerTube
|
||||
* [Mastodon](http://joinmastodon.org/)
|
||||
* [Pleroma](https://git.pleroma.social/pleroma)
|
||||
* [Pixelfed](https://pixelfed.org/)
|
||||
* [PeerTube](https://joinpeertube.org/)
|
||||
|
||||
= Planned supported software =
|
||||
* See the [board](https://git.feneas.org/mediaformat/fediembedi/-/boards)
|
||||
|
@ -59,6 +59,9 @@ and redirected to your site with a secure token. Similar to how you would connec
|
|||
|
||||
== Changelog ==
|
||||
|
||||
= 0.8.5 =
|
||||
* CSS for small columns
|
||||
|
||||
= 0.8.4 =
|
||||
* Fix large video player
|
||||
|
||||
|
@ -68,7 +71,7 @@ and redirected to your site with a secure token. Similar to how you would connec
|
|||
= 0.8.2 =
|
||||
* Add Support links
|
||||
|
||||
= 0.8.1 =
|
||||
= 0.8.1 =
|
||||
* CSS fix
|
||||
|
||||
= 0.8.0 =
|
||||
|
|
|
@ -6,28 +6,28 @@
|
|||
<div class="account__header">
|
||||
<div class="account__header__image">
|
||||
<div class="account__header__info"></div>
|
||||
<?php if ($status[0]->account->header): echo "<img src=" . $status[0]->account->header . " loading='lazy'>"; endif; ?>
|
||||
<?php if ($account->header): echo "<img src=" . $account->header . " loading='lazy'>"; endif; ?>
|
||||
</div>
|
||||
<div class="account__header__bar">
|
||||
<div class="account__header__tabs">
|
||||
<a href="<?php echo $status[0]->account->url; ?>" class="avatar" rel="noreferrer noopener" target="_blank">
|
||||
<div class="account__avatar" style="width:90px; height: 90px; background-image: url('<?php echo $status[0]->account->avatar; ?>'); background-size: cover;"></div>
|
||||
<a href="<?php echo $account->url; ?>" class="avatar" rel="noreferrer noopener" target="_blank">
|
||||
<div class="account__avatar" style="width:90px; height: 90px; background-image: url('<?php echo $account->avatar; ?>'); background-size: cover;"></div>
|
||||
</a>
|
||||
<div class="spacer"></div>
|
||||
<div class="account__header__tabs__buttons">
|
||||
<a href="<?php echo $status[0]->account->url; ?>" rel="noreferrer noopener" class="button logo-button"><?php _e('Follow', 'fediembedi'); ?></a>
|
||||
<a href="<?php echo $account->url; ?>" rel="noreferrer noopener" class="button logo-button"><?php _e('Follow', 'fediembedi'); ?></a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="account__header__tabs__name">
|
||||
<h1>
|
||||
<span><?php echo $status[0]->account->display_name; ?></span>
|
||||
<small><a href="" target="_blank" rel="noreferrer noopener"><?php echo $status[0]->account->url; ?></a></small>
|
||||
<span><?php echo $account->display_name; ?></span>
|
||||
<small><a href="" target="_blank" rel="noreferrer noopener"><?php echo $account->url; ?></a></small>
|
||||
</h1>
|
||||
</div>
|
||||
<div class="account__header__extra">
|
||||
<div class="account__header__bio">
|
||||
<div class="account__header__content">
|
||||
<?php echo $status[0]->account->note; ?>
|
||||
<?php echo $account->note; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -76,7 +76,7 @@
|
|||
<div class="status-card__image"><div class="status-card__image-image" style="background-image: url(<?php echo $statut->card->image; ?>);"></div></div>
|
||||
<div class="status-card__content">
|
||||
<strong class="status-card__title" title="<?php echo $statut->card->title; ?>"><?php echo htmlentities($statut->card->title); ?></strong>
|
||||
<p class="status-card__description"><?php echo wp_trim_words(htmlentities($statut->card->description)); ?></p>
|
||||
<p class="status-card__description"><?php echo wp_trim_words(htmlentities($statut->card->description), 10); ?></p>
|
||||
<span class="status-card__host"><?php echo $statut->card->url; ?></span>
|
||||
</div>
|
||||
</a>
|
||||
|
|
|
@ -4,11 +4,11 @@
|
|||
<div class="pixelfed-inner card status-card-embed card-md-rounded-0 border">
|
||||
<?php if($show_header): ?>
|
||||
<div class="pixelfed-header card-header d-inline-flex align-items-center justify-content-between bg-white">
|
||||
<div>
|
||||
<div class="pixelfed-account">
|
||||
<img src="<?php echo $status[0]->account->avatar; ?>" height="32px" width="32px" style="border-radius: 32px;">
|
||||
<a href="<?php echo $status[0]->account->url; ?>" class="username font-weight-bold pl-2 text-dark" rel="noreferrer noopener" target="_blank"><?php echo $status[0]->account->username; ?></a>
|
||||
</div>
|
||||
<div>
|
||||
<div class="pixelfed-instance">
|
||||
<a class="small font-weight-bold text-muted pr-1" href="<?php echo $instance_url; ?>"><?php echo parse_url($instance_url, PHP_URL_HOST); ?></a>
|
||||
<img src="<?php echo plugin_dir_url( __FILE__ ) . '../img/pixelfed.svg';?>" width="26px" loading="lazy">
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue