UNPKG

4.13 kBJavaScriptView Raw
1/*
2 Terminal Kit
3
4 Copyright (c) 2009 - 2020 Cédric Ronvel
5
6 The MIT License (MIT)
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25*/
26
27"use strict" ;
28
29
30
31// Container: an enclosed surface (ScreenBuffer).
32// Later it will feature a viewport and an internal surface, to allow scrolling.
33
34
35
36const Element = require( './Element.js' ) ;
37const ScreenBuffer = require( '../ScreenBuffer.js' ) ;
38
39
40
41function Container( options ) {
42 // Clone options if necessary
43 options = ! options ? {} : options.internal ? options : Object.create( options ) ;
44 options.internal = true ;
45
46 Element.call( this , options ) ;
47
48 // No scrolling for instance: input coords is equals to output coords
49 this.inputX = options.inputX || this.outputX ;
50 this.inputY = options.inputY || this.outputY ;
51 this.inputWidth = options.inputWidth || this.outputWidth ;
52 this.inputHeight = options.inputHeight || this.outputHeight ;
53
54 //console.error( 'this.document:' , !! this.document , !! ( this.document && this.document.palette ) , !! options.palette ) ;
55 this.palette = options.palette || ( this.document && this.document.palette ) ;
56 this.object2attr = object => ScreenBuffer.object2attr( object , this.palette && this.palette.colorNameToIndex ) ;
57
58 this.inputDst = new ScreenBuffer( {
59 dst: this.outputDst ,
60 x: this.inputX ,
61 y: this.inputY ,
62 width: this.inputWidth ,
63 height: this.inputHeight ,
64 palette: this.palette
65 } ) ;
66
67 this.deltaDraw = false ; // Useful for Document, not so useful for other containers
68 this.backgroundAttr = options.backgroundAttr || { bgColor: 'default' } ;
69
70 // Only draw if we are not a superclass of the object
71 if ( this.elementType === 'Container' && ! options.noDraw ) { this.draw() ; }
72}
73
74module.exports = Container ;
75
76Container.prototype = Object.create( Element.prototype ) ;
77Container.prototype.constructor = Container ;
78Container.prototype.elementType = 'Container' ;
79
80Container.prototype.isContainer = true ;
81
82
83
84Container.prototype.destroy = function( isSubDestroy ) {
85 Element.prototype.destroy.call( this , isSubDestroy ) ;
86} ;
87
88
89
90// /!\ TODO /!\
91/*
92 Accept ScreenBuffer#resize() argument: x, y, width, height.
93 Should it support output* and input* args?
94*/
95Container.prototype.resize =
96Container.prototype.resizeInput = function( to ) {
97 if ( ! to.x ) { to.x = 0 ; }
98 if ( ! to.y ) { to.y = 0 ; }
99
100 this.inputDst.resize( to ) ;
101} ;
102
103
104
105Container.prototype.moveTo = function( x , y ) {
106 this.outputX = this.inputDst.x = x ;
107 this.outputY = this.inputDst.y = y ;
108} ;
109
110
111
112Container.prototype.preDrawSelf = function() {
113 this.inputDst.fill( { char: ' ' , attr: this.backgroundAttr } ) ;
114} ;
115
116
117
118Container.prototype.postDrawSelf = function() {
119 // No scrolling for instance, so nothing to do, just draw it
120 //this.inputDst.x = this.outputX ;
121 //this.inputDst.y = this.outputY ;
122
123 this.inputDst.draw( {
124 delta: this.deltaDraw , // Draw only diff or not?
125 inline: this.strictInline
126 } ) ;
127} ;
128
129
130
131Container.prototype.drawSelfCursor = function( elementTargeted ) {
132 if ( elementTargeted ) { this.restoreCursor() ; }
133 else { this.inputDst.drawCursor() ; }
134} ;
135
136
137
138Container.prototype.onOutputDstResize = function( data ) {
139} ;
140