Advertisement
stevelam

app.js video working

Nov 21st, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.10 KB | None | 0 0
  1. // external library used for image caching by Kevin Hopkins
  2. Ti.include('cachedImageView.js');
  3.  
  4. var win = Titanium.UI.createWindow({
  5. backgroundColor:'#333',
  6. backgroundImage:'/assets/bg.png',
  7. fullscreen:true,
  8. })
  9. win.open();
  10.  
  11. /*
  12. var leftImage = Ti.UI.createView({
  13. backgroundImage:'../images/icon_arrow_left.png',
  14. height:30,
  15. width:30,
  16. top:18,
  17. left:5,
  18. visible:false
  19. });
  20. win.add(leftImage);
  21. var rightImage = Ti.UI.createView({
  22. backgroundImage:'../images/icon_arrow_right.png',
  23. height:30,
  24. width:30,
  25. top:18,
  26. right:5
  27. });
  28. win.add(rightImage);
  29. */
  30.  
  31. // add title graphic
  32. var titleImage = Ti.UI.createImageView({
  33. image:'/assets/title.png',
  34. top:10,
  35. left:50,
  36. width:250,
  37. height:30
  38. })
  39.  
  40. win.add(titleImage);
  41.  
  42. // add main navigation background
  43. var navBackground = Ti.UI.createImageView({
  44. image:'/assets/navbg.png',
  45. width:320,
  46. height:34,
  47. top:55,
  48. left:0
  49. })
  50.  
  51. win.add(navBackground);
  52.  
  53. // adding settings button
  54. var settingsBtn = Ti.UI.createButton({
  55. backgroundImage:'/assets/settings_btn.png',
  56. height:60,
  57. width:60,
  58. top:0,
  59. left:0,
  60. zIndex:100
  61. })
  62.  
  63. win.add(settingsBtn);
  64.  
  65. // window that contains settings modal window
  66. var settingSlide = Ti.UI.createWindow({
  67. backgroundImage:'/assets/bg.png',
  68. modal:true,
  69. navBarHidden:true
  70. })
  71.  
  72. //close button to return to main portfolio screen
  73. var closeSettingsBtn = Ti.UI.createButton({
  74. width:50,
  75. height:50,
  76. title:'close'
  77. })
  78.  
  79. closeSettingsBtn.addEventListener('click', function(e){
  80. settingSlide.close();
  81. })
  82.  
  83. // add the close button to the settings screen
  84. settingSlide.add(closeSettingsBtn);
  85.  
  86. //make the setting button work
  87. settingsBtn.addEventListener('click', function(e) {
  88. settingSlide.open();
  89. })
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. // facebook integration
  97.  
  98. Titanium.Facebook.appid = "243686829029483";
  99. Titanium.Facebook.permissions = ['publish_stream', 'read_stream'];
  100. //
  101. // Login Status
  102. //
  103. var label = Ti.UI.createLabel({
  104. text:'Logged In = ' + Titanium.Facebook.loggedIn,
  105. font:{fontSize:14},
  106. height:'auto',
  107. top:10,
  108. textAlign:'center'
  109. });
  110. settingSlide.add(label);
  111.  
  112. var forceButton = Ti.UI.createButton({
  113. title:'Force dialog: '+Titanium.Facebook.forceDialogAuth,
  114. top:50,
  115. width:160,
  116. height:40
  117. });
  118. forceButton.addEventListener('click', function() {
  119. Titanium.Facebook.forceDialogAuth = !Titanium.Facebook.forceDialogAuth;
  120. forceButton.title = "Force dialog: "+Titanium.Facebook.forceDialogAuth;
  121. });
  122. settingSlide.add(forceButton);
  123.  
  124. function updateLoginStatus() {
  125. label.text = 'Logged In = ' + Titanium.Facebook.loggedIn;
  126. }
  127.  
  128. // capture
  129. Titanium.Facebook.addEventListener('login', updateLoginStatus);
  130. Titanium.Facebook.addEventListener('logout', updateLoginStatus);
  131.  
  132. //
  133. // Login Button
  134. //
  135. settingSlide.add(Titanium.Facebook.createLoginButton({
  136. style:'wide',
  137. bottom:30
  138. }));
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. // adding info button
  147. var infoBtn = Ti.UI.createButton({
  148. backgroundImage:'/assets/info_btn.png',
  149. height:60,
  150. width:60,
  151. bottom:0,
  152. right:0,
  153. // keep it on top of the portfolio images
  154. zIndex:100
  155. })
  156.  
  157. win.add(infoBtn);
  158.  
  159. // the window that contains the info modal window
  160. var infoSlide = Ti.UI.createWindow({
  161. backgroundImage:'/assets/bg.png',
  162. //top:0,
  163. //fullscreen:true
  164. modal:true,
  165. navBarHidden:true
  166. })
  167.  
  168. // close button to return to main portfolio screen
  169. var closeInfoBtn = Ti.UI.createButton({
  170. width:50,
  171. title:'close',
  172. height:50
  173. })
  174.  
  175. // makes the close button work!
  176. closeInfoBtn.addEventListener('click', function(e) {
  177. infoSlide.close();
  178. })
  179.  
  180. // adds close button to the info screen
  181. infoSlide.add(closeInfoBtn);
  182.  
  183. // makes the info button work!
  184. infoBtn.addEventListener('click', function(e) {
  185. infoSlide.open();
  186. })
  187.  
  188. /*
  189. var tableWrapper = Ti.UI.createTableView({
  190. backgroundColor:'transparent',
  191. separatorColor:'transparent'
  192. })
  193.  
  194. win.add(tableWrapper);
  195. */
  196. // keep all image urls in an array
  197. var imageArray = [];
  198.  
  199. // keep all youtube Id's used in an array
  200. var youtubeIdArray = [];
  201. // keep the youtube embed codes in an array
  202. var htmlTrailer = [];
  203. // keeps track of img counter in the loop so we can reference the proper one later
  204. var startImgCounter = 0;
  205. // keeps track of video count/position in array
  206. var startVidCounter = 0;
  207. // takes 3 parameters: url of the feed, the scrollview increment, and the screen location of the scrollview, followed by type (image or video)
  208. var loadPortfolioSection = function(url, scrollView, padding, type) {
  209. // set up a blank array for scrollviews. we will need 3 of them
  210. var scrollViews = [];
  211.  
  212. // takes the scrolLView attribute provided by the function call and creates an individual scrollview
  213. scrollViews[scrollView] = Ti.UI.createScrollView({
  214. contentWidth:'auto',
  215. contentHeight:100,
  216. top:padding,
  217. height:110,
  218. width:320,
  219. backgroundColor:'#000'
  220. });
  221. // adds it to the window
  222. win.add(scrollViews[scrollView]);
  223.  
  224. var data = [];
  225. var xhr = Ti.Network.createHTTPClient();
  226. // here's where to insert the rss feed's link.
  227. xhr.open("GET",url)
  228.  
  229. xhr.onload = function(e)
  230. {
  231. try
  232. {
  233. var doc = this.responseXML.documentElement;
  234. if(type == 'image')
  235. {
  236. var items = doc.getElementsByTagName("item");
  237. var doctitle = doc.evaluate("//channel/title/text()").item(0).nodeValue;
  238. }
  239. else
  240. {
  241. var items = doc.getElementsByTagName("entry");
  242. var doctitle = doc.evaluate("//media:title").item(0).nodeValue;
  243. }
  244. var x = 0;
  245.  
  246. // initial spacing between portfolio pieces. this will be incremented in the for loop
  247. var spacing = 0;
  248.  
  249. for (var c=0;c<items.length;c++)
  250. {
  251. var item = items.item(c);
  252. // assign the title of blog post from feed
  253. var title = item.getElementsByTagName("title").item(0).text;
  254. // place the title into a label for each portfolio piece
  255. var label = Ti.UI.createLabel({
  256. text:title,
  257. color:'#fff',
  258. font: {fontSize:12},
  259. backgroundColor: '#000',
  260. top:5,
  261. left:5,
  262. opacity:0.8,
  263. width:100,
  264. height:30,
  265. left:spacing
  266. });
  267.  
  268. // assign the url of the image of the blog post into a variable
  269. if(type == 'image') {
  270. startImgCounter++;
  271. var media = doc.getElementsByTagName("media:content").item(c).getAttribute("url");
  272. imageArray.push(media);
  273. } else
  274. {
  275. // this grabs unique youtube url for each video seeing as we can't grab thumbnails properly
  276. var youtubeUrl = doc.getElementsByTagName("media:player").item(c).getAttribute("url");
  277. //Ti.API.info(youtubeUrl);
  278. // this splits the url in order to get the unique ID
  279. var getVideoId = youtubeUrl.split("v=");
  280. // strip the end off to get the final ID #
  281. var finalVideoId = getVideoId[1].substring(0,11);
  282. //Ti.API.info(finalVideoId);
  283. // thumbnail for each video by default url : http://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
  284. var media = 'http://img.youtube.com/vi/' + finalVideoId + '/hqdefault.jpg';
  285. youtubeIdArray.push(finalVideoId);
  286. }
  287. // Ti.API.info(media);
  288.  
  289. // create an image view for the image
  290. var portfolioPiece = Ti.UI.createImageView({
  291. image:media,
  292. // keeps track of which portfolio pieces are video
  293. vidCounter:startVidCounter,
  294. defaultImage:'',
  295. width:200,
  296. height:200
  297. });
  298.  
  299. if(type == 'image') {
  300.  
  301. portfolioPiece.addEventListener('click', function(e){
  302. var popup = Ti.UI.createWindow({
  303. width:320,
  304. height:640,
  305. modal:true,
  306. navBarHidden:true
  307. })
  308.  
  309. var portfolioItem = Ti.UI.createImageView({
  310. image:e.source.image,
  311. width:320
  312. })
  313.  
  314. popup.add(portfolioItem);
  315.  
  316. var popupCloseBtn = Ti.UI.createButton({
  317. width:50,
  318. height:30,
  319. top:80,
  320. right:0,
  321. title:'close'
  322. })
  323.  
  324. popupCloseBtn.addEventListener('click',function(){
  325. popup.close();
  326. })
  327. popup.open();
  328. popup.add(popupCloseBtn);
  329.  
  330. // post image to facebook
  331. var facebookPost = Ti.UI.createButton({
  332. //image:e.source.image,
  333. width:150,
  334. height:30,
  335. top:150,
  336. right:0,
  337. title:'post to facebook'
  338. })
  339.  
  340.  
  341.  
  342. function showRequestResult(e) {
  343. var s = '';
  344. if (e.success) {
  345. s = "SUCCESS";
  346. if (e.result) {
  347. s += "; " + e.result;
  348. }
  349. } else {
  350. s = "FAIL";
  351. if (e.error) {
  352. s += "; " + e.error;
  353. }
  354. }
  355.  
  356.  
  357. alert(s);
  358. }
  359.  
  360. /*
  361. facebookPost.addEventListener('click', function(e){
  362. Titanium.Facebook.permissions = ['publish_stream'];
  363.  
  364. if(Titanium.Facebook.loggedIn) {
  365. Ti.Facebook.authorize();
  366. Ti.API.info('yes');
  367.  
  368. facebookPost.title = 'Uploading Photo...';
  369. // (Ti.Filesystem.applicationDataDirectory, imageDirectoryName, filename);
  370. var filename = media.split('/');
  371. filename = filename[filename.length - 1];
  372.  
  373.  
  374. var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'cache', filename);
  375. Ti.API.info(f);
  376. var blob = f.read();
  377. var data = {
  378. caption: title,
  379. picture: blob
  380. };
  381. Titanium.Facebook.request('photos.upload', data, showRequestResult);
  382.  
  383.  
  384. } else {
  385.  
  386. }
  387. })
  388.  
  389. popup.add(facebookPost);
  390. */
  391.  
  392. Titanium.Facebook.permissions = ['publish_stream'];
  393. Titanium.Facebook.authorize();
  394.  
  395. facebookPost.addEventListener('click', function(e){
  396.  
  397. //var fileUrl = imageArray[c];
  398. var fileUrl = e.source.image;
  399. Ti.API.info(fileUrl);
  400. Ti.API.info(media);
  401. var filename = fileUrl.split('/');
  402. Ti.API.info(filename);
  403.  
  404. //filename = filename[filename.length - 1];
  405. //Ti.API.info(filename);
  406. var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'cache', filename);
  407. var blob = f.read();
  408. var data = {
  409. message: title,
  410. picture: blob
  411. };
  412. facebookPost.title = 'Uploading Photo...';
  413. /*
  414. Ti.Facebook.requestWithGraphPath('me/photos', data, 'POST', function(e){
  415. if (e.success) {
  416. alert("Success! From FB: " + e.result);
  417. } else {
  418. if (e.error) {
  419. alert(e.error);
  420. } else {
  421. alert("Unkown result");
  422. }
  423. }
  424. });*/
  425.  
  426. })
  427.  
  428. popup.add(facebookPost);
  429. // Now post the photo after you've confirmed that authorize() succeeded
  430.  
  431.  
  432.  
  433. //Ti.API.info(e.source.image);
  434. })
  435.  
  436. // video thumbs
  437. } else {
  438.  
  439. htmlTrailer[startVidCounter] = '<html><body style="padding:0; padding-top:100px; width:320px;"><div style="width:320px;"><iframe width="320" height="200" src="http://www.youtube.com/embed/' + youtubeIdArray[startVidCounter] + '" frameborder="0" allowfullscreen></iframe></div></body></html>';
  440.  
  441. startVidCounter++;
  442. portfolioPiece.addEventListener('click', function(e){
  443.  
  444. Ti.API.info(youtubeIdArray[startVidCounter]);
  445. var popup = Ti.UI.createWebView({
  446. html:htmlTrailer[e.source.vidCounter],
  447. backgroundColor:"#000",
  448. top:0,
  449. width:320,
  450. height:640,
  451. modal:true,
  452. zIndex:200
  453. })
  454.  
  455. var closeVideoBtn = Ti.UI.createButton({
  456. width:50,
  457. height:30,
  458. title:'close',
  459. top:0,
  460. right:0
  461. })
  462.  
  463. popup.add(closeVideoBtn);
  464.  
  465. closeVideoBtn.addEventListener('click', function(e){
  466. popup.hide();
  467. })
  468.  
  469. win.add(popup);
  470.  
  471. })
  472. }
  473.  
  474. // a cropping hack that places the image into a view with a fake border that crops the image
  475. var croppedImage = Ti.UI.createView({
  476. borderRadius:0.1,
  477. height:100,
  478. width:100,
  479. backgroundColor:"#333",
  480. left:spacing
  481. })
  482.  
  483.  
  484. // activity indicator for images loading
  485. var actInd = Titanium.UI.createActivityIndicator({
  486. touchEnabled:false,
  487. style:Titanium.UI.iPhone.ActivityIndicatorStyle.DARK
  488. });
  489.  
  490. // caches the images on load locally
  491. // cachedImageView('/cache/', media, portfolioPiece);
  492.  
  493. // show the activity indicator
  494. actInd.show();
  495. portfolioPiece.add(actInd);
  496.  
  497. // when the image loads, we hide the indicator
  498.  
  499. portfolioPiece.addEventListener('load', function(e)
  500. {
  501. actInd.hide();
  502. });
  503.  
  504.  
  505. croppedImage.add(portfolioPiece);
  506. // moves the next portfolio piece over to not overlap
  507. spacing += 110;
  508. // places the image onto the scroller view
  509. scrollViews[scrollView].add(croppedImage);
  510. // adds label to each portfolio image
  511. scrollViews[scrollView].add(label);
  512.  
  513. // hope to use this part as pull to refresh call
  514. scrollViews[scrollView].addEventListener('scroll', function(e)
  515. {
  516. //Ti.API.info('x ' + e.x + ' y ' + e.y);
  517.  
  518. if (e.x < -40)
  519. {
  520.  
  521. }
  522. else
  523. {
  524.  
  525. }
  526.  
  527. });
  528.  
  529. //scrollView.add(img);
  530. // data[x++] = row;
  531. // row.url = item.getElementsByTagName("link").item(0).text;
  532.  
  533. }
  534.  
  535. // Ti.API.info(imageArray);
  536. /*
  537. var tableview = Titanium.UI.createTableView({data:data});
  538. // win.add(tableview);
  539. tableview.addEventListener('click',function(e)
  540. {
  541. var w = Ti.UI.createWindow({title:doctitle});
  542. var wb = Ti.UI.createWebView({url:e.row.url});
  543. w.add(wb);
  544. var b = Titanium.UI.createButton({
  545. title:'Close',
  546. style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
  547. });
  548. w.setLeftNavButton(b);
  549. b.addEventListener('click',function()
  550. {
  551. w.close();
  552. });
  553. w.open({modal:true});
  554. });*/
  555. }
  556. catch(E)
  557. {
  558. alert(E);
  559. }
  560. };
  561. xhr.send();
  562. }
  563.  
  564.  
  565.  
  566. // call the function to load each rss feed
  567. loadPortfolioSection('http://iblastoff.ca/humbershowcase/category/1st-year/design/feed/', 1, 100, 'image');
  568. loadPortfolioSection('http://iblastoff.ca/humbershowcase/category/1st-year/feed/', 2, 235, 'image');
  569.  
  570. // video for mobile projects
  571. loadPortfolioSection('http://gdata.youtube.com/feeds/api/playlists/9011A3E344EB3021', 3, 370, 'video');
  572.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement