utb
Version:
Unified todomvc benchmark
72 lines (58 loc) • 1.74 kB
JavaScript
// @flow
import {action, mem} from 'lom_atom'
import {ESCAPE_KEY, ENTER_KEY} from '../../../common/interfaces'
import TodoItemViewOrig from '../../../common/TodoItemView'
import {Todo} from './TodoRepository'
export class TodoItemService {
editingId: ?string = null
editText: string = ''
_todo: Todo
beginEdit() {
const todo = this._todo
this.editText = todo.title
this.editingId = todo.id
}
.defer setFocus(el: ?HTMLInputElement) {
if (el) el.focus()
}
setEditText(e: Event) {
this.editText = (e.target: any).value
}
cancel() {
this.editText = ''
this.editingId = null
}
submit() {
if (!this.editingId) return
this._todo.update({title: this.editText})
this.editText = ''
this.editingId = null
}
toggle() {
this._todo.update({completed: !this._todo.completed})
}
onKey(e: KeyboardEvent) {
if (e.which === ESCAPE_KEY) {
this.cancel()
} else if (e.which === ENTER_KEY) {
this.submit()
}
}
}
const editCache = Symbol('editCache')
export default function TodoItemView({todo}: {+todo: Todo}) {
let srv: TodoItemService = (todo: Object)[editCache]
if (!srv) {
srv = new TodoItemService()
srv._todo = todo
;(todo: Object)[editCache] = srv
}
const {beginEdit, onKey, submit, setEditText, setFocus, editingId, editText, toggle} = srv
return TodoItemViewOrig({
onKey, submit, setEditText, setFocus, editingId, editText,
beginEdit,
todo,
destroy: todo.delete,
toggle
})
}