<commit_before>// save-form-as.js
/*
   Copyright (c) 2011 3 Round Stones Inc, Some Rights Reserved
   Licensed under the Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
*/

(function($){

var originalSubmit = true;
function resubmit(form) {
	var previously = originalSubmit;
	originalSubmit = false;
	try {
		overrideLocation(form, $(form).attr('about'));
		$(form).submit(); // this time with an about attribute
	} finally {
		originalSubmit = previously;
	}
}
window.calli.saveFormAs = function(form, fileName, create) {
	$(form).find("input").change(); // IE may not have called onchange before onsubmit
	var about = $(form).attr('about');
	if (originalSubmit && fileName) { // prompt for a new URI
		openSaveAsDialog(form, fileName, create, function(ns, local){
			$(form).attr('about', ns + local.replace(/\+/g,'-').toLowerCase());
			resubmit(form);
		});
		return false;
	} else if (about && about.indexOf(':') < 0 && about.indexOf('/') != 0 && about.indexOf('?') != 0) {
		promptLocation(form, decodeURI(about), create, function(ns, local){
			$(form).attr('about', ns + local);
			resubmit(form); // this time with an about attribute
		});
		return false;
	} else if (about) { // absolute about attribute already set
		overrideLocation(form, $(form).attr('about'));
		return true;
	} else { // no identifier at all
		var field = $($(form).find('input')[0]);
		var input = field.val();
		if (input) {
			field.change(function() {
				if (input != $(form).find('input').val()) {
					// restore the about attribute when this field changes
					if (about) {
						$(form).attr('about', about);
					} else {
						$(form).removeAttr('about');
					}
				}
			});
		}
		var label = input;
		if (label && label.lastIndexOf('\\') > 0) {
			label = label.substring(label.lastIndexOf('\\') + 1);
		}
		promptLocation(form, label, create, function(ns, local){
			$(form).attr('about', ns + local.toLowerCase());
			resubmit(form); // this time with an about attribute
		});
		return false;
	}
};

function promptLocation(form, label, create, callback) {
	if (label && label.search(/^[\w\.\-\_ ]*\/?$/) == 0 && location.search.search(/\?\w+=/) >= 0) {
		var ns = calli.listResourceIRIs(getPageLocationURL())[0];
		if (ns.lastIndexOf('/') != ns.length - 1) {
			ns += '/';
		}
		var local = encodeURI(label).replace(/%20/g, '+');
		callback(ns, local);
	} else {
		openSaveAsDialog(form, label, create, callback);
	}
}

function openSaveAsDialog(form, label, create, callback) {
	var src = "/callimachus/pages/location-prompt.html#" + encodeURIComponent(label);
	if (location.search.search(/\?\w+=/) == 0) {
		src += '!' + calli.listResourceIRIs(getPageLocationURL())[0];<commit_msg>Don't double POST create forms<commit_after>// save-form-as.js
/*
   Copyright (c) 2011 3 Round Stones Inc, Some Rights Reserved
   Licensed under the Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
*/

(function($){

var originalSubmit = true;
function resubmit(form) {
	var previously = originalSubmit;
	originalSubmit = false;
	try {
		overrideLocation(form, $(form).attr('about'));
		$(form).submit(); // this time with an about attribute
	} finally {
		originalSubmit = previously;
	}
}
window.calli.saveFormAs = function(form, fileName, create) {
	$(form).find("input").change(); // IE may not have called onchange before onsubmit
	var about = $(form).attr('about');
	if (originalSubmit && fileName) { // prompt for a new URI
		openSaveAsDialog(form, fileName, create, function(ns, local){
			$(form).attr('about', ns + local.replace(/\+/g,'-').toLowerCase());
			resubmit(form);
		});
		return false;
	} else if (about && about.indexOf(':') < 0 && about.indexOf('/') != 0 && about.indexOf('?') != 0) {
		return promptIfNeeded(form, decodeURI(about), create, function(ns, local){
			$(form).attr('about', ns + local);
		});
	} else if (about) { // absolute about attribute already set
		overrideLocation(form, $(form).attr('about'));
		return true;
	} else { // no identifier at all
		var field = $($(form).find('input')[0]);
		var input = field.val();
		if (input) {
			field.change(function() {
				if (input != $(form).find('input').val()) {
					// restore the about attribute when this field changes
					if (about) {
						$(form).attr('about', about);
					} else {
						$(form).removeAttr('about');
					}
				}
			});
		}
		var label = input;
		if (label && label.lastIndexOf('\\') > 0) {
			label = label.substring(label.lastIndexOf('\\') + 1);
		}
		return promptIfNeeded(form, label, create, function(ns, local){
			$(form).attr('about', ns + local.toLowerCase());
		});
	}
};

function promptIfNeeded(form, label, create, callback) {
	if (label && label.search(/^[\w\.\-\_ ]*\/?$/) == 0 && location.search.search(/\?\w+=/) >= 0) {
		var ns = calli.listResourceIRIs(getPageLocationURL())[0];
		if (ns.lastIndexOf('/') != ns.length - 1) {
			ns += '/';
		}
		var local = encodeURI(label).replace(/%20/g, '+');
		callback(ns, local);
		return true;
	} else {
		openSaveAsDialog(form, label, create, function(ns, local) {
			callback(ns, local);
			resubmit(form); // this time with an about attribute
		});
		return false;
	}
}

function openSaveAsDialog(form, label, create, callback) {
	var src = "/callimachus/pages/location-prompt.html#" + encodeURIComponent(label);
	if (location.search.search(/\?\w+=/) == 0) {
		src += '!' + calli.listResourceIRIs(getPageLocationURL())[0];
{'fraction_non_alphanumeric': 0.19476690620969228, 'fraction_numerical': 0.00847613782937166, 'mean_word_length': 3.7530647985989494, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5385502', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1788, 'n_words': 437}