UNPKG

4.05 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var Serializer_1;
9Object.defineProperty(exports, "__esModule", { value: true });
10const utils_1 = require("../utils");
11const component_1 = require("../utils/component");
12const components_1 = require("./components");
13const events_1 = require("./events");
14let Serializer = Serializer_1 = class Serializer extends utils_1.ChildableComponent {
15 initialize() {
16 this.router = new Map();
17 this.routes = [];
18 }
19 addComponent(name, componentClass) {
20 const component = super.addComponent(name, componentClass);
21 if (component.serializeGroup && component.serializeGroupSymbol) {
22 let match = this.router.get(component.serializeGroup);
23 if (!match) {
24 match = Array.from(this.router.values()).find(v => v.symbol === component.serializeGroupSymbol)
25 || { symbol: component.serializeGroupSymbol, group: [] };
26 this.router.set(component.serializeGroup, match);
27 this.routes.push(component.serializeGroup);
28 }
29 match.group.push(component);
30 match.group.sort((a, b) => (b.priority || 0) - (a.priority || 0));
31 }
32 return component;
33 }
34 removeComponent(name) {
35 const component = super.removeComponent(name);
36 const symbol = component && component.serializeGroupSymbol;
37 if (symbol) {
38 const values = Array.from(this.router.values());
39 for (let i = 0, len = values.length; i < len; i++) {
40 const idx = values[i].group.findIndex(o => o === symbol);
41 if (idx > -1) {
42 values[i].group.splice(idx, 1);
43 break;
44 }
45 }
46 }
47 return component;
48 }
49 removeAllComponents() {
50 super.removeAllComponents();
51 this.router = new Map();
52 this.routes = [];
53 }
54 toObject(value, obj) {
55 return this.findRoutes(value)
56 .reduce((result, curr) => curr.toObject(value, result), obj);
57 }
58 projectToObject(value, eventData) {
59 const eventBegin = new events_1.SerializeEvent(Serializer_1.EVENT_BEGIN, value);
60 if (eventData && eventData.begin) {
61 Object.assign(eventBegin, eventData.begin);
62 }
63 let project = eventBegin.output = {};
64 this.trigger(eventBegin);
65 project = this.toObject(value, project);
66 const eventEnd = new events_1.SerializeEvent(Serializer_1.EVENT_END, value);
67 if (eventData && eventData.end) {
68 Object.assign(eventEnd, eventData.end);
69 }
70 eventEnd.output = project;
71 this.trigger(eventEnd);
72 return project;
73 }
74 findRoutes(value) {
75 const routes = [];
76 for (let i = 0, len = this.routes.length; i < len; i++) {
77 if (this.routes[i](value)) {
78 const serializers = this.router.get(this.routes[i]).group;
79 for (let serializer of serializers) {
80 if (serializer.supports(value)) {
81 routes.push(serializer);
82 }
83 }
84 }
85 }
86 return routes;
87 }
88};
89Serializer.EVENT_BEGIN = 'begin';
90Serializer.EVENT_END = 'end';
91Serializer = Serializer_1 = __decorate([
92 component_1.Component({ name: 'serializer', internal: true, childClass: components_1.SerializerComponent })
93], Serializer);
94exports.Serializer = Serializer;
95//# sourceMappingURL=serializer.js.map
\No newline at end of file