UNPKG

@ngal/ui

Version:

Addons for ui.bootstrap

71 lines (56 loc) 2.45 kB
(function() { "use strict"; function callScriptFn(scope, scriptIn, paramNames) { if (!(scope && scriptIn)) return undefined; var matches; var paramCount; var parameters; var paramObject; var scriptOut; // Break the scriptIn into three parts: // All of the characters from the start up to and including the '(' // The characters within the parentheses (the parameters). // All of the characters from the ')' to the end. matches = scriptIn.match(/([^(]+\()([^)]*)(\).*)/); if (!(matches && matches.length === 4)) return undefined; // Strip all spaces from the parameter list and convert it into an array. parameters = matches[2].replace(/\s/g, "").split(","); // Determine the lessor of (1) The number of parameters in the list, // or (2) The number of expected parameters (from the paramNames.) paramNames = paramNames || []; paramCount = Math.min(parameters.length, paramNames.length); // Build a Javascript object (textual), that has property names for each of the paramNames. // The right hand side is the corresponding parameter from the scriptIn. paramObject = paramNames.reduce(function(acc, value, index) { if (parameters[index]) { acc += value + ": " + parameters[index]; if (index < paramCount && parameters[1 + index]) acc += ", "; } return acc; }, "{ "); paramObject += " }"; // Build the script to evaluate; using the constructed paramObject rather than the original parameter list. // (This is the normal form for a component or directive calling through an attribute.) scriptOut = matches[1] + paramObject + matches[3]; return scope.$eval(scriptOut); } function findParentElem(name, childElem) { while (childElem && childElem.length) { if (childElem[0].nodeName === name.toUpperCase()) return childElem; childElem = childElem.parent(); } return undefined; } angular .module("ngal.ui") .factory("ngal.ui.directiveUtil", [function() { return { callScriptFn: callScriptFn, findParentElem: findParentElem }; }]); })();