UNPKG

babel-angular

Version:

Helper library for using classes to create Angular services, components, controllers, etc using ES6 classes

136 lines (112 loc) 5.34 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; exports.toCamelCase = toCamelCase; exports.inject = inject; exports.injectDependencies = injectDependencies; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } function toCamelCase(name) { name = name.replace(/\_([a-zA-Z])/g, function (x) { return x.replace('_', '').toUpperCase(); }); return '' + name[0].toLowerCase() + name.slice(1); } /** * Injects dependencies into a class * @param {Function} target - Class target * @param {Array} target.$inject - array of injections * @param {Function} target.handleInjections - Injects dependency parameters into the scope * @param {Array} dependencies - Array of dependencies to inject * @returns {DependencyInjection} */ function commonInject(target, dependencies) { // Instantiate the $inject member. target.$inject = target.$inject || []; // Add dependencies to $inject member if they are not yet there. var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = dependencies[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var dependency = _step.value; if (target.$inject.indexOf(dependency) === -1) { target.$inject.push(dependency); } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator['return']) { _iterator['return'](); } } finally { if (_didIteratorError) { throw _iteratorError; } } } if (!target.prototype.handleInjections) { /** * @class DependencyInjection * Contains the required functions and hook necessary to register dependencies. * The super() chain will instantiate all dependencies on this. */ return (function (_target) { /** * Pass along the arguments to the target and call the handleInjections method. * @param {Array} args - Array of arguments */ function DependencyInjection() { _classCallCheck(this, DependencyInjection); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _get(Object.getPrototypeOf(DependencyInjection.prototype), 'constructor', this).apply(this, args); this.handleInjections(args); } _inherits(DependencyInjection, _target); _createClass(DependencyInjection, [{ key: 'handleInjections', /** * Loop through parameters and add them to the scope/this. * @param {Array} parameters */ value: function handleInjections(parameters) { var _this = this; parameters.forEach(function (param, idx) { _this[_this.constructor.$inject[idx]] = param; }); } }]); return DependencyInjection; })(target); } } /** * Decorator to inject dependencies into angular components. * @param dependencies * @returns {Function} */ function inject(dependencies) { /** * @var {Object} target */ return function (target) { return commonInject(target, dependencies); }; } /** * Non ES7 way of doing the same thing, Just call injectInto(className, [depencency array]); * @param target * @param dependencies * @returns {*} */ function injectDependencies(target, dependencies) { return commonInject(target, dependencies); }