-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathadapter.js
More file actions
265 lines (232 loc) · 8.75 KB
/
Copy pathadapter.js
File metadata and controls
265 lines (232 loc) · 8.75 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
var Events = require("../interface/notification.js");
var config = require("../interface/elements.js").config;
var Resource = require("../resource");
var URI = require("../utils/uri.js").URI;
/**
* A normal adapter that doesn't need to be connected to a DOM node
* @constructor
* @param {XML3D.base.AdapterFactory} factory - the factory this adapter was created from
*/
var Adapter = function(factory) {
this.factory = factory;
};
/**
* Connect an adapterHandle to a certain key.
* This will enable the ConnectedAdapterNotifcations for notifyChanged.
* @param {string} key - the key that will also be provided in connectAdapterChanged callback
* @param {XML3D.base.AdapterHandle} adapterHandle handle of adapter to be added
*/
Adapter.prototype.connectAdapterHandle = function(key, adapterHandle) {
if (!this.connectedAdapterHandles) {
this.connectedAdapterHandles = {};
this._bindedAdapterHandleCallback = adapterHandleCallback.bind(this);
}
this.disconnectAdapterHandle(key);
if (adapterHandle) {
this.connectedAdapterHandles[key] = adapterHandle;
this.connectedAdapterHandles[key].addListener(this._bindedAdapterHandleCallback);
}
else
delete this.connectedAdapterHandles[key];
};
/**
* Disconnects the adapter handle from the given key.
* @param {string} key - the key that was provided when this adapter handle was connected
*/
Adapter.prototype.disconnectAdapterHandle = function(key) {
if (this.connectedAdapterHandles && this.connectedAdapterHandles[key]) {
this.connectedAdapterHandles[key].removeListener(this._bindedAdapterHandleCallback);
delete this.connectedAdapterHandles[key];
}
};
/**
* Disconnects all adapter handles.
*/
Adapter.prototype.clearAdapterHandles = function() {
for (var i in this.connectedAdapterHandles) {
this.connectedAdapterHandles[i].removeListener(this._bindedAdapterHandleCallback);
}
this.connectedAdapterHandles = null;
};
/**
* Get the connected AdapterHandle of a certain key.
* This will only return AdapterHandles previously added via connectAdapterHandle
* @param {string} key
* @return {?AdapterHandle} the adapter of that key, or null if not available
*/
Adapter.prototype.getConnectedAdapterHandle = function(key) {
return this.connectedAdapterHandles && this.connectedAdapterHandles[key];
};
/**
* Get the connected adapter of a certain key.
* This will only return adapters of AdapterHandles previously added via connectAdapter
* @param {string} key
* @return {?XML3D.base.Adapter} the adapter of that key, or null if not available
*/
Adapter.prototype.getConnectedAdapter = function(key) {
var handle = this.getConnectedAdapterHandle(key);
return handle && handle.getAdapter();
};
/**
* This function is called, when the adapater is detached from the node.
* At this point, the adapater should disconnect from any other adapter and prepare to be properly garbage collected
*/
Adapter.prototype.dispose = function() {
};
/**
* Internal function that converts an AdapterHandleNotification to a ConnectedAdapterNotification
* @private
* @param {Events.AdapterHandleNotification} evt
*/
function adapterHandleCallback(evt) {
for (var key in this.connectedAdapterHandles) {
if (this.connectedAdapterHandles[key] == evt.adapterHandle) {
var subEvent = new Events.ConnectedAdapterNotification(evt, key);
this.notifyChanged(subEvent);
}
}
}
/**
* An Adapter connected to a DOMNode (possibly of an external document)
* @constructor
* @param {AdapterFactory} factory the AdapterFactory this adapter was created from
* @param {Object} node - DOM node of this Adapter
*/
var NodeAdapter = function(factory, node) {
Adapter.call(this, factory);
this.node = node;
};
XML3D.createClass(NodeAdapter, Adapter);
/**
* called by the factory after adding the adapter to the node
*/
NodeAdapter.prototype.init = function() {
};
/**
* Notifiction due to a change in DOM, related adapters and so on.
* @param {Events.Notification} e
*/
NodeAdapter.prototype.notifyChanged = function(e) {
};
/**
* @param {string|XML3D.URI} uri Uri to referred adapterHandle
* @param {Object=} aspectType Optional: the type of adapter (use same adapter type by default)
* @param {number=} canvasId Optional: the canvas id of the adapter (use canvas id of this adapter by default)
* @returns an AdapterHandle to the referred Adapter of the same aspect and canvasId
*/
NodeAdapter.prototype.getAdapterHandle = function(uri, aspectType, canvasId) {
canvasId = canvasId === undefined ? this.factory.canvasId : canvasId;
if (typeof uri == "string") {
uri = new URI(uri);
}
return Resource.getAdapterHandle(this.node, uri, aspectType || this.factory.aspect, canvasId);
};
/**
* notifies all adapter that refer to this adapter through AdapterHandles.
* @param {number?} type The type of change
*/
NodeAdapter.prototype.notifyOppositeAdapters = function(type) {
type = type || Events.ADAPTER_HANDLE_CHANGED;
return Resource.notifyNodeAdapterChange(this.node,
this.factory.aspect, this.factory.canvasId, type);
};
/**
* Depth-first traversal over element hierarchy
* @param {function(NodeAdapter)} callback
*/
NodeAdapter.prototype.traverse = function(callback) {
callback(this);
var children = this.getChildren();
for (var i=0; i < children.length; i++) {
var adapter = this.factory.getAdapter(children[i]);
adapter && adapter.traverse(callback);
}
};
/**
* Returns all child nodes of this adapter, including possible shadow dom/distributed nodes
* @returns {*|Array.<ProcessNode>|Array|HTMLElement[]}
*/
NodeAdapter.prototype.getChildren = function() {
var children = this.node.children;
if (this.node.shadowRoot) {
children = this.node.shadowRoot.children;
} else if (this.node.getDistributedNodes) {
children = this.node.getDistributedNodes();
}
return children;
};
/**
* @interface
*/
var IFactory = function() {
};
IFactory.prototype.createAdapter = function() {};
/**
* An adapter factory is responsible for creating adapter from a certain data source.
* Note that any AdapterFactory is registered with Resource
* @constructor
* @implements {IFactory}
* @param {Object} aspect The aspect this factory serves (e.g. XML3D.data or XML3D.webgl)
* @param {string|Array.<string>} mimetypes The mimetype this factory is compatible to
* @param {number} canvasId The id of the corresponding canvas handler. 0, if not dependent on any GLCanvasHandler
*/
var AdapterFactory = function(aspect, mimetypes, canvasId) {
this.aspect = aspect;
this.canvasId = canvasId || 0;
this.mimetypes = typeof mimetypes == "string" ? [ mimetypes] : mimetypes;
};
/** Implemented by subclass
* Create adapter from an object (node in case of an xml, and object in case of json)
* @param {object} obj
* @returns {?Adapter} created adapter or null if no adapter can be created
*/
AdapterFactory.prototype.createAdapter = function(obj) {
return null;
};
/**
* Checks if the adapter factory supports specified mimetype. Can be overridden by subclass.
* @param {String} mimetype
* @return {Boolean} true if the adapter factory supports specified mimetype
*/
AdapterFactory.prototype.supportsMimetype = function(mimetype) {
return this.mimetypes.indexOf(mimetype) != -1;
};
/**
* A NodeAdaperFactory is a AdapterFactory, that works specifically for DOM nodes / elements.
* @constructor
* @param {Object} aspect The aspect this factory serves (e.g. XML3D.data or XML3D.webgl)
* @param {number} canvasId The id of the corresponding canvas handler. 0, if not dependent on any GLCanvasHandler
*/
var NodeAdapterFactory = function(aspect, canvasId) {
AdapterFactory.call(this, aspect, ["text/xml", "application/xml"], canvasId);
};
XML3D.createClass(NodeAdapterFactory, AdapterFactory);
/**
* This function first checks, if an adapter has been already created for the corresponding node
* If yes, this adapter is returned, otherwise, a new adapter is created and returned.
* @param {Object} node
* @returns {Adapter} The adapter of the node
*/
NodeAdapterFactory.prototype.getAdapter = function(node) {
if(node && node._configured === undefined)
config.element(node);
if (!node || node._configured === undefined)
return null;
var elemHandler = node._configured;
var key = this.aspect + "_" + this.canvasId;
var adapter = elemHandler.adapters[key];
if (adapter !== undefined)
return adapter;
// No adapter found, try to create one
adapter = this.createAdapter(node);
if (adapter) {
elemHandler.adapters[key] = adapter;
adapter.init();
}
return adapter;
};
module.exports = {
NodeAdapter : NodeAdapter,
AdapterFactory : AdapterFactory,
NodeAdapterFactory : NodeAdapterFactory
};