WidgetIFrame.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. SC.loadPackage({ 'WidgetIFrame': {
  2. comment: 'I am the widget controlling an IFrame element.',
  3. traits: ['Widget'],
  4. sharedProperties: {
  5. widgetMenu: { initValue: '<div class="sg-editing-widget-container"><button id="sg-editing-widget-iframe" class="sg-editing-widget-button"></button></div>' },
  6. widgetPanel: { initValue: '<div id="sg-editing-widget-iframe-panel">'
  7. +'<div class="sg-editing-widget-panel">'
  8. +'<div class="sg-widget-triangle-up"></div>'
  9. +'<input id="sg-editing-widget-iframe-input" type="text"></input>'
  10. +'<button id="sg-editing-widget-iframe-chooseFile" class="sg-editing-widget-button"></button>'
  11. +'<button id="sg-editing-widget-iframe-clear" class="sg-editing-widget-button"></button>'
  12. +'</div>'
  13. +'</div>' }
  14. },
  15. properties: {
  16. widgetPanel: { comment: 'I store the DOMElement containing the panel of controls of the widget.' },
  17. isWidgetActive: { comment: 'Wether the widget is active.',
  18. transform: function(aBoolean){
  19. if(aBoolean){
  20. if(this.get('widgetPanel').parentNode !== this.get('widgetMenu')){
  21. this.get('widgetMenu').appendChild(this.get('widgetPanel'));
  22. }
  23. this.get('widgetMenu').classList.add('active');
  24. var elements = this.get('selection').get('elements')
  25. if(elements.length === 1){
  26. this.get('widgetPanel').querySelector('#sg-editing-widget-iframe-input').value = (
  27. elements[0].get('contentNode').firstElementChild.getAttribute('src')
  28. );
  29. }
  30. // prepare undo
  31. this.set({ aValueWasChoosen: false });
  32. SuperGlue.get('history').do('actionHasStarted', this.do('createState'));
  33. }else{
  34. if(this.get('widgetPanel').parentNode === this.get('widgetMenu')){
  35. this.get('widgetMenu').removeChild(this.get('widgetPanel'));
  36. }
  37. this.get('widgetMenu').classList.remove('active');
  38. // finish undo
  39. if(this.get('aValueWasChoosen')){
  40. SuperGlue.get('history').do('actionHasSucceeded', this.do('createState'));
  41. }
  42. this.set({ aValueWasChoosen: false })
  43. }
  44. return aBoolean
  45. }
  46. },
  47. aValueWasChoosen: { comment: 'Wether a value was choosen (needed for undo).' }
  48. },
  49. methods: {
  50. init: {
  51. comment: 'I init the widget.',
  52. code: function(theSelection){
  53. this.delegate('Widget', 'init', theSelection);
  54. var self = this,
  55. widgetPanel = (new DOMParser()).parseFromString(this.class.get('widgetPanel'), 'text/html').body.firstChild;
  56. widgetPanel.querySelector('#sg-editing-widget-iframe-input').addEventListener('mouseup', function(evt){
  57. theSelection.set({ activeWidget: self });
  58. evt.stopPropagation();
  59. }, false);
  60. widgetPanel.querySelector('#sg-editing-widget-iframe-input').addEventListener('input', function(evt){
  61. self.do('setIFrameSrc', this.value)
  62. }, false);
  63. widgetPanel.querySelector('#sg-editing-widget-iframe-chooseFile').addEventListener('mouseup', function(evt){
  64. theSelection.set({ activeWidget: self });
  65. evt.stopPropagation();
  66. var pathParser = document.createElement('a');
  67. pathParser.href = widgetPanel.querySelector('#sg-editing-widget-iframe-input').value;
  68. SuperGlue.get('fileManager').do('chooseFile', {
  69. oldPath: pathParser.pathname,
  70. callback: function(srcPath){
  71. widgetPanel.querySelector('#sg-editing-widget-iframe-input').value = srcPath;
  72. self.do('setIFrameSrc', srcPath);
  73. }
  74. });
  75. }, false);
  76. widgetPanel.querySelector('#sg-editing-widget-iframe-clear').addEventListener('mouseup', function(evt){
  77. theSelection.set({ activeWidget: self });
  78. evt.stopPropagation();
  79. widgetPanel.querySelector('#sg-editing-widget-iframe-input').value = '';
  80. self.do('setIFrameSrc', '');
  81. }, false);
  82. this.set({
  83. widgetPanel: widgetPanel
  84. });
  85. }
  86. },
  87. setIFrameSrc: {
  88. comment: 'I set the src for an iFrameElement.',
  89. code: function(srcURL){
  90. var elements = this.get('selection').get('elements');
  91. for(var i = 0, l = elements.length; i < l; i++){
  92. elements[i].get('contentNode').firstElementChild.src = srcURL;
  93. }
  94. this.set({ aValueWasChoosen: true });
  95. }
  96. },
  97. createState: {
  98. comment: 'I create a reflection function to restore a state.',
  99. code: function(){
  100. return (function(elements){
  101. var savedIFrameSrc = []
  102. for(var i = 0, l = elements.length; i < l; i++){
  103. savedIFrameSrc.push(elements[i].get('node').firstElementChild.src)
  104. }
  105. return function(){
  106. for(var i = 0, l = elements.length; i < l; i++){
  107. elements[i].get('node').firstElementChild.src = savedIFrameSrc[i]
  108. }
  109. }
  110. }).call(this, this.get('selection').get('elements'));
  111. }
  112. }
  113. }
  114. }});