let msn_text; let lastIntervalCaseInfoCheck = Date.now(); // Global variable let previewCache = {}; let div_dialog = "chat_div-dialog"; let category_select_id; let explanation; // Force domain to use www. if ( !window.location.hostname.startsWith( 'www.' ) ){ // Redirect to the new URL window.location.href = '//www.' + window.location.hostname + window.location.pathname + window.location.search + window.location.hash; } // MAIN $(document).ready( function(){ console.log( _settings.categories ); // Add categories var selectElement = document.getElementById('category_select-part1_3'); console.log( selectElement ); Object.keys( _settings.categories ).forEach( function( key ){ var option = document.createElement( 'option' ); option.value = key; console.log( _settings.categories[key] ); option.text = _settings.categories[key]; selectElement.appendChild( option ); }); $('#part1_4 #case_button-explanation').on('click dblclick', function(event) { if ($('#part1_4 #explanation_text-part1_4').is(':visible')) { explanation = $('#part1_4 #explanation_text-part1_4').val(); console.log( explanation ); // Set up our HTTP request fetch( 'https://api.'+ root_domain +'/api/2023-06/case_explanation_update', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify( { 'case_access_token': case_access_token, 'explanation': explanation } ) }) .then( response=>response.json() ) .then( data=> { if ( data.status.code == 200 ){ } } ) .catch( ( error )=>{ console.error( 'Error2:', error ); }); } $('#part0').hide(); $('#part1').hide(); $('#part1_2').hide(); $('#part1_4').slideUp(function() { $('#part2').slideDown(); if ( event.type === "click" ){ info_text = $('#part2 #instructial_text').text(); $('#part2 #instructial_text').html(""); speak( info_text, 'female', function(){ div = 'instructial_text'; updateDiv(div, "", true); rantokmize( info_text, div, 70, function(){ }); }); } // Call the checkCaseInfo function every 5 seconds - refresh document table checkCaseInfo(); setInterval( function() { if ( case_access_token && case_access_token.length > 0 && ( !isSocketConnected() || ( Date.now() - lastIntervalCaseInfoCheck ) > ( 60000 * 5 ) ) ){ // 60.000 = 60 sec (5min) lastIntervalCaseInfoCheck = Date.now(); checkCaseInfo(); } }, 4000 ); }); }); // Cut text if to long and also show counter $('#part1_4 #explanation_text-part1_4').on('input', function( event ){ var maxLength = $(this).attr('maxlength'); $(this).val( $(this).val().slice( 0, maxLength ) ); var currentLength = $(this).val().length; $('#explanation_text_counter-part1_4').text( currentLength +' / '+ maxLength ); }); // Variable to store the text var text = ""; // Click event for element $('#case-table').on('click', 'td.note > b', function() { text = $(this).text(); // Get the text of the clicked element }); $('#case-table').on('click', 'td.note > div > span', function() { text = $(this).text().substring(2).trim(); // Get current text without bullet point }); updateDiv(div_dialog, "", true); // Add a click event listener to the start button $('#chat_button-start').click(function() { $('#part0').slideUp( 100, function() { info_text = htmlDecode( msn_text ); speak( info_text, 'female', function(){ $('#part1').slideDown( 90 ); div = 'chat_div-msninfo'; updateDiv(div, "", true); rantokmize( info_text, div, 70, function(){ $("#part1 #chat_input-msn").focus(); }); }); }); }) // Add a click event listener to the msn button $('#chat_button-msn').click(function() { let msn = $('#chat_input-msn').val().replace(/ +/g, '' ).replace( '+'+ _settings.translations.js_mobile_country_code, '' ); let msnRegEx = new RegExp( _settings.translations.js_mobile_number_syntax ); if ( msn.length > 0 ){ let errorMessage = null; if (!msnRegEx.test(msn)) { errorMessage = _settings.translations.invalid_mobile_number_error; } if (errorMessage) { info_text = htmlDecode( errorMessage ); speak( info_text, 'female', function(){ showDialog( _settings.translations.error_message, info_text ); }); } else { checkMsnSyntax( case_access_token, msn, function(data) { if (data.status.code == 200 || data.status.code == 405 ) { state = data.state; if ( state != 'new' ){ $('#contentPincodeValidate').data('state', state); $('#contentPincodeValidate').data('msn', msn); // Position the dialog to align its top with the top of the clicked cmd-edit $('#contentPincodeValidate').dialog('option', 'position', { my: 'center top', of: this }); $('#contentPincodeValidate').dialog('open'); } console.log( state ); console.log( case_access_token ); } else { // if ( data.status.code == 401 ){ // let info_text = "Mobilnummeret er allerede registrert, vennligst logg inn her." + state; // speak( info_text, 'female', function(){ // showDialog('Registeringen feilet', info_text ); // }); // } else { info_text = htmlDecode( data.status.message ); speak( info_text, 'female', function(){ showDialog( _settings.translations.error_message, info_text ); }); // } } }); } } }); // Bind keypress event to the msn input field to trigger the click on Enter key press $('#chat_input-msn').on('keypress', function(e) { if (e.which == 13) { // If Enter key is pressed $('#chat_button-msn').click(); // Trigger the click event on the button e.preventDefault(); // Prevent the default form submit action } }); // Add a click event listener to the init button $('#chat_button-part1_2_').click(function() { $('#part0').slideUp(); $('#part1').slideUp(); $('#part1_2').slideUp(function() { $('#part2').slideDown(); // Call the checkCaseInfo function every 5 seconds - refresh document table checkCaseInfo(); setInterval( function() { if ( case_access_token && case_access_token.length > 0 && ( !isSocketConnected() || ( Date.now() - lastIntervalCaseInfoCheck ) > ( 60000 * 5 ) ) ){ // 60.000 = 60 sec (5min) lastIntervalCaseInfoCheck = Date.now(); checkCaseInfo(); } }, 4000 ); }); }); // Add a click event listener to the init button $('#chat_button-part1_2').click(function() { $('#part0').slideUp(); $('#part1').slideUp(); $('#part1_2').slideUp( function() { $('#part1_4').slideDown(); info_text = htmlDecode( _settings.translations.explanation_info ); speak( info_text, 'female', function() { div = 'explanation_info'; updateDiv(div, "", true); rantokmize( info_text, div, 81, function() { $('#part1_4 #explanation_text-part1_4').slideDown(); $('#part1_4 #case_button-explanation').slideDown(); }); }); }); }); // Case email copy on click $('#part2 #case-email-copy, #part2 #case-email').on('click', function(){ copyToClipboard($('#part2 #case-email')); let info_text = htmlDecode( _settings.translations.email_copied_info ); speak( info_text, 'female', function(){ showDialog( _settings.translations.email_address_copied, info_text ); }); }); // Make a listener for click on the datetime td's in the rows in table inside case-table $('#case-table').on('click', 'td.date-info', function() { $(this).closest('tr').addClass('sorting-drag'); handleDateInfoClick(this); }); // Make the table rows draggable $('#case-table table tbody').sortable({ handle: "td:first-child", // Only the first td will allow sort dragging placeholder: "sorting-placeholder", helper: function(event, ui) { ui.children().each(function() { $(this).width($(this).width()); }); return ui; }, start: function(event, ui) { ui.item.addClass('sorting-drag'); pause_case_documents_syncing = true; }, update: function(event, ui) { // Get the current row and its first td let currentRow = ui.item; let currentTd = currentRow.children('td.date-info:first'); // Find the neighbor row (next if exists, previous if not) let neighborRow = currentRow.next().length ? currentRow.next() : currentRow.prev(); // If neighborRow exists if (neighborRow.length) { // Get the first td of the neighbor row let neighborTd = neighborRow.children('td:first'); // If the neighbor td has a data-timestamp if (neighborTd.attr('data-timestamp')) { // Get the data-timestamp from the neighbor td let neighborTimestamp = neighborTd.attr('data-timestamp'); // Set the data-timestamp to the current td currentTd.attr('data-timestamp', neighborTimestamp); } } }, stop: function(event, ui) { // Get the current row and its first td let currentRow = ui.item; let currentTd = currentRow.children('td.date-info:first'); if ( currentRow.children('td').length > 1 ){ handleDateInfoClick(currentTd); } } }); $('#case-table').on('click', 'td span.cmd-trash', function(e) { let document_id = $(this).closest('tr').attr('data-nid'); let document_name = $(this).closest('tr').find('td.note > b').text(); if (!document_id) { console.log('DOCUMENT_ID is not defined'); return; } let info_text = htmlDecode( _settings.translations.confirm_document_deletion.replace( '{document_name}', document_name ) ); speak( info_text, 'female', function(){ showConfirmDialog( _settings.translations.confirm_deletion, info_text, function(result) { if (result) { deleteDocument(case_access_token, document_id); } else { console.log('Deletion cancelled'); } stopSpeaking(); }, $(e.currentTarget)); }); }); // Listen for click event on elements within the table $('#case-table').on('click', 'td.note > b', function() { let document_id = $(this).closest('tr').attr('data-nid'); $('#newDocumentName').val( $(this).text() ); // Set input value to current text // Store the document_id in the dialog's data attribute $('#contentDocumentName').data('document-id', document_id); // Position the dialog to align its top with the top of the clicked cmd-edit $('#contentDocumentName').dialog('option', 'position', { my: 'right top', of: this }); $('#contentDocumentName').dialog('open'); }); $('#case-table').on('click', 'td span.cmd-edit', function(e) { info_text = htmlDecode( _settings.translations.edit_document_instructions ); speak( info_text, 'female', function(){ showDialog( _settings.translations.text_editing, info_text ); }); }); // Listen for click event on
elements within the table $('#case-table').on('click', 'td.note > div > span', function() { let document_id = $(this).closest('tr').attr('data-nid'); let document_complete_element = $(this).text().slice(1).trim(); let document_complete_summary = $(this).closest('div').text().replace( document_complete_element, '{#newDocumentSummary}' ); $('#newDocumentSummary').val( document_complete_element ); // Set input value to current text // Store the document_id in the dialog's data attribute $('#contentDocumentSummary').data('document-id', document_id); $('#contentDocumentSummary').data('document-complete-summary', document_complete_summary ); // Position the dialog to align its top with the top of the clicked cmd-edit $('#contentDocumentSummary').dialog('option', 'position', { my: 'right top', of: this }); $('#contentDocumentSummary').dialog('open'); }); $('#case-table').on('click', 'td i.cmd-view', function(e) { $('#pdf-preview').hide().empty(); let filename = $(this).attr('data-filename'); openPDF( '/cdn/'+ encodeURIComponent( filename ) ); console.log( '/cdn/'+ encodeURIComponent( filename ) ); }); $('#case_button-upload').on('click', function () { if ( document_uploading == false ){ $('#case_input-upload').trigger('click'); pause_case_documents_syncing = true; } }); $('#case_input-upload').on('change', function (e) { let file = e.target.files[0]; uploadFile(file); $(this).val(''); }); $('#case_button-upload').on('dragenter', function (e) { e.preventDefault(); e.stopPropagation(); $('#case_button-upload').css('border', '3px solid #333333'); // Change the border style as you need }); $('#case_button-upload').on('dragleave', function (e) { e.preventDefault(); e.stopPropagation(); $('#case_button-upload').css('border', 'none'); // Reset border style }); $('#case_button-upload').on('dragover', function (e) { e.preventDefault(); e.stopPropagation(); pause_case_documents_syncing = true; }); $('#case_button-upload').on('drop', function (e) { e.preventDefault(); e.stopPropagation(); $('#case_button-upload').css('border', 'none'); // Reset border style if ( document_uploading == false ){ pause_case_documents_syncing = true; if (e.originalEvent.dataTransfer) { if (e.originalEvent.dataTransfer.files.length) { // Handle the files directly here let file = e.originalEvent.dataTransfer.files[0]; uploadFile(file); } } } }); $('#pdf-close-overlay').on('click', function() { $('#pdf-overlay').hide(); $('#pdf-embed').attr('src', ''); // Optionally clear the PDF source }); $('#pdf-overlay').on('click', function() { $('#pdf-overlay').hide(); $('#pdf-embed').attr('src', ''); // Optionally clear the PDF source }); $('#pdf-container').on('click', function(e) { e.stopPropagation(); // Prevent click on container from closing the overlay }); $('#case-table').on('mouseover', 'td i.cmd-view', function(e) { let filename = $(this).attr('data-filename'); let url = '/cdn/'+ encodeURIComponent( filename ); // Get the position of the hovered icon relative to the document let iconPosition = $(this).offset(); // Adjust the position to make it relative to the viewport iconPosition.top -= $(window).scrollTop(); iconPosition.left -= $(window).scrollLeft(); openPreview(url, iconPosition); }); // Function to close PDF preview $('#case-table').on('mouseout', 'td i.cmd-view', function(e) { $('#pdf-preview').hide().empty(); }); $('#chat_button-part2').on('click', function(e) { if ( document_uploading == false ){ let info_text = htmlDecode( _settings.translations.finalize_case_confirmation ); speak(info_text, 'female', function() { showConfirmDialog( _settings.translations.confirmation_dialog_summary, info_text, function(result) { if (result) { requestSummary(case_access_token); } else { console.log('Navigation cancelled'); } stopSpeaking(); }, $(e.currentTarget)); }); } }); $('#contentDocumentName').dialog({ autoOpen: false, modal: true, width: function() { return Math.max($(window).width() * 0.3, 360); }, buttons: { 'OK': function() { var newDocumentName = $('#newDocumentName').val().trim(); if (newDocumentName.length > 0) { let document_id = $(this).data('document-id'); nameDocument( case_access_token, document_id, newDocumentName ); $(this).dialog('close'); } }, Avbryt: function() { $(this).dialog('close'); } } }); $('#contentDocumentSummary').dialog({ autoOpen: false, modal: true, width: function() { return Math.max($(window).width() * 0.3, 360); }, buttons: { 'OK': function() { var newDocumentSummary = $('#newDocumentSummary').val().trim(); if (newDocumentSummary.length > 0) { let document_id = $(this).data('document-id'); let document_complete_summary = $(this).data('document-complete-summary'); let new_document_complete_summary = document_complete_summary.replace( '{#newDocumentSummary}', newDocumentSummary ); summaryDocument( case_access_token, document_id, new_document_complete_summary ); $(this).dialog('close'); } }, Avbryt: function() { $(this).dialog('close'); } } }); $('#contentPincodeValidate').dialog({ autoOpen: false, modal: true, width: function() { return Math.max($(window).width() * 0.3, 360); }, buttons: { 'OK': function() { var pincode = $('#pincode').val().trim(); if (pincode.length > 0) { let msn = $(this).data('msn'); pincodeValidate( msn, pincode ); } }, Avbryt: function() { $(this).dialog('close'); } } }); // Bind keypress event to the pincode input field $('#contentPincodeValidate #pincode').on('keypress', function(e) { if (e.keyCode == 13) { // 13 is the keycode for the Enter key let msn = $('#contentPincodeValidate').data('msn'); var pincode = $(this).val().trim(); pincodeValidate( msn, pincode ); // Prevent the default action to avoid form submission if it's part of a form e.preventDefault(); } }); // Routing if(window.location.hash === "#innlogging") { msn_text = _settings.translations.login_prompt; $('#chat_button-start').click(); // Routing } else if(window.location.hash === "#utlogging") { set_cookie("cat", "", -1, "/"); deleteSession(); cleanUrl(); } else { msn_text = _settings.translations.usage_info; $('#part0').slideDown(500); } $(window).on('hashchange', function() { if(window.location.hash === "#innlogging") { msn_text = _settings.translations.login_prompt; $('#chat_button-start').click(); } else if(window.location.hash === "#utlogging") { set_cookie("cat", "", -1, "/"); deleteSession(); cleanUrl(); } }); getSessionCaseAccessToken( sourcePage ); setDateInfo(); initializeTooltips(); });