Sidebar.History.js 3.0 KB

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