File size: 5,891 Bytes
1110b36
839ae79
f930b4d
1110b36
 
744be20
b44092b
1110b36
6eb753d
1110b36
6eb753d
 
d540822
8d865f2
d540822
 
 
a925393
e74c0fb
 
 
839ae79
 
 
 
 
 
d540822
744be20
6eb753d
6a3eb62
f1a3d5b
6eb753d
6a3eb62
f1a3d5b
6eb753d
6a3eb62
 
 
eb99830
f1a3d5b
eb99830
4b40455
 
 
 
d18eb9c
4b40455
1110b36
 
 
071c698
 
1110b36
 
6eb753d
1110b36
 
d18eb9c
1110b36
 
 
 
 
 
 
eb99830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28f9967
dc3d3b3
 
3a33259
26ac93d
 
 
 
 
 
 
 
3a33259
 
 
 
 
dc3d3b3
 
1110b36
 
6a3eb62
b44092b
 
 
0478785
b44092b
744be20
b44092b
 
 
 
 
 
 
744be20
 
 
b44092b
6a3eb62
b44092b
 
 
 
a0afc4f
744be20
 
b44092b
6a3eb62
eb99830
3a33259
b44092b
 
0478785
b44092b
3a33259
 
744be20
eb99830
6a3eb62
 
 
eb99830
 
 
8f3a47f
eb99830
 
 
 
 
 
 
 
 
 
 
 
744be20
 
 
1e122c4
7adddda
1e122c4
 
a582ff4
 
 
 
 
839ae79
26e1cfd
325d2d4
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
class WindowHandler{
  //en este se hacen los cambios visuales
    constructor(){
        this.chatbox = $("#chat");
        this.template = $('<div class="message"><div><p></p></div></div>');
        this.active = false;
        this.mensaje = "";

        // this.evCtx = document;

        $(document).on("chat:enviar", (event, params) => this.recalcularTextarea());
        $(document).on("chat:limpiar", () => this.limpiarChat());

        $("#input-text").keypress((event) => {
            if (!event.shiftKey && event.keyCode === 13) {
              this.manejadorEnviar();
        }});
        $("#input-send").click(() => this.manejadorEnviar());
        $("#input-text").keypress(() => this.recalcularTextarea());
        $("#input-text").keyup(() => this.recalcularTextarea());
        $("#input-text").keydown(() => this.recalcularTextarea());
        
        
      $("#input-activar").change((event) => this.toggleSwitch(event));
        
        
        
        this.cargarChat(chatH.convesacion);

        $(document).on("precarga:inicio", (event, params) => {
            this.respuestaInicio(params)
        });
        $(document).on("precarga:mensaje", (event, params) => {
            this.respuestaMensaje(params)}
        );
        $(document).on("precarga:fin", (event, params) => {
            this.respuestaFin()
        });
        $(document).on("precarga:error", (event, params) => {
            this.respuestaError(params)
        });
 
    }

    limpiarChat(){
        $("#input-text").val("");
        this.recalcularTextarea();
        $("#chat").html("");
    }

    manejadorEnviar(){
        let mensaje = $("#input-text").val();
        if(mensaje==""){
            return false;
        }
        $(document).trigger("chat:enviar", mensaje);
    }
    
    recalcularTextarea(){
        $(".input-box").css("height", "30px");
        let height = parseInt(($(".input-text").prop('scrollHeight')+15)/15)*15;
        $(".input-box").css("height", height+"px");
        height -= 30;
        $(".chat").css("--textarea", height+"px");  
    }

    procesarTexto(texto){
        let segmentos = texto.split("```");
        let resultado = "";
    
        for(let i=0; i<segmentos.length;i++){
            let textoActual = segmentos[i];
            if(i%2==0){
                resultado += textoActual.replace(/\n/g, "<br>").replace(/`(.*?)`/gm, "<b>$1</b>");
            }else{
                let temp = textoActual.split("\n",1);
                resultado += "<pre><code class='language-";
                resultado += temp[0].length>1?temp[0]:"none";      
                temp = $("<div></div>").text(textoActual.substr(temp[0].length)).html()
                resultado += "'>"+temp+"</code></pre>";
            }
        }

        return resultado
    }

    cargarChat(mensajes){
        mensajes.forEach((mensaje) => {
            if(mensaje.role!="system"){
                let clone = this.template.clone();
                let texto = mensaje.content;
                if(mensaje.role=="user") {
                    clone.addClass("me");
                    clone.find("div p").text(texto.replace(/\n/g, "<br>"));
                }else{
                    texto = this.procesarTexto(texto);
                    clone.find("div p").html(texto);
                }   
                this.chatbox.append(clone);
                this.active = clone;
                Prism.highlightAllUnder(this.active[0])
                this.active = false;
                this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
            }
        });
    }

    respuestaInicio(mensaje){
        $("#input-text").val("");
        $("button").prop("disabled", true);
        $("textarea").prop("disabled", true);
        $(".switch input").prop("disabled", true);
        this.mensaje = ""
        let clone = this.template.clone();
        clone.addClass("me");
        clone.find("div p").text(mensaje);
        this.chatbox.append(clone);
        
        clone = this.template.clone();
        clone.find("div p").html("");
        this.chatbox.append(clone);
        this.active = clone;
        this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
    }

    respuestaMensaje(mensaje){
        this.mensaje += mensaje
        let html = this.active.find("div p").html();
        html += mensaje.replace(/\n/g, "<br>").replace(/`([^`\w\W]+?)`/gm, "<b>$1</b>")
        this.active.find("div p").html(html);
        this.chatbox.scrollTop(this.chatbox[0].scrollHeight);

    }

    respuestaFin(){
        let msgProcesado = this.procesarTexto(this.mensaje);
        this.mensaje = "";
        $("button").prop("disabled", false);
        $("textarea").prop("disabled", false);
        $(".switch input").prop("disabled", false);
        $("textarea").focus();
        this.active.find("div p").html(msgProcesado);
        Prism.highlightAllUnder(this.active[0]);
        this.active = false;
        this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
    }

    respuestaError(error){
        $("button").prop("disabled", false);
        $("textarea").prop("disabled", false);
        $("textarea").focus();
        this.active.find("div p").html(error.mensaje)
        switch(error.status){
            case 404:
                this.active.addClass("error")
            case 408:
                this.active.addClass("warning")
            default:
                this.active.addClass("warning")
        }
        this.active = false;
        this.chatbox.scrollTop(this.chatbox[0].scrollHeight)



    }

    toggleSwitch(event) {
      //boton de switch para consultar con google.
      let checkBox = event.currentTarget;
      $(document).trigger("accion:switch_change", checkBox.checked);
      if (checkBox.checked) {
        console.log("Switch is turned on.");
      } else {
        console.log("Switch is turned off.");
      }
  }

}