-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathtada.js
387 lines (334 loc) · 12.2 KB
/
tada.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
377
378
379
380
381
382
383
384
385
386
387
//
// tada.js
//
// LICENSE: {{{
// Copyright (c) 2009 snaka<[email protected]>
//
// Distributable under the terms of an MIT-style license.
// http://www.opensource.jp/licenses/mit-license.html
// }}}
// PLUGIN INFO: {{{
var PLUGIN_INFO = xml`
<VimperatorPlugin>
<name>{NAME}</name>
<description>Show ToDo items in commandline buffer. Also add item to your Ta-da list.</description>
<description lang="ja">コマンドラインバッファからTa-Da list のToDo一覧を参照したり、からToDo項目を追加したりします。</description>
<minVersion>2.0pre</minVersion>
<maxVersion>2.2pre</maxVersion>
<updateURL>https://github.com/vimpr/vimperator-plugins/raw/master/tada.js</updateURL>
<author mail="[email protected]" homepage="http://vimperator.g.hatena.ne.jp/snaka72/">snaka</author>
<license>MIT style license</license>
<version>0.10.0</version>
<require type="plugin">_libly.js</require>
<detail><![CDATA[
== Subject ==
Show ToDo items in commandline buffer.
Also add item to your Ta-da list.
== Global variables ==
Following variable is *requied*.
tada_userId:
Please specify your user ID.
Following variable is optional.
tada_defaultListName:
Default list name.
Use this list name if command parameter is not supplied.
However this variable not specified, use first one of lists as default.
== Command ==
Usage:
:tada [list name] [subject]:
Show default list unless 'list name' supplied.
Show specified list if 'list name' is supplied.
Add todo item to default list if 'list name' is not supplied and 'subject' is supplied.
Add todo item to specified list if 'list name' and 'subject' are supplied.
:tada -open [list name]:
Open default list unless 'list name' supplied.
Open specified list if 'list name' is supplied.
:tadamail:
Send list to registered mail address.
:tadaclearcache:
Clear cache data.
== ToDo ==
- 'Done' functionlity.
- Item deletion.
- Performance tunning.
]]></detail>
<detail lang="ja"><![CDATA[
== 概要 ==
コマンドラインバッファからTa-Da list のToDo一覧を参照したり、ToDo項目を追加したりします。
== グローバル変数 ==
以下の変数は必須です。
tada_userId:
あなたのユーザIDを設定してください。
以下の変数は任意で設定可能です。
tada_defaultListName:
デフォルトのリスト名
コマンドのパラメタでリスト名を省略した場合に使用するリストの名前を定義してください。
この変数が設定されていない場合、デフォルトのリストとして先頭のリストが使用されます。
== コマンド ==
Usage:
:tada [list name] [subject]:
パラメタが省略された場合、デフォルトのリストの内容が表示されます。
'list name' が指定された場合、該当リストの内容が表示されます。
'list name' が指定されず 'subject' のみ指定されている場合、デフォルトのリストに'subject'の内容を追加します。
'list name' も 'subject' も指定されている場合、該当リストに'subject'の内容を追加します。
:tada -open [list name]:
'list name' が省略された場合、デフォルトのリストのページ開きます。
'list name' が指定された場合、該当リストのページを開きます。
:tadamail:
Ta-da lists に登録されているアドレスにリストを送ります。
:clearcache:
キャッシュされたデータを破棄します
== ToDo ==
- 'Done'機能の実装
- 項目の削除機能
- 表示のパフォーマンス改善
]]></detail>
</VimperatorPlugin>`;
// }}}
liberator.plugins.tada = (function(){
// COMMAND ////////////////////////////////////////////////////////////{{{
commands.addUserCommand(
["tada"],
"Show / Add ToDo items to Ta-Da list. (:tada [LISTNAME] [SUBJECT])",
function(args) {
liberator.echo("Please wait ...");
let action = parseAction(args, args["-open"]);
action();
}, {
completer: tadaListCompleter,
argCount: "*",
options: [
[["-open", "-o"], commands.OPTION_NOARG],
],
literal: true
},
true // for DEVELOP
);
function parseAction(args, isOpen) {
let listId;
let defaultListIdName = getDefaultListIdName();
let defaultListURI = getURI() + defaultListIdName[0];
switch (args.length) {
case 0:
// Open default tada list page
if (isOpen)
return function() liberator.open(defaultListURI, liberator.NEW_TAB);
// list default list items
return function() showTodoItems(defaultListIdName);
case 1:
// list name or tado item for default list.
if (listId = getListId(args[0])) {
if (isOpen)
return function() liberator.open(getURI() + listId, liberator.NEW_TAB);
return function() showTodoItems(listId);
}
if (isOpen)
return function() liberator.open(defaultListURI, liberator.NEW_TAB);
return function() addTodoItem(defaultListIdName, args[0]);
}
if (isOpen)
return function() liberator.open(defaultListURI, liberator.NEW_TAB);
// list name / tado item for selected list or
// tado item for default list
if (listId = getListId(args[0]))
return function() addTodoItem([listId, args[0]], args[1]);
return function() addTodoItem(defaultListIdName, args.join(' '));
}
commands.addUserCommand(
["done"],
"Done to-do item.",
function(args) {
let listId = getListId(args[0]);
let itemId = args[1];
liberator.echo("Done...");
doneItem(listId, itemId);
},{
completer : function(context, args) {
context.title = ["id", "title"];
context.completions = args.length <= 1
? tadaListCompleter(context, args)
: tadaItemCompleter(args[0]);
},
argCount : "*",
literal : 1,
},
true
);
function tadaItemCompleter(listName) {
let result = [];
getTodoItems(getListId(listName)).forEach(function(item) {
result.push([item.itemId, item.title]);
});
return result;
}
commands.addUserCommand(
["tadamail"],
"Mail Ta-Da list. (:tadamail [LISTNAME])",
function(args) {
if (args.length > 0)
sendEmail([getListId(args[0]), args[0]]);
}, {
completer: tadaListCompleter,
argCount: "*"
},
true
);
commands.addUserCommand(
["tadaclearcache"],
"Clear Ta-da lists cache",
function()
cachedLists = []
);
// }}}
// PRIVATE ////////////////////////////////////////////////////////////{{{
function tadaListCompleter(context, args) {
if (args.length > 1)
return;
context.title = ["List", "Items left"];
context.completions = getLists().map(function(item) [item[1], item[2]]);
}
function getURI() {
if (userId = $g('tada_userId'))
return "http://" + userId + ".tadalist.com/lists/";
throw "Please specify your user id to global variable 'tada_userId'.";
}
function parseListId(source) {
let m;
if (m = source.match(/\/lists\/([0-9]+)/))
return m[1];
return source;
}
function getListId(name) {
var list = getLists();
for(var i in list) {
if (list[i][1] == name) return list[i][0];
}
return null;
}
// Get default list id and name by Array
// @return [defaultListId, defaultListName]
// 1.Use global variable g:tadaDefaultListName if specified.
// 2.Use first list if global variable not specified or specified list name is not
// exist in your lists.
function getDefaultListIdName() {
var defaultId;
var defaultName;
if (defaultName = $g('tadaDefaultListName'))
if (defaultId = getListId(defaultName))
return [defaultId, defaultName];
var lists = getLists(); // [[id, name], ...]
return [lists[0][0], lists[0][1]];
}
// Get Your 'MyLists'
// @return [[id, name, left], .... ]
function getLists() {
let result = [];
var req = new libly.Request(getURI(), null, {asynchronous: false});
req.addEventListener('success', function(data) {
liberator.log("success");
data.getHTMLDocument("//div[@id='Container']/div[2]/div/div/ul/li/a").forEach(function(item){
var left = $LX("../span/strong[text()]", item);
result.push([parseListId(item.href), item.innerHTML, left.innerHTML]);
});
});
req.get();
if (result.length == 0)
throw "Cannot get your list. Please chehek " + getURI() + " is accessible.";
return result;
}
function showTodoItems(listId) {
let list = <ul></ul>;
getTodoItems(listId).forEach(function(item) {
list.li += <li>{item.title}</li>;
});
liberator.echo(list, commandline.FORCE_MULTILINE);
}
/*
* getTodoItems
* @return [[itemId, title], [itemId, title] ... ]
*/
function getTodoItems(listId) {
let result = [];
var req = new libly.Request(getURI() + listId.toString(), null, {asynchronous: false});
req.addEventListener('success', function(res) {
liberator.log("success");
res.getHTMLDocument("//ul[@id='incomplete_items']/li/form").forEach(function(item) {
result.push({
itemId : item.id.match(/edit_item_([^\"]+)/)[1],
title : item.textContent.replace(/^\s*|\n|\r|\s*$/g, '')
});
});
});
req.get();
return result;
}
function addTodoItem([listId, listName], content) {
var endpoint = getURI() + listId + "/items"
liberator.log("endpoint:" + endpoint);
var req = new libly.Request(
endpoint,
null,
{
postBody: "item[content]=" + encodeURIComponent(content)
}
);
req.addEventListener('success', function(data) {
liberator.echo("Posted[" + listName + "]:" + content);
liberator.plugins.posted = data;
});
req.addEventListener('failure', function(data) {
liberator.echoerr("POST FAILURE: " + content);
});
req.post();
}
function sendEmail([listId, listName]) {
var endpoint = getURI() + listId + "/email";
liberator.log("endpoint:" + endpoint);
var req = new libly.Request(endpoint, null, {postBody: "dummy=hoge"});
req.addEventListener('success', function(data) {
liberator.echo("Send Ta-Da list '" + listName + "' to your email address.");
});
req.addEventListener('failure', function(data) {
liberator.echoerr("EMAIL SENDING ERROR.");
liberator.log(data.responseText);
});
req.post();
}
function doneItem(listId, itemId) {
let endpoint = getURI() + listId + "/items/" + itemId;
liberator.dump("endpoint: " + endpoint);
var req = new libly.Request(endpoint, null,
{
postBody: toQuery({
"_method" : "put",
"item[completed]" : "1",
})
}
);
req.addEventListener('success', function(data) {
liberator.echo("Done: " + itemId);
});
req.addEventListener('failure', function(data) {
liberator.echoerr("Done item failed.");
liberator.log(data.responseText);
});
req.post();
}
// Utilities
function $s(obj) util.objectToString(obj);
function $g(str) liberator.globalVariables[str];
function $LXs(a,b) libly.$U.getNodesFromXPath(a, b);
function $LX(a,b) libly.$U.getFirstNodeFromXPath(a, b);
function toQuery(source)
[encodeURIComponent(i) + "=" + encodeURIComponent(source[i])
for (i in source)
].join('&');
// }}}
// API ////////////////////////////////////////////////////////////////{{{
return {
getLists: getLists,
getTodoItems: getTodoItems,
};
// }}}
})();
// vim: sw=2 ts=2 et si fdm=marker: