alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Dynamic Expresso Web Shell javascript code is 
// based on the mongulator project (https://github.com/banker/mongulator)
//
// Copyright (c) 2009 Kyle Banker
// Licensed under the MIT Licence.
// http://www.opensource.org/licenses/mit-license.php
 
 
var DefaultInputHtml = function (stack) {
    var linePrompt = "";
    if (stack == 0) {
        linePrompt += "<span class='prompt'> ></span>";
    }
    else {
        for (var i = 0; i <= stack; i++) {
            linePrompt += "<span class='prompt'>.</span>";
        }
    }
    return "<div class='line'>" +
           linePrompt +
           "<input type='text' class='readLine active' />" +
           "</div>";
}
 
var EnterKeyCode = 13;
var UpArrowKeyCode = 38;
var DownArrowKeyCode = 40;
 
var PTAG = function (str) {
    return "<pre>" + str + "</pre>";
}
 
var BR = function () {
    return "<br/>";
}
 
// Readline class to handle line input.
var ReadLine = function (options) {
    this.options = options || {};
    this.htmlForInput = this.options.htmlForInput;
    this.inputHandler = this.options.handler;
    //this.scoper = this.options.scoper;
    //  this.connection   = new Connection();
    this.terminal = $(this.options.terminalId || "#terminal");
    this.lineClass = this.options.lineClass || '.readLine';
    this.history = [];
    this.historyPtr = 0;
 
    this.initialize();
};
 
ReadLine.prototype = {
 
    initialize: function () {
        this.addInputLine();
 
        var inputElement = this.lineClass + '.active';
        var $terminal = this.terminal;
        this.terminal.click(function (event) {
            var $target = $(event.target);
            if ($target.is($terminal))
                $(inputElement).focus();
        });
    },
 
    // Enter a new input line with proper behavior.
    addInputLine: function (stackLevel) {
        stackLevel = stackLevel || 0;
        this.terminal.append(this.htmlForInput(stackLevel));
        var ctx = this;
        ctx.activeLine = $(this.lineClass + '.active');
 
        // Bind key events for entering and navigting history.
        ctx.activeLine.bind("keydown", function (ev) {
            switch (ev.keyCode) {
                case EnterKeyCode:
                    ctx.processInput(this.value);
                    break;
                case UpArrowKeyCode:
                    ctx.getCommand('previous');
                    break;
                case DownArrowKeyCode:
                    ctx.getCommand('next');
                    break;
            }
        });
 
        this.activeLine.focus();
    },
 
    // Returns the 'next' or 'previous' command in this history.
    getCommand: function (direction) {
        if (this.history.length === 0) {
            return;
        }
        this.adjustHistoryPointer(direction);
        this.activeLine[0].value = this.history[this.historyPtr];
        $(this.activeLine[0]).focus();
        //this.activeLine[0].value = this.activeLine[0].value;
    },
 
    // Moves the history pointer to the 'next' or 'previous' position. 
    adjustHistoryPointer: function (direction) {
        if (direction == 'previous') {
            if (this.historyPtr - 1 >= 0) {
                this.historyPtr -= 1;
            }
        }
        else {
            if (this.historyPtr + 1 < this.history.length) {
                this.historyPtr += 1;
            }
        }
    },
 
    // Return the handler's response.
    processInput: function (expression) {
        var me = this;
        this.inputHandler.eval(expression, function (response) {
            me.processOutput(expression, response);
                });
    },
 
    processOutput: function (expression, response) {
        this.insertResponse(response.result);
 
        // Save to the command history...
        if ((lineValue = expression.trim()) !== "") {
            this.history.push(lineValue);
            this.historyPtr = this.history.length;
        }
 
        // deactivate the line...
        this.activeLine.value = "";
        this.activeLine.attr({ disabled: true });
        this.activeLine.removeClass('active');
 
        // and add a new command line.
        this.addInputLine(response.stack);
    },
 
    insertResponse: function (response) {
        var pClass = "response";
        if (!response.success)
            pClass += " error";
 
        var $newP = $("<pre class='" + pClass + "'></pre>")
        $newP.text(response.result);
 
        this.activeLine.parent().append($newP);
    }
};
 
 
$htmlFormat = function (obj) {
    return tojson(obj, ' ', ' ', true);
}
 
 
 
var DynamicExpressoHandler = function (options) {
    this.options = options || {};
    this._commandStack = 0;
    this._interpreterUrl = this.options.interpreterUrl;
};
 
DynamicExpressoHandler.prototype = {
 
    eval: function (inputString, onSuccedeed) {
 
        $.ajax({
            type: "POST",
            url: this._interpreterUrl,
            data: { expression: inputString },
            dataType: "json"
        }).done(function (result) {
            onSuccedeed({ stack: this._commandStack, result: result });
        });
    }
};