You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.0 KiB
70 lines
2.0 KiB
var $ = function(id) { |
|
return document.getElementById(id); |
|
} |
|
var iosDetected = navigator.userAgent.match("(iPad|iPod|iPhone)"); |
|
var timer = null; |
|
var timerDelay = iosDetected ? 800 : 400; |
|
var $note, $action, $preview, $plain_password, $tableau; |
|
var backendTimer; |
|
|
|
function md2html(input) { |
|
return marked(input); |
|
} |
|
|
|
function saveDraft() { |
|
if ($action == "UPDATE") return; |
|
console.log("draft autosave..."); |
|
$tableau.innerHTML = "Draft autosaved." |
|
localStorage.setItem("draft", $note.value); |
|
} |
|
|
|
function enableButton() { |
|
var checkbox = $('tos'); |
|
var button = $('publish-button'); |
|
button.disabled = !checkbox.checked; |
|
} |
|
|
|
function onLoad() { |
|
// Hook point |
|
events.publish('document:loaded'); |
|
|
|
$note = $("note"); |
|
$action = $("action").value; |
|
$preview = $("draft"); |
|
$tableau = $("tableau"); |
|
$plain_password = $("plain-password"); |
|
var updatePreview = function() { |
|
clearTimeout(timer); |
|
var content = $note.value; |
|
var delay = Math.min(timerDelay, timerDelay * (content.length / 400)); |
|
timer = setTimeout(function() { |
|
$preview.innerHTML = md2html(content); |
|
$tableau.innerHTML = content.split(/\s+/).length + " words"; |
|
|
|
// Hook point |
|
events.publish('content:rendered'); |
|
}, delay); |
|
}; |
|
if ($action == "UPDATE") updatePreview(); |
|
else { |
|
$("delete-button").style.display = "none"; |
|
$note.value = ""; |
|
var draft = localStorage.getItem("draft"); |
|
if (draft) { |
|
$note.value = draft; |
|
updatePreview(); |
|
} |
|
} |
|
$note.onkeyup = updatePreview; |
|
$("delete-button").onclick = $("publish-button").onclick = function(e) { |
|
localStorage.removeItem("draft"); |
|
self.onbeforeunload = null; |
|
if ($plain_password.value != "") $("password").value = md5($plain_password.value); |
|
$plain_password.value = null; |
|
$("signature").value = md5($("session").value + $note.value.replace(/[\n\r]/g, "")); |
|
}; |
|
if (iosDetected) $note.className += " ui-border"; |
|
else $note.focus(); |
|
self.onbeforeunload = saveDraft; |
|
setInterval(saveDraft, 60 * 1000) |
|
}
|
|
|