Last active
October 31, 2016 13:06
-
-
Save mdehoog/1c59a1dcdb18eec69a8e to your computer and use it in GitHub Desktop.
ember-wormhole component action not bubbled
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ember from 'ember'; | |
| export default Ember.Controller.extend({ | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ember from 'ember'; | |
| var computed = Ember.computed; | |
| var observer = Ember.observer; | |
| var run = Ember.run; | |
| export default Ember.Component.extend({ | |
| to: computed.alias('destinationElementId'), | |
| destinationElementId: null, | |
| destinationElement: computed('destinationElementId', 'renderInPlace', function() { | |
| return this.get('renderInPlace') ? this.element : document.getElementById(this.get('destinationElementId')); | |
| }), | |
| renderInPlace: false, | |
| didInsertElement: function() { | |
| this._super(...arguments); | |
| this._firstNode = this.element.firstChild; | |
| this._lastNode = this.element.lastChild; | |
| this.appendToDestination(); | |
| }, | |
| willDestroyElement: function() { | |
| this._super(...arguments); | |
| var firstNode = this._firstNode; | |
| var lastNode = this._lastNode; | |
| run.schedule('render', () => { | |
| this.removeRange(firstNode, lastNode); | |
| }); | |
| }, | |
| destinationDidChange: observer('destinationElement', function() { | |
| var destinationElement = this.get('destinationElement'); | |
| if (destinationElement !== this._firstNode.parentNode) { | |
| run.schedule('render', this, 'appendToDestination'); | |
| } | |
| }), | |
| appendToDestination: function() { | |
| var destinationElement = this.get('destinationElement'); | |
| var currentActiveElement = document.activeElement; | |
| if (!destinationElement) { | |
| var destinationElementId = this.get('destinationElementId'); | |
| if (destinationElementId) { | |
| throw new Error(`ember-wormhole failed to render into '#${this.get('destinationElementId')}' because the element is not in the DOM`); | |
| } | |
| throw new Error('ember-wormhole failed to render content because the destinationElementId was set to an undefined or falsy value.'); | |
| } | |
| this.appendRange(destinationElement, this._firstNode, this._lastNode); | |
| if (document.activeElement !== currentActiveElement) { | |
| currentActiveElement.focus(); | |
| } | |
| }, | |
| appendRange: function(destinationElement, firstNode, lastNode) { | |
| while(firstNode) { | |
| destinationElement.insertBefore(firstNode, null); | |
| firstNode = firstNode !== lastNode ? lastNode.parentNode.firstChild : null; | |
| } | |
| }, | |
| removeRange: function(firstNode, lastNode) { | |
| var node = lastNode; | |
| do { | |
| var next = node.previousSibling; | |
| if (node.parentNode) { | |
| node.parentNode.removeChild(node); | |
| if (node === firstNode) { | |
| break; | |
| } | |
| } | |
| node = next; | |
| } while (node); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ember from 'ember'; | |
| export default Ember.Component.extend({ | |
| actions: { | |
| openWormhole: function() { | |
| this.set('isWormholeOpen', true); | |
| }, | |
| closeWormhole: function() { | |
| this.set('isWormholeOpen', false); | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "version": "0.4.10", | |
| "dependencies": { | |
| "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js", | |
| "ember": "http://builds.emberjs.com/canary/ember.debug.js", | |
| "ember-template-compiler": "http://builds.emberjs.com/canary/ember-template-compiler.js" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment