Skip to content

Commit

Permalink
Hype DataFill v1.2.1
Browse files Browse the repository at this point in the history
Also, updating when class is modified (only in IDE)
  • Loading branch information
worldoptimizer authored Mar 2, 2021
1 parent fcb26a8 commit 299c0a3
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions HypeDataFill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
Hype DataFill 1.2.0
Hype DataFill 1.2.1
copyright (c) 2019 Max Ziebell, (https://maxziebell.de). MIT-license
*/

Expand All @@ -8,12 +8,14 @@ copyright (c) 2019 Max Ziebell, (https://maxziebell.de). MIT-license
* 1.0 Initial release under MIT-license
* 1.1 Added option to set initial value
* 1.2.0 Inspired by Symbol Override I added a callback
* 1.2.1 Also updating when class is modified (only in IDE)
*/
if("HypeDataFill" in window === false) window['HypeDataFill'] = (function () {

var _mapList = [];
var _activated = {};

var _lastRefresh = 0;

/* @const */
const _isHypeIDE = window.location.href.indexOf("/Hype/Scratch/HypeScratch.") != -1;

Expand All @@ -26,18 +28,30 @@ if("HypeDataFill" in window === false) window['HypeDataFill'] = (function () {
if (_isHypeIDE){
var baseContainer = document.documentElement || document.body;
activateObserver(baseContainer);
var classObserver = new MutationObserver(function(m){ refresh(); });
classObserver.observe(baseContainer, { attributes: true, subtree: true, attributeFilter: [ 'class' ]});
}
}

function activateObserver (baseContainer){
_mapList.forEach(function(mapItem) {
if (!_activated[baseContainer+'_'+mapItem.attributeName]){
_activated[baseContainer+'_'+mapItem.attributeName] = true;
mapItem.baseContainer = baseContainer;
mapItem.startObserver(baseContainer);
}
});
}

function refresh (){
var now = new Date().getTime(); if (_lastRefresh == now) return; _lastRefresh = now;
_mapList.forEach(function(mapItem) {
mapItem.baseContainer.querySelectorAll('['+mapItem.attributeName+']').forEach(function(elm){
elm.setAttribute(mapItem.attributeName, elm.getAttribute(mapItem.attributeName));
});
});
}

function observerFactory(attributeName, selector, callback){
callback = typeof callback == 'function'? callback: function(elm, value){
elm.innerHTML = value;
Expand Down Expand Up @@ -96,7 +110,7 @@ if("HypeDataFill" in window === false) window['HypeDataFill'] = (function () {

/* Reveal Public interface to window['HypeDataFill'] */
return {
version: '1.2.0',
version: '1.2.1',
'mapDatasetToClass' : mapDatasetToClass,
};
})();

0 comments on commit 299c0a3

Please sign in to comment.