aboutsummaryrefslogtreecommitdiffstats
path: root/nicontroller.js
AgeCommit message (Collapse)Author
2008-12-16 * fixed regexes.drry
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@26897 d0d07461-0603-4401-acd4-de1884942a52
2008-11-27Follow CVS Headanekos
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@25162 d0d07461-0603-4401-acd4-de1884942a52
2008-11-23ドキュメント追加anekos
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@24678 d0d07461-0603-4401-acd4-de1884942a52
2008-11-23nicoseek -10 の様な指定を可能にした。anekos
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@24677 d0d07461-0603-4401-acd4-de1884942a52
2008-11-18冗長なコードを変更anekos
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@24204 d0d07461-0603-4401-acd4-de1884942a52
2008-11-18停止中かつ終端のときに再生すると、初めに戻ってから ↵anekos
生するようにした。 git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@24182 d0d07461-0603-4401-acd4-de1884942a52
2008-11-01 * メタデータブロックを閉じました。drry
* ほか。 git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@22555 d0d07461-0603-4401-acd4-de1884942a52
2008-11-01patch to avoid flvplayer's bug, when repeat mode on.janus_wel
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@22529 d0d07461-0603-4401-acd4-de1884942a52
2008-10-21liberator -> liberator.modulesjanus_wel
- buffer - commandline - commands - hints - ..etc コマンド追加時に使う addUserCommand の第 3 引数に指定する関数の第 1 引数が String から Object に変更したのに対応 - ldrize_cooperation_fetch_flv.js - lookupDictionary.js - matanico.js - nicontroller.js - nnp_cooperation.js - reading.js - youtubeamp.js migemo_hint.js で一時的にグローバルオブジェクト plugins を定義 応急処置なのであとで削る必要あり git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@21797 d0d07461-0603-4401-acd4-de1884942a52
2008-10-07add "bang" property to extra object when addUserCommand (matanico.js, ↵janus_wel
nicontroller.js) change https on post message (matanico.js) correct URL for pathtraq in function canonicalizeURL (reading.js) git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@20930 d0d07461-0603-4401-acd4-de1884942a52
2008-10-01refactoring.janus_wel
use Error object when throw exception. extract constant variables from code and define in NicoPlayerController.constants. git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@20436 d0d07461-0603-4401-acd4-de1884942a52
2008-10-01add :nicodescriptionjanus_wel
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@20390 d0d07461-0603-4401-acd4-de1884942a52
2008-09-23 * fixed regexes.drry
* et cetera. git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@19784 d0d07461-0603-4401-acd4-de1884942a52
2008-09-22import matanico.js, nicontroller.js, nnp_cooperation.jsjanus_wel
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@19750 d0d07461-0603-4401-acd4-de1884942a52
' href='#n246'>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<snaka.gml@gmail.com>
//
//   Distributable under the terms of an MIT-style license.
//   http://www.opensource.jp/licenses/mit-license.html
// }}}

// PLUGIN INFO: {{{
var PLUGIN_INFO =
<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>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/tada.js</updateURL>
  <author mail="snaka.gml@gmail.com" 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('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 = <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('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: