PageSourceSearch

https://tigerlandarchive.org/lib/ajax/autosave.js

js tigerlandarchive.org collected 2026-10-02 23:42:28 UTC 6,583 bytes, 214 lines download raw bytes

1// $Id$
2
3var auto_save_submit = false;
4var sending_auto_save = false;
5var auto_save_debug = false;	// for development use
6
7function remove_save(editorId, autoSaveId) {
8	if (typeof editorId !== 'string' || !editorId || !autoSaveId || auto_save_submit) {
9		return;	// seems to get jQuery events arriving here or has been submitted before
10	}
11	if (sending_auto_save) {	// wait if autosaving
12		setTimeout(function () { remove_save(editorId, autoSaveId); }, 100);
13	}
14	auto_save_submit = true;
15	$.ajax({
16		url: $.service("autosave", "delete"),
17		data: {
18			editor_id: editorId,
19			referer: autoSaveId
20		},
21		type: "POST",
22		async: false,	// called on form submit (save or cancel) so need to wait otherwise the call gets cancelled
23		// good callback
24		success: function(data) {
25			// act casual?
26		},
27		// bad callback - no good info in the params :(
28		error: function(req, status, error) {
29			if (error && auto_save_debug) {
30				alert(tr("Auto Save removal returned an error: ") + error);
31			}
32		}
33	});
34}
35
36function toggle_autosaved(editorId, autoSaveId) {
37	if (typeof autoSaveId === 'undefined') { autoSaveId = ''; }
38	var output = '', prefix = '';
39	var $textarea = $('#' + editorId);
40	var textareaEditor = syntaxHighlighter.get($textarea);
41	var cked = typeof CKEDITOR !== 'undefined' ? CKEDITOR.instances[editorId] : null;
42	if (!$textarea.data("original")) {	// no save version already?
43		if (cked) { prefix = 'cke_contents_'; }
44		ajaxLoadingShow(prefix + editorId);
45
46		$.getJSON( $.service("autosave", "get"), {
47				editor_id: editorId,
48				referer: autoSaveId
49			},
50			function(data) {
51				output = data.data;
52				// back up current
53				$textarea.data("original", $textarea.val());
54				if (cked) {
55					cked.setData(output);
56				} else if (textareaEditor) {
57					textareaEditor.setValue(output);
58				} else if ($("#"+editorId).length) {	// wiki editor
59					$textarea.val(output);
60				}
61				ajaxLoadingHide();
62			}
63		);
64
65	} else {	// toggle back to original
66		syntaxHighlighter.sync($textarea);
67		
68		output = $textarea.data("original");
69		if (cked) {
70			cked.setData(output);	// cked leaves the original content in the ta
71		} else if (textareaEditor) {
72			textareaEditor.setValue(output);
73		} else if ($("#"+editorId).length) {	// wiki editor
74			$("#"+editorId).val(output);
75		}
76		$textarea.data("original", "");
77	}
78	// swap the messages around (fixed to first textarea only for now)
79	var msg = $(".autosave_message_2:first").text();
80	$(".autosave_message_2:first").text($(".autosave_message:first").text());
81	$(".autosave_message:first").text(msg);
82
83	return output;
84}
85
86function auto_save_allowHtml(form) {
87	return $("input[name=allowhtml]:checked", form).length ||
88		($("input[name=allowhtml][type=hidden]", form).length &&
89			$("input[name=allowhtml][type=hidden]", form).val() ? 1 : 0);
90}
91
92function auto_save( editorId ) {
93
94	if (!editorId && auto_save_debug) { alert(tr("Auto save: No editorId set")) }
95
96	if ( !auto_save_submit && !sending_auto_save) {
97
98		var $textarea = $('#' + editorId);
99		var autoSaveId = $textarea.data("autoSaveId");
100
101		syntaxHighlighter.sync($textarea);
102		
103		var data = $textarea.val();
104		var allowHtml = auto_save_allowHtml($textarea.prop("form"));
105
106		if (data !== $textarea.data("original") || $textarea.data("last") !== data || $textarea.data("old_allowhtml") != allowHtml) {
107			$textarea.data("last", data);
108			sending_auto_save = true;
109			$.ajax({
110				url: $.service("autosave", "save"),
111				data: {
112					editor_id: editorId,
113					referer: autoSaveId,
114					data: data
115				},
116				type: "POST",
117				// good callback
118				success: function(data) {
119					// update button when it's there (TODO)
120					if (ajaxPreviewWindow && typeof ajaxPreviewWindow.get_new_preview === 'function') {
121						 ajaxPreviewWindow.get_new_preview();
122					} else {
123						ajax_preview( editorId, autoSaveId, true );
124					}
125					$textarea.data("old_allowhtml", allowHtml);
126					sending_auto_save = false;
127				},
128				// bad callback - no good info in the params :(
129				error: function(req, status, error) {
130					if (error) {
131						if (error == "Forbidden") {
132							error = req.responseText.replace(/(<([^>]+)>)/ig,"");
133						}
134						alert(tr("Auto Save error: ") + error);
135					}
136					sending_auto_save = false;
137				}
138			});
139		} else {	// not changed, reset timeout
140		}
141	}
142}
143
144function register_id( editorId, autoSaveId ) {
145	var $textarea = $('#' + editorId);
146	$textarea.data("autoSaveId", autoSaveId);
147	if (typeof syntaxHighlighter.sync === 'function') {
148		syntaxHighlighter.sync($textarea);
149	}
150	$textarea.parents('form').submit(function() { remove_save(editorId, autoSaveId); });
151	$textarea.change(function () { auto_save( editorId ); });
152}
153
154var ajaxPreviewWindow;
155
156function ajax_preview(editorId, autoSaveId, inPage) {
157	if (editorId) {
158		var $textarea = $('#' + editorId);
159		var $autosavepreview = $("#autosave_preview");
160		syntaxHighlighter.sync($textarea);
161		var allowHtml = auto_save_allowHtml($textarea.prop("form"));
162		if (!ajaxPreviewWindow) {
163			if (inPage) {
164				var $prvw = $("#autosave_preview:visible");
165				if ($prvw.length) {
166					ajaxLoadingShow($autosavepreview);
167					var h = location.search.match(/&hdr=(\d?)/);
168					h = h && h.length ? h[1] : "";
169					$.get($.service("edit", "preview"), {
170						editor_id: editorId,
171						autoSaveId: autoSaveId,
172						inPage: 1,
173						hdr: h,
174						allowHtml: allowHtml
175					}, function(data) {
176						// remove JS and disarm links
177						data = data.replace(/\shref/gi, " tiki_href").
178								replace(/\sonclick/gi, " tiki_onclick").
179								replace(/<script[.\s\S]*?<\/script>/mgi, "");
180						$(".preview_contents", $autosavepreview).html(data);
181						$("#preview_diff_style").val(function(){return getCookie("preview_diff_style","preview","");});
182						ajaxLoadingHide();
183					});
184				}
185			} else {
186				initPreviewWindow(editorId, autoSaveId, allowHtml);
187			}
188		} else {
189			if (typeof ajaxPreviewWindow.get_new_preview === 'function') {
190				ajaxPreviewWindow.get_new_preview();
191				ajaxPreviewWindow.focus();
192			} else {
193				initPreviewWindow(editorId, autoSaveId, allowHtml);
194			}
195		}
196	} else {
197		if (auto_save_debug) {
198			alert("Auto save data not found");
199		}
200	}
201	
202}
203
204function initPreviewWindow (editorId, autoSaveId, allowHtml) {
205	var features = 'menubar=no,toolbar=no,fullscreen=no,titlebar=no,status=no,width=600';
206	var url = $.service("edit", "preview", {editor_id: editorId, autoSaveId: autoSaveId, allowHtml: allowHtml });
207	window.open(url, "_blank", features);
208}
209
210$(window).on("unload", function () {
211	if (ajaxPreviewWindow && typeof ajaxPreviewWindow.get_new_preview === 'function') {
212		ajaxPreviewWindow.close();
213	}
214});

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.