summaryrefslogtreecommitdiffstats
path: root/app.js
blob: 73bf3038ca9f444aed3b8060d08f3d4b573475c9 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
(function($) {
    $(function() {
        var terminal = $('.terminal'),
        input = $('#input'),
        colours = $('[name="colours"]'),
        light = $('[name="light"]'),

        _shareLink = function() {
            var url = window.location.href.replace(/#.+/, '');

            if (input.val()) {
                url += '#!input=' + btoa(input.val()) + '&' + $('.global-options :checked').serialize();
            }

            if (window.location.href != url) {
                history.pushState(history.state, url, url);
            }

            return url;
        },

        _loadFromHash = function() {
            if (window.location.hash) {
                var data = window.location.hash.replace(/^#!/, ''),
                elements = {
                    input: input,
                    colours: colours,
                    light: light
                };

                $('.global-options [type="checkbox"]').prop('checked', false).trigger('change');

                data.split(/&/).forEach(function(item) {
                    var data = item.split(/=/),
                    element = elements[data[0]],
                    value;

                    if (element === input) {
                        value = atob(data[1]);
                        element.val(value).trigger('change');
                    }
                    else {
                        value = data[1];

                        if (element) {
                            if (element.is('[type="radio"], [type="checkbox"]')) {
                                element.filter('[value="' + value + '"]').prop('checked', true).trigger('change');
                            }
                            else {
                                element.val(value).trigger('change');
                            }
                        }
                    }
                });
            }
        };

        new Clipboard('.share', {
            text: _shareLink
        });

        $('.share').on('click', function(event) {
            event.preventDefault();

            $('.url-copied').removeClass('hidden').show();

            window.setTimeout(function() {
                $('.url-copied').fadeOut('slow');
            }, 5000);
        });

        input.on('change keyup paste', function() {
            input.val().split(/:/).forEach(function(block) {
                // var [selector, styles] = block.split(/=/);
                // styles = parse.parseStyles(styles);
                var data = block.split(/=/, 2),
                selector = data[0],
                styles = parse.parseStyles(data[1]);

                $('.' + selector).removeAttr('style').each(function() {
                    var classes = selector,
                    el = $(this);

                    $.each(styles, function(style) {
                        if (style.match(/bg|fg/)) {
                            classes += ' ' + style + '-' + styles[style];

                            if (styles[style].match(/^true-/)) {
                                if (style.match(/bg/)) {
                                    el.css('background-color', parse.rgbToHex(styles[style].replace(/^true-/).split(/-/)));
                                }
                                else if (style.match(/fg/)) {
                                    el.css('color', parse.rgbToHex(styles[style].replace(/^true-/).split(/-/)));
                                }
                            }
                        }
                        else if (styles[style]) {
                            classes += ' ' + style;
                        }
                    });

                    this.className = classes;
                });

                $('[data-fragment="' + selector + '"] [name="bg-default"]').prop('checked', false);
                $('[data-fragment="' + selector + '"] [name="bg"]').show();
                $('[data-fragment="' + selector + '"] [name="fg-default"]').prop('checked', false);
                $('[data-fragment="' + selector + '"] [name="fg"]').show();

                $.each(styles, function(style) {
                    if (style == 'bg' || style == 'fg') {
                        var colour = styles[style];

                        if (colour.match(/true-/)) {
                            colour = parse.rgbToHex(colour.replace(/true-/, '').split(/-/));
                        }
                        else if (colour.match(/256-/)) {
                            colour = parse.rgbToHex(parse.term256ToRgb(colour.replace(/256-/, '')));
                        }
                        else if ((colour == 49 || !colour) && style == 'bg') {
                            colour = '';
                            $('[data-fragment="' + selector + '"] [name="bg-default"]').prop('checked', true);
                            $('[data-fragment="' + selector + '"] [name="bg"]').hide();
                        }
                        else if ((colour == 39 || !colour) && style == 'fg') {
                            colour = '';
                            $('[data-fragment="' + selector + '"] [name="fg-default"]').prop('checked', true);
                            $('[data-fragment="' + selector + '"] [name="fg"]').hide();
                        }
                        else {
                            colour = parse.rgbToHex(parse.term16ToRgb(colour));
                        }

                        $('[data-fragment="' + selector + '"] [name="' + style + '"]').val(colour);
                    }
                    else {
                        $('[data-fragment="' + selector + '"] [name="' + style + '"]').prop('checked', styles[style]);
                    }
                });
            });

            $('.copy-paste .contents').html(this.value);
        });

        $('.builder .block').on('change', 'input', function() {
            _updateInput();
        });

        var _updateInput = function() {
            var value = '';

            $('.builder .block').each(function() {
                value += $(this).attr('data-fragment') + '=' + _getStyle($(this)) + ':';
            });

            value = value.replace(/:$/, '');

            return input.val(value).trigger('change');
        },
        _getStyle = function(block) {
            var inputs = block.find(':input'),
            style = $.extend({}, parse.defaultStyle);

            inputs.filter(':not([name="bg-default"], [name="fg-default"])').each(function() {
                var value;

                if ($(this).is('select')) {
                    value = $(this).val();
                }
                else if ($(this).is('[type="checkbox"]')) {
                    value = this.checked;
                }
                else if ($(this).is('[type="radio"]')) {
                    if (this.checked) {
                        value = this.value;
                    }
                }
                else {
                    value = this.value;
                }

                if (this.name === 'bg' || this.name === 'fg') {
                    if ($('[name="colours"]:checked, [name="colours"][type="hidden"]').val() === '16') {
                        style[this.name] = parse.rgbToTerm16(parse.hexToRgb(value), this.name == 'bg');
                    }
                    else if ($('[name="colours"]:checked, [name="colours"][type="hidden"]').val() === '256') {
                        style[this.name] = parse.simplifyColour('256-' + parse.rgbToTerm256(parse.hexToRgb(value)), this.name == 'bg');
                    }
                    else {
                        style[this.name] = parse.simplifyColour('true-' + parse.hexToRgb(value).join('-'), this.name == 'bg');
                    }
                }
                else if (['bold', 'dim', 'italic', 'underline', 'blink', 'overline', 'invert', 'hidden', 'strikethrough'].includes(this.name)) {
                    style[this.name] = value;
                }
            });

            inputs.filter('[name="bg-default"], [name="fg-default"]').each(function() {
                if (this.checked) {
                    if (this.name === 'fg-default') {
                        style.fg = '39';
                    }
                    else if (this.name === 'bg-default') {
                        style.bg = '49';
                    }
                }
            });

            return parse.buildStyles(style).substr(3).replace(/m$/, '');
        };

        $('.global-options input[type="checkbox"]').on('change', function() {
            if (this.checked) {
                terminal.addClass(this.name);
            }
            else {
                terminal.removeClass(this.name);
            }
        });

        $('.global-options input[type="radio"]').on('change', function() {
            terminal.removeClass(function() {
                return Array.from(this.classList).filter(function(className) {
                    return className.match(/cursor/);
                }).join(' ');
            });

            if (this.checked) {
                terminal.addClass(this.name + '-' + this.value);
            }
        });


        $('.load-example').on('click', function(event) {
            event.preventDefault();

            $('#input').val($(this).attr('data-content')).change();
            _shareLink();
        });

        $('.builder .blocks').on('change', '[name="fg-default"], [name="bg-default"]', function() {
            var container = $(this).parents('.checkbox');

            if (this.checked) {
                container.next().hide().attr('disabled');
            }
            else {
                container.next().show().removeAttr('disabled');
            }
        });

        new Clipboard('a.copy');

        $('a.copy').on('click', function(event) {
            event.preventDefault();
        });

        $(window).on("popstate", function(e) {
            _loadFromHash();
        });

        _loadFromHash();
    });
})(jQuery);