WindowManager.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. SC.loadPackage({ 'WindowManager': {
  2. comment: 'I am the WindowManager which organizes the editing interface in the 2nd level.',
  3. properties: {
  4. windowsContainer: { comment: 'I hold the DOM node which contains the windows.' },
  5. windows: { comment: 'I hold an map of id-->references to my window DOM nodes.' },
  6. activityIndicator: { comment: 'I can show wether SuperGlue is working or not.',
  7. transform: function(aBoolean){
  8. if(aBoolean){
  9. if(!this.get('windowsContainer').querySelector('#sg-editing-activity-indicator')){
  10. var indicator = document.createElement('div');
  11. indicator.setAttribute('id', 'sg-editing-activity-indicator');
  12. this.get('windowsContainer').appendChild(indicator)
  13. }
  14. }else{
  15. if(this.get('windowsContainer').querySelector('#sg-editing-activity-indicator')){
  16. this.get('windowsContainer').removeChild(
  17. this.get('windowsContainer').querySelector('#sg-editing-activity-indicator')
  18. );
  19. }
  20. }
  21. return aBoolean;
  22. }
  23. }
  24. },
  25. methods: {
  26. init: {
  27. comment: 'I init the WindowManager.',
  28. code: function(){
  29. var self = this;
  30. this.set({
  31. windows: [],
  32. windowsContainer: (function(){
  33. var windowsContainer = document.createElement('div');
  34. windowsContainer.setAttribute('id', 'sg-editing-windows-container');
  35. document.body.appendChild(windowsContainer);
  36. return windowsContainer;
  37. }).call(this)
  38. });
  39. window.addEventListener('resize', function(){
  40. var windows = self.get('windows');
  41. for(var i in windows){
  42. windows[i].set({
  43. top: windows[i].get('top'),
  44. left: windows[i].get('left'),
  45. width: windows[i].get('width'),
  46. height: windows[i].get('height')
  47. });
  48. }
  49. }, false)
  50. }
  51. },
  52. createWindow: {
  53. comment: 'I create a Window according to the windowConfig object: '+
  54. '{ class: aWindowClass, top: anInt, left: anInt, width: anInt, height: anInt }',
  55. code: function(windowConfig){
  56. var self = this,
  57. newWindow = SC.init(windowConfig.class, windowConfig),
  58. windowsContainer = this.get('windowsContainer');
  59. newWindow.get('node').addEventListener('mousedown', function(evt){
  60. if(windowsContainer.lastElementChild !== newWindow.get('node')){
  61. windowsContainer.appendChild(newWindow.get('node'));
  62. }
  63. }, false);
  64. windowsContainer.appendChild(newWindow.get('node'));
  65. this.get('windows').push(newWindow);
  66. return newWindow;
  67. }
  68. },
  69. closeWindow: {
  70. comment: 'I close a given window',
  71. code: function(aWindow){
  72. var windows = this.get('windows');
  73. if('function' === typeof aWindow.get('onClose')){
  74. aWindow.get('onClose').call(aWindow)
  75. }
  76. windows.splice(windows.indexOf(aWindow), 1);
  77. this.get('windowsContainer').removeChild(aWindow.get('node'));
  78. }
  79. }
  80. }
  81. }});