summaryrefslogtreecommitdiffstats
path: root/js/terminal-animation.js
blob: 1648b3ecf9fab4dfa5272bbd4483d543d5f9ec31 (plain)
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
181
182
/**
 * Terminal Animation
 * http://edewata.fedorapeople.org/terminal-animation/
 *
 * @author Endi S. Dewata <edewata@redhat.com>
 */

(function($) {

    var terminal_animation = function() {

        var $this = $(this);
        var interval = 2000;
        var speed = 80;

        $this.init = function() {

            var text = $this.text();
            var list = $this.contents().detach();

            $this.console = $('<div/>', {
                'class': 'terminal-animation-console'
            }).appendTo($this);

            $this.content = $('<span/>', {
                'class': 'terminal-animation-content'
            }).appendTo($this.console);

            $this.elements = $('<span/>', {
                'class': 'terminal-animation-hidden'
            }).hide().appendTo($this.console);

            $this.caret = $('<span/>', {
                'text': ' ',
                'class': 'terminal-animation-caret'
            }).appendTo($this.console);

            $this.control = $('<div/>', {
                'class': 'terminal-animation-control'
            }).appendTo($this);

            $this.playButton = $('<button/>', {
                'html': 'Play',
                'class': 'terminal-animation-button',
                'click': function() { $this.play(); }
            }).appendTo($this.control);

            $this.prevButton = $('<button/>', {
                'html': 'Prev',
                'class': 'terminal-animation-button',
                'click': function() { $this.prev(); }
            }).appendTo($this.control);

            $this.nextButton = $('<button/>', {
                'html': 'Next',
                'class': 'terminal-animation-button',
                'click': function() { $this.next(); }
            }).appendTo($this.control);

            $this.resetButton = $('<button/>', {
                'html': 'Reset',
                'class': 'terminal-animation-button',
                'click': function() { $this.reset(); }
            }).appendTo($this.control);

            if ($this.is('pre')) {
                $this.elements.html(text);
            } else {
                $this.elements.append(list);
            }

            $this.flush();
        };

        $this.flush = function() {
            var done = function() {
                var height = $this.console.height();
                $this.console.scrollTop(height);
            };

            var run = function() {
                if ($this.elements.contents().length == 0) {
                    done.call();
                    return;
                }

                var element = $this.elements.contents().first();
                if (!element.hasClass('terminal-animation-input')) {
                    $this.next(run);
                    return;
                }

                done.call();
            };

            run.call();
        };

        $this.play = function() {

            var run = function() {
                if ($this.elements.contents().length == 0) return;

                $this.next(function() {
                    setTimeout(run, interval);
                });
            };

            run.call();
        };

        $this.prev = function() {
            if ($this.content.contents().length == 0) return;

            var element = $this.content.contents().last().detach();
            $this.elements.prepend(element);

            var text = element.text();
            if (element.hasClass('terminal-animation-input') && text.length > 0) return;

            if ($this.content.contents().length > 0) {
                element = $this.content.contents().last();
                if (element.hasClass('terminal-animation-input')) return;
                element.detach();
                $this.elements.prepend(element);
            }

            $this.flush();
        };

        $this.next = function(callback) {
            if ($this.animator || $this.elements.contents().length == 0) {
                return;
            }

            var element = $this.elements.contents().first().detach();

            if (element.hasClass('terminal-animation-input')) {
                var text = element.text();
                if (text.length == 0) {
                    $this.content.append(element);
                    $this.next(callback);

                } else {
                    element.text('');
                    var i = 0;

                    $this.content.append(element);
                    $this.animator = setInterval(function() {
                        if (i <= text.length) {
                            element.text(text.substring(0, i));
                            i++;
                        } else {
                            clearInterval($this.animator);
                            delete $this.animator;
                            if (callback) callback.call();
                        }
                    }, speed);
                }

            } else {
                $this.content.append(element);
                $this.flush();
                if (callback) callback.call();
            }
        };

        $this.reset = function() {
            $this.elements.append($this.content.contents().detach());
            $this.flush();
        };

        $this.init();

        return $this;
    };

    $.fn.terminal_animation = function() {
        return this.each(terminal_animation);
    };

}(jQuery));