~holger-seelig/cobweb.js/trunk

« back to all changes in this revision

Viewing changes to cobweb.js/cobweb/Fields/SFNode.js

  • Committer: Holger Seelig
  • Date: 2017-08-22 04:53:24 UTC
  • Revision ID: holger.seelig@yahoo.de-20170822045324-4of4xxgt79669gbt
Switched to npm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
 
 *******************************************************************************
3
 
 *
4
 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
 
 *
6
 
 * Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
7
 
 *
8
 
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
9
 
 *
10
 
 * The copyright notice above does not evidence any actual of intended
11
 
 * publication of such source code, and is an unpublished work by create3000.
12
 
 * This material contains CONFIDENTIAL INFORMATION that is the property of
13
 
 * create3000.
14
 
 *
15
 
 * No permission is granted to copy, distribute, or create derivative works from
16
 
 * the contents of this software, in whole or in part, without the prior written
17
 
 * permission of create3000.
18
 
 *
19
 
 * NON-MILITARY USE ONLY
20
 
 *
21
 
 * All create3000 software are effectively free software with a non-military use
22
 
 * restriction. It is free. Well commented source is provided. You may reuse the
23
 
 * source in any way you please with the exception anything that uses it must be
24
 
 * marked to indicate is contains 'non-military use only' components.
25
 
 *
26
 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
27
 
 *
28
 
 * Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
29
 
 *
30
 
 * This file is part of the Cobweb Project.
31
 
 *
32
 
 * Cobweb is free software: you can redistribute it and/or modify it under the
33
 
 * terms of the GNU General Public License version 3 only, as published by the
34
 
 * Free Software Foundation.
35
 
 *
36
 
 * Cobweb is distributed in the hope that it will be useful, but WITHOUT ANY
37
 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
38
 
 * A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
39
 
 * details (a copy is included in the LICENSE file that accompanied this code).
40
 
 *
41
 
 * You should have received a copy of the GNU General Public License version 3
42
 
 * along with Cobweb.  If not, see <http://www.gnu.org/licenses/gpl.html> for a
43
 
 * copy of the GPLv3 License.
44
 
 *
45
 
 * For Silvio, Joy and Adi.
46
 
 *
47
 
 ******************************************************************************/
48
 
 
49
 
 
50
 
