jw-ng-forward
Version:
Temporary package. The default solution for those that want to write Angular 2.x style code in Angular 1.x
147 lines (146 loc) • 6.7 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 module_1 = require("../classes/module");
var events_1 = require("../events/events");
var inputs_builder_1 = require("../properties/inputs-builder");
var directive_controller_1 = require("../util/directive-controller");
var parse_selector_1 = __importDefault(require("../util/parse-selector"));
var writers_1 = require("../writers");
var input_output_1 = require("./input-output");
var providers_1 = require("./providers");
var TYPE = 'component';
exports.componentHooks = {
_after: [],
_extendDDO: [],
_beforeCtrlInvoke: [],
_afterCtrlInvoke: [],
after: function (fn) {
this._after.push(fn);
},
extendDDO: function (fn) {
this._extendDDO.push(fn);
},
beforeCtrlInvoke: function (fn) {
this._beforeCtrlInvoke.push(fn);
},
afterCtrlInvoke: function (fn) {
this._afterCtrlInvoke.push(fn);
},
};
function Component(_a) {
var selector = _a.selector, controllerAs = _a.controllerAs, template = _a.template, templateUrl = _a.templateUrl, _b = _a.bindings, bindings = _b === void 0 ? {} : _b, _c = _a.transclude, transclude = _c === void 0 ? true : _c, _d = _a.providers, providers = _d === void 0 ? [] : _d, _e = _a.inputs, inputs = _e === void 0 ? [] : _e, _f = _a.outputs, outputs = _f === void 0 ? [] : _f, _g = _a.pipes, pipes = _g === void 0 ? [] : _g, _h = _a.directives, directives = _h === void 0 ? [] : _h, _j = _a.require, require = _j === void 0 ? {} : _j;
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('transclude', transclude, t);
writers_1.componentStore.set('bindings', bindings, t);
writers_1.componentStore.set('require', require, 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);
}
View({
selector: selector,
template: template,
templateUrl: templateUrl,
pipes: pipes,
directives: directives,
})(t);
};
}
exports.Component = Component;
function View(_a) {
var selector = _a.selector, template = _a.template, templateUrl = _a.templateUrl, _b = _a.pipes, pipes = _b === void 0 ? [] : _b, _c = _a.directives, directives = _c === void 0 ? [] : _c;
return function (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.View = View;
module_1.Module.addProvider(TYPE, function (target, name, injects, ngModule) {
var ddo = {};
writers_1.componentStore.forEach(function (val, key) { return ddo[key] = val; }, target);
if (target.state) {
for (var key in ddo.inputMap) {
ddo.bindings[key] = '<';
}
}
else {
ddo.bindings = Object.assign(ddo.bindings, inputs_builder_1.inputsMap(ddo.inputMap));
}
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 (ddo.template && ddo.template.replace) {
ddo.template = ddo.template.replace(/ng-content/g, 'ng-transclude');
}
exports.componentHooks._extendDDO.forEach(function (hook) { return hook(ddo, target, name, injects, ngModule); });
ngModule.component(name, ddo);
exports.componentHooks._after.forEach(function (hook) { return hook(target, name, injects, ngModule); });
});