From 7076343338ae3439f3c86f01144818abe8c31978 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Sun, 12 Mar 2023 07:16:23 +0200 Subject: add container helpers; constuct public directory-tree --- html/content/lib/require/AttributePlugin.js | 138 ++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 html/content/lib/require/AttributePlugin.js (limited to 'html/content/lib/require/AttributePlugin.js') diff --git a/html/content/lib/require/AttributePlugin.js b/html/content/lib/require/AttributePlugin.js new file mode 100644 index 0000000..79dc5af --- /dev/null +++ b/html/content/lib/require/AttributePlugin.js @@ -0,0 +1,138 @@ +require('jQuery', function ($) { + + function debounce(func, wait, immediate) { + var timeout; + return function () { + var context = this, args = arguments; + var later = function () { + timeout = null; + if (!immediate) func.apply(context, args); + }; + var callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) func.apply(context, args); + }; + }; + + function init($element, pluginName, pluginConfiguration, callback) { + if (typeof (pluginName) === 'undefined') { + return; + } + + require([pluginName], function (plugin) { + if (typeof (plugin) === 'undefined') { + throw 'plugin ' + pluginName + ' is undefined'; + } + plugin($element, pluginConfiguration); + }); + } + + + var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/; + function parseOptions(data, element) { + if (data === "true") { + return true; + } + + if (data === "false") { + return false; + } + + if (data === "null") { + return null; + } + + // Only convert to a number if it doesn't change the string + if (data === +data + "") { + return +data; + } + + if (rbrace.test(data)) { + return tryParseJson(data, element); + } + + return data; + } + function tryParseJson(json, element) { + try { + return JSON.parse(json) + } + catch (err) { + console.warn(element, 'error during parsing of json', json, err); + return json; + } + } + + function attach($container) { + $container.find('*').addBack().each(function () { + var domNode = this; + var attributeNamePrefix = "data-plugin-"; + var plugins = []; + var initialized = domNode.initializedPlugins = (domNode.initializedPlugins || {}); + var $element = $(this); + for (var i = 0; i < domNode.attributes.length; i++) { + var attribute = domNode.attributes[i]; + + if (attribute.name.indexOf(attributeNamePrefix) != 0) { + continue; + } + if (initialized[attribute.name]) { + continue; + } + + var plugin = { name: attribute.name.substring(attributeNamePrefix.length), options: parseOptions(attribute.value, domNode) }; + init($element, plugin.name, plugin.options); + initialized[attribute.name] = true; + } + }); + } + $.fn.parseAttributePlugins = function () { + attach($(this)); + return $(this); + }; + var attachFn = debounce(function () { + attach($('body')); + }, 100); + + $(function () { + attachFn(); + }); + $('body').on('contentInjected', function (e) { + attachFn(); + }); + $.defineAttributePlugin = function (pluginName, dependencies, plugin) { + if (typeof (plugin) == 'undefined') { + plugin = dependencies; + dependencies = []; + } + define(pluginName, dependencies, function () { + var dependencyValues = arguments; + if (dependencies.length) { + return function ($element, options) { + var args = [$element, options]; + for (var i = 0; i < dependencyValues.length; i++) { + args.push(dependencyValues[i]); + } + + plugin.apply(this, args); + }; + } + return function ($element, options) { + + plugin($element, options); + }; + }); + }; + + $.fn.executePlugin = function (pluginName, pluginConfiguration, callback) { + return $(this).each(function () { + var $element = $(this); + init($element, pluginName, pluginConfiguration, function () { + if (typeof (callback) !== 'undefined') { + callback.apply($element, []); + } + }); + }); + }; +}); \ No newline at end of file -- cgit v1.2.3