Sidebar.History.js 2.6 KB

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