Guest User

Untitled

a guest
Jun 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | None | 0 0
  1. //--------------------
  2. // SpilitWindowの定義
  3. //--------------------
  4. // SplitWindowはmasterViewとdetailViewを指定するファクトリメソッドで生成しなければなりません。
  5. // そのため、事前にそれぞれに相当するWindow/Viewを生成しておく必要があります。
  6. // 無名関数で返す事もできなくもありませんが、SplitWindow内Viewへのアクセスのためにscopeを開いておいたほうが何かと便利ではないかと。
  7. // ここでは階層ナビゲーションできるNavigationGroupとして左右のウィンドウを定義し、それをSplitWindowに引き渡しています。ナビゲーションが不要なら通常のWindowなどで問題ありません。
  8. // あと気にする必要があるのは、splitView.visibleイベントでdetailView側のleftNavButtonの制御を行うことぐらいでしょうか。
  9.  
  10. // 商品検索を構成するウィンドウを定義
  11. // (構成する各種オブジェクトをMainWindowsオブジェクトにネストさせることで名前空間の汚染範囲を抑えようとしています)
  12. // 左側:マスタウィンドウ
  13. // 右側:詳細ウィンドウ
  14. MainWindows = {};
  15. MainWindows.masterWindow = Ti.UI.createWindow({
  16. title:'サンドウィッチ',
  17. backgroundColor:'#fff',
  18. barColor: '#060',
  19. width: 320
  20. });
  21. MainWindows.detailWindow = Ti.UI.createWindow({
  22. title:'代金・カロリー計算 (参考値)',
  23. backgroundColor:'#fff',
  24. barColor: '#060'
  25. });
  26.  
  27. // それぞれ階層ナビゲーションをするためにNavigationGroupを構成する
  28. MainWindows.masterNav = Ti.UI.iPhone.createNavigationGroup({
  29. window:MainWindows.masterWindow
  30. });
  31. MainWindows.detailNav = Ti.UI.iPhone.createNavigationGroup({
  32. window:MainWindows.detailWindow
  33. });
  34.  
  35. // スプリットビューを割当、イベントリスナの割当
  36. MainWindows.splitView = Titanium.UI.iPad.createSplitWindow({
  37. masterView:MainWindows.masterNav,
  38. detailView:MainWindows.detailNav
  39. });
  40. MainWindows.splitView.addEventListener('visible', function(e){
  41. // 左側のナビゲーションボタンを表示する(タテ方向)
  42. if (e.view == 'detail'){
  43. e.button.title = "メニュー";
  44. MainWindows.detailWindow.leftNavButton = e.button;
  45. }
  46. // 左側のナビゲーションボタンを隠す(ヨコ方向)
  47. else if (e.view == 'master'){
  48. MainWindows.detailWindow.leftNavButton = null;
  49. }
  50. });
  51.  
  52. // 詳細ウィンドウ
  53.  
  54. Ti.include('./db.js');
  55.  
  56. MainWindows.detailTableView = {};
  57. MainWindows.detailTableView.tableView = Ti.UI.createTableView({style: Ti.UI.iPhone.TableViewStyle.GROUPED});
  58. MainWindows.detailTableView.rows = {};
  59. MainWindows.detailTableView.rows.sandwich = Ti.UI.createTableViewRow({header: 'サンドイッチ', title: ''});
  60. MainWindows.detailTableView.rows.sandwichsize = Ti.UI.createTableViewRow({title: ''});
  61. MainWindows.detailTableView.rows.bread = Ti.UI.createTableViewRow({title: ''});
  62. MainWindows.detailTableView.rows.topping = Ti.UI.createTableViewRow({header: 'トッピング', title: ''});
  63. MainWindows.detailTableView.rows.dressing = Ti.UI.createTableViewRow({header: 'ドレッシング', title: ''});
  64.  
  65. MainWindows.detailTableView.rows.price = Ti.UI.createTableViewRow({header: '代金', title: ''});
  66. MainWindows.detailTableView.rows.kcal = Ti.UI.createTableViewRow({header: 'カロリー', title: ''});
  67. MainWindows.detailTableView.clear= function(){
  68. MainWindows.detailTableView.rows.sandwich.title = '';
  69. MainWindows.detailTableView.rows.sandwichsize.title = '';
  70. MainWindows.detailTableView.rows.bread.title = '';
  71. MainWindows.detailTableView.rows.topping.title = '';
  72. MainWindows.detailTableView.rows.dressing.title = '';
  73. MainWindows.detailTableView.rows.price.title = '';
  74. MainWindows.detailTableView.rows.kcal.title = '';
  75. };
  76. MainWindows.detailTableView.init = function(){
  77. MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.sandwich);
  78. MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.sandwichsize);
  79. MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.bread);
  80. MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.topping);
  81. MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.dressing);
  82. MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.price);
  83. MainWindows.detailTableView.tableView.appendRow(MainWindows.detailTableView.rows.kcal);
  84.  
  85. MainWindows.detailWindow.add(MainWindows.detailTableView.tableView);
  86. };
  87. MainWindows.detailTableView.init();
  88.  
  89. //
  90. // マスタナビゲーション用TableView
  91. //
  92. MainWindows.goNextTreeView = function(title, nextTreeViewData){
  93. var nextWin = Ti.UI.createWindow({title: title, barColor: '#060'});
  94. var nextTreeView = Ti.UI.createTableView({data: nextTreeViewData.data});
  95. if(nextTreeViewData.click){
  96. nextTreeView.addEventListener('click', nextTreeViewData.click);
  97. }
  98. nextWin.add(nextTreeView);
  99. MainWindows.masterNav.add(nextWin);
  100. MainWindows.masterNav.open(nextWin);
  101. };
  102. MainWindows.tvSandwich = {
  103. data: db.table.sandwich
  104. };
  105.  
  106. MainWindows.tvSandwichSize = {
  107. data: db.table.size
  108. };
  109.  
  110. MainWindows.tvBread = {
  111. data: db.table.bread
  112. };
  113.  
  114. MainWindows.tvTopping = {
  115. data: (function(){
  116. var rows = [];
  117. var tpData = db.table.topping;
  118.  
  119. for(var i = 0; i < tpData.length; i++){
  120. (function(i){
  121. var row = Ti.UI.createTableViewRow();
  122. var rowView = Ti.UI.createView();
  123. var rowTitle = tpData[i].title;
  124. var rowValue = Ti.UI.createTextField({
  125. borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
  126. value: '0',
  127. enabled: false,
  128. editable:false,
  129. left:250,
  130. width:60
  131. });
  132. rowView.add(Ti.UI.createLabel({
  133. text: rowTitle,
  134. left:8,
  135. width:240
  136. }));
  137. rowView.add(rowValue);
  138. rowView.addEventListener('click', function(e){
  139. // PopOverの作成
  140. var popTableView = Ti.UI.createTableView({
  141. data : [
  142. {title: '0'},
  143. {title: '1'},
  144. {title: '2'}
  145. ]
  146. });
  147. var popOver = Ti.UI.iPad.createPopover({
  148. title: rowTitle,
  149. barColor: '#060',
  150. width: 240,
  151. height:140
  152. });
  153. popTableView.addEventListener('click', function(e){
  154. rowValue.value = e.index;
  155. tpData[i].qty = e.index;
  156. popOver.hide();
  157. popTableView = null;
  158. popOver = null;
  159. });
  160. popOver.add(popTableView);
  161. popOver.show({view: rowValue, animate: true});
  162. });
  163. row.add(rowView);
  164. rows.push(row);
  165. })(i);
  166. }
  167. rows.push((function(){
  168. var row = Ti.UI.createTableViewRow();
  169. row.add(Ti.UI.createButton({title:'決定'}));
  170. row.addEventListener('click', function(e){
  171. nowprice.topping = 0;
  172. nowkcal.topping = 0;
  173. var title = '';
  174. for(var j = 0; j < tpData.length; j++){
  175. nowprice.topping += tpData[j].yen * tpData[j].qty;
  176. nowkcal.topping += tpData[j].kcal * tpData[j].qty;
  177. if(tpData[j].qty > 0){
  178. if(title.length > 0){
  179. title += ', ';
  180. }
  181. title += tpData[j].title + ' x ' + tpData[j].qty;
  182. }
  183. }
  184. if(title.length == 0){
  185. title = 'なし';
  186. }
  187. MainWindows.detailTableView.rows.topping.title = title;
  188. MainWindows.goNextTreeView('ドレッシング', MainWindows.tvDressing);
  189. });
  190. return row;
  191. })());
  192. return rows;
  193. })()
  194. };
  195.  
  196. MainWindows.tvDressing = {
  197. data: db.table.dressing
  198. };
  199.  
  200. //
  201. // ナビゲーション
  202. //
  203. MainWindows.tvSandwich.click = function(e){
  204. MainWindows.detailTableView.rows.sandwich.title = e.row.title;
  205. nowprice.sandwich = 0;
  206. nowprice.sandwich1 = e.rowData.yen;
  207. nowprice.sandwich2 = e.rowData.yen2;
  208. nowkcal.sandwich = e.rowData.kcal;
  209. MainWindows.goNextTreeView('パンの種類', MainWindows.tvBread);
  210. };
  211. MainWindows.tvBread.click = function(e){
  212. MainWindows.detailTableView.rows.bread.title = e.row.title;
  213. nowkcal.bread = e.rowData.kcal;
  214. MainWindows.goNextTreeView('パンのサイズ', MainWindows.tvSandwichSize);
  215. };
  216. MainWindows.tvSandwichSize.click = function(e){
  217. nowprice.sandwich = nowprice.sandwich1;
  218. if(e.index == 1){
  219. nowprice.sandwich = nowprice.sandwich2;
  220. nowkcal.sandwich *= 2;
  221. nowkcal.bread *= 2;
  222. }
  223.  
  224. MainWindows.detailTableView.rows.sandwichsize.title = e.row.title;
  225. MainWindows.goNextTreeView('トッピング', MainWindows.tvTopping);
  226. };
  227. MainWindows.tvDressing.click = function(e){
  228. MainWindows.detailTableView.rows.dressing.title = e.row.title;
  229. nowkcal.dressing = e.rowData.kcal;
  230.  
  231. var price = nowprice.sandwich + nowprice.topping;
  232. var kcal = nowkcal.sandwich + nowkcal.bread + nowkcal.topping + nowkcal.dressing;
  233. MainWindows.detailTableView.rows.price.title = price + '円';
  234. MainWindows.detailTableView.rows.kcal.title = kcal + 'kcal';
  235. };
  236.  
  237. MainWindows.masterWindow.add((function(nextTreeViewData){
  238. var nextTreeView = Ti.UI.createTableView({data: nextTreeViewData.data});
  239. if(nextTreeViewData.click){
  240. nextTreeView.addEventListener('click', nextTreeViewData.click);
  241. }
  242. return nextTreeView;
  243. })(MainWindows.tvSandwich));
  244.  
  245. // クリアボタン
  246. MainWindows.detailWindow.rightNavButton = (function(){
  247. var button = Titanium.UI.createButton({
  248. title:'クリア'
  249. });
  250. button.addEventListener('click', function(){
  251. MainWindows.detailTableView.clear();
  252. });
  253. return button;
  254. })();
  255.  
  256. MainWindows.open = function(){
  257. Ti.UI.currentWindow.add(MainWindows.splitView);
  258. MainWindows.splitView.open();
  259. };
  260. MainWindows.open();
Add Comment
Please, Sign In to add comment