「Suica 利用履歴から運賃を算出」ガジェットの UI をタブ仕様に変更

先日作った、Suica(スイカ)のガジェットだが、ユーザーインターフェイスが微妙だ。入出力のエリアを並べているので領域を広い。入力と出力が同時に見えている必要もないと思ったのでタブ化して分けてみた。それにタブ名をナビゲーションメッセージになったので、ちょっとわかりやすくなったかもしれない。
Add to Google

Google ガジェットでタブを使うには

ModulePrefs で と使用を宣言。

<?xml version="1.0" encoding="utf-8"?>
<Module>
	<ModulePrefs …>
	    <Require feature="tabs" />
	</ModulePrefs>

下記のようにタブのインスタンスを作って、addTab でタブのキャプションとそのタブの領域となる div タグの ID を引数に渡す。三番目の引数はタブが選択されたときのイベント関数を指定できる。chg の中で変換を走らせている。

var tabs = new _IG_Tabs(__MODULE_ID__);
tabs.addTab("履歴を入力", "tabIn");
tabs.addTab("結果を表示", "tabOut", chg);

タブ化すると、DOMツリーの構造が変わるため document.getElementById ではなく _gel(id) で取得する必要がある。

Content のソース

<script type="text/javascript">
<!--

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
String.prototype.getLines = function() {
  var lines = this.split("\n");
  for (var i=lines.length-1; i>=0; i--) {
    if (lines[i].trim().length == 0) {
      lines.splice(i, 1);
    }
  }
  return lines;
};

function line2map(line) {
  var c = line.split(' ');
  if (c[1] == "物販") c[2] = c[1];
  return {
    date: c[0], dept:c[2], arrv:c[4], acc: c[5].substr(1).replace(',',"")
  };
}

function outtable(input) {
  try {
    var lines = input.getLines();
    if (lines[0].indexOf('月/日') > -1) lines.shift();
    var i = lines.length - 1;
    var pre_acc = line2map(lines[i]).acc;
    var out = "";
    while (--i >= 0) {
      if (lines[i].replace(' ',"").length == 0) continue;
      var item = line2map(lines[i]);
      if (item.dept.length > 0) {
        out += "<tr><td>" + item.date + "</td><td>" + item.dept + "</td><td>"
        out += item.arrv + "</td><td>" + (pre_acc - item.acc) + "</tr>";
      }
      pre_acc = item.acc;
    }
    return '<table cellspacing="0" style="font-size:10.5pt" width="300">' + out + '</table>';
  } catch(err) {
    return null;
  }
}

function chg(evt) {
  var textIn = _gel("textIn");
  var ret = outtable(textIn.value);
  if (ret != null)
     _gel("textOut").innerHTML = ret;
}

function firstFocus(obj) {
  obj.value='';
  obj.style.color='#000';
  obj.onfocus = null;
}
//-->
</script>
<style type="text/css">
@import url(http://www.google.com/ig/tablib.css);
</style>

<div id="tabIn" style="height:135px;">
<textarea id="textIn" style="color:gray;font-size:10pt;width:100%;height:135px;overflow:scroll"
  onfocus="firstFocus(this)" onchange="chg()"
  >SF(電子マネー)利用履歴をペースト</textarea></td>
</div>
<div id="tabOut" style="height:135px;">
<div id="textOut" style="font-size:10.5pt;height:135px;padding:2px 0;overflow:scroll"></div>
</div>
<script>
var tabs = new _IG_Tabs(__MODULE_ID__);
tabs.addTab("履歴を入力", "tabIn");
tabs.addTab("結果を表示", "tabOut", chg);
</script>