Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
todomvc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Eugene Shen
todomvc
Commits
8c997cfd
Commit
8c997cfd
authored
Dec 14, 2011
by
Florian Fesseler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added pure js version
parent
8d3c03c8
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
891 additions
and
0 deletions
+891
-0
index.html
index.html
+3
-0
todo-example/vanillajs/css/destroy.png
todo-example/vanillajs/css/destroy.png
+0
-0
todo-example/vanillajs/css/todos.css
todo-example/vanillajs/css/todos.css
+532
-0
todo-example/vanillajs/index.html
todo-example/vanillajs/index.html
+39
-0
todo-example/vanillajs/js/todo.js
todo-example/vanillajs/js/todo.js
+317
-0
No files found.
index.html
View file @
8c997cfd
...
...
@@ -90,6 +90,7 @@
<li><a
href=
"todo-example/angularjs/persistencejs/index.html"
>
Angular + PersistenceJS
</a></li>
<li><a
href=
"todo-example/broke/index.html"
>
Broke.js
</a></li>
<li><a
href=
"todo-example/fidel/index.html"
>
Fidel.js
</a></li>
<li><a
href=
"todo-example/vanillajs/index.html"
>
Vanilla JS
</a></li>
</ul>
<h5>
Contributors
</h5>
...
...
@@ -104,6 +105,7 @@
<li><a
href=
"http://developer.yahoo.com"
>
The YUI team
</a></li>
<li><a
href=
"https://github.com/jacobmumm"
>
Jacob Mumm
</a></li>
<li><a
href=
"https://github.com/brokenseal"
>
Davide Callegari
</a></li>
<li><a
href=
"https://twitter.com/ffesseler"
>
Florian Fesseler
</a></li>
</ul>
<h5>
Coming Soon
</h5>
...
...
@@ -112,6 +114,7 @@
<li>
Dojo Todo app (with MVC patterns)
</li>
<li>
Batman.js Todo app
</li>
<li>
Todo app boilerplate
</li>
<li>
Todo app pure JS : no framework, no MVC
</li>
</ul>
</div>
...
...
todo-example/vanillajs/css/destroy.png
0 → 100644
View file @
8c997cfd
555 Bytes
todo-example/vanillajs/css/todos.css
0 → 100644
View file @
8c997cfd
This diff is collapsed.
Click to expand it.
todo-example/vanillajs/index.html
0 → 100644
View file @
8c997cfd
<!DOCTYPE html>
<html>
<head>
<link
href=
"css/todos.css"
rel=
"stylesheet"
type=
"text/css"
>
<script
src=
"js/todo.js"
></script>
</head>
<body
onload=
"bodyLoadHandler()"
>
<div
id=
"todoapp"
>
<div
class=
"title"
>
<h1>
Todos
</h1>
</div>
<div
class=
"content"
>
<div
id=
"create-todo"
>
<input
id=
"new-todo"
placeholder=
"What needs to be done?"
onkeypress=
"newTodoKeyPressHandler(event)"
>
<span
class=
"ui-tooltip-top"
style=
"display:none;"
>
Press Enter to save this task
</span>
</div>
<div
id=
"todos"
>
<ul
id=
"todo-list"
>
</ul>
</div>
<div
id=
"todo-stats"
>
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
todo-example/vanillajs/js/todo.js
0 → 100644
View file @
8c997cfd
var
tasks
=
[];
var
stat
;
/**************************/
/* MODEL /
/**************************/
function
Todo
(
name
,
done
)
{
this
.
id
=
uuid
();
this
.
name
=
name
;
this
.
done
=
done
;
}
function
Stat
()
{
this
.
todoLeft
=
0
;
this
.
todoCompleted
=
0
;
this
.
totalTodo
=
0
;
}
/**************************/
/* EVENT HANDLERS /
/**************************/
function
bodyLoadHandler
()
{
loadTasks
();
refreshData
();
}
function
inputEditTodoKeyPressHandler
(
event
)
{
var
inputEditTodo
=
event
.
target
;
var
taskId
=
event
.
target
.
id
.
slice
(
6
);
if
(
event
.
keyCode
===
13
)
{
editTask
(
taskId
,
inputEditTodo
.
value
);
}
}
function
newTodoKeyPressHandler
(
event
)
{
if
(
event
.
keyCode
===
13
)
{
addTask
(
document
.
getElementById
(
"
new-todo
"
).
value
);
}
}
function
spanDeleteClickHandler
(
event
)
{
removeTaskById
(
event
.
target
.
id
);
refreshData
();
}
function
hrefClearClickHandler
()
{
removeTasksDone
();
refreshData
();
}
function
todoContentHandler
(
event
)
{
var
taskId
=
event
.
target
.
id
;
var
div
=
document
.
getElementById
(
"
li_
"
+
taskId
);
div
.
className
=
"
editing
"
;
var
inputEditTodo
=
document
.
getElementById
(
"
input_
"
+
taskId
);
inputEditTodo
.
focus
();
}
function
checkboxChangeHandler
(
event
)
{
var
checkbox
=
event
.
target
;
var
todo
=
getTodoById
(
checkbox
.
id
);
todo
.
done
=
checkbox
.
checked
;
refreshData
();
}
/**************************/
/* ACTIONS /
/**************************/
function
loadTasks
()
{
if
(
!
localStorage
.
todo
)
{
localStorage
.
todo
=
JSON
.
stringify
([]);
}
tasks
=
JSON
.
parse
(
localStorage
[
'
todo
'
]);
}
function
addTask
(
text
)
{
var
todo
=
new
Todo
(
text
,
false
);
tasks
.
push
(
todo
);
refreshData
();
}
function
editTask
(
taskId
,
text
)
{
for
(
var
i
=
0
;
i
<
tasks
.
length
;
i
++
)
{
if
(
tasks
[
i
].
id
===
taskId
)
{
tasks
[
i
].
name
=
text
;
}
}
refreshData
();
}
function
removeTaskById
(
id
)
{
for
(
var
i
=
0
;
i
<
tasks
.
length
;
i
++
)
{
if
(
tasks
[
i
].
id
===
id
)
{
tasks
.
splice
(
i
,
1
);
}
}
}
function
removeTasksDone
()
{
for
(
var
i
=
tasks
.
length
-
1
;
i
>=
0
;
--
i
)
{
if
(
tasks
[
i
].
done
)
{
tasks
.
splice
(
i
,
1
);
}
}
}
function
getTodoById
(
id
)
{
for
(
var
i
=
0
;
i
<
tasks
.
length
;
i
++
)
{
if
(
tasks
[
i
].
id
===
id
)
{
return
tasks
[
i
];
}
}
}
function
refreshData
()
{
saveTasks
();
computeStats
();
redrawTasksUI
();
redrawStatsUI
();
}
function
saveTasks
()
{
localStorage
[
'
todo
'
]
=
JSON
.
stringify
(
tasks
);
}
function
computeStats
()
{
stat
=
new
Stat
();
stat
.
totalTodo
=
tasks
.
length
;
for
(
var
i
=
0
;
i
<
tasks
.
length
;
i
++
)
{
if
(
tasks
[
i
].
done
)
{
stat
.
todoCompleted
+=
1
;
}
}
stat
.
todoLeft
=
stat
.
totalTodo
-
stat
.
todoCompleted
;
}
/**************************/
/* DRAWING /
/**************************/
function
redrawTasksUI
()
{
var
ul
=
document
.
getElementById
(
"
todo-list
"
);
var
todo
;
removeChildren
(
ul
);
document
.
getElementById
(
"
new-todo
"
).
value
=
""
;
for
(
var
i
=
0
;
i
<
tasks
.
length
;
i
++
)
{
todo
=
tasks
[
i
];
//create checkbox
var
checkbox
=
document
.
createElement
(
"
input
"
);
checkbox
.
className
=
"
check
"
;
checkbox
.
id
=
todo
.
id
;
checkbox
.
type
=
"
checkbox
"
;
checkbox
.
addEventListener
(
"
change
"
,
checkboxChangeHandler
);
//create div text
var
divText
=
document
.
createElement
(
"
div
"
);
divText
.
className
=
"
todo-content
"
;
divText
.
id
=
todo
.
id
;
divText
.
appendChild
(
document
.
createTextNode
(
todo
.
name
));
divText
.
addEventListener
(
"
dblclick
"
,
todoContentHandler
);
//create delete button
var
spanDelete
=
document
.
createElement
(
"
span
"
);
spanDelete
.
className
=
"
todo-destroy
"
;
spanDelete
.
id
=
todo
.
id
;
spanDelete
.
addEventListener
(
"
click
"
,
spanDeleteClickHandler
);
//create divDisplay
var
divDisplay
=
document
.
createElement
(
"
div
"
);
divDisplay
.
className
=
"
display
"
;
divDisplay
.
appendChild
(
checkbox
);
divDisplay
.
appendChild
(
divText
);
divDisplay
.
appendChild
(
spanDelete
);
//create div todo
var
divTodo
=
document
.
createElement
(
"
div
"
);
divTodo
.
className
=
"
todo
"
;
divTodo
.
appendChild
(
divDisplay
);
//create todo input
var
inputEditTodo
=
document
.
createElement
(
"
input
"
);
inputEditTodo
.
id
=
"
input_
"
+
todo
.
id
;
inputEditTodo
.
type
=
"
text
"
;
inputEditTodo
.
className
=
"
todo-input
"
;
inputEditTodo
.
value
=
todo
.
name
;
inputEditTodo
.
addEventListener
(
"
keypress
"
,
inputEditTodoKeyPressHandler
);
//create div edit
var
divEdit
=
document
.
createElement
(
"
div
"
);
divEdit
.
className
=
"
edit
"
;
divEdit
.
appendChild
(
inputEditTodo
);
//create li
var
li
=
document
.
createElement
(
"
li
"
);
li
.
id
=
"
li_
"
+
todo
.
id
;
li
.
appendChild
(
divTodo
);
li
.
appendChild
(
divEdit
);
if
(
todo
.
done
)
{
divTodo
.
className
+=
"
done
"
;
checkbox
.
checked
=
true
;
}
ul
.
appendChild
(
li
);
}
}
function
redrawStatsUI
()
{
removeChildren
(
document
.
getElementById
(
"
todo-stats
"
))
if
(
stat
.
totalTodo
>
0
)
{
drawTodoCount
();
}
if
(
stat
.
todoCompleted
>
0
)
{
drawTodoClear
();
}
}
function
drawTodoCount
()
{
//create span number
var
spanNumber
=
document
.
createElement
(
"
span
"
);
spanNumber
.
className
=
"
number
"
;
spanNumber
.
innerHTML
=
stat
.
todoLeft
;
//create span word
var
spanWord
=
document
.
createElement
(
"
span
"
);
spanWord
.
className
=
"
word
"
;
spanWord
.
innerHTML
=
"
item
"
;
if
(
stat
.
todoLeft
>
1
)
{
spanWord
.
innerHTML
+=
"
s
"
;
}
var
spanTodoCount
=
document
.
createElement
(
"
span
"
);
spanTodoCount
.
className
=
"
todo-count
"
;
spanTodoCount
.
appendChild
(
spanNumber
);
spanTodoCount
.
appendChild
(
spanWord
);
spanTodoCount
.
innerHTML
+=
"
left.
"
;
document
.
getElementById
(
"
todo-stats
"
).
appendChild
(
spanTodoCount
);
}
function
drawTodoClear
()
{
//create a href
var
hrefClear
=
document
.
createElement
(
"
a
"
);
hrefClear
.
href
=
"
#
"
;
hrefClear
.
addEventListener
(
"
click
"
,
hrefClearClickHandler
);
hrefClear
.
innerHTML
=
"
Clear
"
;
//create span number done
var
spanNumberDone
=
document
.
createElement
(
"
span
"
);
spanNumberDone
.
className
=
"
number-done
"
;
spanNumberDone
.
innerHTML
=
stat
.
todoCompleted
;
hrefClear
.
appendChild
(
spanNumberDone
);
hrefClear
.
innerHTML
+=
"
completed
"
;
//create span word
var
spanWordDone
=
document
.
createElement
(
"
span
"
);
spanWordDone
.
className
=
"
word-done
"
;
spanWordDone
.
innerHTML
=
"
item
"
;
if
(
stat
.
todoCompleted
>
1
)
{
spanWordDone
.
innerHTML
+=
"
s
"
;
}
hrefClear
.
appendChild
(
spanWordDone
);
var
spanTodoClear
=
document
.
createElement
(
"
span
"
);
spanTodoClear
.
className
=
"
todo-clear
"
;
spanTodoClear
.
appendChild
(
hrefClear
);
document
.
getElementById
(
"
todo-stats
"
).
appendChild
(
spanTodoClear
);
}
function
removeChildren
(
node
)
{
while
(
node
.
hasChildNodes
())
{
node
.
removeChild
(
node
.
firstChild
);
}
}
/**************************/
/* UTILS /
/**************************/
function
uuid
()
{
var
uuid
=
""
,
i
,
random
;
for
(
i
=
0
;
i
<
32
;
i
++
)
{
random
=
Math
.
random
()
*
16
|
0
;
if
(
i
==
8
||
i
==
12
||
i
==
16
||
i
==
20
)
{
uuid
+=
"
-
"
}
uuid
+=
(
i
==
12
?
4
:
(
i
==
16
?
(
random
&
3
|
8
)
:
random
)).
toString
(
16
);
}
return
uuid
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment