Sidebar.History.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // Checkbox 'Save History'
  12. var saveHistorySpan = new UI.Span().setPosition( 'absolute' ).setRight( '8px' );
  13. var saveHistoryCheckbox = new UI.Checkbox( config.getKey( 'settings/history' ) ).onChange( function () {
  14. var value = this.getValue();
  15. config.setKey( 'settings/history', value );
  16. if ( value ) {
  17. alert( 'The history will be preserved across sessions.\nThis can have an impact on performance when working with textures.' );
  18. var lastUndoCmd = history.undos[ history.undos.length - 1 ];
  19. var lastUndoId = ( lastUndoCmd !== undefined ) ? lastUndoCmd.id : 0;
  20. editor.history.enableSerialization( lastUndoId );
  21. } else {
  22. signals.historyChanged.dispatch();
  23. }
  24. } );
  25. saveHistorySpan.add( saveHistoryCheckbox );
  26. saveHistorySpan.onClick( function ( event ) {
  27. event.stopPropagation(); // Avoid panel collapsing
  28. } );
  29. container.add( saveHistorySpan );
  30. container.add( new UI.Break(), new UI.Break() );
  31. var ignoreObjectSelectedSignal = false;
  32. var outliner = new UI.Outliner( editor );
  33. outliner.onChange( function () {
  34. ignoreObjectSelectedSignal = true;
  35. editor.history.goToState( parseInt( outliner.getValue() ) );
  36. ignoreObjectSelectedSignal = false;
  37. } );
  38. outliner.onDblClick( function () {
  39. //editor.focusById( parseInt( outliner.getValue() ) );
  40. } );
  41. container.add( outliner );
  42. //
  43. var refreshUI = function () {
  44. var options = [];
  45. var enumerator = 1;
  46. ( function addObjects( objects, pad ) {
  47. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  48. var object = objects[ i ];
  49. var html = pad + "<span style='color: #0000cc '>" + enumerator ++ + ". Undo: " + object.name + "</span>";
  50. options.push( { value: object.id, html: html } );
  51. }
  52. } )( history.undos, '&nbsp;' );
  53. ( function addObjects( objects, pad ) {
  54. for ( var i = objects.length - 1; i >= 0; i -- ) {
  55. var object = objects[ i ];
  56. var html = pad + "<span style='color: #71544e'>" + enumerator ++ + ". Redo: " + object.name + "</span>";
  57. options.push( { value: object.id, html: html } );
  58. }
  59. } )( history.redos, '&nbsp;' );
  60. outliner.setOptions( options );
  61. };
  62. refreshUI();
  63. // events
  64. signals.editorCleared.add( refreshUI );
  65. signals.historyChanged.add( refreshUI );
  66. signals.historyChanged.add( function ( cmd ) {
  67. outliner.setValue( cmd !== undefined ? cmd.id : null );
  68. } );
  69. return container;
  70. };