diff options
Diffstat (limited to 'html/assets/js/require/AttributePlugin.js')
| -rw-r--r-- | html/assets/js/require/AttributePlugin.js | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/html/assets/js/require/AttributePlugin.js b/html/assets/js/require/AttributePlugin.js new file mode 100644 index 0000000..d0568a2 --- /dev/null +++ b/html/assets/js/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 |
