summaryrefslogtreecommitdiffstats
path: root/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/emberjs-debug/source/app/components/todo-item.js
blob: 0de7fbf1165b2614541a56a390d1c338c498e093 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import Ember from 'ember';

export default Ember.Component.extend({
    repo: Ember.inject.service(),
    tagName: 'li',
    editing: false,
    classNameBindings: ['todo.completed', 'editing'],

    actions: {
        startEditing() {
            this.get('onStartEdit')();
            this.set('editing', true);
            Ember.run.scheduleOnce('afterRender', this, 'focusInput');
        },

        doneEditing(todoTitle) {
            if (!this.get('editing')) { return; }
            if (Ember.isBlank(todoTitle)) {
                this.send('removeTodo');
            } else {
                this.set('todo.title', todoTitle.trim());
                this.set('editing', false);
                this.get('onEndEdit')();
            }
        },

        handleKeydown(e) {
            if (e.keyCode === 13) {
                e.target.blur();
            } else if (e.keyCode === 27) {
                this.set('editing', false);
            }
        },

        toggleCompleted(e) {
            let todo = this.get('todo');
            Ember.set(todo, 'completed', e.target.checked);
            this.get('repo').persist();
        },

        removeTodo() {
            this.get('repo').delete(this.get('todo'));
        }
    },

    focusInput() {
        this.element.querySelector('input.edit').focus();
    }
});