angular2
Version:
Angular 2 - a web framework for modern web apps
142 lines (141 loc) • 5.61 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { isArray, isPresent, serializeEnum } from "angular2/src/facade/lang";
import { BaseException } from 'angular2/src/facade/exceptions';
import { Map, StringMapWrapper, MapWrapper } from "angular2/src/facade/collection";
import { RenderComponentType } from "angular2/src/core/render/api";
import { Injectable } from "angular2/src/core/di";
import { RenderStore } from 'angular2/src/web_workers/shared/render_store';
import { ViewEncapsulation, VIEW_ENCAPSULATION_VALUES } from 'angular2/src/core/metadata/view';
import { LocationType } from './serialized_types';
// PRIMITIVE is any type that does not need to be serialized (string, number, boolean)
// We set it to String so that it is considered a Type.
export const PRIMITIVE = String;
export let Serializer = class Serializer {
constructor(_renderStore) {
this._renderStore = _renderStore;
}
serialize(obj, type) {
if (!isPresent(obj)) {
return null;
}
if (isArray(obj)) {
return obj.map(v => this.serialize(v, type));
}
if (type == PRIMITIVE) {
return obj;
}
if (type == RenderStoreObject) {
return this._renderStore.serialize(obj);
}
else if (type === RenderComponentType) {
return this._serializeRenderComponentType(obj);
}
else if (type === ViewEncapsulation) {
return serializeEnum(obj);
}
else if (type === LocationType) {
return this._serializeLocation(obj);
}
else {
throw new BaseException("No serializer for " + type.toString());
}
}
deserialize(map, type, data) {
if (!isPresent(map)) {
return null;
}
if (isArray(map)) {
var obj = [];
map.forEach(val => obj.push(this.deserialize(val, type, data)));
return obj;
}
if (type == PRIMITIVE) {
return map;
}
if (type == RenderStoreObject) {
return this._renderStore.deserialize(map);
}
else if (type === RenderComponentType) {
return this._deserializeRenderComponentType(map);
}
else if (type === ViewEncapsulation) {
return VIEW_ENCAPSULATION_VALUES[map];
}
else if (type === LocationType) {
return this._deserializeLocation(map);
}
else {
throw new BaseException("No deserializer for " + type.toString());
}
}
mapToObject(map, type) {
var object = {};
var serialize = isPresent(type);
map.forEach((value, key) => {
if (serialize) {
object[key] = this.serialize(value, type);
}
else {
object[key] = value;
}
});
return object;
}
/*
* Transforms a Javascript object (StringMap) into a Map<string, V>
* If the values need to be deserialized pass in their type
* and they will be deserialized before being placed in the map
*/
objectToMap(obj, type, data) {
if (isPresent(type)) {
var map = new Map();
StringMapWrapper.forEach(obj, (val, key) => { map.set(key, this.deserialize(val, type, data)); });
return map;
}
else {
return MapWrapper.createFromStringMap(obj);
}
}
_serializeLocation(loc) {
return {
'href': loc.href,
'protocol': loc.protocol,
'host': loc.host,
'hostname': loc.hostname,
'port': loc.port,
'pathname': loc.pathname,
'search': loc.search,
'hash': loc.hash,
'origin': loc.origin
};
}
_deserializeLocation(loc) {
return new LocationType(loc['href'], loc['protocol'], loc['host'], loc['hostname'], loc['port'], loc['pathname'], loc['search'], loc['hash'], loc['origin']);
}
_serializeRenderComponentType(obj) {
return {
'id': obj.id,
'templateUrl': obj.templateUrl,
'slotCount': obj.slotCount,
'encapsulation': this.serialize(obj.encapsulation, ViewEncapsulation),
'styles': this.serialize(obj.styles, PRIMITIVE)
};
}
_deserializeRenderComponentType(map) {
return new RenderComponentType(map['id'], map['templateUrl'], map['slotCount'], this.deserialize(map['encapsulation'], ViewEncapsulation), this.deserialize(map['styles'], PRIMITIVE));
}
};
Serializer = __decorate([
Injectable(),
__metadata('design:paramtypes', [RenderStore])
], Serializer);
export class RenderStoreObject {
}