Utilisateur:Zelda/todo-list.js
De Wikipedia, l'encyclopédie encyclopedia
function initTodoList() {
if (typeof globalStorage == "undefined") { alert("This todo list requires client side persistent storage features which are currently available on Firefox 2.0 only"); return; }
// retrieve stored text var textContent = globalStorage[location.hostname].getItem("todo-list.content"); if (! textContent) textContent = "";
var todoList = document.createElement("table"); todoList.id = "todo-list"; todoList.setAttribute("style", "height:60%; position:fixed; top:20%; right:0; z-index:10; opacity:.8; background:white; border:1px solid grey; border-spacing:0; -moz-border-radius-topleft: 5px; -moz-border-radius-bottomleft: 5px; border-right:none");
/**
* Simple todo list docked on the right side of the page to take rapid notes. * The list is saved and survives over a browser restart. * This script uses client side storage which is currently only * available on Firefox 2, and will NOT work on other non gecko browsers (IE, Opera, ...) * Some wiki syntax is rendered for : * - links and templates (e.g. foo and (bar)) * - bold and italic text (e.g. foo and bar) * Author : Zelda for fr.wikipedia.org */
/**
* Creates and restore the todo list */
todoList.innerHTML = '' +
't
o
d
o
l
i
s
t' +
'
<textarea style="width:100%; height:99%; overflow:auto; border:none" id="todo-list-edit" onchange="saveTodoList()" onblur="renderTodoList()">' + textContent + '</textarea>' + ''; document.body.appendChild(todoList); renderTodoList(); var visible = sessionStorage.getItem("todo-list.visible"); if (visible == "true") { showTodoList(); } else { hideTodoList(); } } /** * Show the todo list */ function showTodoList() { var todoList = document.getElementById("todo-list"); var listContent = document.getElementById("todo-list-content"); var todoListBar = document.getElementById("todo-list-bar"); listContent.parentNode.style.display = ""; todoList.style.width="40%"; todoListBar.onclick = hideTodoList; sessionStorage.setItem("todo-list.visible", "true"); } /** * Hide the todo list */ function hideTodoList() { var todoList = document.getElementById("todo-list"); var listContent = document.getElementById("todo-list-content"); var todoListBar = document.getElementById("todo-list-bar"); listContent.parentNode.style.display = "none"; todoList.style.width="auto"; todoListBar.onclick = showTodoList; sessionStorage.setItem("todo-list.visible", "false"); } /** * Save the todo list (each time the content changes) */ function saveTodoList() { globalStorage[location.hostname].setItem("todo-list.content", document.getElementById("todo-list-edit").value); } /** * Shows a textarea to edit the todo list */ function editTodoList() { var div = document.getElementById("todo-list-content"); var textarea = document.getElementById("todo-list-edit"); div.innerHTML = textarea.value; div.style.display = "none"; textarea.style.display = ""; textarea.focus(); } /** * Renders the todo list interpreting some wiki syntax (links, templates, bold, italic) */ function renderTodoList() { var div = document.getElementById("todo-list-content"); var textarea = document.getElementById("todo-list-edit"); var content = textarea.value; if (!content || content == "") { content = "Empty list... Double click to edit."; } // Escape HTML characters content = content.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); // Render wiki links content = content.replace(/\[\[:?([^\|\]]*)(?:\|([^\|\]]+))?\]\]/g, function(match, article, title, offset, s) { return "<a href=\"" + mw.config.get('wgServer') + "/wiki/" + article + "\">" + ((title) ? title : article) + "</a>"; }); // Render wiki templates content = content.replace(/\{\{([^\}\|]+)(\|[^\}]+)?\}\}/g, "<a href=\"" + mw.config.get('wgServer') + "/wiki/Template:$1\">Modèle:$1$2</a>"); // Render bold content = content.replace(/((?:[^']|'(?!))+)/g, "$1"); // Render italic content = content.replace(/((?:[^']|'(?!'))+)/g, "$1"); div.innerHTML = content; div.style.display = ""; textarea.style.display = "none"; } $(initTodoList);