Commit cbbdbae6 authored by Mathieu Lorber's avatar Mathieu Lorber

New formatter on all vanilladart code

parent fe473ce1
......@@ -5,7 +5,7 @@ class Todo {
String title;
bool completed;
Todo(this.id, this.title, {this.completed : false});
Todo(this.id, this.title, {this.completed: false});
Todo.fromJson(Map json) {
id = json['id'];
......@@ -15,10 +15,6 @@ class Todo {
// this is automatically called by JSON.encode
Map toJson() {
return {
'id': id,
'title': title,
'completed': completed
};
return {'id': id, 'title': title, 'completed': completed};
}
}
\ No newline at end of file
}
part of todomvc;
class TodoApp {
List<TodoWidget> todoWidgets = new List<TodoWidget>();
Element todoListElement = querySelector('#todo-list');
Element mainElement = querySelector('#main');
InputElement checkAllCheckboxElement = querySelector('#toggle-all');
Element footerElement = querySelector('#footer');
Element countElement = querySelector('#todo-count');
Element clearCompletedElement = querySelector('#clear-completed');
Element showAllElement = querySelector('#filters a[href="#/"]');
Element showActiveElement = querySelector('#filters a[href="#/active"]');
Element showCompletedElement = querySelector('#filters a[href="#/completed"]');
TodoApp() {
initLocalStorage();
initElementEventListeners();
window.onHashChange.listen((e) => updateFilter());
updateFooterDisplay();
}
void initLocalStorage() {
var jsonList = window.localStorage['todos-vanilladart'];
if (jsonList != null) {
try {
List<Map> todos = JSON.decode(jsonList);
todos.forEach((todo) => addTodo(new Todo.fromJson(todo)));
} catch (e) {
print('Could not load todos form local storage.');
}
}
}
void initElementEventListeners() {
InputElement newTodoElement = querySelector('#new-todo');
newTodoElement.onKeyDown.listen((KeyboardEvent e) {
if (e.keyCode == KeyCode.ENTER) {
List<TodoWidget> todoWidgets = new List<TodoWidget>();
Element todoListElement = querySelector('#todo-list');
Element mainElement = querySelector('#main');
InputElement checkAllCheckboxElement = querySelector('#toggle-all');
Element footerElement = querySelector('#footer');
Element countElement = querySelector('#todo-count');
Element clearCompletedElement = querySelector('#clear-completed');
Element showAllElement = querySelector('#filters a[href="#/"]');
Element showActiveElement = querySelector('#filters a[href="#/active"]');
Element showCompletedElement =
querySelector('#filters a[href="#/completed"]');
TodoApp() {
initLocalStorage();
initElementEventListeners();
window.onHashChange.listen((e) => updateFilter());
updateFooterDisplay();
}
void initLocalStorage() {
var jsonList = window.localStorage['todos-vanilladart'];
if (jsonList != null) {
try {
List<Map> todos = JSON.decode(jsonList);
todos.forEach((todo) => addTodo(new Todo.fromJson(todo)));
} catch (e) {
print('Could not load todos form local storage.');
}
}
}
void initElementEventListeners() {
InputElement newTodoElement = querySelector('#new-todo');
newTodoElement.onKeyDown.listen((KeyboardEvent e) {
if (e.keyCode == KeyCode.ENTER) {
var title = newTodoElement.value.trim();
if (title.isNotEmpty) {
addTodo(new Todo(uuid(), title));
......@@ -47,113 +47,114 @@ class TodoApp {
updateFooterDisplay();
save();
}
}
});
checkAllCheckboxElement.onClick.listen((e) {
for (var todoWidget in todoWidgets) {
if (todoWidget.todo.completed != checkAllCheckboxElement.checked) {
todoWidget.toggle();
}
}
updateCounts();
save();
});
clearCompletedElement.onClick.listen((_) {
var newList = new List<TodoWidget>();
for (TodoWidget todoWidget in todoWidgets) {
if (todoWidget.todo.completed) {
todoWidget.element.remove();
} else {
newList.add(todoWidget);
}
}
todoWidgets = newList;
updateFooterDisplay();
save();
});
}
void addTodo(Todo todo) {
var todoWidget = new TodoWidget(this, todo);
todoWidgets.add(todoWidget);
todoListElement.nodes.add(todoWidget.createElement());
}
void updateFooterDisplay() {
var display = todoWidgets.length == 0 ? 'none' : 'block';
checkAllCheckboxElement.style.display = display;
mainElement.style.display = display;
footerElement.style.display = display;
updateCounts();
}
void updateCounts() {
var complete = todoWidgets.where((w) => w.todo.completed).length;
checkAllCheckboxElement.checked = (complete == todoWidgets.length);
var left = todoWidgets.length - complete;
countElement.innerHtml = '<strong>$left</strong> item${left != 1 ? 's' : ''} left';
if (complete == 0) {
clearCompletedElement.style.display = 'none';
} else {
clearCompletedElement.style.display = 'block';
clearCompletedElement.text = 'Clear completed ($complete)';
}
updateFilter();
}
void removeTodo(TodoWidget todoWidget) {
todoWidgets.removeAt(todoWidgets.indexOf(todoWidget));
}
void updateFilter() {
switch(window.location.hash) {
case '#/active':
showActive();
break;
case '#/completed':
showCompleted();
break;
default:
showAll();
return;
}
}
void showAll() {
setSelectedFilter(showAllElement);
for (var todoWidget in todoWidgets) {
todoWidget.visible = true;
}
}
void showActive() {
setSelectedFilter(showActiveElement);
for (var todoWidget in todoWidgets) {
todoWidget.visible = !todoWidget.todo.completed;
}
}
void showCompleted() {
setSelectedFilter(showCompletedElement);
for (var todoWidget in todoWidgets) {
todoWidget.visible = todoWidget.todo.completed;
}
}
void setSelectedFilter(Element e) {
showAllElement.classes.remove('selected');
showActiveElement.classes.remove('selected');
showCompletedElement.classes.remove('selected');
e.classes.add('selected');
}
void save() {
var todos = new List<Todo>();
for (var todoWidget in todoWidgets) {
todos.add(todoWidget.todo);
}
window.localStorage['todos-vanilladart'] = JSON.encode(todos);
}
}
});
checkAllCheckboxElement.onClick.listen((e) {
for (var todoWidget in todoWidgets) {
if (todoWidget.todo.completed != checkAllCheckboxElement.checked) {
todoWidget.toggle();
}
}
updateCounts();
save();
});
clearCompletedElement.onClick.listen((_) {
var newList = new List<TodoWidget>();
for (TodoWidget todoWidget in todoWidgets) {
if (todoWidget.todo.completed) {
todoWidget.element.remove();
} else {
newList.add(todoWidget);
}
}
todoWidgets = newList;
updateFooterDisplay();
save();
});
}
void addTodo(Todo todo) {
var todoWidget = new TodoWidget(this, todo);
todoWidgets.add(todoWidget);
todoListElement.nodes.add(todoWidget.createElement());
}
void updateFooterDisplay() {
var display = todoWidgets.length == 0 ? 'none' : 'block';
checkAllCheckboxElement.style.display = display;
mainElement.style.display = display;
footerElement.style.display = display;
updateCounts();
}
void updateCounts() {
var complete = todoWidgets.where((w) => w.todo.completed).length;
checkAllCheckboxElement.checked = (complete == todoWidgets.length);
var left = todoWidgets.length - complete;
countElement.innerHtml =
'<strong>$left</strong> item${left != 1 ? 's' : ''} left';
if (complete == 0) {
clearCompletedElement.style.display = 'none';
} else {
clearCompletedElement.style.display = 'block';
clearCompletedElement.text = 'Clear completed ($complete)';
}
updateFilter();
}
void removeTodo(TodoWidget todoWidget) {
todoWidgets.removeAt(todoWidgets.indexOf(todoWidget));
}
void updateFilter() {
switch (window.location.hash) {
case '#/active':
showActive();
break;
case '#/completed':
showCompleted();
break;
default:
showAll();
return;
}
}
void showAll() {
setSelectedFilter(showAllElement);
for (var todoWidget in todoWidgets) {
todoWidget.visible = true;
}
}
void showActive() {
setSelectedFilter(showActiveElement);
for (var todoWidget in todoWidgets) {
todoWidget.visible = !todoWidget.todo.completed;
}
}
void showCompleted() {
setSelectedFilter(showCompletedElement);
for (var todoWidget in todoWidgets) {
todoWidget.visible = todoWidget.todo.completed;
}
}
void setSelectedFilter(Element e) {
showAllElement.classes.remove('selected');
showActiveElement.classes.remove('selected');
showCompletedElement.classes.remove('selected');
e.classes.add('selected');
}
void save() {
var todos = new List<Todo>();
for (var todoWidget in todoWidgets) {
todos.add(todoWidget.todo);
}
window.localStorage['todos-vanilladart'] = JSON.encode(todos);
}
}
part of todomvc;
class TodoWidget {
static const HtmlEscape htmlEscape = const HtmlEscape();
static const HtmlEscape htmlEscape = const HtmlEscape();
TodoApp todoApp;
Todo todo;
Element element;
InputElement toggleElement;
TodoApp todoApp;
Todo todo;
Element element;
InputElement toggleElement;
TodoWidget(this.todoApp, this.todo);
TodoWidget(this.todoApp, this.todo);
Element createElement() {
element = new Element.html('''
Element createElement() {
element = new Element.html('''
<li ${todo.completed ? 'class="completed"' : ''}>
<div class='view'>
<input class='toggle' type='checkbox' ${todo.completed ? 'checked' : ''}>
......@@ -23,78 +22,78 @@ class TodoWidget {
</li>
''');
Element contentElement = element.querySelector('.todo-content');
InputElement editElement = element.querySelector('.edit');
toggleElement = element.querySelector('.toggle');
toggleElement.onClick.listen((_) {
toggle();
todoApp.updateCounts();
todoApp.save();
});
contentElement.onDoubleClick.listen((_) {
element.classes.add('editing');
editElement.selectionStart = todo.title.length;
editElement.focus();
});
void removeTodo() {
element.remove();
todoApp.removeTodo(this);
todoApp.updateFooterDisplay();
}
element.querySelector('.destroy').onClick.listen((_) {
removeTodo();
todoApp.save();
});
void doneEditing() {
editElement.value = editElement.value.trim();
todo.title = editElement.value;
if (todo.title.isNotEmpty) {
contentElement.text = todo.title;
element.classes.remove('editing');
} else {
removeTodo();
}
todoApp.save();
}
void undoEditing() {
element.classes.remove('editing');
editElement.value = todo.title;
}
editElement
..onKeyDown.listen((KeyboardEvent e) {
switch (e.keyCode) {
case KeyCode.ENTER:
doneEditing();
break;
case KeyCode.ESC:
undoEditing();
break;
}
})
..onBlur.listen((_) => doneEditing());
return element;
}
void set visible(bool visible) {
element.style.display = visible ? 'block' : 'none';
}
void toggle() {
todo.completed = !todo.completed;
toggleElement.checked = todo.completed;
if (todo.completed) {
element.classes.add('completed');
} else {
element.classes.remove('completed');
}
}
Element contentElement = element.querySelector('.todo-content');
InputElement editElement = element.querySelector('.edit');
toggleElement = element.querySelector('.toggle');
toggleElement.onClick.listen((_) {
toggle();
todoApp.updateCounts();
todoApp.save();
});
contentElement.onDoubleClick.listen((_) {
element.classes.add('editing');
editElement.selectionStart = todo.title.length;
editElement.focus();
});
void removeTodo() {
element.remove();
todoApp.removeTodo(this);
todoApp.updateFooterDisplay();
}
element.querySelector('.destroy').onClick.listen((_) {
removeTodo();
todoApp.save();
});
void doneEditing() {
editElement.value = editElement.value.trim();
todo.title = editElement.value;
if (todo.title.isNotEmpty) {
contentElement.text = todo.title;
element.classes.remove('editing');
} else {
removeTodo();
}
todoApp.save();
}
void undoEditing() {
element.classes.remove('editing');
editElement.value = todo.title;
}
editElement
..onKeyDown.listen((KeyboardEvent e) {
switch (e.keyCode) {
case KeyCode.ENTER:
doneEditing();
break;
case KeyCode.ESC:
undoEditing();
break;
}
})
..onBlur.listen((_) => doneEditing());
return element;
}
void set visible(bool visible) {
element.style.display = visible ? 'block' : 'none';
}
void toggle() {
todo.completed = !todo.completed;
toggleElement.checked = todo.completed;
if (todo.completed) {
element.classes.add('completed');
} else {
element.classes.remove('completed');
}
}
}
library todomvc;
import 'dart:html'
show Element, InputElement, KeyCode, KeyboardEvent, querySelector, window;
show Element, InputElement, KeyCode, KeyboardEvent, querySelector, window;
import 'dart:convert' show HtmlEscape, JSON;
import 'package:todomvc_vanilladart/models.dart';
import 'package:todomvc_vanilladart/uuid.dart';
......@@ -10,5 +10,5 @@ part 'TodoWidget.dart';
part 'TodoApp.dart';
void main() {
new TodoApp();
new TodoApp();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment