UNPKG

6.15 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
8 * React Blessed ID Operations
9 * ============================
10 *
11 * Cache register for blessed nodes stored by ID.
12 */
13
14
15var _lodash = require('lodash');
16
17function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
18
19/**
20 * The blessed nodes internal index;
21 */
22var blessedNodes = {};
23
24/**
25 * Backend for blessed ID operations.
26 *
27 * @constructor ReactBlessedIDOperations
28 */
29
30var ReactBlessedIDOperations = function () {
31 function ReactBlessedIDOperations() {
32 _classCallCheck(this, ReactBlessedIDOperations);
33
34 this.screen = null;
35 }
36
37 /**
38 * Set the current screen.
39 *
40 * @param {BlessedScreen} screen - The screen to attach.
41 * @return {ReactBlessedIDOperations} - Returns itself.
42 */
43
44
45 _createClass(ReactBlessedIDOperations, [{
46 key: 'setScreen',
47 value: function setScreen(screen) {
48 this.screen = screen;
49
50 // Creating a debounced version of the render method so we won't render
51 // multiple time per frame, in vain.
52 screen.debouncedRender = (0, _lodash.debounce)(function () {
53 return screen.render();
54 }, 0);
55
56 return this;
57 }
58
59 /**
60 * Add a new node to the index.
61 *
62 * @param {string} ID - The node's id.
63 * @param {BlessedNode} node - The node itself.
64 * @return {ReactBlessedIDOperations} - Returns itself.
65 */
66
67 }, {
68 key: 'add',
69 value: function add(ID, node) {
70 blessedNodes[ID] = node;
71 return this;
72 }
73
74 /**
75 * Get a node from the index.
76 *
77 * @param {string} ID - The node's id.
78 * @return {BlessedNode} - The node.
79 */
80
81 }, {
82 key: 'get',
83 value: function get(ID) {
84 return blessedNodes[ID];
85 }
86
87 /**
88 * Get the parent of a node from the index.
89 *
90 * @param {string} ID - The node's id.
91 * @return {BlessedScreen|BlessedNode} - The node.
92 */
93
94 }, {
95 key: 'getParent',
96 value: function getParent(ID) {
97
98 // If the node is root, we return the screen itself
99 if (ID.match(/\./g).length === 1) return this.screen;
100
101 var parentID = ID.split('.').slice(0, -1).join('.');
102 return this.get(parentID);
103 }
104
105 /**
106 * Drop a node from the index.
107 *
108 * @param {string} ID - The node's id.
109 * @return {ReactBlessedIDOperations} - Returns itself.
110 */
111
112 }, {
113 key: 'drop',
114 value: function drop(ID) {
115 delete blessedNodes[ID];
116 return this;
117 }
118 }]);
119
120 return ReactBlessedIDOperations;
121}();
122
123exports.default = new ReactBlessedIDOperations();
\No newline at end of file