// // tada.js // // LICENSE: {{{ // Copyright (c) 2009 snaka // // Distributable under the terms of an MIT-style license. // http://www.opensource.jp/licenses/mit-license.html // }}} // PLUGIN INFO: {{{ var PLUGIN_INFO = {NAME} Show ToDo items in commandline buffer. Also add item to your Ta-da list. コマンドラインバッファからTa-Da list のToDo一覧を参照したり、からToDo項目を追加したりします。 2.0pre 2.2pre http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/tada.js snaka MIT style license 0.10.0 _libly.js ; // }}} 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('onSuccess', 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 = ; getTodoItems(listId).forEach(function(item) { list.li +=
  • {item.title}
  • ; }); 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('onSuccess', 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('onSuccess', function(data) { liberator.echo("Posted[" + listName + "]:" + content); liberator.plugins.posted = data; }); req.addEventListener('onFailure', 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('onSuccess', function(data) { liberator.echo("Send Ta-Da list '" + listName + "' to your email address."); }); req.addEventListener('onFailure', 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('onSuccess', function(data) { liberator.echo("Done: " + itemId); }); req.addEventListener('onFailure', 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: ='n268' href='#n268'>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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476