-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotodo.js
More file actions
226 lines (203 loc) · 6.5 KB
/
Copy pathprotodo.js
File metadata and controls
226 lines (203 loc) · 6.5 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
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
/**
* ProtoDo - A Cloud-based task manager using ProotoAPI
*/
// Imports
$script( 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', 'jquery' );
$script( 'lib/jquery.validate.js', 'validate' );
$script( 'lib/jquery.infieldlabel.min.js', 'infieldlabel' );
$script( 'lib/protoapi.js', 'protoapi' );
// App module
$script.ready( [ 'jquery', 'validate', 'infieldlabel', 'protoapi' ], function($){
return function(){
/**
* Task is a warper around the document we save to ProtoAPI.
* We can easily handle conversions here but since we can store anything
* into ProtoAPI backend we don't require much conversions
*/
var Task = function( data, notes, done ){
if( typeof data === 'object' ){
this.document = data;
} else {
var $done = done ? done : false;
this.document = {
title: data,
done: $done,
notes: notes,
};
}
}
Task.prototype = {
setDone: function( value ){
this.document.done = value;
},
getDone: function(){
return this.document.done;
},
setTitle: function( value ){
this.document.title = value;
},
getTitle: function(){
return this.document.title;
},
setNotes: function( value ){
this.document.notes = value;
},
getNotes: function(){
return this.document.notes;
},
setDate: function( value ){
// Right now protoapi.js doesn't do this conversion for us
if( typeof value === 'object' )
value = value.toISOString();
this.document.date = value;
},
getDate: function(){
if( typeof this.document.date !== 'undefined')
return new Date(this.document.date);
return undefined;
},
getDocument: function(){
return this.document;
}
};
/**
* TasksView is our 'view' in this little app.
* It handles all DOM interaction using jQuery for simplicity
*/
var TasksView = function( ctrl ){
this.ctrl = ctrl;
this.__buildInitialInterface();
}
TasksView.prototype = {
/** Interanl mothod for initial setup of event handlers */
__buildInitialInterface: function(){
$('#newtask').keypress(this.onKeyPressAtTitleInput.bind(this));
},
/** Event handler for new task input */
onKeyPressAtTitleInput: function( event ){
if( event.which == 13 ) {
event.preventDefault();
var value = $( '#newtask' ).val();
this.ctrl.onAddTask( value );
}
},
/** Handles click on a task. Currently does nothing. */
onTaskClick: function( event ){
this.ctrl.onTaskClick()
},
/** Clear task input from any text */
clearInput: function(){
$('#newtask').val('');
},
/** Add a new task on top of other */
addNewTaskOnTop: function( title, date, remove_cb, taskclick_cb, taskdone_cb ){
var done_button = $( '<span class="done_button"></span>' ).click(function(){
$( this ).parent().parent().fadeToggle( 'fast' );
taskdone_cb();
return false;
});
var delete_button = $( '<span class="delete_button"></span>' ).click(function(){
$( this ).parent().parent().fadeToggle( 'fast' );
remove_cb();
return false;
});
var container = $('<div class="task_inner"></div>')
.append( done_button )
.append( '<h3><div>' + title + '</div></h3>' )
.append( delete_button )
.append( '<h4><div>' + date + '</div></h4>' );
var item = $( '<li class="task"></li>' )
.click( function(){ taskclick_cb(); return false; } )
.append( container )
.prependTo( '#tlist' );
}
}
/** TasksManager acts as our controller/domain */
var TasksManager = function( appid, appkey, userid ){
// Register a handy 'bind' function to Function's prototype which warps 'this' nicely
this.__registerBind();
this.appid = appid;
this.appkey = appkey;
this.userid = userid;
this._tasks = [];
this.view = new TasksView( this );
// Configure protoapi.js
protoapi( {
appid: '4f2ebbc700ac0fd23a00005e',
appkey: 'ed756d6c2b62eecc9dcfcbeaf517edfb',
apiuri: 'http://localhost/api/1/objects/' // It's great to have ProtoAPI at localhost ;)
} );
this.fetchFromCloud();
}
TasksManager.prototype = {
/** Fetch data from ProtoAPI backend */
fetchFromCloud: function(){
protoapi( 'todos' ).get( { done: false }, this.onFetchedData.bind(this) );
},
/** Event handler called once the data is ready to be used */
onFetchedData: function( response ){
for( key in response.data ){
this.onAddTask( response.data[ key ], true );
}
},
/**
* Adds a new tasks to the list.
*
* This fires a post or put to ProtoAPI and a new task view is added.
* Date is set to now before saving. We use protoapi.js's save method so
* that we can forget about handling creation or updates of data.
*
* @param data: task document (what is save to ProtoAPI).
* @param avoid_save: if true, we treat this task as coming from ProtoAPI
* so we don't need to re-save it again.
*/
onAddTask: function( data, avoid_save ){
var task = new Task( data );
this._tasks.unshift( task );
var date = task.getDate();
if( typeof date !== 'object' )
date = 'Just now';
else
date = date.toLocaleDateString();
this.view.clearInput();
this.view.addNewTaskOnTop(
task.getTitle(),
date,
this.onTaskRemove.bind(this, task), // Callback when user removes the task from list
this.onTaskClick.bind(this, task),
this.onTaskDone.bind(this, task) // Callback when user presses done button
);
if( !avoid_save ){
task.setDate( new Date() );
protoapi( 'todos' ).save( task.getDocument() );
}
},
/** Called by our View when user presses remove button on a task */
onTaskRemove: function( task ){
var index = this._tasks.indexOf( task );
this._tasks.splice( index, 1 );
protoapi( 'todos' ).delete( { _id: task.getDocument()._id } );
},
/** Called by our View when user presses the 'done' button */
onTaskDone: function( task ){
task.setDone( true );
protoapi( 'todos' ).save( task.document );
},
onTaskClick: function( task ){
},
__registerBind: function(){
if (!Function.prototype.bind) { // check if native implementation available
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments),
object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
}
},
};
var manager_instance = new TasksManager();
}}(jQuery)
);