Files
xxx/publish/utilities.js
T
2013-05-17 23:38:44 +02:00

125 lines
4.4 KiB
JavaScript

// GLOBAL
var tinyMCEstate = true;
function showHide(show) {
if (show == 'text') {
var hide = 'upload';
} else if (show == 'upload') {
var hide = 'text';
} else {
return false;
}
if (show == 'text') {
document.getElementById(hide+'parent').style.display = "none";
document.getElementById(show+'parent').style.display = "block";
document.getElementById('wordcounter').style.display = "block";
if (!tinyMCEstate && tinyMCE) {
// Toggling breaks getContent if the inital state is toggled off.
//tinyMCE.execCommand('mceToggleEditor',false,'text');
tinyMCEstate = true;
}
tinyMCE.execCommand('mceFocus',false,'text');
} else if (show == 'upload') {
if (tinyMCEstate && tinyMCE) {
// Toggling breaks getContent if the inital state is toggled off.
//tinyMCE.execCommand('mceToggleEditor',false,'text');
tinyMCEstate = false;
}
document.getElementById('wordcounter').style.display = "none";
document.getElementById(hide+'parent').style.display = "none";
document.getElementById(show+'parent').style.display = "block";
}
return true;
}
// GLOBAL
var is_delete = 0;
function do_delete() {
if (confirm('Are you certain you wish to delete this post?')) {
is_delete = 1;
return true;
} else
return false;
}
function review() {
var title = document.getElementById('title');
var author = document.getElementById('author');
var email = document.getElementById('email');
var upload = document.getElementById('upload');
var uploaded = document.getElementById('uploaded');
var remove_img = document.getElementById('remove_img');
var txtcnt = wc(tinyMCE.get('story').getContent());
if (is_delete) return true;
if (!title.value) {
alert("Please add a title to your submission; one makes everything much prettier.");
return false;
} if (!author.value) {
alert("You haven't added an author. You can use a pseudonym if you like.");
return false;
} if (!email.value) {
if (!confirm("You haven't added an email address.\n\nThis will not be published. It will only be used by editors if we need to contact you.\n\nClick 'cancel' if you would like to go back and add one now."))
return false;
} if ((txtcnt == 0) && (upload.value == '') && ((!remove_img || remove_img.value == 1) || (!uploaded || uploaded.value == ''))) {
alert("You haven't submitted anything (not even a picture!).");
return false;
} else if ((txtcnt != '100') && (upload.value == '') && ((!remove_img || remove_img.value == 1) || (!uploaded || uploaded.value == ''))) {
if (!confirm("Your submission doesn't appear to be 100 words long. I'll continue anyway, but you may want to review."))
return false;
}
return true;
}
// GLOBAL
if (document.images) {
trashcan_img = new Image(16,22);
trashcan_img.src="/graphics/trashcan-open.png";
}
function toggle_remove() {
var virtual_checkbox = document.getElementById('remove_img');
var img = document.getElementById('remove_img_icon');
if(virtual_checkbox.value == 0) {
virtual_checkbox.value=1;
img.src='/graphics/trashcan-open.png'
img.title = 'Image is marked for removal.';
} else {
virtual_checkbox.value=0;
img.src='/graphics/trashcan.png'
img.title = 'Image has been restored; it will not be removed.';
}
}
function mceWordCount (e) {
if (e && e.type != 'keyup')
return true;
var txt = tinyMCE.get('story').getContent();
var wordcounter = document.getElementById("wordcounter");
var cnt = wc(txt);
var words = (cnt == 1) ? " word" : " words";
if (wordcounter) {
wordcounter.innerHTML = cnt + words;
wordcounter.className = "counter";
if (cnt == 100) wordcounter.className += " yes";
if (cnt > 100) wordcounter.className += " no";
}
return true;
}
function wc (txt) {
txt = txt.replace(/(<br( \/)?>)/ig," "); // replace ending <br>s with spaces
txt = txt.replace(/(<([^>]+)>)/ig,""); //strip HTML
txt = txt.replace(/(\s?[a-zA-Z0-9]+)\s?(?:&.dash;\s?|-(?:-+\s?|\s))([a-zA-Z0-9]+)/g,"$1 $2"); //strip mdashes
txt = txt.replace(/\s[^a-zA-Z0-9]+\s/g," "); //strip punctuation
txt = txt.replace(/\s*((\S+\s*)*)/, "$1"); //ltrim
txt = txt.replace(/((\s*\S+)*)\s*/, "$1"); //rtrim
txt = txt.replace(/(&nbsp;)/g, " "); //TinyMCE newlines leave spaces behind
txt = txt.replace(/(^\s+|\s+$)/, " "); // Remove trailing spaces
if (txt.match(/^ *$/))
return 0;
var cnt = txt.split(/\s+/g);
return cnt.length;
}