Sidebar.History.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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' ).setLeft( '180px' ).setFontSize( '13px' );
  17. var saveHistoryCheckbox = new UI.Checkbox( config.getKey( 'project/history/stored' ) ).setLeft( '50px' ).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 a browser refresh.\nThis can have an impact on performance (mainly 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.add( new UI.Text( 'Save History' ).setPosition( 'relative' ).setLeft( '5px' ) );
  31. saveHistorySpan.onClick( function ( event ) {
  32. event.stopPropagation(); // Avoid panel collapsing
  33. } );
  34. container.addStatic( saveHistorySpan );
  35. container.add( new UI.Break() );
  36. var ignoreObjectSelectedSignal = false;
  37. var outliner = new UI.Outliner( editor );
  38. outliner.onChange( function () {
  39. ignoreObjectSelectedSignal = true;
  40. editor.history.goToState( parseInt( outliner.getValue() ) );
  41. ignoreObjectSelectedSignal = false;
  42. } );
  43. outliner.onDblClick( function () {
  44. //editor.focusById( parseInt( outliner.getValue() ) );
  45. } );
  46. container.add( outliner );
  47. //
  48. var refreshUI = function () {
  49. var options = [];
  50. var enumerator = 1;
  51. ( function addObjects( objects, pad ) {
  52. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  53. var object = objects[ i ];
  54. var html = pad + "<span style='color: #0000cc '>" + enumerator ++ + ". Undo: " + object.name + "</span>";
  55. options.push( { value: object.id, html: html } );
  56. }
  57. } )( history.undos, '&nbsp;' );
  58. ( function addObjects( objects, pad ) {
  59. for ( var i = objects.length - 1; i >= 0; i -- ) {
  60. var object = objects[ i ];
  61. var html = pad + "<span style='color: #71544e'>" + enumerator ++ + ". Redo: " + object.name + "</span>";
  62. options.push( { value: object.id, html: html } );
  63. }
  64. } )( history.redos, '&nbsp;' );
  65. outliner.setOptions( options );
  66. };
  67. refreshUI();
  68. // events
  69. signals.editorCleared.add( refreshUI );
  70. signals.historyChanged.add( refreshUI );
  71. signals.historyChanged.add( function ( cmd ) {
  72. outliner.setValue( cmd !== undefined ? cmd.id : null );
  73. } );
  74. return container;
  75. };