define ([
51
 
        "jquery",
52
 
        "cobweb/Basic/X3DField",
53
 
        "cobweb/Bits/X3DConstants",
54
 
],
55
 
function ($, X3DField, X3DConstants)
56
 
{
57
 
"use strict";
58
 
 
59
 
        var handler =
60
 
        {
61
 
                get: function (target, key)
62
 
                {
63
 
                        if (key in target)
64
 
                                return target [key];
65
 
 
66
 
                        try
67
 
                        {
68
 
                                var
69
 
                                        field      = target .getValue () .getField (key),
70
 
                                        accessType = field .getAccessType ();
71
 
 
72
 
                                // Specification conform would be: accessType & X3DConstants .outputOnly.
73
 
                                // But we allow read access to plain fields, too.
74
 
                                if (accessType === X3DConstants .inputOnly)
75
 
                                        return undefined;
76
 
 
77
 
                                return field .valueOf ();
78
 
                        }
79
 
                        catch (error)
80
 
                        {
81
 
                                return undefined;
82
 
                        }
83
 
                },
84
 
                set: function (target, key, value)
85
 
                {
86
 
                        if (key in target)
87
 
                        {
88
 
                                target [key] = value;
89
 
                                return true;
90
 
                        }
91
 
 
92
 
                        try
93
 
                        {
94
 
                                var
95
 
                                        field      = target .getValue () .getField (key),
96
 
                                        accessType = field .getAccessType ();
97
 
 
98
 
                                if (accessType & X3DConstants .inputOnly)
99
 
                                        field .setValue (value);
100
 
 
101
 
                                return true;
102
 
                        }
103
 
                        catch (error)
104
 
                        {
105
 
                                console .error (target, key, error);
106
 
                                return false;
107
 
                        }
108
 
                },
109
 
        };
110
 
 
111
 
        function SFNode (value)
112
 
        {
113
 
           if (this instanceof SFNode)
114
 
           {
115
 
                        if (value)
116
 
                        {
117
 
                                value .addParent (this);
118
 
 
119
 
                                X3DField .call (this, value);
120
 
                        }
121
 
                        else
122
 
                                X3DField .call (this, null);
123
 
 
124
 
                        return new Proxy (this, handler);
125
 
                }
126
 
 
127
 
                return SFNode .call (Object .create (SFNode .prototype), value);
128
 
        }
129
 
 
130
 
        SFNode .prototype = $.extend (Object .create (X3DField .prototype),
131
 
        {
132
 
                constructor: SFNode,
133
 
                _cloneCount: 0,
134
 
                clone: function ()
135
 
                {
136
 
                        return new SFNode (this .getValue ());
137
 
                },
138
 
                copy: function (executionContext)
139
 
                {
140
 
                        var value = this .getValue ();
141
 
                        
142
 
                        if (value)
143
 
                                return new SFNode (value .copy (executionContext));
144
 
 
145
 
                        return new SFNode ();
146
 
                },
147
 
                getTypeName: function ()
148
 
                {
149
 
                        return "SFNode";
150
 
                },
151
 
                getType: function ()
152
 
                {
153
 
                        return X3DConstants .SFNode;
154
 
                },
155
 
                equals: function (node)
156
 
                {
157
 
                        if (node)
158
 
                                return this .getValue () === node .getValue ();
159
 
 
160
 
                        return this .getValue () === null;
161
 
                },
162
 
                isDefaultValue: function ()
163
 
                {
164
 
                        return this .getValue () === null;
165
 
                },
166
 
                set: function (value)
167
 
                {
168
 
                        var current = this .getValue ();
169
 
 
170
 
                        if (current)
171
 
                        {
172
 
                                current .removeClones (this ._cloneCount);
173
 
                                current .removeParent (this);
174
 
                        }
175
 
 
176
 
                        if (value)
177
 
                        {
178
 
                                value .addParent (this);
179
 
                                value .addClones (this ._cloneCount);
180
 
 
181
 
                                X3DField .prototype .set .call (this, value);
182
 
                        }
183
 
                        else
184
 
                                X3DField .prototype .set .call (this, null);
185
 
                },
186
 
                getNodeTypeName: function ()
187
 
                {
188
 
                        var value = this .getValue ();
189
 
 
190
 
                        if (value)
191
 
                                return value .getTypeName ();
192
 
 
193
 
                        throw new Error ("SFNode.getNodeTypeName: node is null.");
194
 
                },
195
 
                getNodeName: function ()
196
 
                {
197
 
                        var value = this .getValue ();
198
 
 
199
 
                        if (value)
200
 
                                return value .getName ();
201
 
 
202
 
                        throw new Error ("SFNode.getNodeName: node is null.");
203
 
                },
204
 
                getNodeType: function ()
205
 
                {
206
 
                        var value = this .getValue ();
207
 
 
208
 
                        if (value)
209
 
                                return value .getType () .slice ();
210
 
 
211
 
                        throw new Error ("SFNode.getNodeType: node is null.");
212
 
                },
213
 
                getFieldDefinitions: function ()
214
 
                {
215
 
                        var value = this .getValue ();
216
 
 
217
 
                        if (value)
218
 
                                return value .getFieldDefinitions ();
219
 
 
220
 
                        throw new Error ("SFNode.getFieldDefinitions: node is null.");
221
 
                },
222
 
                addClones: function (count)
223
 
                {
224
 
                        var value = this .getValue ();
225
 
 
226
 
                        this ._cloneCount += count;
227
 
 
228
 
                        if (value)
229
 
                                value .addClones (count);
230
 
                },
231
 
                removeClones: function (count)
232
 
                {
233
 
                        var value = this .getValue ();
234
 
 
235
 
                        this ._cloneCount -= count;
236
 
 
237
 
                        if (value)
238
 
                                value .removeClones (count);
239
 
                },
240
 
                valueOf: function ()
241
 
                {
242
 
                        if (this .getValue ())
243
 
                                return this;
244
 
 
245
 
                        return null;    
246
 
                },
247
 
                toString: function ()
248
 
                {
249
 
                        var node = this .getValue ();
250
 
 
251
 
                        return node ? node .toString () : "NULL";
252
 
                },
253
 
                toVRMLString: function ()
254
 
                {
255
 
                        var node = this .getValue ();
256
 
 
257
 
                        return node ? node .toVRMLString () : "NULL";
258
 
                },
259
 
                toXMLStream: function (stream)
260
 
                {
261
 
                        var node = this .getValue ();
262
 
 
263
 
                        stream .string += node ? node .toXMLString () : "NULL";
264
 
                },
265
 
                dispose: function ()
266
 
                {
267
 
                        this .set (null);
268
 
 
269
 
                        X3DField .prototype .dispose .call (this);
270
 
                },
271
 
        });
272
 
 
273
 
        return SFNode;
274
 
});