123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- SC.loadPackage({ 'WidgetIFrame': {
- comment: 'I am the widget controlling an IFrame element.',
- traits: ['Widget'],
- sharedProperties: {
- widgetMenu: { initValue: '<div class="sg-editing-widget-container"><button id="sg-editing-widget-iframe" class="sg-editing-widget-button"></button></div>' },
- widgetPanel: { initValue: '<div id="sg-editing-widget-iframe-panel">'
- +'<div class="sg-editing-widget-panel">'
- +'<div class="sg-widget-triangle-up"></div>'
- +'<input id="sg-editing-widget-iframe-input" type="text"></input>'
- +'<button id="sg-editing-widget-iframe-chooseFile" class="sg-editing-widget-button"></button>'
- +'<button id="sg-editing-widget-iframe-clear" class="sg-editing-widget-button"></button>'
- +'</div>'
- +'</div>' }
- },
- properties: {
- widgetPanel: { comment: 'I store the DOMElement containing the panel of controls of the widget.' },
- isWidgetActive: { comment: 'Wether the widget is active.',
- transform: function(aBoolean){
- if(aBoolean){
- if(this.get('widgetPanel').parentNode !== this.get('widgetMenu')){
- this.get('widgetMenu').appendChild(this.get('widgetPanel'));
- }
- this.get('widgetMenu').classList.add('active');
- var elements = this.get('selection').get('elements')
- if(elements.length === 1){
- this.get('widgetPanel').querySelector('#sg-editing-widget-iframe-input').value = (
- elements[0].get('contentNode').firstElementChild.getAttribute('src')
- );
- }
- // prepare undo
- this.set({ aValueWasChoosen: false });
- SuperGlue.get('history').do('actionHasStarted', this.do('createState'));
- }else{
- if(this.get('widgetPanel').parentNode === this.get('widgetMenu')){
- this.get('widgetMenu').removeChild(this.get('widgetPanel'));
- }
- this.get('widgetMenu').classList.remove('active');
- // finish undo
- if(this.get('aValueWasChoosen')){
- SuperGlue.get('history').do('actionHasSucceeded', this.do('createState'));
- }
- this.set({ aValueWasChoosen: false })
-
- }
- return aBoolean
- }
- },
- aValueWasChoosen: { comment: 'Wether a value was choosen (needed for undo).' }
- },
- methods: {
- init: {
- comment: 'I init the widget.',
- code: function(theSelection){
- this.delegate('Widget', 'init', theSelection);
- var self = this,
- widgetPanel = (new DOMParser()).parseFromString(this.class.get('widgetPanel'), 'text/html').body.firstChild;
-
- widgetPanel.querySelector('#sg-editing-widget-iframe-input').addEventListener('mouseup', function(evt){
- theSelection.set({ activeWidget: self });
- evt.stopPropagation();
- }, false);
- widgetPanel.querySelector('#sg-editing-widget-iframe-input').addEventListener('input', function(evt){
- self.do('setIFrameSrc', this.value)
- }, false);
- widgetPanel.querySelector('#sg-editing-widget-iframe-chooseFile').addEventListener('mouseup', function(evt){
- theSelection.set({ activeWidget: self });
- evt.stopPropagation();
- var pathParser = document.createElement('a');
- pathParser.href = widgetPanel.querySelector('#sg-editing-widget-iframe-input').value;
- SuperGlue.get('fileManager').do('chooseFile', {
- oldPath: pathParser.pathname,
- callback: function(srcPath){
- widgetPanel.querySelector('#sg-editing-widget-iframe-input').value = srcPath;
- self.do('setIFrameSrc', srcPath);
- }
- });
- }, false);
- widgetPanel.querySelector('#sg-editing-widget-iframe-clear').addEventListener('mouseup', function(evt){
- theSelection.set({ activeWidget: self });
- evt.stopPropagation();
- widgetPanel.querySelector('#sg-editing-widget-iframe-input').value = '';
- self.do('setIFrameSrc', '');
- }, false);
- this.set({
- widgetPanel: widgetPanel
- });
- }
- },
- setIFrameSrc: {
- comment: 'I set the src for an iFrameElement.',
- code: function(srcURL){
- var elements = this.get('selection').get('elements');
- for(var i = 0, l = elements.length; i < l; i++){
- elements[i].get('contentNode').firstElementChild.src = srcURL;
- }
- this.set({ aValueWasChoosen: true });
- }
- },
- createState: {
- comment: 'I create a reflection function to restore a state.',
- code: function(){
- return (function(elements){
- var savedIFrameSrc = []
- for(var i = 0, l = elements.length; i < l; i++){
- savedIFrameSrc.push(elements[i].get('node').firstElementChild.src)
- }
- return function(){
- for(var i = 0, l = elements.length; i < l; i++){
- elements[i].get('node').firstElementChild.src = savedIFrameSrc[i]
- }
- }
- }).call(this, this.get('selection').get('elements'));
- }
- }
- }
- }});
|