Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------
- // SpilitWindowの定義
- //--------------------
- // SplitWindowはmasterViewとdetailViewを指定するファクトリメソッドで生成しなければなりません。
- // そのため、事前にそれぞれに相当するWindow/Viewを生成しておく必要があります。
- // 無名関数で返す事もできなくもありませんが、SplitWindow内Viewへのアクセスのためにscopeを開いておいたほうが何かと便利ではないかと。
- // ここでは階層ナビゲーションできるNavigationGroupとして左右のウィンドウを定義し、それをSplitWindowに引き渡しています。ナビゲーションが不要なら通常のWindowなどで問題ありません。
- // あと気にする必要があるのは、splitView.visibleイベントでdetailView側のleftNavButtonの制御を行うことぐらいでしょうか。
- // 商品検索を構成するウィンドウを定義
- // (構成する各種オブジェクトをMainWindowsオブジェクトにネストさせることで名前空間の汚染範囲を抑えようとしています)
- // 左側:マスタウィンドウ
- // 右側:詳細ウィンドウ
- MainWindows = {};
- MainWindows.masterWindow = Ti.UI.createWindow({
- title:'サンドウィッチ',
- backgroundColor:'#fff',
- barColor: '#060',
- width: 320
- });
- MainWindows.detailWindow = Ti.UI.createWindow({
- title:'代金・カロリー計算 (参考値)',
- backgroundColor:'#fff',
- barColor: '#060'
- });
- // それぞれ階層ナビゲーションをするためにNavigationGroupを構成する
- MainWindows.masterNav = Ti.UI.iPhone.createNavigationGroup({
- window:MainWindows.masterWindow
- });
- MainWindows.detailNav = Ti.UI.iPhone.createNavigationGroup({
- window:MainWindows.detailWindow
- });
- // スプリットビューを割当、イベントリスナの割当
- MainWindows.splitView = Titanium.UI.iPad.createSplitWindow({
- masterView:MainWindows.masterNav,
- detailView:MainWindows.detailNav
- });
- MainWindows.splitView.addEventListener('visible', function(e){
- // 左側のナビゲーションボタンを表示する(タテ方向)
- if (e.view == 'detail'){
- e.button.title = "メニュー";
- MainWindows.detailWindow.leftNavButton = e.button;
- }
- // 左側のナビゲーションボタンを隠す(ヨコ方向)
- else if (e.view == 'master'){
- MainWindows.detailWindow.leftNavButton = null;
- }
- });
- // 詳細ウィンドウ
- Ti.include('./db.js');
- MainWindows.detailTableView = {};
- MainWindows.detailTableView.tableView = Ti.UI.createTableView({style: Ti.UI.iPhone.TableViewStyle.GROUPED});
- MainWindows.detailTableView.rows = {};
- MainWindows.detailTableView.rows.sandwich = Ti.UI.createTableViewRow({header: 'サンドイッチ', title: ''});
- MainWindows.detailTableView.rows.sandwichsize = Ti.UI.createTableViewRow({title: ''});
- MainWindows.detailTableView.rows.bread = Ti.UI.createTableViewRow({title: ''});
- MainWindows.detailTableView.rows.topping = Ti.UI.createTableViewRow({header: 'トッピング', title: ''});
- MainWindows.detailTableView.rows.dressing = Ti.UI.createTableViewRow({header: 'ドレッシング', title: ''});
- MainWindows.detailTableView.rows.price = Ti.UI.createTableViewRow({header: '代金', title: ''});
- MainWindows.detailTableView.rows.kcal = Ti.UI.createTableViewRow({header: 'カロリー', title: ''});
- MainWindows.detailTableView.clear= function(){
- MainWindows.detailTableView.rows.sandwich.title = '';
- MainWindows.detailTableView.rows.sandwichsize.title = '';
- MainWindows.detailTableView.rows.bread.title = '';
- MainWindows.detailTableView.rows.topping.title = '';
- MainWindows.detailTableView.rows.dressing.title = '';
- MainWindows.detailTableView.rows.price.title = '';
- MainWindows.detailTableView.rows.kcal.title = '';
- };
- MainWindows.detailTableView.init = function(){
- MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.sandwich);
- MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.sandwichsize);
- MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.bread);
- MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.topping);
- MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.dressing);
- MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.price);
- MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.kcal);
- MainWindows.detailWindow.add(MainWindows.detailTableView.tableView);
- };
- MainWindows.detailTableView.init();
- //
- // マスタナビゲーション用TableView
- //
- MainWindows.goNextTreeView = function(title, nextTreeViewData){
- var nextWin = Ti.UI.createWindow({title: title, barColor: '#060'});
- var nextTreeView = Ti.UI.createTableView({data: nextTreeViewData.data});
- if(nextTreeViewData.click){
- nextTreeView.addEventListener('click', nextTreeViewData.click);
- }
- nextWin.add(nextTreeView);
- MainWindows.masterNav.add(nextWin);
- MainWindows.masterNav.open(nextWin);
- };
- MainWindows.tvSandwich = {
- data: db.table.sandwich
- };
- MainWindows.tvSandwichSize = {
- data: db.table.size
- };
- MainWindows.tvBread = {
- data: db.table.bread
- };
- MainWindows.tvTopping = {
- data: (function(){
- var rows = [];
- var tpData = db.table.topping;
- for(var i = 0; i < tpData.length; i++){
- (function(i){
- var row = Ti.UI.createTableViewRow();
- var rowView = Ti.UI.createView();
- var rowTitle = tpData[i].title;
- var rowValue = Ti.UI.createTextField({
- borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
- value: '0',
- enabled: false,
- editable:false,
- left:250,
- width:60
- });
- rowView.add(Ti.UI.createLabel({
- text: rowTitle,
- left:8,
- width:240
- }));
- rowView.add(rowValue);
- rowView.addEventListener('click', function(e){
- // PopOverの作成
- var popTableView = Ti.UI.createTableView({
- data : [
- {title: '0'},
- {title: '1'},
- {title: '2'}
- ]
- });
- var popOver = Ti.UI.iPad.createPopover({
- title: rowTitle,
- barColor: '#060',
- width: 240,
- height:140
- });
- popTableView.addEventListener('click', function(e){
- rowValue.value = e.index;
- tpData[i].qty = e.index;
- popOver.hide();
- popTableView = null;
- popOver = null;
- });
- popOver.add(popTableView);
- popOver.show({view: rowValue, animate: true});
- });
- row.add(rowView);
- rows.push(row);
- })(i);
- }
- rows.push((function(){
- var row = Ti.UI.createTableViewRow();
- row.add(Ti.UI.createButton({title:'決定'}));
- row.addEventListener('click', function(e){
- nowprice.topping = 0;
- nowkcal.topping = 0;
- var title = '';
- for(var j = 0; j < tpData.length; j++){
- nowprice.topping += tpData[j].yen * tpData[j].qty;
- nowkcal.topping += tpData[j].kcal * tpData[j].qty;
- if(tpData[j].qty > 0){
- if(title.length > 0){
- title += ', ';
- }
- title += tpData[j].title + ' x ' + tpData[j].qty;
- }
- }
- if(title.length == 0){
- title = 'なし';
- }
- MainWindows.detailTableView.rows.topping.title = title;
- MainWindows.goNextTreeView('ドレッシング', MainWindows.tvDressing);
- });
- return row;
- })());
- return rows;
- })()
- };
- MainWindows.tvDressing = {
- data: db.table.dressing
- };
- //
- // ナビゲーション
- //
- MainWindows.tvSandwich.click = function(e){
- MainWindows.detailTableView.rows.sandwich.title = e.row.title;
- nowprice.sandwich = 0;
- nowprice.sandwich1 = e.rowData.yen;
- nowprice.sandwich2 = e.rowData.yen2;
- nowkcal.sandwich = e.rowData.kcal;
- MainWindows.goNextTreeView('パンの種類', MainWindows.tvBread);
- };
- MainWindows.tvBread.click = function(e){
- MainWindows.detailTableView.rows.bread.title = e.row.title;
- nowkcal.bread = e.rowData.kcal;
- MainWindows.goNextTreeView('パンのサイズ', MainWindows.tvSandwichSize);
- };
- MainWindows.tvSandwichSize.click = function(e){
- nowprice.sandwich = nowprice.sandwich1;
- if(e.index == 1){
- nowprice.sandwich = nowprice.sandwich2;
- nowkcal.sandwich *= 2;
- nowkcal.bread *= 2;
- }
- MainWindows.detailTableView.rows.sandwichsize.title = e.row.title;
- MainWindows.goNextTreeView('トッピング', MainWindows.tvTopping);
- };
- MainWindows.tvDressing.click = function(e){
- MainWindows.detailTableView.rows.dressing.title = e.row.title;
- nowkcal.dressing = e.rowData.kcal;
- var price = nowprice.sandwich + nowprice.topping;
- var kcal = nowkcal.sandwich + nowkcal.bread + nowkcal.topping + nowkcal.dressing;
- MainWindows.detailTableView.rows.price.title = price + '円';
- MainWindows.detailTableView.rows.kcal.title = kcal + 'kcal';
- };
- MainWindows.masterWindow.add((function(nextTreeViewData){
- var nextTreeView = Ti.UI.createTableView({data: nextTreeViewData.data});
- if(nextTreeViewData.click){
- nextTreeView.addEventListener('click', nextTreeViewData.click);
- }
- return nextTreeView;
- })(MainWindows.tvSandwich));
- // クリアボタン
- MainWindows.detailWindow.rightNavButton = (function(){
- var button = Titanium.UI.createButton({
- title:'クリア'
- });
- button.addEventListener('click', function(){
- MainWindows.detailTableView.clear();
- });
- return button;
- })();
- MainWindows.open = function(){
- Ti.UI.currentWindow.add(MainWindows.splitView);
- MainWindows.splitView.open();
- };
- MainWindows.open();
Add Comment
Please, Sign In to add comment