Last active
December 16, 2023 14:00
-
-
Save jon-dixon/cdb535ecbe66ee6b3942fed5e142203b to your computer and use it in GitHub Desktop.
RTE AutoSave Function and Global Variable Declaration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var iteration = 0; | |
// Javascript Interval used to call the function saveNote every X seconds. | |
var saveInterval = null; | |
// Page item used to store how many Milliseconds to wait | |
// between checks to see if the RTE field has changed. | |
var saveIntervalMS = $v("P50_SAVE_INTERVAL_MS"); | |
// The maximum number of times we should check for changes. | |
// Between these two values we could say chek for changes every | |
// 30 seconds and stop after an hour. | |
var saveIterations = $v("P50_SAVE_ITERATIONS"); | |
function saveNote () { | |
// Keep count of how many times we have checked to see if the note has changed. | |
iteration ++; | |
// Detect if there have been changes to the Rich Text field. | |
// apex.item( "P50_NOTE_CONTENT" ).isChanged() will not work because | |
// the item does not get marked as changed until you have tabbed out of it. | |
if (tinymce.activeEditor.isDirty()) { | |
apex.debug.info (' > Iteration [%s] - Saving Changes', iteration); | |
// tinymce.activeEditor.getContent() fetches the content of the Rich Text field. | |
// apex.server.chunk splits the CLOB into an | |
// array with no more than 8000 chars in each chunk. | |
var f1 = apex.server.chunk( tinymce.activeEditor.getContent() ); | |
if ( !Array.isArray( f1 ) ) { | |
f1 = [f1]; | |
} | |
// Call the PL/SQL Ajax Process called SAVE_NOTE. Set global variables x01, x02, and f01 | |
// with the Note ID, the latest Checksum for the note, and the chuncked array. | |
// We will be able to pick up the values for these variables in the PL/SQL Process. | |
apex.server.process( "SAVE_NOTE", { | |
x01: $v("P50_NOTE_ID"), | |
x02: $v("P50_NOTE_CHECKSUM"), | |
f01: f1 | |
}, | |
{ | |
success: function( data ) { | |
// Process the response from the PL/SQL Process. | |
if (data.status_code === 'S') { | |
// Let the TinyMCE Editor know there are no more changes to save. | |
tinymce.activeEditor.isNotDirty = true; | |
// Set a page item with the new checksum calculkated by the PL/SQL process. | |
apex.debug.info (' > New Checksum: ', data.new_checksum); | |
$s("P50_NOTE_CHECKSUM",data.new_checksum); | |
// Let APEX know there are no changes to save. | |
apex.page.cancelWarnOnUnsavedChanges(); | |
} else { | |
// Stop the Interval so we don't keep trying to save after an error. | |
clearInterval(saveInterval); | |
// Show the error from the PL/SQL Ajax Process. | |
apex.message.clearErrors(); | |
apex.message.showErrors([ | |
{type: "error", location: ["page"], message: data.status_msg, unsafe: false} | |
]); | |
} | |
}, | |
// Something bad happened. | |
error: function( jqXHR, textStatus, errorThrown ) { | |
apex.message.clearErrors(); | |
/* Show a Generic Error Message */ | |
apex.message.showErrors([ | |
{type: "error", location: ["page"], message: 'Unable to Save Note', unsafe: false} | |
]); | |
} | |
}); | |
} else { | |
// The note has not changed, nothing to do. | |
apex.debug.info (' > Iteration [%s] - No Changes to Save', iteration); | |
} | |
// If we have reached the Mac number of iterations then stop the interval. | |
if (iteration > saveIterations) { | |
clearInterval(saveInterval); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment