-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathractive_tutorial.js
More file actions
86 lines (71 loc) · 2 KB
/
Copy pathractive_tutorial.js
File metadata and controls
86 lines (71 loc) · 2 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var ractive = new Ractive({
target: '#target',
template: '#template',
data: {
todos: localStorage.todos ? JSON.parse(localStorage.todos) : [],
editing: -1,
edit_mode: true,
reorder_mode: false,
},
})
function add() {
var item = ractive.get('item')
if (item.trim() == '') return
var todos = ractive.get('todos')
todos.push(item)
ractive.set('todos', todos)
localStorage.todos = JSON.stringify(todos)
ractive.set('item', '')
}
function remove(item) {
ractive.set('editing', -1)
var todos = ractive.get('todos')
todos.splice(item, 1)
ractive.set('todos', todos)
localStorage.todos = JSON.stringify(todos)
}
function edit(index) {
ractive.set('item_edit', ractive.get('todos')[index])
ractive.set('editing', index)
document.querySelector('#item_edit').focus()
}
function save(index) {
ractive.set('editing', -1)
var item_edit = ractive.get('item_edit')
if (item_edit.trim() == '') {
remove(index)
return
}
var todos = ractive.get('todos')
todos[index] = item_edit
ractive.set('todos', todos)
localStorage.todos = JSON.stringify(todos)
}
function swap(array, index_a, index_b) {
var temp = array[index_a]
array[index_a] = array[index_b]
array[index_b] = temp
return array
}
function up(index) {
var todos = ractive.get('todos')
if (index <= 0) return
todos = swap(todos, index, index - 1)
ractive.set('todos', todos)
localStorage.todos = JSON.stringify(todos)
}
function down(index) {
var todos = ractive.get('todos')
if (index >= (todos.length - 1)) return
todos = swap(todos, index, index + 1)
ractive.set('todos', todos)
localStorage.todos = JSON.stringify(todos)
}
function toggle_edit_mode() {
ractive.set('reorder_mode', false)
ractive.set('edit_mode', !ractive.get('edit_mode'))
}
function toggle_reorder_mode() {
ractive.set('editing', -1)
ractive.set('reorder_mode', !ractive.get('reorder_mode'))
}