Annotation of todotxt/Text-Todo-REST-API/example/htdocs/lib/todo.js, Revision 1.2
1.1 andrew 1: var TODO = function () {
1.2 ! andrew 2: var base_url = '/~andrew/user-bin/todo-rest.cgi',
1.1 andrew 3: editors = {},
4:
5: makeEntryEditor = function(list, entry) {
6: var url = base_url + '/' + list + '/entry/' + entry.line,
7: id = entry.id;
8:
9: if (!id) {
10: id = 'entry_' + entry.line;
11: }
12:
13: if (editors[id]) {
14: editors[id].dispose();
15: delete editors[id];
16: }
17:
18: editors[id] = new Ajax.InPlaceEditor( id, url + '.json', {
1.2 ! andrew 19: ajaxOptions: {
! 20: method: 'put',
! 21: },
1.1 andrew 22: cols: 80,
23: rows: 1,
24: callback: function(form, value) {
25: entry.oldText = entry.text;
26: entry.text = value;
27: return entry;
28: },
29: onComplete: function (transport, element) {
30: if (transport && transport.responseJSON) {
31: updateEntry( list, transport.responseJSON, element);
32: }
33: }
34: }
35: );
36: },
37:
38: updateEntry = function(list, entry, element) {
39: if (!entry.id) {
40: entry.id = 'entry_' + entry.line;
41: }
42: var liId = 'li_' + entry.id;
43:
44: if (parseInt(entry.line) !== $(liId).value) {
45: for (k in editors) {
46: if (editors.hasOwnProperty(k)) {
47: editors[k].dispose();
48: delete editors[k];
49: }
50: }
51: return getList(list);
52: }
53:
54: $(liId).update(
55: new Element('span', { id: entry.id }). update(entry.text)
56: ).insert({
57: top: new Element('input', {
58: type: 'checkbox' ,
59: id: 'do_' + entry.line,
60: checked: entry.done,
61: disabled: true,
62: })
63: });
64:
65: makeEntryEditor(list, entry);
66: },
67:
68: updateList = function (list, transport) {
69: var i,
70: todo = transport.responseJSON,
71: element = new Element('ol'),
72: entryElement;
73:
74: $("list").update( element );
75:
76: for (i=0; i <= todo.length; i++) {
77: todo[i].id = 'entry_' + todo[i].line;
78:
79: entryElement = new Element('li', {
80: id: 'li_' + todo[i].id,
81: value: todo[i].line
82: });
83:
84: element.insert(entryElement);
85:
86: updateEntry(list, todo[i], entryElement);
87: }
88: },
89:
90: getFiles = function () {
91: $('files').update("Getting Files . . .");
92:
93: new Ajax.Updater('files', base_url, {
94: method: 'get',
95: });
96: },
97:
98: getTags = function (list) {
99: $('tags').update("Getting Tags. . .");
100:
101: var url = base_url + '/' + list + '/tags';
102:
103: new Ajax.Request(url + '.json', {
104: method: 'get',
105: onSuccess: function (transport) {
106: var k,
107: data = transport.responseJSON,
108: element = new Element('ul');
109:
110: $("tags").update(element);
111: for (k in data) {
112: if (data.hasOwnProperty(k)) {
113: element.insert(
114: new Element('li', {
115: id: 'tag_' + k,
116: }).update( k + ": " + data[k] )
117: );
118: getTag(list, k);
119: }
120: }
121: },
122: });
123: },
124:
125: getTag = function( list, tag ) {
126: new Ajax.Request(base_url + '/' + list + '/tags/' + tag + '.json', {
127: method: 'get',
128: onSuccess: function (transport) {
129: if (transport && transport.responseJSON) {
130: var i,
131: myTags = transport.responseJSON,
132: element = new Element('ul');
133:
134: if (myTags.length) {
135: $('tag_' + tag).insert(element);
136:
137: for (i=0; i<myTags.length; i++) {
138: element.insert( new Element('li').
139: update( myTags[i] ));
140: }
141: }
142: }
143: }
144: });
145: },
146:
147: getList = function (list) {
148: $('list').update("Getting List . . .");
149:
150: new Ajax.Request(base_url + '/' + list + '.json', {
151: method: 'get',
152: onSuccess: function (transport) { updateList(list, transport) },
153: });
154:
155: };
156:
157: return {
158: getList: getList,
159: getFiles: getFiles,
160: getTags: getTags,
161: };
162: }();
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>