ui.editor.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. UI.ScriptEditor = function () {
  5. UI.Panel.call( this );
  6. var scope = this;
  7. var timeout;
  8. var event = new UI.Text( '' );
  9. this.add( event );
  10. this.add( new UI.Break() );
  11. var textarea = new UI.TextArea();
  12. textarea.setWidth( '100%' );
  13. textarea.setHeight( '100px' );
  14. textarea.onKeyUp( function () {
  15. clearTimeout( timeout );
  16. timeout = setTimeout( function () {
  17. var object = editor.selected;
  18. var source = textarea.getValue();
  19. try {
  20. ( new Function( 'event', source ).bind( object.clone() ) )( {} );
  21. textarea.dom.classList.add( 'success' );
  22. textarea.dom.classList.remove( 'fail' );
  23. } catch ( error ) {
  24. console.error( error );
  25. textarea.dom.classList.remove( 'success' );
  26. textarea.dom.classList.add( 'fail' );
  27. return;
  28. }
  29. if ( scope.onChangeCallback !== undefined ) {
  30. scope.onChangeCallback();
  31. }
  32. }, 500 );
  33. } );
  34. this.add( textarea );
  35. this.event = event;
  36. this.textarea = textarea;
  37. };
  38. UI.ScriptEditor.prototype = Object.create( UI.Panel.prototype );
  39. UI.ScriptEditor.prototype.constructor = UI.ScriptEditor;
  40. UI.ScriptEditor.prototype.getValue = function () {
  41. return { event: this.event.getValue(), source: this.textarea.getValue() };
  42. };
  43. UI.ScriptEditor.prototype.setValue = function ( value ) {
  44. this.event.setValue( value.event );
  45. this.textarea.setValue( value.source );
  46. return this;
  47. };
  48. UI.ScriptEditor.prototype.onChange = function ( callback ) {
  49. this.onChangeCallback = callback;
  50. return this;
  51. };