Sidebar.History.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. Sidebar.History = function ( editor ) {
  6. var signals = editor.signals;
  7. var config = editor.config;
  8. var history = editor.history;
  9. var container = new UI.Panel();
  10. container.add( new UI.Text( 'HISTORY' ) );
  11. //
  12. var persistent = new UI.THREE.Boolean( config.getKey( 'settings/history' ), 'persistent' );
  13. persistent.setPosition( 'absolute' ).setRight( '8px' );
  14. persistent.onChange( function () {
  15. var value = this.getValue();
  16. config.setKey( 'settings/history', value );
  17. if ( value ) {
  18. alert( 'The history will be preserved across sessions.\nThis can have an impact on performance when working with textures.' );
  19. var lastUndoCmd = history.undos[ history.undos.length - 1 ];
  20. var lastUndoId = ( lastUndoCmd !== undefined ) ? lastUndoCmd.id : 0;
  21. editor.history.enableSerialization( lastUndoId );
  22. } else {
  23. signals.historyChanged.dispatch();
  24. }
  25. } );
  26. container.add( persistent );
  27. container.add( new UI.Break(), new UI.Break() );
  28. var ignoreObjectSelectedSignal = false;
  29. var outliner = new UI.Outliner( editor );
  30. outliner.onChange( function () {
  31. ignoreObjectSelectedSignal = true;
  32. editor.history.goToState( parseInt( outliner.getValue() ) );
  33. ignoreObjectSelectedSignal = false;
  34. } );
  35. container.add( outliner );
  36. //
  37. var refreshUI = function () {
  38. var options = [];
  39. var enumerator = 1;
  40. function buildOption( object ) {
  41. var option = document.createElement( 'div' );
  42. option.value = object.id;
  43. return option;
  44. }
  45. ( function addObjects( objects ) {
  46. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  47. var object = objects[ i ];
  48. var option = buildOption( object );
  49. option.innerHTML = '&nbsp;' + object.name;
  50. options.push( option );
  51. }
  52. } )( history.undos );
  53. ( function addObjects( objects, pad ) {
  54. for ( var i = objects.length - 1; i >= 0; i -- ) {
  55. var object = objects[ i ];
  56. var option = buildOption( object );
  57. option.innerHTML = '&nbsp;' + object.name;
  58. option.style.opacity = 0.3;
  59. options.push( option );
  60. }
  61. } )( history.redos, '&nbsp;' );
  62. outliner.setOptions( options );
  63. };
  64. refreshUI();
  65. // events
  66. signals.editorCleared.add( refreshUI );
  67. signals.historyChanged.add( refreshUI );
  68. signals.historyChanged.add( function ( cmd ) {
  69. outliner.setValue( cmd !== undefined ? cmd.id : null );
  70. } );
  71. return container;
  72. };