- todoList.textContent = '';
-
- addTodoForm.addEventListener( 'submit', event => {
- event.preventDefault();
- const title = newTodoText.value;
-
- // add it to the list
- // create an li
- //
{title}
- const newLi = document.createElement( 'li' );
- newLi.textContent = title; // vs newLi.innerHTML
-
- newLi.addEventListener( 'click', () => {
- newLi.classList.toggle( 'todo--complete' );
- });
+let Todo = require('./Todo.js');
+
+let state = {
+ todos: {}
+};
- // put it in the ul
- todoList.appendChild( newLi );
+let todoList;
+let todoForm;
+let todoText;
+let showState;
- newTodoText.value = '';
- });
+if(document.readyState!=='loading'){
+ onReady();
+}else{
+ document.addEventListener('DOMContentLoaded',onReady);
}
-if ( document.readyState !== 'loading' ) {
- onReady();
-} else {
- document.addEventListener( 'DOMContentLoaded', onReady );
+function onReady(){
+ todoList = document.getElementById('todolist');
+ todoForm=document.getElementById('todoform');
+ todoText=document.getElementById('todotext');
+ showState=document.getElementById('showstate');
+
+ AddEvents();
+ MakeTodoList(state);
}
+function AddEvents(){
+ showState.addEventListener('click',event=>{
+ alert(JSON.stringify(state));
+ });
+
+ todoForm.addEventListener('submit',event=>{
+ event.preventDefault();
+ let todo=new Todo({title:todoText.value});
+ state.todos[todo.getId()]=todo;
+ AddItem(todo);
+ todoText.value='';
+ });
+}
+
+function MakeTodoList(state){
+ let taskIds=Object.keys(state.todos);
+ taskIds.forEach(task=>{
+ AddItem(state.todos[task]);
+ });
+}
+
+function AddItem(task){
+ let linode = document.createElement("li");
+ linode.textContent=`${task.title}`;
+ linode.setAttribute('id',task.getId());
+ if(task.complete){linode.setAttribute('class','todo--complete')}
+ linode.addEventListener('click',()=>{
+ linode.classList.toggle('todo--complete');
+ state.todos[task.getId()].toggleComplete();
+
+ });
+ todoList.appendChild(linode);
+}
diff --git a/src/spec.js b/src/spec.js
index e69de29..18c259a 100644
--- a/src/spec.js
+++ b/src/spec.js
@@ -0,0 +1,59 @@
+
+import test from 'tape';
+import Todo from './Todo';
+
+test( 'Todo', t => {
+ let actual, expected, todo;
+ let testTodo = {
+ id: 'test',
+ title: 'Test',
+ complete: true,
+ };
+
+ todo = new Todo( testTodo ); // use new if using a constructor
+
+ actual = todo.getId();
+ expected = testTodo.id;
+ t.equal( actual, expected, 'with object, should store the id' );
+
+ actual = todo.getTitle();
+ expected = testTodo.title;
+ t.equal( actual, expected, 'with object, should store the title' );
+
+ actual = todo.isComplete();
+ expected = testTodo.complete;
+ t.equal( actual, expected, 'with object, should store the completion' );
+
+ todo = new Todo( 'Test' ); // use new if using a constructor
+
+ actual = typeof todo.getId();
+ expected = 'string';
+ t.equal( actual, expected, 'with title, should generate an id' );
+
+ actual = todo.getTitle();
+ expected = 'Test';
+ t.equal( actual, expected, 'with title, should store the title' );
+
+ actual = todo.isComplete();
+ expected = false;
+ t.equal( actual, expected, 'with title, should default to not complete' );
+
+ todo = new Todo( 'Test' ); // use new if using a constructor
+ todo.toggleComplete();
+ actual = todo.isComplete();
+ expected = true;
+ t.equal( actual, expected, 'toggleComplete should complete uncompleted todos' );
+
+ todo.toggleComplete();
+ actual = todo.isComplete();
+ expected = false;
+ t.equal( actual, expected, 'toggleComplete should uncomplete completed todos' );
+
+ todo = new Todo( 'Test' ); // use new if using a constructor
+ todo.setTitle( 'New' );
+ actual = todo.getTitle();
+ expected = 'New';
+ t.equal( actual, expected, 'setTitle should change the title' );
+
+ t.end();
+});
\ No newline at end of file
diff --git a/static/css/index.css b/static/css/index.css
new file mode 100644
index 0000000..bbd0eed
--- /dev/null
+++ b/static/css/index.css
@@ -0,0 +1,3 @@
+.todo--complete{
+ text-decoration:line-through;
+}
\ No newline at end of file
diff --git a/static/index.html b/static/index.html
index 425855a..9d60a85 100644
--- a/static/index.html
+++ b/static/index.html
@@ -2,33 +2,23 @@
-
+
Todo App
-
-