-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.js
179 lines (146 loc) · 5.14 KB
/
base.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
$(function() {
ShareThingy();
var i = Number(localStorage.getItem('todo-counter')) + 1,
j = 0,
k,
$form = $('#todo-form'),
$removeLink = $('#show-items li a'),
$itemList = $('#show-items'),
$editable = $('.editable'),
$clearAll = $('#clear-all'),
$newTodo = $('#todo'),
order = [],
orderList;
// Load todo list
orderList = localStorage.getItem('todo-orders');
orderList = orderList ? orderList.split(',') : [];
for( j = 0, k = orderList.length; j < k; j++) {
$itemList.append(
"<li id='" + orderList[j] + "'>"
+ "<span class='editable'>"
+ localStorage.getItem(orderList[j])
+ "</span> <a href='#'>X</a></li>"
);
}
// Add todo
$form.submit(function(e) {
e.preventDefault();
$.publish('/add/', []);
});
// Remove todo
$itemList.delegate('a', 'click', function(e) {
var $this = $(this);
e.preventDefault();
$.publish('/remove/', [$this]);
});
// Sort todo
$itemList.sortable({
revert: true,
stop: function() {
$.publish('/regenerate-list/', []);
}
});
// Edit and save todo
$editable.inlineEdit({
save: function(e, data) {
var $this = $(this);
localStorage.setItem(
$this.parent().attr("id"), data.value
);
}
});
// Clear all
$clearAll.click(function(e) {
e.preventDefault();
$.publish('/clear-all/', []);
});
// Fade In and Fade Out the Remove link on hover
$itemList.delegate('li', 'mouseover mouseout', function(event) {
var $this = $(this).find('a');
if(event.type === 'mouseover') {
$this.stop(true, true).fadeIn();
} else {
$this.stop(true, true).fadeOut();
}
});
// Subscribes
$.subscribe('/add/', function() {
if ($newTodo.val() !== "") {
// Take the value of the input field and save it to localStorage
localStorage.setItem(
"todo-" + i, $newTodo.val()
);
// Set the to-do max counter so on page refresh it keeps going up instead of reset
localStorage.setItem('todo-counter', i);
// Append a new list item with the value of the new todo list
$itemList.append(
"<li id='todo-" + i + "'>"
+ "<span class='editable'>"
+ localStorage.getItem("todo-" + i)
+ " </span><a href='#'>X</a></li>"
);
$.publish('/regenerate-list/', []);
// Hide the new list, then fade it in for effects
$("#todo-" + i)
.css('display', 'none')
.fadeIn();
// Empty the input field
$newTodo.val("");
i++;
}
});
$.subscribe('/remove/', function($this) {
var parentId = $this.parent().attr('id');
// Remove todo list from localStorage based on the id of the clicked parent element
localStorage.removeItem(
"'" + parentId + "'"
);
// Fade out the list item then remove from DOM
$this.parent().fadeOut(function() {
$this.parent().remove();
$.publish('/regenerate-list/', []);
});
});
$.subscribe('/regenerate-list/', function() {
var $todoItemLi = $('#show-items li');
// Empty the order array
order.length = 0;
// Go through the list item, grab the ID then push into the array
$todoItemLi.each(function() {
var id = $(this).attr('id');
order.push(id);
});
// Convert the array into string and save to localStorage
localStorage.setItem(
'todo-orders', order.join(',')
);
});
$.subscribe('/clear-all/', function() {
var $todoListLi = $('#show-items li');
order.length = 0;
localStorage.clear();
$todoListLi.remove();
});
});
function ShareThingy ()
{
$('#share').click(function(){
console.log("thingy is being shared");
if( Omlet.isInstalled() )
{
var rdl = Omlet.createRDL({
noun: "task",
displayTitle: "Tasks",
displayThumbnailUrl: "http://mobi-summer-vidur.s3.amazonaws.com/casket/icon.jpg",
displayText: "Save (and eventually share) tasks",
webCallback: "http://mobi-summer-vidur.s3.amazonaws.com/tasq-3/tasq.html",
callback: window.location.href,
});
Omlet.exit(rdl);
}
else
{
console.log("Omlet not properly set up.");
}
});
}