Skip to content

Instantly share code, notes, and snippets.

@kellymredd
Created August 25, 2011 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellymredd/1172091 to your computer and use it in GitHub Desktop.
Save kellymredd/1172091 to your computer and use it in GitHub Desktop.
can't remove all windows with for loop
// data starts @ line 10
// functions starts @line 47
// ui starts @ line 210
Ti.UI.setBackgroundColor('#252525');
var device = Ti.Platform.osname;
var ng = {};
// start data
(function() {
ng.data = {};
ng.data.device = Ti.Platform.osname;
ng.data.page_name = 'News';
ng.data.current_window = null;
ng.data.previous_window = null;
ng.data.globalZ = 1;
ng.data.i = 0;
ng.data.windowID = 0;
ng.data.window_stack = [];
ng.data.win_origin = 350;
ng.data.win_open = 70;
ng.data.ipadmenu = [{
title : 'News'
}, {
title : 'Photos'
}, {
title : 'Videos'
}, {
title : 'Facts'
}, {
title : 'Ranks'
}, {
title : 'Uniform'
}, {
title : 'Unit GPS'
}, {
title : 'Jobs & Pay'
}, {
title : 'Fitness'
}, {
title : 'Games'
}];
})();
// end data
// start functions
(function() {
ng.functions = {};
ng.functions.tableviewTop = 0;
ng.functions.l2_win_open = 0;
ng.functions.wrapper_view_l2 = null;
ng.functions.num_cols = 4;
ng.functions.col_num = 0;
ng.functions.row_num = 0;
// character limit on tablerow label text
ng.functions.slice = function(t) {
t = (t.length < 29) ? t : t.slice(0, 29) + ' ...';
return t;
};
ng.functions.orient = function(o) {
if(o == 1 || o == 2) {
ng.data.portrait = 1;
} else {
ng.data.portrait = 0;
}
};
// listen for article close
Ti.App.addEventListener('state', function(d) {
ng.functions.l2_win_open = d.state;
});
ng.functions.open_current = Ti.UI.createAnimation({
left : ng.data.win_origin,
duration : 400
});
ng.functions.new_window = function(url, title, id) {
ng.data.globalZ++;
ng.data.current_window = ng.ui.ipad.createl2Window('100%');
// create wrapper so we can remove it AND whatever is in it.
ng.functions.wrapper = Ti.UI.createView({
top : 60,
left : 60,
bottom : 20,
right : 20
});
ng.data.current_window.add(ng.functions.wrapper);
// this mimics an included url
var data = [{
title : 'All Videos',
url : 'favorites.js'
}, {
title : 'Playlists',
url : 'files/videos/pad/playlists_list_feed.js'
}, {
title : 'Favorites',
url : 'files/videos/pad/favorites_feed.js'
}, {
title : 'Uploads',
url : 'files/videos/pad/most_recent_feed.js'
}];
// create the rss table
ng.functions.wrapper.add(ng.ui.ipad.create_GENERIC_TableView(data, 0));
// end mimic included url
// make id access inside url
ng.data.vid_id = id;
//Ti.include(url); // commented out for demo
win.add(ng.data.current_window);
ng.functions.open_current.animate(ng.data.current_window);
// push new window onto stack
var this_win_id = ng.data.windowID++;
ng.data.current_window.winID = this_win_id;
ng.data.window_stack.push(ng.data.current_window);
};
ng.functions.set_ipad_menu = function(device, menu) {
menu_array = [];
ipad = 1;
var len = menu.length;
for( m = 0; m < len; m++) {
var menu_item = Ti.UI.createTableViewRow({
backgroundImage : 'files/images/pad/main-menu/main-menu-row-bg.jpg',
height : 50,
width : 440,
left : 0,
top : 0,
url : menu[m].url,
id : m,
ti : menu[m].title,
leftImage : 'files/images/pad/main-menu/divot-unselected.png',
selectedBackgroundColor : 'none'
});
var img = ng.ui.createIpadAppMenuImageView(menu[m].img);
var label = ng.ui.createAppMenuLabel(ipad, menu[m].title, menu[m].url);
menu_item.add(img, label);
menu_item.addEventListener('click', function(e) {
var id = e.rowData.id;
var url = e.rowData.url;
var title = e.rowData.ti;
// opening new top-level window so close everything and replace
if(ng.data.window_stack.length){
for(w in ng.data.window_stack){
win.remove(ng.data.window_stack[w]);
}
// clean out array
ng.data.window_stack = [];
}
//ng.functions.new_window(url, title, id);
ng.data.windowID = 0;
ng.data.globalZ = 1;
});
menu_array.push(menu_item);
}
return menu_array;
};
})();
// end functions
// start ui
(function() {
ng.ui = {};
ng.ui.ipad = {};
// first level window (ipad)
ng.ui.ipad.createRootWindow = function(url) {
return Ti.UI.createWindow({
navBarHidden : true,
zIndex : 1
});
};
// second level window (ipad)
ng.ui.ipad.createl2Window = function(left) {// left = 320 for main wins, 100% for sub wins
var l2 = 'window_' + Math.random() * 99;
l2 = Ti.UI.createWindow({
device : ng.data.device,
backgroundColor : '#888',
backgroundLeftCap : 1,
backgroundTopCap : 1,
top : 70,
height : 618,
left : left,
width : 500, // 639
zIndex : ng.data.globalZ,
borderRadius : 10
});
var l2_handle = Ti.UI.createView({
backgroundColor : 'green',
left : 20,
top : 60,
width : 40,
height : 538,
view_origin : ng.data.win_origin,
view_open : ng.data.win_open
});
l2.add(l2_handle);
return l2;
};
ng.ui.ipad.createMainMenu = function(data) {
var main_menu = Ti.UI.createTableView({
data : data,
backgroundColor : '#333',
left : 10,
top : 70,
width : 444,
height : 554,
borderRadius : 10,
separatorColor : 'transparent'
});
main_menu.footerView = ng.ui.createAppTableFooter();
return main_menu;
};
// top level tabgroup for ipad
ng.ui.ipad.createRootTabgroup = function() {
var index = Titanium.UI.createTabGroup();
win = ng.ui.ipad.createRootWindow();
var tab = Titanium.UI.createTab({
window : win
});
win.hideTabBar();
index.addTab(tab);
ng.functions.new_window('files/news/index.js', 'News', 0);
ng.ui.main_menu = ng.ui.ipad.createMainMenu(ng.functions.set_ipad_menu('ipad', ng.data.ipadmenu));
win.add(ng.ui.main_menu);
ng.functions.orient(Ti.UI.orientation);
Ti.Gesture.addEventListener('orientationchange', function(e) {
ng.functions.orient(e.orientation);
});
return index;
};
// l2 tableview
ng.ui.createAppTableview_l2 = function(data) {
return Ti.UI.createTableView({
data : data,
left : 50,
top : 0,
right : 0,
bottom : 0
});
};
// tableview header
ng.ui.createAppTableHeader = function() {
return Ti.UI.createView();
};
// tableview footer
ng.ui.createAppTableFooter = function() {
return Ti.UI.createView({
height : 0
});
};
// main menu image views
ng.ui.createIpadAppMenuImageView = function(img) {
return Ti.UI.createImageView({
image : img,
left : 40,
width : 30,
height : 30,
top : 0,
bottom : 0,
touchEnabled : false
});
};
// main menu image views
ng.ui.createAppMenuLabel = function(ipad, title, url) {
return Ti.UI.createLabel({
text : title,
font : {
fontSize : 16
},
color : '#fff',
left : 20,
right : 0,
bottom : 0,
textAlign : 'left',
url : url,
height : 54,
touchEnabled : false
});
};
// table row label
ng.ui.ipad.create_GENERIC_TableRow = function(title, rightImage) {
var row = Ti.UI.createTableViewRow({
backgroundColor : 'transparent',
selectedBackgroundColor : 'transparent',
top : 0,
left : 0,
right : 0,
height : 60,
rightImage : rightImage,
backgroundGradient : {
type : 'linear',
colors : [{
color : '#fff',
position : 0.0
}, {
color : '#f1f1f1',
position : 0.50
}, {
color : '#e2e2e2',
position : 1.0
}]
}
});
var wrap = ng.ui.ipad.create_GENERIC_TableRowWrap();
row.add(wrap);
wrap.add(ng.ui.ipad.create_GENERIC_TableRowTitle(title));
return row;
};
ng.ui.ipad.create_GENERIC_TableRowWrap = function() {
return Ti.UI.createView({
top : 0,
left : 10,
height : 50.5,
width : 290,
backgroundColor : 'none'
});
};
// table row label
ng.ui.ipad.create_GENERIC_TableRowTitle = function(t) {
return Ti.UI.createLabel({
text : ng.functions.slice(t),
height : 'auto',
top : 16,
left : 10,
right : 10,
textAlign : 'left',
color : '#555',
font : {
fontSize : 15
}
});
};
ng.ui.ipad.create_GENERIC_TableViewData = function(d) {
var data = [];
for(var i = 0; i < d.length; i++) {
var title = d[i].title;
var url = d[i].url;
var rightImage = (d[i].rightImage != null) ? d[i].rightImage : 'files/images/tablerow-right-arrow.png';
// Create a table row for this item
row = ng.ui.ipad.create_GENERIC_TableRow(title, rightImage);
row.ti = title;
row.url = url;
data[i] = row;
// Create tableView row event listener
row.addEventListener('singletap', function(e) {
ng.functions.new_window(e.rowData.url, e.rowData.ti, null);
});
// end row listener
}// end function
return data;
};
ng.ui.ipad.create_GENERIC_TableView = function(data, type) {
tv = Ti.UI.createTableView({
backgroundColor : 'orange',
left : 0,
top : 0,
right : 0,
bottom : 0,
layout : 'vertical',
separatorColor : 'transparent',
view_origin : ng.data.win_origin,
view_open : 70
});
switch(type) {
case 0:
tv.setData(ng.ui.ipad.create_GENERIC_TableViewData(data));
// generic - ranks, facts, weapons, etc
break;
case 1:
tv.setData(ng.ui.ipad.create_NEWS_TableViewData(data));
// news
break;
case 2:
tv.setData(ng.ui.ipad.create_VIDEO_TableViewData(data));
// video
break;
case 3:
tv.setData(ng.ui.ipad.create_PLAYLIST_TableViewData(data));
// video playlist
break;
}
return tv;
};
})();
// end ui
// app.js
win = ng.ui.ipad.createRootTabgroup();
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment