1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/**
* $Id: mxCodecRegistry.js,v 1.12 2010-04-30 13:18:21 gaudenz Exp $
* Copyright (c) 2006-2010, JGraph Ltd
*/
var mxCodecRegistry =
{
/**
* Class: mxCodecRegistry
*
* Singleton class that acts as a global registry for codecs.
*
* Adding an <mxCodec>:
*
* 1. Define a default codec with a new instance of the
* object to be handled.
*
* (code)
* var codec = new mxObjectCodec(new mxGraphModel());
* (end)
*
* 2. Define the functions required for encoding and decoding
* objects.
*
* (code)
* codec.encode = function(enc, obj) { ... }
* codec.decode = function(dec, node, into) { ... }
* (end)
*
* 3. Register the codec in the <mxCodecRegistry>.
*
* (code)
* mxCodecRegistry.register(codec);
* (end)
*
* <mxObjectCodec.decode> may be used to either create a new
* instance of an object or to configure an existing instance,
* in which case the into argument points to the existing
* object. In this case, we say the codec "configures" the
* object.
*
* Variable: codecs
*
* Maps from constructor names to codecs.
*/
codecs: [],
/**
* Variable: aliases
*
* Maps from classnames to codecnames.
*/
aliases: [],
/**
* Function: register
*
* Registers a new codec and associates the name of the template
* constructor in the codec with the codec object.
*
* Parameters:
*
* codec - <mxObjectCodec> to be registered.
*/
register: function(codec)
{
if (codec != null)
{
var name = codec.getName();
mxCodecRegistry.codecs[name] = codec;
var classname = mxUtils.getFunctionName(codec.template.constructor);
if (classname != name)
{
mxCodecRegistry.addAlias(classname, name);
}
}
return codec;
},
/**
* Function: addAlias
*
* Adds an alias for mapping a classname to a codecname.
*/
addAlias: function(classname, codecname)
{
mxCodecRegistry.aliases[classname] = codecname;
},
/**
* Function: getCodec
*
* Returns a codec that handles objects that are constructed
* using the given constructor.
*
* Parameters:
*
* ctor - JavaScript constructor function.
*/
getCodec: function(ctor)
{
var codec = null;
if (ctor != null)
{
var name = mxUtils.getFunctionName(ctor);
var tmp = mxCodecRegistry.aliases[name];
if (tmp != null)
{
name = tmp;
}
codec = mxCodecRegistry.codecs[name];
// Registers a new default codec for the given constructor
// if no codec has been previously defined.
if (codec == null)
{
try
{
codec = new mxObjectCodec(new ctor());
mxCodecRegistry.register(codec);
}
catch (e)
{
// ignore
}
}
}
return codec;
}
};
|