jw-ng-forward
Version:
Temporary package. The default solution for those that want to write Angular 2.x style code in Angular 1.x
120 lines (119 loc) • 6.11 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var parse_selector_1 = __importDefault(require("../util/parse-selector"));
var writers_1 = require("../writers");
var providers_1 = require("./providers");
var module_1 = require("../classes/module");
var input_output_1 = require("./input-output");
var inputs_builder_1 = require("../properties/inputs-builder");
var events_1 = require("../events/events");
var helpers_1 = require("../util/helpers");
var directive_controller_1 = require("../util/directive-controller");
var component_1 = require("./component");
var TYPE = 'dircomp';
function DirComp(_a) {
var selector = _a.selector, controllerAs = _a.controllerAs, template = _a.template, templateUrl = _a.templateUrl, _b = _a.transclude, transclude = _b === void 0 ? true : _b, _c = _a.providers, providers = _c === void 0 ? [] : _c, _d = _a.inputs, inputs = _d === void 0 ? [] : _d, _e = _a.outputs, outputs = _e === void 0 ? [] : _e, _f = _a.pipes, pipes = _f === void 0 ? [] : _f, _g = _a.directives, directives = _g === void 0 ? [] : _g;
return function (t) {
if (!selector) {
throw new Error("Component Decorator Error in \"" + t.name + "\": Component selector must be provided");
}
var _a = parse_selector_1.default(selector), name = _a.name, restrict = _a.type;
writers_1.providerStore.set('name', name, t);
writers_1.providerStore.set('type', TYPE, t);
writers_1.bundleStore.set('selector', selector, t);
providers_1.Providers.apply(void 0, __spread(providers))(t, "while analyzing Component '" + t.name + "' providers");
writers_1.componentStore.set('restrict', restrict, t);
writers_1.componentStore.set('scope', {}, t);
writers_1.componentStore.set('transclude', transclude, t);
writers_1.componentStore.set('bindToController', true, t);
[
['inputs', inputs],
['providers', providers],
['directives', directives],
['outputs', outputs]
].forEach(function (_a) {
var _b = __read(_a, 2), propName = _b[0], propVal = _b[1];
if (propVal !== undefined && !Array.isArray(propVal)) {
throw new TypeError("Component Decorator Error in \"" + t.name + "\": Component " + propName + " must be an array");
}
});
input_output_1.writeMapMulti(t, inputs, 'inputMap');
var outputMap = input_output_1.writeMapMulti(t, outputs, 'outputMap');
Object.keys(outputMap).forEach(function (key) { return events_1.events.add(key); });
if (controllerAs === '$auto') {
writers_1.componentStore.set('controllerAs', name, t);
}
else if (controllerAs) {
writers_1.componentStore.set('controllerAs', controllerAs, t);
}
else {
writers_1.componentStore.set('controllerAs', 'ctrl', t);
}
if (t.link) {
writers_1.componentStore.set('link', t.link, t);
}
if (t.compile) {
writers_1.componentStore.set('compile', t.compile, t);
}
if (templateUrl) {
writers_1.componentStore.set('templateUrl', templateUrl, t);
}
else if (template) {
writers_1.componentStore.set('template', template, t);
}
else {
throw new Error("@Component config must include either a template or a template url for component with selector " + selector + " on " + t.name);
}
providers_1.Providers.apply(void 0, __spread(directives))(t, "while analyzing Component '" + t.name + "' directives");
providers_1.Providers.apply(void 0, __spread(pipes))(t, "while analyzing Component '" + t.name + "' pipes");
};
}
exports.DirComp = DirComp;
module_1.Module.addProvider(TYPE, function (target, name, injects, ngModule) {
var ddo = {};
writers_1.componentStore.forEach(function (val, key) { return ddo[key] = val; }, target);
var bindProp = 'bindToController';
ddo[bindProp] = inputs_builder_1.inputsMap(ddo.inputMap);
if (ddo.restrict !== 'E') {
throw new Error(helpers_1.createConfigErrorMessage(target, ngModule, "@Component selectors can only be elements. " +
"Perhaps you meant to use @Directive?"));
}
controller.$inject = ['$scope', '$element', '$attrs', '$transclude', '$injector'];
function controller($scope, $element, $attrs, $transclude, $injector) {
var locals = { $scope: $scope, $element: $element, $attrs: $attrs, $transclude: $transclude };
return directive_controller_1.directiveControllerFactory(this, injects, target, ddo, $injector, locals);
}
ddo.controller = controller;
if (typeof target.prototype.ngAfterViewInit === 'function') {
ddo.link = function () { return ddo.ngAfterViewInitBound(); };
}
if (ddo.template && ddo.template.replace) {
ddo.template = ddo.template.replace(/ng-content/g, 'ng-transclude');
}
component_1.componentHooks._extendDDO.forEach(function (hook) { return hook(ddo, target, name, injects, ngModule); });
ngModule.directive(name, function () { return ddo; });
component_1.componentHooks._after.forEach(function (hook) { return hook(target, name, injects, ngModule); });
});