Skip to content

Commit

Permalink
Adapters: Callback for attribute changes has an extra callback now
Browse files Browse the repository at this point in the history
Old callback: notifyChanged (also callback for structural and reference changes)
New callback: attributeChangedCallback (similar to custom element callback)
  • Loading branch information
Kristian Sons authored and ariyapour committed Nov 9, 2015
1 parent 6b3783c commit 1e9038e
Show file tree
Hide file tree
Showing 17 changed files with 458 additions and 431 deletions.
115 changes: 59 additions & 56 deletions src/data/adapter/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,10 @@ AssetAdapter.prototype.onTransformChange = function (attrName, matrix) {
this.asset.setTransform(matrix);
};


AssetAdapter.prototype.notifyChanged = function (evt) {
if (evt.type == Events.ADAPTER_HANDLE_CHANGED) {
this.connectedAdapterChanged(evt.key, evt.adapter);
if (evt.handleStatus == AdapterHandle.STATUS.NOT_FOUND) {
XML3D.debug.logError("Could not find <asset> element of url '" + evt.url + "' for " + evt.key);
}
} else if (evt.type == Events.NODE_INSERTED) {
updateChildren(this);

} else if (evt.type == Events.NODE_REMOVED) {
updateChildren(this);

} else if (evt.type == Events.VALUE_MODIFIED) {
var attr = evt.mutation.attributeName;
switch (attr) {
AssetAdapter.prototype.attributeChangedCallback = function(name, oldValue, newValue) {
switch (name) {
case "name":
this.asset.setName(this.node.getAttribute("name"));
this.asset.setName(newValue);
break;
case "material":
setMaterialUrl(this, this.asset);
Expand All @@ -135,14 +121,28 @@ AssetAdapter.prototype.notifyChanged = function (evt) {
this.transformFetcher && this.transformFetcher.update();
break;
case "src":
updateAdapterHandle(this, "src", this.node.getAttribute("src"));
updateAdapterHandle(this, "src", newValue);
break;
case "pick":
updatePickFilter(this);
break;
}
};


AssetAdapter.prototype.notifyChanged = function (evt) {
if (evt.type == Events.ADAPTER_HANDLE_CHANGED) {
this.connectedAdapterChanged(evt.key, evt.adapter);
if (evt.handleStatus == AdapterHandle.STATUS.NOT_FOUND) {
XML3D.debug.logError("Could not find <asset> element of url '" + evt.url + "' for " + evt.key);
}
} else if (evt.type == Events.NODE_INSERTED) {
updateChildren(this);

} else if (evt.type == Events.NODE_REMOVED) {
updateChildren(this);

} else if (evt.type == Events.THIS_REMOVED) {
} else if (evt.type == Events.THIS_REMOVED) {
this.clearAdapterHandles();
}
};
Expand Down Expand Up @@ -177,29 +177,30 @@ AssetDataAdapter.prototype.connectedAdapterChanged = function (attributeName, ad
}
};

AssetDataAdapter.prototype.attributeChangedCallback = function (name, oldValue, newValue) {
DataAdapter.prototype.attributeChangedCallback.call(this, name, oldValue, newValue);
switch (name) {
case "name":
this.assetEntry.setName(newValue);
break;
case "compute":
updatePostCompute(this);
break;
case "class":
updateClassNames(this);
break;
case "filter":
this.assetEntry.setPostFilter(newValue);
break;
case "includes":
updateIncludes(newValue);
break;
}
};

AssetDataAdapter.prototype.notifyChanged = function (evt) {
DataAdapter.prototype.notifyChanged.call(this, evt);
if (evt.type == Events.VALUE_MODIFIED) {
var attr = evt.mutation.attributeName;
switch (attr) {
case "name":
this.assetEntry.setName(this.node.getAttribute("name"));
break;
case "compute":
updatePostCompute(this);
break;
case "class":
updateClassNames(this);
break;
case "filter":
this.assetEntry.setPostFilter(this.node.getAttribute("filter"));
break;
case "includes":
updateIncludes(this.node.getAttribute("includes"));
break;
}

}
};

AssetDataAdapter.prototype.onTransformChange = function (attrName, matrix) {
Expand Down Expand Up @@ -260,36 +261,38 @@ var AssetMeshAdapter = function (factory, node) {
AssetDataAdapter.call(this, factory, node);
this.transformFetcher = new DOMTransformFetcher(this, "transform", "transform");
};
createClass(AssetMeshAdapter, AssetDataAdapter);

AssetMeshAdapter.prototype.init = function () {
AssetDataAdapter.prototype.init.call(this);
setMaterialUrl(this, this.assetEntry);
this.assetEntry.setMeshType(this.node.getAttribute("type") || "triangles");
this.assetEntry.setMatchFilter(this.node.getAttribute("match"));
this.transformFetcher.update();
};
AssetMeshAdapter.prototype.notifyChanged = function (evt) {
AssetDataAdapter.prototype.notifyChanged.call(this, evt);
if (evt.type == Events.VALUE_MODIFIED) {
var attr = evt.mutation.attributeName;
switch (attr) {
createClass(AssetMeshAdapter, AssetDataAdapter, {

init: function () {
AssetDataAdapter.prototype.init.call(this);
setMaterialUrl(this, this.assetEntry);
this.assetEntry.setMeshType(this.node.getAttribute("type") || "triangles");
this.assetEntry.setMatchFilter(this.node.getAttribute("match"));
this.transformFetcher.update();
},

attributeChangedCallback: function (name, oldValue, newValue) {
AssetDataAdapter.prototype.attributeChangedCallback.call(this, name, oldValue, newValue);
switch (name) {
case "material":
setMaterialUrl(this, this.assetEntry);
break;
case "match":
this.assetEntry.setMatchFilter(this.node.getAttribute("match"));
this.assetEntry.setMatchFilter(newValue);
break;
case "style":
case "transform":
this.transformFetcher.update();
break;
case "type":
this.assetEntry.setMeshType(this.node.getAttribute("type") || "triangles")
this.assetEntry.setMeshType(newValue || "triangles")
}
},

notifyChanged: function (evt) {
AssetDataAdapter.prototype.notifyChanged.call(this, evt);
}
};
});

module.exports = {
AssetAdapter: AssetAdapter, AssetMeshAdapter: AssetMeshAdapter, AssetDataAdapter: AssetDataAdapter
Expand Down
27 changes: 12 additions & 15 deletions src/data/adapter/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,26 +164,23 @@ DataAdapter.prototype.notifyChanged = function (evt) {
var removedXflowNode = adapter.getXflowNode();
this.xflowDataNode.removeChild(removedXflowNode);

} else if (evt.type === Events.VALUE_MODIFIED) {
var attr = evt.mutation.attributeName;

if (attr === "filter" && !this.assetData) {
this.xflowDataNode.setFilter(this.node.getAttribute(attr));
}
else if (attr === "compute" && !this.assetData) {
updateCompute(this);
}
else if (attr === "src") {
this.updateAdapterHandle(attr, this.node.getAttribute(attr));
} else if (attr === "platform") {
updatePlatform(this);
}

} else if (evt.type === Events.THIS_REMOVED) {
this.clearAdapterHandles();
}
};

DataAdapter.prototype.attributeChangedCallback = function (name, oldValue, newValue) {
if (name === "filter" && !this.assetData) {
this.xflowDataNode.setFilter(newValue);
} else if (name === "compute" && !this.assetData) {
updateCompute(this);
} else if (name === "src") {
this.updateAdapterHandle(name, newValue);
} else if (name === "platform") {
updatePlatform(this);
}
};

DataAdapter.prototype.connectedAdapterChanged = function (key, adapter /*, status */) {
if (key === "src") {
this.xflowDataNode.sourceNode = adapter ? adapter.getXflowNode() : null;
Expand Down
143 changes: 73 additions & 70 deletions src/data/adapter/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@ var Util = require("../../utils/misc.js");
var Resource = require("../../base/resourcemanager.js").Resource;
var NodeAdapter = require("../../base/adapter.js").NodeAdapter;
var createClass = XML3D.createClass;
/**
* SinkDataAdapter represents the sink in the data hierarchy (no parents).
* @constructor
* @extends {DataAdapter}
* @param factory
* @param node
*/
var SinkDataAdapter = function(factory, node) {
DataAdapter.call(this, factory, node);
};
createClass(SinkDataAdapter, DataAdapter);

/**
* SinkDataAdapter represents the sink in the data hierarchy (no parents).
* @constructor
* @extends {DataAdapter}
* @param factory
* @param node
*/
var SinkDataAdapter = function(factory, node) {
DataAdapter.call(this, factory, node);
};
createClass(SinkDataAdapter, DataAdapter, {

/**
* Indicates whether this DataAdapter is a SinkAdapter (has no parent
* DataAdapter).
*
* @returns true if this DataAdapter is a SinkAdapter, otherwise false.
*/
SinkDataAdapter.prototype.isSinkAdapter = function() {
isSinkAdapter: function () {
return true;
};
},

/**
* Returns String representation of this DataAdapter
*/
SinkDataAdapter.prototype.toString = function() {
toString: function () {
return "XML3D.data.SinkDataAdapter";
};
}
});


var ImgDataAdapter = function(factory, node) {
Expand All @@ -43,62 +45,63 @@ var createClass = XML3D.createClass;
if (node.src)
this.createImageFromURL(node.src);
};
createClass(ImgDataAdapter, NodeAdapter);

/**
* Creates a new image object
*
* @param {string} url
*/
ImgDataAdapter.prototype.createImageFromURL = function(url) {
var that = this;
var uri = new URI(url).getAbsoluteURI(this.node.ownerDocument._documentURL || this.node.ownerDocument.URL);
var onload = function (e, image) {
createClass(ImgDataAdapter, NodeAdapter, {

/**
* Creates a new image object
*
* @param {string} url
*/
createImageFromURL: function (url) {
var that = this;
var uri = new URI(url).getAbsoluteURI(this.node.ownerDocument._documentURL || this.node.ownerDocument.URL);
var onload = function (e, image) {
if (that.textureEntry) {
that.textureEntry.setImage(image, true);
}
};
var onerror = function (e, image) {
XML3D.debug.logError("Could not load image URI=" + image.src);
};
this.image = Resource.getImage(uri, onload, onerror);
if (that.textureEntry) {
that.textureEntry.setImage(image, true);
that.textureEntry.setImage(this.image, true);
}
};
var onerror = function (e, image) {
XML3D.debug.logError("Could not load image URI="+image.src);
};
this.image = Resource.getImage(uri, onload, onerror);
if (that.textureEntry) {
that.textureEntry.setImage(this.image, true);
}
};

/**
* @param {Xflow.TextureEntry} entry
*/
ImgDataAdapter.prototype.setTextureEntry = function(entry) {
this.textureEntry = entry;
if (this.image) {
this.textureEntry.setImage(this.image, true);
}
};
},

/**
* @param {Xflow.TextureEntry} entry
*/
setTextureEntry: function (entry) {
this.textureEntry = entry;
if (this.image) {
this.textureEntry.setImage(this.image, true);
}
},

ImgDataAdapter.prototype.notifyChanged = function(evt) {
if (evt.type == Events.VALUE_MODIFIED) {
var attr = evt.mutation.attributeName;
if(attr == "src"){
this.createImageFromURL(this.node.src);
attributeChangedCallback: function (name, oldValue, newValue) {
if (name == "src") {
this.createImageFromURL(newValue);
}
};
};
},

ImgDataAdapter.prototype.getValue = function(cb, obj) {
return this.image;
};
notifyChanged: function (evt) {
},

ImgDataAdapter.prototype.getOutputs = function() {
var result = {};
result['image'] = this;
return result;
};
getValue: function (cb, obj) {
return this.image;
},

ImgDataAdapter.prototype.resolveScript = function() {
return null;
};
getOutputs: function () {
var result = {};
result['image'] = this;
return result;
},

resolveScript: function () {
return null;
}
});

var VideoDataAdapter = function(factory, node) {
DataAdapter.call(this, factory, node);
Expand Down Expand Up @@ -176,12 +179,12 @@ var createClass = XML3D.createClass;
};

VideoDataAdapter.prototype.notifyChanged = function(evt) {
if (evt.type == Events.VALUE_MODIFIED) {
var attr = evt.mutation.attributeName;
if(attr == "src"){
this.createVideoFromURL(this.node.src);
}
};
};

VideoDataAdapter.prototype.attributeChangedCallback = function (name, oldValue, newValue) {
if (name == "src") {
this.createVideoFromURL(newValue);
}
};

VideoDataAdapter.prototype.getValue = function(cb, obj) {
Expand Down
Loading

0 comments on commit 1e9038e

Please sign in to comment.