1// source --> https://www.socialnews.xyz/wp-content/plugins/wp-rag-0.9.0/core/includes/assets/js/frontend-scripts.js?ver=0.9.0 2/*------------------------- 3Frontend related javascript 4-------------------------*/ 5 6/** 7 * HELPER COMMENT START 8 * 9 * This file contains all of the frontend related javascript. 10 * With frontend, it is meant the WordPress site that is visible for every visitor. 11 * 12 * Since you added the jQuery dependency within the "Add JS support" module, you see down below 13 * the helper comment a function that allows you to use jQuery with the commonly known notation: $('') 14 * By default, this notation is deactivated since WordPress uses the noConflict mode of jQuery 15 * You can also use jQuery outside using the following notation: jQuery('') 16 * 17 * Here's some jQuery example code you can use to fire code once the page is loaded: $(document).ready( function(){} ); 18 * 19 * Using the ajax example, you can send data back and forth between your frontend and the 20 * backend of the website (PHP to ajax and vice-versa). 21 * As seen in the example below, we use the jQuery $.ajax function to send data to the WordPress 22 * callback my_demo_ajax_call, which was added within the Wp_Rag_Run class. 23 * From there, we process the data and send it back to the code below, which will then display the 24 * example within the console of your browser. 25 * 26 * You can add the localized variables in here as followed: wprag.plugin_name 27 * These variables are defined within the localization function in the following file: 28 * core/includes/classes/class-wp-rag-run.php 29 * 30 * HELPER COMMENT END 31 */ 32 33(function ( $ ) { 34 35 "use strict"; 36 37 function showUserMessage(messages, userName, message) { 38 const container = $( '<div class="wp-rag-message wp-rag-message--user"></div>' ); 39 container.append( $( '<div class="wp-rag-message__author">' ).text( userName ) ) 40 container.append( $( '<div class="wp-rag-message__text--user">' ).text( message ) ); 41 messages.append( container ); 42 } 43 44 function showBotMessage(messages, botName, message, contextPosts = null) { 45 const container = $( '<div class="wp-rag-message wp-rag-message--bot"></div>' ); 46 container.append( $( '<div class="wp-rag-message__author--bot">' ).text( botName ) ) 47 container.append( $( '<div class="wp-rag-message__text--bot">' ).text( message ) ); 48 if (contextPosts !== null) { 49 showContextLinks( container, contextPosts ) 50 } 51 messages.append( container ); 52 } 53 54 function showContextLinks(container, contextPosts) { 55 if (contextPosts.length === 0) { 56 return; 57 } 58 59 const relatedInfoDiv = $( '<div class="wp-rag-related"></div>' ); 60 61 const titleDiv = $( '<div class="wp-rag-related__title"></div>' ); 62 titleDiv.append( '<span class="wp-rag-related__icon">ð</span>' ); 63 titleDiv.append( '<span class="wp-rag-related__text">Related info</span>' ); 64 relatedInfoDiv.append( titleDiv ); 65 66 const linksDiv = $( '<div class="wp-rag-related__links"></div>' ); 67 contextPosts.forEach( 68 post => { 69 const a = $( `<a href="${post.url}" target="_blank" class="wp-rag-related__link"></a>` ); 70 a.append( '<span class="wp-rag-related__link-icon">ð</span>' ); 71 a.append( $( '<span class="wp-rag-related__link-text"></span>' ).text( post.title ) ); 72 linksDiv.append(a); 73 } 74 ) 75 relatedInfoDiv.append( linksDiv ); 76 container.append( relatedInfoDiv ); 77 } 78 79 $( document ).ready( 80 function () { 81 const chatWindow = $( '#wp-rag-chat-window' ); 82 const chatIcon = $( '#wp-rag-chat-icon' ); 83 const form = $( '#wp-rag-chat-form' ); 84 const input = $( '#wp-rag-chat-input' ); 85 const submitButton = $( '#wp-rag-chat-submit-button' ); 86 const messages = $( '#wp-rag-chat-messages' ); 87 const minimizeButton = $( '#wp-rag-chat-minimize-button' ); 88 89 const userName = wpRag.chat_ui_options['user_name'] || 'You'; 90 const botName = wpRag.chat_ui_options['bot_name'] || 'Bot'; 91 const initialMessage = wpRag.chat_ui_options['initial_message']; 92 const initialChatWindowState = wpRag.chat_ui_options['initial_chat_window_state']; 93 94 if ( initialMessage ) { 95 showBotMessage( messages, botName, initialMessage ); 96 } 97 98 const minimizedInStorage = localStorage.getItem( 'wp-rag-chat-minimized' ) 99 const isMinimized = minimizedInStorage === 'true' || (minimizedInStorage === null && initialChatWindowState === 'minimized'); 100 if (isMinimized) { 101 chatWindow.addClass( 'wp-rag--hidden' ); 102 chatIcon.removeClass( 'wp-rag--hidden' ); 103 } 104 minimizeButton.on( 105 'click', 106 function () { 107 chatWindow.addClass( 'wp-rag--hidden' ); 108 chatIcon.removeClass( 'wp-rag--hidden' ); 109 localStorage.setItem( 'wp-rag-chat-minimized', 'true' ); 110 } 111 ); 112 chatIcon.on( 113 'click', 114 function () { 115 chatWindow.removeClass( 'wp-rag--hidden' ); 116 chatIcon.addClass( 'wp-rag--hidden' ); 117 localStorage.setItem( 'wp-rag-chat-minimized', 'false' ); 118 input.focus(); 119 } 120 ); 121 122 form.on( 123 'submit', 124 function (e) { 125 e.preventDefault(); 126 const message = $( '#wp-rag-chat-input' ).val(); 127 128 if (message.trim() === '') { 129 return; 130 } 131 132 submitButton.prop( 'disabled', true ).addClass( 'wp-rag-chat__submit--loading' ); 133 134 $.ajax( 135 { 136 url: wpRag.ajaxurl, 137 type: 'POST', 138 data: { 139 action: 'wp_rag_process_chat', 140 message: message 141 }, 142 success: function (response) { 143 if (response.success) { 144 showUserMessage( messages, userName, message ); 145 if ('yes' === wpRag.chat_ui_options['display_context_links']) { 146 showBotMessage( messages, botName, response.data.answer, response.data.context_posts ); 147 } else { 148 showBotMessage( messages, botName, response.data.answer ); 149 } 150 } else { 151 messages.append( '<p><strong>Error:</strong> ' + response.data + '</p>' ); 152 } 153 }, 154 error: function (jqXHR) { 155 var errorMessage = 'Unable to process your request.'; 156 if ( jqXHR.responseJSON.data && jqXHR.responseJSON.data.message ) { 157 errorMessage = jqXHR.responseJSON.data.message; 158 } 159 160 messages.append( '<p><strong>Error:</strong> ' + errorMessage + '</p>' ); 161 }, 162 complete: function () { 163 input.val( '' ).focus(); 164 submitButton.prop( 'disabled', false ).removeClass( 'wp-rag-chat__submit--loading' ); 165 } 166 } 167 ); 168 } 169 ); 170 } 171 ); 172})( jQuery );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.