panel.js
1 const { Emitter } = require('event-kit'); 2 3 // Extended: A container representing a panel on the edges of the editor window. 4 // You should not create a `Panel` directly, instead use {Workspace::addTopPanel} 5 // and friends to add panels. 6 // 7 // Examples: [status-bar](https://github.com/atom/status-bar) 8 // and [find-and-replace](https://github.com/atom/find-and-replace) both use 9 // panels. 10 module.exports = class Panel { 11 /* 12 Section: Construction and Destruction 13 */ 14 15 constructor({ item, autoFocus, visible, priority, className }, viewRegistry) { 16 this.destroyed = false; 17 this.item = item; 18 this.autoFocus = autoFocus == null ? false : autoFocus; 19 this.visible = visible == null ? true : visible; 20 this.priority = priority == null ? 100 : priority; 21 this.className = className; 22 this.viewRegistry = viewRegistry; 23 this.emitter = new Emitter(); 24 } 25 26 // Public: Destroy and remove this panel from the UI. 27 destroy() { 28 if (this.destroyed) return; 29 this.destroyed = true; 30 this.hide(); 31 if (this.element) this.element.remove(); 32 this.emitter.emit('did-destroy', this); 33 return this.emitter.dispose(); 34 } 35 36 getElement() { 37 if (!this.element) { 38 this.element = document.createElement('atom-panel'); 39 if (!this.visible) this.element.style.display = 'none'; 40 if (this.className) 41 this.element.classList.add(...this.className.split(' ')); 42 this.element.appendChild(this.viewRegistry.getView(this.item)); 43 } 44 return this.element; 45 } 46 47 /* 48 Section: Event Subscription 49 */ 50 51 // Public: Invoke the given callback when the pane hidden or shown. 52 // 53 // * `callback` {Function} to be called when the pane is destroyed. 54 // * `visible` {Boolean} true when the panel has been shown 55 // 56 // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. 57 onDidChangeVisible(callback) { 58 return this.emitter.on('did-change-visible', callback); 59 } 60 61 // Public: Invoke the given callback when the pane is destroyed. 62 // 63 // * `callback` {Function} to be called when the pane is destroyed. 64 // * `panel` {Panel} this panel 65 // 66 // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. 67 onDidDestroy(callback) { 68 return this.emitter.once('did-destroy', callback); 69 } 70 71 /* 72 Section: Panel Details 73 */ 74 75 // Public: Returns the panel's item. 76 getItem() { 77 return this.item; 78 } 79 80 // Public: Returns a {Number} indicating this panel's priority. 81 getPriority() { 82 return this.priority; 83 } 84 85 getClassName() { 86 return this.className; 87 } 88 89 // Public: Returns a {Boolean} true when the panel is visible. 90 isVisible() { 91 return this.visible; 92 } 93 94 // Public: Hide this panel 95 hide() { 96 let wasVisible = this.visible; 97 this.visible = false; 98 if (this.element) this.element.style.display = 'none'; 99 if (wasVisible) this.emitter.emit('did-change-visible', this.visible); 100 } 101 102 // Public: Show this panel 103 show() { 104 let wasVisible = this.visible; 105 this.visible = true; 106 if (this.element) this.element.style.display = null; 107 if (!wasVisible) this.emitter.emit('did-change-visible', this.visible); 108 } 109 };