Changeset 30068

Show
Ignore:
Timestamp:
02/15/09 08:47:40 (4 years ago)
Author:
snaka
Message:

E4Xize

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/vimperator-plugins/trunk/tada.js

    r30066 r30068  
    8484 
    8585liberator.plugins.tada = (function(){ 
    86  
    87 // PRIVATE {{{ 
    88  
    89   function g(str) { 
    90     return liberator.globalVariables[str]; 
    91   } 
    92  
    93   function getURI() { 
    94     if (userId = g('tada_userId')) 
    95       return "http://" + userId + ".tadalist.com/lists/"; 
    96  
    97     throw "Please specify your user id to global variable 'tada_userId'."; 
    98   } 
    99  
    100   function parseListId(source) { 
    101     let m; 
    102     if (m = source.match(/\/lists\/([0-9]+)/))  
    103       return m[1]; 
    104  
    105     return source; 
    106   } 
    107  
    108   function getListId(name) { 
    109     var list = getLists(); 
    110     for(var i in list) { 
    111       if (list[i][1] == name) return list[i][0]; 
    112     } 
    113  
    114     return null; 
    115   } 
    116  
    117   // Get default list id and name by Array 
    118   // @return [defaultListId, defaultListName] 
    119   //         1.Use global variable g:tadaDefaultListName if specified. 
    120   //         2.Use first list if global variable not specified or specified list name is not 
    121   //           exist in your lists. 
    122   function getDefaultListId() { 
    123     var defaultId; 
    124     var defaultName; 
    125  
    126     if (defaultName = g('tadaDefaultListName'))  
    127       if (defaultId = getListId(defaultName)) 
    128         return [defaultId, defaultName]; 
    129  
    130     var lists = getLists(); // [[id, name], ...] 
    131     return [lists[0][0], lists[0][1]]; 
    132   } 
    133  
    134   function getLists() { 
    135     var lists = []; 
    136     var req = new libly.Request(getURI(), null, {asynchronous: false}); 
    137  
    138     req.addEventListener('onSuccess', function(data) { 
    139       liberator.log("success"); 
    140       data.getHTMLDocument(); 
    141       var xpath = "//div[@id='Container']/div[2]/div/div/ul/li/a" 
    142       libly.$U.getNodesFromXPath(xpath, data.doc).forEach(function(item){ 
    143         lists.push([parseListId(item.href), item.innerHTML]); 
    144         //liberator.log(item.innerHTML); 
    145       }); 
    146     }); 
    147     req.get(); 
    148  
    149     if (lists.length == 0) 
    150       throw "Cannot get your list. Please chehek " + getURI() + " is accessible."; 
    151     return lists; 
    152   } 
    153  
    154   function showTodoItems(listId) { 
    155     var req = new libly.Request(getURI() + listId.toString()); 
    156  
    157     req.addEventListener('onSuccess', function(data) { 
    158       liberator.log("success"); 
    159       data.getHTMLDocument(); 
    160  
    161       var html = []; 
    162       html.push("<ul>"); 
    163       libly.$U.getNodesFromXPath("//ul[@id='incomplete_items']/li/form", data.doc).forEach(function(item){ 
    164         html.push([ 
    165           "<li>", 
    166           item.textContent.replace(/^\s*|\n|\r|\s*$/g, ''), 
    167           "</li>" 
    168         ].join('')); 
    169       }); 
    170       html.push("</ul>"); 
    171  
    172       liberator.echo(html.join(''), commandline.FORCE_MULTILINE); 
    173       liberator.log(html.join('')); 
    174     }); 
    175     req.get(); 
    176   } 
    177  
    178   function  addTodoItem([listId, listName], content) { 
    179     var endpoint = getURI() + listId + "/items" 
    180     liberator.log("endpoint:" + endpoint); 
    181     var req = new libly.Request( 
    182       endpoint, 
    183       null, 
    184       { 
    185         asyncronus: true, 
    186         postBody: "item[content]=" + encodeURIComponent(content) 
    187       } 
    188     ); 
    189  
    190     req.addEventListener('onSuccess', function(data) { 
    191       liberator.echo("Posted[" + listName + "]:" + content); 
    192       liberator.plugins.posted = data; 
    193     }); 
    194  
    195     req.addEventListener('onFailure', function(data) { 
    196       liberator.echoerr("POST FAILURE: " + content); 
    197     }); 
    198  
    199     req.post(); 
    200   } 
    201  
    202 // }}} 
    20386// COMMAND {{{ 
    204  
    20587  commands.addUserCommand( 
    20688    ["tada"], 
     
    236118    true  // for DEVELOP 
    237119  ); 
    238  
    239120// }}} 
    240121// PUBLIC {{{ 
    241  
    242   return {  
     122  var PUBLICS = {  
    243123    // for DEBUG {{{ 
    244124    // getListId: getListId, 
     
    251131  }; 
    252132// }}} 
    253  
     133// PRIVATE {{{ 
     134  function g(str) { 
     135    return liberator.globalVariables[str]; 
     136  } 
     137 
     138  function getURI() { 
     139    if (userId = g('tada_userId')) 
     140      return "http://" + userId + ".tadalist.com/lists/"; 
     141 
     142    throw "Please specify your user id to global variable 'tada_userId'."; 
     143  } 
     144 
     145  function parseListId(source) { 
     146    let m; 
     147    if (m = source.match(/\/lists\/([0-9]+)/))  
     148      return m[1]; 
     149 
     150    return source; 
     151  } 
     152 
     153  function getListId(name) { 
     154    var list = getLists(); 
     155    for(var i in list) { 
     156      if (list[i][1] == name) return list[i][0]; 
     157    } 
     158 
     159    return null; 
     160  } 
     161 
     162  // Get default list id and name by Array 
     163  // @return [defaultListId, defaultListName] 
     164  //         1.Use global variable g:tadaDefaultListName if specified. 
     165  //         2.Use first list if global variable not specified or specified list name is not 
     166  //           exist in your lists. 
     167  function getDefaultListId() { 
     168    var defaultId; 
     169    var defaultName; 
     170 
     171    if (defaultName = g('tadaDefaultListName'))  
     172      if (defaultId = getListId(defaultName)) 
     173        return [defaultId, defaultName]; 
     174 
     175    var lists = getLists(); // [[id, name], ...] 
     176    return [lists[0][0], lists[0][1]]; 
     177  } 
     178 
     179  function getLists() { 
     180    var lists = []; 
     181    var req = new libly.Request(getURI(), null, {asynchronous: false}); 
     182 
     183    req.addEventListener('onSuccess', function(data) { 
     184      liberator.log("success"); 
     185      data.getHTMLDocument(); 
     186      var xpath = "//div[@id='Container']/div[2]/div/div/ul/li/a" 
     187      libly.$U.getNodesFromXPath(xpath, data.doc).forEach(function(item){ 
     188        lists.push([parseListId(item.href), item.innerHTML]); 
     189      }); 
     190    }); 
     191    req.get(); 
     192 
     193    if (lists.length == 0) 
     194      throw "Cannot get your list. Please chehek " + getURI() + " is accessible."; 
     195    return lists; 
     196  } 
     197 
     198  function showTodoItems(listId) { 
     199    var req = new libly.Request(getURI() + listId.toString()); 
     200 
     201    req.addEventListener('onSuccess', function(data) { 
     202      liberator.log("success"); 
     203      data.getHTMLDocument(); 
     204 
     205      var list = <ul></ul>; 
     206      libly.$U.getNodesFromXPath("//ul[@id='incomplete_items']/li/form", data.doc).forEach(function(item){ 
     207        list.li += <li>{item.textContent.replace(/^\s*|\n|\r|\s*$/g, '')}</li>; 
     208      }); 
     209 
     210      liberator.echo(list.toXMLString(), commandline.FORCE_MULTILINE); 
     211      liberator.log(list.toXMLString()); 
     212    }); 
     213    req.get(); 
     214  } 
     215 
     216  function  addTodoItem([listId, listName], content) { 
     217    var endpoint = getURI() + listId + "/items" 
     218    liberator.log("endpoint:" + endpoint); 
     219    var req = new libly.Request( 
     220      endpoint, 
     221      null, 
     222      { 
     223        asyncronus: true, 
     224        postBody: "item[content]=" + encodeURIComponent(content) 
     225      } 
     226    ); 
     227 
     228    req.addEventListener('onSuccess', function(data) { 
     229      liberator.echo("Posted[" + listName + "]:" + content); 
     230      liberator.plugins.posted = data; 
     231    }); 
     232 
     233    req.addEventListener('onFailure', function(data) { 
     234      liberator.echoerr("POST FAILURE: " + content); 
     235    }); 
     236 
     237    req.post(); 
     238  } 
     239 
     240  return PUBLICS; 
     241// }}} 
    254242})(); 
    255243