r3-legacy/src/r3-customCode.js

139 lines
3.2 KiB
JavaScript

/**
* R3.CustomCode
* @param apiComponent
* @constructor
*/
R3.CustomCode = function(
apiComponent
) {
__RUNTIME_COMPONENT__;
this.editor = null;
__UPGRADE_TO_RUNTIME__;
};
R3.CustomCode.prototype = Object.create(R3.Component.prototype);
R3.CustomCode.prototype.constructor = R3.CustomCode;
R3.CustomCode.prototype.createInstance = function() {
try {
this.instance = new Function('data', this.code).bind(this);
} catch (error) {
/**
* Set the instance to true here to indicate that even though the compilation failed, the instance will be fine and
* this component loaded fine.
*/
this.instance = new Function('data', "console.log('compilation failed for : " + this.name + "');").bind(this);
}
__CREATE_INSTANCE__;
};
/**
* Updates the instance with the current state
*/
R3.CustomCode.prototype.updateInstance = function(property) {
if (property === 'name') {
R3.Event.Emit(
R3.Event.NAME_UPDATE,
{
component : this
}
);
return;
}
if (property === 'code') {
try {
this.instance = new Function('data', this.code).bind(this);
this.emit(
R3.Event.COMPILE_SUCCESS,
{
component: this
}
)
} catch (error) {
this.instance = new Function('data', "console.log('compilation update failed for : " + this.name + "');").bind(this);
this.emit(
R3.Event.COMPILE_FAILED,
{
component: this
}
)
}
return;
}
if (property === 'eventId') {
this.emit(
R3.Event.EVENT_ID_UPDATE,
{
component : this
}
)
}
__UPDATE_INSTANCE__;
};
R3.CustomCode.prototype.launchEditor = function(){
if (this instanceof R3.D3.Shader.Vertex) {
this.editor = this.coder.instance(
document.body,
{
value: this.code,
mode: 'x-shader/x-vertex',
lineNumbers: true,
scrollbarStyle: 'overlay',
indentWithTabs: true,
indentUnit: 4
}
);
} else if (this instanceof R3.D3.Shader.Fragment) {
this.editor = this.coder.instance(
document.body,
{
value: this.code,
mode: 'x-shader/x-fragment',
lineNumbers: true,
scrollbarStyle: 'overlay',
indentWithTabs: true,
indentUnit: 4
}
);
} else {
this.editor = this.coder.instance(
document.body,
{
value: this.code,
mode: 'javascript',
lineNumbers: true,
scrollbarStyle: 'overlay',
indentWithTabs: true,
indentUnit: 4
}
);
}
this.editor.on('change', function(){
this.code = this.editor.getValue();
this.updateInstance('code');
}.bind(this))
};
R3.CustomCode.prototype.closeEditor = function(){
var dom = this.editor.getWrapperElement();
dom.parentElement.removeChild(dom);
};