UNPKG

pogojs

Version:

Library to accompany the Pogo stack

59 lines (45 loc) 2.06 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _pogoMap = require('./pogoMap'); var _each = require('./utils/each'); var _each2 = _interopRequireDefault(_each); var _hyphenToCamelCase = require('./utils/hyphenToCamelCase'); var _hyphenToCamelCase2 = _interopRequireDefault(_hyphenToCamelCase); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } var pogoMap = (0, _pogoMap.getMap)(); /** * Gets all `pogo-` classes and either binds events or triggers their initialisation functions by * checking them against the function map. * @param {Node} context [document] - The element to check for `pogo-` classes. */ function pogoBind() { var context = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document; var pogoEls = context.querySelectorAll('[class*="pogo-"]'); var onloadFuncs = []; (0, _each2.default)(pogoEls, function (el) { var hooks = el.className.match(/pogo-[a-z\d-_]+/ig); hooks.map(function (hook) { var key = (0, _hyphenToCamelCase2.default)(hook.slice(5)); // removes `pogo-` if (pogoMap[key]) { pogoMap[key].map(function (item) { var type = item.type, func = item.func; if (type === 'immediate') { onloadFuncs = [].concat(_toConsumableArray(onloadFuncs), [func.bind(null, el, context)]); } else { el.addEventListener(type, func); } }); } }); }); // Remove and execute all functions that are supposed to trigger immediately onload // not just event bindings while (onloadFuncs.length > 0) { onloadFuncs.shift()(); } } exports.default = pogoBind;