can3p (can3p) wrote in changelog,
can3p
can3p
changelog

[livejournal] r19856: LJSUP-9625: Backspace works as back butt...

Committer: dpetrov
LJSUP-9625: Backspace works as back button in RTE - add prompt message
U   trunk/htdocs/js/rte.js
Modified: trunk/htdocs/js/rte.js
===================================================================
--- trunk/htdocs/js/rte.js	2011-08-26 02:25:54 UTC (rev 19855)
+++ trunk/htdocs/js/rte.js	2011-08-26 03:13:20 UTC (rev 19856)
@@ -2,8 +2,64 @@
 	window.switchedRteOn = false;
 
 	var CKEditor,
-		draftData;
+		draftData,
+		isDirty, //flag is true in case if user has unsaved text in the editor
+		processDraft,
+		lastValue;
 
+	window.initEditor = function(data) {
+		data = data || {};
+		processDraft = !!('isInitDraft' in window);
+		if (processDraft) {
+			initDraft(data);
+		} else {
+			draftData = {
+				textArea: $('#draft')
+			};
+		}
+
+		lastValue = normalizeValue(draftData.textArea.val());
+		$('#updateForm')
+			.delegate('#draft', 'keypress click', updateDraftState)
+			.submit(function(ev) {
+				isDirty = false;
+			} );
+
+		window.onbeforeunload = confirmExit;
+	}
+
+	function confirmExit(ev) {
+		if(isDirty) {
+			return  Site.ml_text["entryform.close.confirm"] || "The page contains unsaved changes.";
+		}
+	}
+
+	function normalizeValue(str) {
+		return str
+				.replace(/<br\s?\/>\n?/g, '\n')
+				.replace(/\s+$/mg, '' )
+				.trim();
+	}
+
+	function updateDraftState() {
+		var value;
+
+		setTimeout(function() {
+			if (window.switchedRteOn && CKEditor) {
+				value = CKEditor.getData();
+			} else {
+				value = draftData.textArea.val();
+			}
+
+			value = normalizeValue(value);
+			isDirty = lastValue !== value;
+		}, 0);
+
+		if(processDraft) {
+			checkDraftTimer();
+		}
+	}
+
 	function initDraftData(){
 		draftData = {
 			textArea: $('#draft'),
@@ -32,8 +88,6 @@
 		} else {
 			draftData.statusNode.val('');
 		}
-
-		$('#updateForm').delegate('#draft', 'keypress click', checkDraftTimer);
 	};
 
 	window.useRichText = function (statPrefix) {
@@ -70,15 +124,13 @@
 						CKEditor.container.show();
 						CKEditor.element.hide();
 
-						if (draftData.hasOwnProperty('draftStatus')) {
-							editor.on('dialogHide', checkDraftTimer);
-							editor.on('afterCommandExec', checkDraftTimer);
-							editor.on('insertElement', checkDraftTimer);
-							editor.on('insertHtml', checkDraftTimer);
-							editor.on('insertText', checkDraftTimer);
-							editor.document.on('keypress', checkDraftTimer);
-							editor.document.on('click', checkDraftTimer);
-						}
+						editor.on('dialogHide', updateDraftState);
+						editor.on('afterCommandExec', updateDraftState);
+						editor.on('insertElement', updateDraftState);
+						editor.on('insertHtml', updateDraftState);
+						editor.on('insertText', updateDraftState);
+						editor.document.on('keypress', updateDraftState);
+						editor.document.on('click', updateDraftState);
 					});
 				});
 			} else {
@@ -187,11 +239,14 @@
 		}
 
 		if (value.length) {
+			value = normalizeValue(value);
 			if (draftData.globalTimer) {
 				draftData.globalTimer = clearTimeout(draftData.globalTimer);
 			}
 
 			draftData.lastValue = value;
+			lastValue = value;
+			isDirty = false;
 			HTTPReq.getJSON({
 				method: 'POST',
 				url: '/tools/endpoints/draft.bml',

Tags: can3p, js, livejournal
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments