loadax
Version:
Universal powerful solution for creating web app
531 lines (427 loc) • 15 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.After = exports.Before = exports.Context = exports.CreateApp = exports.LoadaxJsx = void 0;
var NOT_ACTIVE = "Channel is not active";
var initPwaApp = function initPwaApp(context, init) {
if (context.installing) {
context.installing.addEventListener('statechange', function () {
if (this.state === 'activated') {
return location.reload();
}
});
} else {
if (context.active) {
init(context);
return context;
}
}
return null;
};
var Bridge = {
channel: null,
connect: function connect(init) {
var sw = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'main/ws-loadax.js';
return navigator.serviceWorker.getRegistration(sw).then(function (context) {
if (context) {
_get__("Bridge").channel = _get__("initPwaApp")(context, init);
} else return navigator.serviceWorker.register(sw).then(function (activeContext) {
_get__("Bridge").channel = _get__("initPwaApp")(activeContext, init);
});
});
},
provide: function provide(command, value) {
var _get__$channel;
if ((_get__$channel = _get__("Bridge").channel) !== null && _get__$channel !== void 0 && _get__$channel.active) {
_get__("Bridge").channel.active.postMessage({
command: command,
value: value
});
} else {
console.error(_get__("NOT_ACTIVE"));
}
},
consume: function consume(command, action) {
var _get__$channel2;
if ((_get__$channel2 = _get__("Bridge").channel) !== null && _get__$channel2 !== void 0 && _get__$channel2.active) {
navigator.serviceWorker.addEventListener("message", function (event) {
var cmd = event.data.command;
if (cmd && cmd === command) action(event.data.value);
});
} else {
console.error(_get__("NOT_ACTIVE"));
}
}
};
var moduleIndex = function moduleIndex(name) {
return _get__("Micro").readyModules.findIndex(function (module) {
return module.name === name;
});
};
var findModule = function findModule(name) {
return _get__("Micro").modules.find(function (module) {
return module.name === name;
});
};
var findLib = function findLib(name) {
return _get__("Micro").libs.find(function (lib) {
return lib.name === name;
});
};
var uploadModule = function uploadModule(js) {
return fetch(js).then(function (response) {
return response.text();
});
};
var Micro = {
modules: [],
readyModules: [],
libs: [],
load: function load(url) {
return fetch(url).then(function (response) {
return _get__("Micro").modules = response.json();
});
},
register: function register(name) {
var module = _get__("findModule")(name);
if (module && _get__("moduleIndex")(name) === -1) {
_get__("Micro").readyModules.push({
name: name,
type: module.type,
js: module.js,
css: module.css
});
}
},
injectModule: function injectModule(module) {
return _get__("uploadModule")(module.js).then(function (res) {
var lib = _get__("findLib")(module.name);
if (!lib) _get__("Micro").libs.push({
name: module.name,
payload: res
});else console.log("MODULE ".concat(module.name, " already loaded"));
});
},
execute: function execute(name) {
var module = _get__("findLib")(name);
if (module && _get__("moduleIndex")(name) !== -1) eval(module.payload);
}
};
var Loadax = {
injectModules: function injectModules(url) {
return _get__("Micro").load(url);
},
executeModule: function executeModule(module) {
_get__("Micro").register(module.name);
return _get__("Micro").injectModule(module).then(function () {
_get__("Micro").execute(module.name);
});
},
asPwa: function asPwa(init) {
var sw = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'main/ws-loadax.js';
return _get__("Bridge").connect(init, sw);
},
send: function send(command, value) {
_get__("Bridge").provide(command, value);
},
listen: function listen(command, action) {
_get__("Bridge").consume(command, action);
}
};
var componentsHooks = [];
exports.LoadaxJsx = function (tagName) {
var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
for (var _len = arguments.length, children = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
children[_key - 2] = arguments[_key];
}
tagName = tagName === 'fragment' ? 'section' : tagName;
if (typeof tagName === 'function') {
tagName.prototype.node = tagName(attrs, children);
tagName.prototype.update = function () {
var nodeTarget = tagName(attrs, children);
tagName.prototype.node.replaceWith(nodeTarget);
tagName.prototype.node = nodeTarget;
};
setTimeout(function () {
var index = _get__("componentsHooks").findIndex(function (ch) {
return ch.uid === tagName.prototype.uid;
});
if (index !== -1) {
_get__("componentsHooks")[index].action.forEach(function (action) {
return action();
});
_get__("componentsHooks").splice(index, 1);
}
});
return tagName.prototype.node;
}
var elem = Object.assign(document.createElement(tagName), attrs);
var _loop = function _loop(attr) {
if (attr.slice(0, 2) === 'on') {
var _attr = attr.toLowerCase();
elem[_attr] = function (e) {
if (elem !== 'undefined') {
attrs[attr](e, elem);
}
if (_attr.toLocaleString() === 'oninput') {
var _document$querySelect;
if (!e.target.name) {
throw new Error('Attribute name is required for input');
}
(_document$querySelect = document.querySelector("[name=".concat(e.target.name, "]"))) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.focus();
}
};
}
};
for (var attr in attrs) {
_loop(attr);
}
for (var _i = 0, _children = children; _i < _children.length; _i++) {
var child = _children[_i];
if (Array.isArray(child)) elem.append.apply(elem, _toConsumableArray(child));else elem.append(child);
}
return elem;
};
exports.CreateApp = function (node, component) {
var _document$getElementB;
(_document$getElementB = document.getElementById(node)) === null || _document$getElementB === void 0 ? void 0 : _document$getElementB.replaceWith(component);
};
exports.Context = function (data) {
var components = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
return {
data: data,
set: function set(obj) {
Object.assign(data, obj);
},
update: function update(obj) {
Object.assign(data, obj);
components.forEach(function (call) {
return call.prototype.update();
});
}
};
};
exports.Before = function (action, component) {
component.prototype.beforeCreateMethod = action;
component.prototype.beforeCreateMethod();
};
exports.After = function (action, component) {
component.prototype.uid = 'uid' + new Date().getTime();
var index = _get__("componentsHooks").findIndex(function (chs) {
return chs.uid === component.prototype.uid;
});
if (index !== -1) {
_get__("componentsHooks")[index].action.push(action);
} else {
_get__("componentsHooks").push({
uid: component.prototype.uid,
action: [action]
});
}
};
exports["default"] = _get__("Loadax");
function _getGlobalObject() {
try {
if (!!global) {
return global;
}
} catch (e) {
try {
if (!!window) {
return window;
}
} catch (e) {
return this;
}
}
}
;
var _RewireModuleId__ = null;
function _getRewireModuleId__() {
if (_RewireModuleId__ === null) {
var globalVariable = _getGlobalObject();
if (!globalVariable.__$$GLOBAL_REWIRE_NEXT_MODULE_ID__) {
globalVariable.__$$GLOBAL_REWIRE_NEXT_MODULE_ID__ = 0;
}
_RewireModuleId__ = __$$GLOBAL_REWIRE_NEXT_MODULE_ID__++;
}
return _RewireModuleId__;
}
function _getRewireRegistry__() {
var theGlobalVariable = _getGlobalObject();
if (!theGlobalVariable.__$$GLOBAL_REWIRE_REGISTRY__) {
theGlobalVariable.__$$GLOBAL_REWIRE_REGISTRY__ = Object.create(null);
}
return theGlobalVariable.__$$GLOBAL_REWIRE_REGISTRY__;
}
function _getRewiredData__() {
var moduleId = _getRewireModuleId__();
var registry = _getRewireRegistry__();
var rewireData = registry[moduleId];
if (!rewireData) {
registry[moduleId] = Object.create(null);
rewireData = registry[moduleId];
}
return rewireData;
}
(function registerResetAll() {
var theGlobalVariable = _getGlobalObject();
if (!theGlobalVariable['__rewire_reset_all__']) {
theGlobalVariable['__rewire_reset_all__'] = function () {
theGlobalVariable.__$$GLOBAL_REWIRE_REGISTRY__ = Object.create(null);
};
}
})();
var INTENTIONAL_UNDEFINED = '__INTENTIONAL_UNDEFINED__';
var _RewireAPI__ = {};
(function () {
function addPropertyToAPIObject(name, value) {
Object.defineProperty(_RewireAPI__, name, {
value: value,
enumerable: false,
configurable: true
});
}
addPropertyToAPIObject('__get__', _get__);
addPropertyToAPIObject('__GetDependency__', _get__);
addPropertyToAPIObject('__Rewire__', _set__);
addPropertyToAPIObject('__set__', _set__);
addPropertyToAPIObject('__reset__', _reset__);
addPropertyToAPIObject('__ResetDependency__', _reset__);
addPropertyToAPIObject('__with__', _with__);
})();
function _get__(variableName) {
var rewireData = _getRewiredData__();
if (rewireData[variableName] === undefined) {
return _get_original__(variableName);
} else {
var value = rewireData[variableName];
if (value === INTENTIONAL_UNDEFINED) {
return undefined;
} else {
return value;
}
}
}
function _get_original__(variableName) {
switch (variableName) {
case "Bridge":
return Bridge;
case "initPwaApp":
return initPwaApp;
case "NOT_ACTIVE":
return NOT_ACTIVE;
case "Micro":
return Micro;
case "findModule":
return findModule;
case "moduleIndex":
return moduleIndex;
case "uploadModule":
return uploadModule;
case "findLib":
return findLib;
case "componentsHooks":
return componentsHooks;
case "Loadax":
return Loadax;
}
return undefined;
}
function _assign__(variableName, value) {
var rewireData = _getRewiredData__();
if (rewireData[variableName] === undefined) {
return _set_original__(variableName, value);
} else {
return rewireData[variableName] = value;
}
}
function _set_original__(variableName, _value) {
switch (variableName) {}
return undefined;
}
function _update_operation__(operation, variableName, prefix) {
var oldValue = _get__(variableName);
var newValue = operation === '++' ? oldValue + 1 : oldValue - 1;
_assign__(variableName, newValue);
return prefix ? newValue : oldValue;
}
function _set__(variableName, value) {
var rewireData = _getRewiredData__();
if (_typeof(variableName) === 'object') {
Object.keys(variableName).forEach(function (name) {
rewireData[name] = variableName[name];
});
return function () {
Object.keys(variableName).forEach(function (name) {
_reset__(variableName);
});
};
} else {
if (value === undefined) {
rewireData[variableName] = INTENTIONAL_UNDEFINED;
} else {
rewireData[variableName] = value;
}
return function () {
_reset__(variableName);
};
}
}
function _reset__(variableName) {
var rewireData = _getRewiredData__();
delete rewireData[variableName];
if (Object.keys(rewireData).length == 0) {
delete _getRewireRegistry__()[_getRewireModuleId__];
}
;
}
function _with__(object) {
var rewireData = _getRewiredData__();
var rewiredVariableNames = Object.keys(object);
var previousValues = {};
function reset() {
rewiredVariableNames.forEach(function (variableName) {
rewireData[variableName] = previousValues[variableName];
});
}
return function (callback) {
rewiredVariableNames.forEach(function (variableName) {
previousValues[variableName] = rewireData[variableName];
rewireData[variableName] = object[variableName];
});
var result = callback();
if (!!result && typeof result.then == 'function') {
result.then(reset)["catch"](reset);
} else {
reset();
}
return result;
};
}
var _typeOfOriginalExport = _typeof(module.exports);
function addNonEnumerableProperty(name, value) {
Object.defineProperty(module.exports, name, {
value: value,
enumerable: false,
configurable: true
});
}
if ((_typeOfOriginalExport === 'object' || _typeOfOriginalExport === 'function') && Object.isExtensible(module.exports)) {
addNonEnumerableProperty('__get__', _get__);
addNonEnumerableProperty('__GetDependency__', _get__);
addNonEnumerableProperty('__Rewire__', _set__);
addNonEnumerableProperty('__set__', _set__);
addNonEnumerableProperty('__reset__', _reset__);
addNonEnumerableProperty('__ResetDependency__', _reset__);
addNonEnumerableProperty('__with__', _with__);
addNonEnumerableProperty('__RewireAPI__', _RewireAPI__);
}