-
Notifications
You must be signed in to change notification settings - Fork 0
/
base2.js
376 lines (311 loc) · 9.19 KB
/
base2.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
var TODO_ORDERS = 'todo-orders';
$(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
localStorage.clear();
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/', []);
orderList = localStorage.getItem(TODO_ORDERS);
//var orderListText = JSON.stringify(orderList);
console.log(orderList);
console.log(myDocId);
documentApi.update(myDocId,Update, {"orderList" : orderList} , ReceiveUpdate, DidNotReceiveUpdate);
});
// Remove todo
$itemList.delegate('a', 'click', function(e)
{
var $this = $(this);
e.preventDefault();
$.publish('/remove/', [$this]);
var orderListText = JSON.stringify(orderList);
documentApi.update(myDocId,Update, {"orderList" : orderListText} , ReceiveUpdate, DidNotReceiveUpdate);
});
// Sort todo
$itemList.sortable(
{
revert: true,
stop: function()
{
$.publish('/regenerate-list/', []);
var orderListText = JSON.stringify(orderList);
documentApi.update(myDocId,Update, {"orderList" : orderListText} , ReceiveUpdate, DidNotReceiveUpdate);
},
});
// 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/', []);
var orderListText = JSON.stringify(orderList);
documentApi.update(myDocId,Update, {"orderList" : orderListText} , ReceiveUpdate, DidNotReceiveUpdate);
});
// 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()
);
console.log("Trying to add: " + $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');
var value = localStorage.getItem(id);
order.push(value);
});
console.log("Pre-Renderered Orders: " + order);
// Convert the array into string and save to localStorage
var joinedOrders = order.join(',');
console.log("Post-Renderered Orders: " + joinedOrders );
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 sendJson = {};
sendJson["docId"] = myDocId;
console.log(sendJson);
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,
json: sendJson,
});
console.log(rdl);
Omlet.exit(rdl);
}
else
{
console.log("Omlet not properly set up.");
}
});
}
Omlet.ready(function() {
var omletPackage = Omlet.getPasteboard();
if (omletPackage)
{
myDocId=omletPackage.json["docId"];
initDocument();
// LoadGame();
}
else
{
initDocument();
//setMessage("Error loading game");
}
} );
function LoadData(){
// Load todo list
console.log( orderList );
//orderList = localStorage.getItem('todo-orders');
//orderList = orderList ? orderList.split(',') : [];
/**/
for( j = 0, k = orderList.length; j < k; j++)
{
$('#show-items').append
(
"<li id='" + orderList[j] + "'>"
+ "<span class='editable'>"
+ orderList[j]
+ "</span> <a href='#'>X</a></li>"
);
}
/**/
}
//Shared Document API
function Initialize(old, params) {
return params;
}
function Update(old, params) {
old.orderList = params["orderList"];
return old;
console.log("Updating!");
}
function InitialDocument() {
var initValues = {
'orderList' : "",
};
return initValues;
}
function DocumentCreated(doc) {
console.log("Document has been created.");
}
function ReceiveUpdate(doc) {
myDoc = doc;
for( key in myDoc)
{
console.log("Key: " + key);
}
console.log("orderList: " + myDoc["orderList"]);
orderList = myDoc["orderList"];
orderList = orderList.split(',');
console.log("Delineated Order List: " + orderList);
LoadData();
console.log("I received an update!");
}
function DidNotReceiveUpdate(doc) {
console.log("I did not receive an update");
}
//////////////////////////////
///// Framework Code ///////
//////////////////////////////
var documentApi;
var myDoc;
var myDocId=null;
function watchDocument(docref, OnUpdate) {
documentApi.watch(docref, function(updatedDocRef) {
if (docref != myDocId) {
console.log("Wrong document!!");
} else {
documentApi.get(docref, OnUpdate);
}
}, function(result) {
var timestamp = result.Expires;
var expires = timestamp - new Date().getTime();
var timeout = 0.8 * expires;
setTimeout(function() {
watchDocument(docref, OnUpdate);
}, timeout);
}, Error);
}
function initDocument() {
if (Omlet.isInstalled()) {
documentApi = Omlet.document;
_loadDocument();
}
}
function hasDocument() {
if(myDocId!==null)
{
return true;
}
else
{
return false;
}
}
function getDocumentReference() {
return myDocId;
}
function _loadDocument() {
if (hasDocument()) {
myDocId = getDocumentReference();
documentApi.get(myDocId, ReceiveUpdate);
watchDocument(myDocId, ReceiveUpdate);
} else {
documentApi.create(function(d) {
myDocId = d.Document;
documentApi.update(myDocId, Initialize, InitialDocument(),
function() {
documentApi.get(myDocId, DocumentCreated);
}, function(e) {
alert("error: " + JSON.stringify(e));
});
watchDocument(myDocId, ReceiveUpdate);
}, function(e) {
alert("error: " + JSON.stringify(e));
});
}
}