1 | "use strict";
|
2 |
|
3 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
4 |
|
5 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
6 |
|
7 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
8 |
|
9 |
|
10 | var Block, SpecialString, cloneAndMergeDeep, terminalWidth;
|
11 | SpecialString = require('./SpecialString');
|
12 | terminalWidth = require('../tools').getCols();
|
13 |
|
14 | var _require = require('../tools');
|
15 |
|
16 | cloneAndMergeDeep = _require.cloneAndMergeDeep;
|
17 |
|
18 | module.exports = Block = function () {
|
19 | var self;
|
20 |
|
21 | var Block = function () {
|
22 | function Block(_layout, _parent) {
|
23 | var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
24 |
|
25 | var _name = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
|
26 |
|
27 | _classCallCheck(this, Block);
|
28 |
|
29 | this._layout = _layout;
|
30 | this._parent = _parent;
|
31 | this._name = _name;
|
32 | this._config = cloneAndMergeDeep(self.defaultConfig, config);
|
33 | this._closed = false;
|
34 | this._wasOpenOnce = false;
|
35 | this._active = false;
|
36 | this._buffer = '';
|
37 | this._didSeparateBlock = false;
|
38 | this._linePrependor = new this._config.linePrependor.fn(this._config.linePrependor.options);
|
39 | this._lineAppendor = new this._config.lineAppendor.fn(this._config.lineAppendor.options);
|
40 | this._blockPrependor = new this._config.blockPrependor.fn(this._config.blockPrependor.options);
|
41 | this._blockAppendor = new this._config.blockAppendor.fn(this._config.blockAppendor.options);
|
42 | }
|
43 |
|
44 | _createClass(Block, [{
|
45 | key: "_activate",
|
46 | value: function _activate() {
|
47 | var deactivateParent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
48 |
|
49 | if (this._active) {
|
50 | throw Error("This block is already active. This is probably a bug in RenderKid itself");
|
51 | }
|
52 |
|
53 | if (this._closed) {
|
54 | throw Error("This block is closed and cannot be activated. This is probably a bug in RenderKid itself");
|
55 | }
|
56 |
|
57 | this._active = true;
|
58 | this._layout._activeBlock = this;
|
59 |
|
60 | if (deactivateParent) {
|
61 | if (this._parent != null) {
|
62 | this._parent._deactivate(false);
|
63 | }
|
64 | }
|
65 |
|
66 | return this;
|
67 | }
|
68 | }, {
|
69 | key: "_deactivate",
|
70 | value: function _deactivate() {
|
71 | var activateParent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
72 |
|
73 | this._ensureActive();
|
74 |
|
75 | this._flushBuffer();
|
76 |
|
77 | if (activateParent) {
|
78 | if (this._parent != null) {
|
79 | this._parent._activate(false);
|
80 | }
|
81 | }
|
82 |
|
83 | this._active = false;
|
84 | return this;
|
85 | }
|
86 | }, {
|
87 | key: "_ensureActive",
|
88 | value: function _ensureActive() {
|
89 | if (!this._wasOpenOnce) {
|
90 | throw Error("This block has never been open before. This is probably a bug in RenderKid itself.");
|
91 | }
|
92 |
|
93 | if (!this._active) {
|
94 | throw Error("This block is not active. This is probably a bug in RenderKid itself.");
|
95 | }
|
96 |
|
97 | if (this._closed) {
|
98 | throw Error("This block is already closed. This is probably a bug in RenderKid itself.");
|
99 | }
|
100 | }
|
101 | }, {
|
102 | key: "_open",
|
103 | value: function _open() {
|
104 | if (this._wasOpenOnce) {
|
105 | throw Error("Block._open() has been called twice. This is probably a RenderKid bug.");
|
106 | }
|
107 |
|
108 | this._wasOpenOnce = true;
|
109 |
|
110 | if (this._parent != null) {
|
111 | this._parent.write(this._whatToPrependToBlock());
|
112 | }
|
113 |
|
114 | this._activate();
|
115 |
|
116 | return this;
|
117 | }
|
118 | }, {
|
119 | key: "close",
|
120 | value: function close() {
|
121 | this._deactivate();
|
122 |
|
123 | this._closed = true;
|
124 |
|
125 | if (this._parent != null) {
|
126 | this._parent.write(this._whatToAppendToBlock());
|
127 | }
|
128 |
|
129 | return this;
|
130 | }
|
131 | }, {
|
132 | key: "isOpen",
|
133 | value: function isOpen() {
|
134 | return this._wasOpenOnce && !this._closed;
|
135 | }
|
136 | }, {
|
137 | key: "write",
|
138 | value: function write(str) {
|
139 | this._ensureActive();
|
140 |
|
141 | if (str === '') {
|
142 | return;
|
143 | }
|
144 |
|
145 | str = String(str);
|
146 | this._buffer += str;
|
147 | return this;
|
148 | }
|
149 | }, {
|
150 | key: "openBlock",
|
151 | value: function openBlock(config, name) {
|
152 | var block;
|
153 |
|
154 | this._ensureActive();
|
155 |
|
156 | block = new Block(this._layout, this, config, name);
|
157 |
|
158 | block._open();
|
159 |
|
160 | return block;
|
161 | }
|
162 | }, {
|
163 | key: "_flushBuffer",
|
164 | value: function _flushBuffer() {
|
165 | var str;
|
166 |
|
167 | if (this._buffer === '') {
|
168 | return;
|
169 | }
|
170 |
|
171 | str = this._buffer;
|
172 | this._buffer = '';
|
173 |
|
174 | this._writeInline(str);
|
175 | }
|
176 | }, {
|
177 | key: "_toPrependToLine",
|
178 | value: function _toPrependToLine() {
|
179 | var fromParent;
|
180 | fromParent = '';
|
181 |
|
182 | if (this._parent != null) {
|
183 | fromParent = this._parent._toPrependToLine();
|
184 | }
|
185 |
|
186 | return this._linePrependor.render(fromParent);
|
187 | }
|
188 | }, {
|
189 | key: "_toAppendToLine",
|
190 | value: function _toAppendToLine() {
|
191 | var fromParent;
|
192 | fromParent = '';
|
193 |
|
194 | if (this._parent != null) {
|
195 | fromParent = this._parent._toAppendToLine();
|
196 | }
|
197 |
|
198 | return this._lineAppendor.render(fromParent);
|
199 | }
|
200 | }, {
|
201 | key: "_whatToPrependToBlock",
|
202 | value: function _whatToPrependToBlock() {
|
203 | return this._blockPrependor.render();
|
204 | }
|
205 | }, {
|
206 | key: "_whatToAppendToBlock",
|
207 | value: function _whatToAppendToBlock() {
|
208 | return this._blockAppendor.render();
|
209 | }
|
210 | }, {
|
211 | key: "_writeInline",
|
212 | value: function _writeInline(str) {
|
213 | var i, j, k, l, lineBreaksToAppend, m, ref, ref1, ref2, remaining;
|
214 |
|
215 |
|
216 | if (new SpecialString(str).isOnlySpecialChars()) {
|
217 |
|
218 | this._layout._append(str);
|
219 |
|
220 | return;
|
221 | }
|
222 |
|
223 |
|
224 | remaining = str;
|
225 |
|
226 | lineBreaksToAppend = 0;
|
227 |
|
228 | if (m = remaining.match(/^\n+/)) {
|
229 |
|
230 |
|
231 | for (i = j = 1, ref = m[0].length; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
|
232 | this._writeLine('');
|
233 | }
|
234 |
|
235 | remaining = remaining.substr(m[0].length, remaining.length);
|
236 | }
|
237 |
|
238 |
|
239 | if (m = remaining.match(/\n+$/)) {
|
240 |
|
241 |
|
242 | lineBreaksToAppend = m[0].length;
|
243 | remaining = remaining.substr(0, remaining.length - m[0].length);
|
244 | }
|
245 |
|
246 |
|
247 | while (remaining.length > 0) {
|
248 |
|
249 | if (m = remaining.match(/^[^\n]+/)) {
|
250 |
|
251 | this._writeLine(m[0]);
|
252 |
|
253 | remaining = remaining.substr(m[0].length, remaining.length);
|
254 | } else if (m = remaining.match(/^\n+/)) {
|
255 |
|
256 | for (i = k = 1, ref1 = m[0].length; 1 <= ref1 ? k < ref1 : k > ref1; i = 1 <= ref1 ? ++k : --k) {
|
257 | this._writeLine('');
|
258 | }
|
259 |
|
260 | remaining = remaining.substr(m[0].length, remaining.length);
|
261 | }
|
262 | }
|
263 |
|
264 |
|
265 | if (lineBreaksToAppend > 0) {
|
266 |
|
267 | for (i = l = 1, ref2 = lineBreaksToAppend; 1 <= ref2 ? l <= ref2 : l >= ref2; i = 1 <= ref2 ? ++l : --l) {
|
268 | this._writeLine('');
|
269 | }
|
270 | }
|
271 | }
|
272 |
|
273 |
|
274 | }, {
|
275 | key: "_writeLine",
|
276 | value: function _writeLine(str) {
|
277 | var line, lineContent, lineContentLength, remaining, roomLeft, toAppend, toAppendLength, toPrepend, toPrependLength;
|
278 |
|
279 | remaining = new SpecialString(str);
|
280 |
|
281 | while (true) {
|
282 |
|
283 |
|
284 | toPrepend = this._toPrependToLine();
|
285 |
|
286 | toPrependLength = new SpecialString(toPrepend).length;
|
287 |
|
288 | toAppend = this._toAppendToLine();
|
289 |
|
290 | toAppendLength = new SpecialString(toAppend).length;
|
291 |
|
292 | roomLeft = this._layout._config.terminalWidth - (toPrependLength + toAppendLength);
|
293 |
|
294 | lineContentLength = Math.min(this._config.width, roomLeft);
|
295 |
|
296 | lineContent = remaining.cut(0, lineContentLength, true);
|
297 |
|
298 | line = toPrepend + lineContent.str + toAppend;
|
299 |
|
300 | this._layout._appendLine(line);
|
301 |
|
302 | if (remaining.isEmpty()) {
|
303 | break;
|
304 | }
|
305 | }
|
306 | }
|
307 | }]);
|
308 |
|
309 | return Block;
|
310 | }();
|
311 |
|
312 | ;
|
313 | self = Block;
|
314 | Block.defaultConfig = {
|
315 | blockPrependor: {
|
316 | fn: require('./block/blockPrependor/Default'),
|
317 | options: {
|
318 | amount: 0
|
319 | }
|
320 | },
|
321 | blockAppendor: {
|
322 | fn: require('./block/blockAppendor/Default'),
|
323 | options: {
|
324 | amount: 0
|
325 | }
|
326 | },
|
327 | linePrependor: {
|
328 | fn: require('./block/linePrependor/Default'),
|
329 | options: {
|
330 | amount: 0
|
331 | }
|
332 | },
|
333 | lineAppendor: {
|
334 | fn: require('./block/lineAppendor/Default'),
|
335 | options: {
|
336 | amount: 0
|
337 | }
|
338 | },
|
339 | lineWrapper: {
|
340 | fn: require('./block/lineWrapper/Default'),
|
341 | options: {
|
342 | lineWidth: null
|
343 | }
|
344 | },
|
345 | width: terminalWidth,
|
346 | prefixRaw: '',
|
347 | suffixRaw: ''
|
348 | };
|
349 | return Block;
|
350 | }.call(void 0); |
\ | No newline at end of file |