newswriter
Version:
NewsWriter is a generic tool for maintaining news and announcements on a website news page. Details are held in an SQLite database, from which the tool generates the index page (typically the top page of the target site), individual pages for events and a
217 lines (197 loc) • 6.9 kB
JavaScript
cache = {};
lastFocusedElement = null;
pageDate = null;
page_status = {
sent:false,
reviewed:false,
uploadID:null,
attachments:{}
}
keyboardSearchTimeout = null;
fieldMap = {
"convenor-name-id":"convenorID",
"presenter-name-id":"presenterID",
"title":"title",
"description":"description",
"note":"note",
"document-attachment":"documentID",
"session-title":"title",
"session-place":"place",
"session-date":"session-date",
"session-hour-start":"session-hour-start",
"session-hour-end":"session-hour-end",
"event-id":"eventID"
};
mimeTypes = {
txt: 'text/plain',
doc: 'application/msword',
pdf: 'application/pdf',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
xls: 'application/excel',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
ppt: 'application/mspowerpoint',
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
zip: 'application/zip'
}
function setStatusHighlight (row,node) {
if (row.published === 0) {
if (row.status === 0) {
node.classList.remove('normal');
node.classList.remove('approved');
node.classList.add('pending');
} else if (row.status === 1) {
node.classList.remove('normal');
node.classList.remove('pending');
node.classList.add('approved');
} else {
node.classList.remove('approved');
node.classList.remove('pending');
node.classList.add('normal');
}
} else {
node.classList.remove('approved');
node.classList.remove('pending');
node.classList.add('normal');
}
}
function updateMenuList(type,selectedId) {
var selectedId = document.getElementById('event-id').value;
if (selectedId) {
selectedId = parseInt(selectedId,10);
} else {
selectedId = 0;
}
var rows = apiRequest(
'/?admin='
+ adminID
+ '&page=' + pageName
+ '&cmd=get' + type + 'list'
);
if (false === rows) return;
var node = document.getElementById(type + '-list');
node.removeEventListener('change',getPageContent);
for (var i=1,ilen=node.childNodes.length;i<ilen;i+=1) {
node.removeChild(node.childNodes[1]);
};
var selectedIndex = 0;
for (var i=0,ilen=rows.length;i<ilen;i+=1) {
var row = rows[i];
var option = document.createElement('option');
option.value = row.eventID;
option.classList.add('truncate');
setStatusHighlight(row,option);
option.innerHTML = row.title;
node.appendChild(option);
if (option.value == selectedId) {
selectedIndex = (i+1);
}
};
node.selectedIndex = selectedIndex;
//if (selectedIndex) {
// setStatusHighlight(rows[selectedIndex-1],node);
//}
node.addEventListener('change',getPageContent);
};
function SmartId (str) {
var m = str.match(/^([-a-z]+)([0-9]+)(.*)$/);
this.num = m[2];
this.id = m[1] + m[3];
};
function keyboardSearchThrottle (delay,callback) {
return function (event) {
if (keyboardSearchTimeout) {
clearTimeout(keyboardSearchTimeout);
}
keyboardSearchTimeout = setTimeout(function() {
callback(event);
},delay);
}
};
function setFileExtension (node) {
var uploadButton = document.getElementById('uploader-attachment-button');
uploadButton.disabled = false;
return;
};
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
var ret = match && decodeURIComponent(match[1].replace(/\+/g, ' '));
if (name === 'page' && !ret) {
ret = 'top';
}
return ret;
}
function fixPath (path) {
var match = RegExp('https?://[^/]*/(.*?)([?#]|$)').exec(window.location.href);
// If a stub exists, assume secure operation, so:
var stub = match && match[1];
if (stub) {
// (1) remove &admin= value from URL
path = path.replace(/(\?)(?:admin=[^&]*)*(.*?)(?:&admin=[^&]*)*/,'$1$2');
// (2) if URL begins with '/?', append stub to '/'
path = path.replace(/^(\/)(\?)/, '$1' + stub + '$2');
// (3) remove any port designation from URL
path = path.replace(/(https?:\/\/[^\/]*):[0-9]+/, '$1');
}
return path;
}
function apiRequest (url, obj, returnAsString) {
url = fixPath(url);
if ("object" === typeof obj) {
obj = JSON.stringify(obj);
} else if (!obj) {
obj = null;
}
var xhr = new XMLHttpRequest();
xhr.open('POST', url, false);
xhr.setRequestHeader("Content-type","application/json");
xhr.send(obj);
if (200 != xhr.status) {
return false;
}
if (xhr.getResponseHeader('content-type') === 'text/html') {
document = xhr.responseXML;
}
var ret = xhr.responseText;
if (!returnAsString) {
ret = JSON.parse(ret);
}
return ret;
}
function markdown (txt) {
if (!txt) return '<p> </p>';
txt = txt.replace(/(:-?\))/g,'(\u0298\u203f\u0298)');
txt = txt.replace(/(:-\/)/g,'_(\u0361\u0e4f\u032f\u0361\u0e4f)_');
txt = txt.replace(/\(\(([a-zA-Z1-9])\)\)/g, function (aChar) {
var c, val, offset;
if (aChar[2].match(/[a-z]/)) {
val = (aChar.charCodeAt(2) - 97)
offset = 9424;
} else if (aChar[2].match(/[A-Z]/)) {
val = (aChar.charCodeAt(2) - 65)
offset = 9398;
} else {
val = (aChar.charCodeAt(2) - 49)
offset = 9312;
}
return String.fromCharCode(val + offset);
});
return marked.parse(txt);
}
// From http://stackoverflow.com/questions/4068373/center-a-popup-window-on-screen
function popupCenter(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
// Puts focus on the newWindow
if (window.focus) {
newWindow.focus();
}
}