Advertisement
loonerz

commentforcomments

Jun 13th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createCommentForCommentsV2(wall_post, wallType, endpoint, table) {
  2.  
  3.     var that = {};
  4.  
  5.     var windowOpen = Ti.UI.createAnimation({
  6.         duration : 150,
  7.         left : 0
  8.     });
  9.  
  10.     var containerViewWidth = 300;
  11.     var viewHeight = 30;
  12.     if (Ti.Platform.osname === 'ipad') {
  13.         if (wallType != null) {
  14.             if (wallType === "iPadCommentsViewer") {
  15.                 containerViewWidth = Ti.Platform.displayCaps.platformWidth - 320;
  16.                 viewHeight = 50;
  17.             } else
  18.                 containerViewWidth = Ti.Platform.displayCaps.platformWidth - 20;
  19.         } else
  20.             containerViewWidth = Ti.Platform.displayCaps.platformWidth - 20;
  21.     }
  22.  
  23.     var containerView = Ti.UI.createView({
  24.         width : containerViewWidth,
  25.         // top : 5,
  26.         height : 100,
  27.         backgroundColor : "white"
  28.     });
  29.  
  30.     var profilePicImageViewHolder = Titanium.UI.createView({
  31.         width : 30,
  32.         height : 30,
  33.         zIndex : 1000,
  34.         left : 2,
  35.         top : 2,
  36.         borderColor : "#dedede",
  37.         borderWidth : 1
  38.     });
  39.  
  40.     var profilePicImageView = Titanium.UI.createImageView({
  41.         // width:50,
  42.         // height:50
  43.     });
  44.  
  45.     profilePicImageViewHolder.add(profilePicImageView);
  46.  
  47.     cachedImageView('cachedImages_CommentsViewer', wall_post.user.avatar.thumb, profilePicImageView);
  48.  
  49.     containerView.add(profilePicImageViewHolder);
  50.  
  51.     profilePicImageView.addEventListener('singletap', function(e) {
  52.         var userProfileWindow = new UserProfileWindow(wall_post.user.veramiko_id);
  53.         userProfileWindow.left = Titanium.Platform.displayCaps.platformWidth;
  54.         userProfileWindow.open(windowOpen);
  55.     });
  56.  
  57.     var usernameLabel = Titanium.UI.createLabel({
  58.         text : wall_post.user.name,
  59.         font : {
  60.             fontSize : 15,
  61.             fontWeight : 'bold'
  62.         },
  63.         height : 14,
  64.         left : 35,
  65.         right : 65,
  66.         top : 9,
  67.  
  68.     });
  69.  
  70.     usernameLabel.addEventListener('singletap', function(e) {
  71.         var userProfileWindow = new UserProfileWindow(wall_post.user.veramiko_id);
  72.         userProfileWindow.left = Titanium.Platform.displayCaps.platformWidth;
  73.         userProfileWindow.open(windowOpen);
  74.     });
  75.  
  76.     containerView.add(usernameLabel);
  77.  
  78.     if (wall_post.created_at != null)
  79.         var created_at = dateFormatter(wall_post.created_at);
  80.  
  81.     var dateLabel = Titanium.UI.createLabel({
  82.         text : created_at,
  83.         color : "#888888",
  84.         font : {
  85.             fontSize : 10
  86.         },
  87.         top : 2,
  88.         right : 5,
  89.  
  90.     });
  91.  
  92.     containerView.add(dateLabel);
  93.    
  94.    
  95.     var extraSpace = 31;
  96.     if(Ti.Platform.osname === 'ipad')
  97.     {
  98.         extraSpace = 62;
  99.     }
  100.     var wallPostCommentTrimmed;
  101.     if(typeof wall_post.comment !== 'undefined')
  102.     {
  103.         wallPostCommentTrimmed = wall_post.comment.replace(/[\n\r]/g, ' ');
  104.         // Ti.API.info('wallpost ' + wallPostCommentTrimmed);
  105.     }
  106.     else if(typeof wall_post.secondary_target.comment !== 'undefined')
  107.     {
  108.         wallPostCommentTrimmed = wall_post.secondary_target.comment.replace(/[\n\r]/g, ' ');
  109.         // Ti.API.info('ex wallpost ' + wallPostCommentTrimmed);
  110.         // extraSpace = 60;
  111.     }
  112.     else if(typeof wall_post.secondary_target.description !== 'undefined')
  113.     {
  114.         wallPostCommentTrimmed = wall_post.secondary_target.description.replace(/[\n\r]/g, ' ');
  115.     }
  116.     // Ti.API.info('length ' + wallPostCommentTrimmed.length);
  117.     // if(wallPostCommentTrimmed > )
  118.    
  119.     containerView.applyProperties({
  120.         height : extraSpace + calculateCommentContentViewHeight(wallPostCommentTrimmed)
  121.     });
  122.     var postContent = Titanium.UI.createLabel({
  123.  
  124.         text : wallPostCommentTrimmed,
  125.         font : {
  126.             fontSize : 12
  127.         },
  128.         height : "auto",
  129.         color : "#3f3f3f",
  130.         top : 35,
  131.         left : 10,
  132.         bottom : 22
  133.  
  134.     });
  135.  
  136.     containerView.add(postContent);
  137.  
  138.    
  139.  
  140.     //***************************
  141.     // LIKE, NO LIKE Layout
  142.    
  143.     var likesLabel = Ti.UI.createLabel({
  144.         text : wall_post.like_counter_cache + ' ' + L('like_string'),
  145.         font : {
  146.             fontSize : 10
  147.         },
  148.         color : '#333333',
  149.         width : (containerViewWidth - 170) / 2,
  150.         height : '100%',
  151.         textAlign : 'left',
  152.         right : 25
  153.     });
  154.  
  155.     var likeness = wall_post.likeness;
  156.  
  157.     var dislikesLabel = Ti.UI.createLabel({
  158.         text : wall_post.dislike_counter_cache + ' ' + L('dislike_string'),
  159.         font : {
  160.             fontSize : 10
  161.         },
  162.         color : '#333333',
  163.         width : (containerViewWidth - 170) / 2,
  164.         height : '100%',
  165.         textAlign : 'right',
  166.         left : 25
  167.     });
  168.  
  169.     var separatorLabel = Ti.UI.createLabel({
  170.         text : ' · ',
  171.         font : {
  172.             fontSize : 20,
  173.             fontWieght : 'bold'
  174.         },
  175.         color : '#333333',
  176.         height : '100%',
  177.         width : 20,
  178.         textAlign : 'center'
  179.         // left : 10,
  180.         // right : 10
  181.     });
  182.    
  183.     var like_msg_str = '';
  184.     var dislike_msg_str = '';
  185.     if(wall_post.like_counter_cache > 0)
  186.     {
  187.         like_msg_str = String.format(L('you_like_string'), wall_post.like_counter_cache);
  188.     }
  189.     else
  190.     {
  191.         like_msg_str = L('i_like_string');
  192.     }
  193.     if(wall_post.dislike_counter_cache > 0)
  194.     {
  195.         dislike_msg_str = String.format(L('you_dislike_string'), wall_post.dislike_counter_cache);
  196.     }
  197.     else
  198.     {
  199.         dislike_msg_str = L('i_dislike_string');
  200.     }
  201.    
  202.     var viewOptionsLayout_Comment = Titanium.UI.createView({
  203.  
  204.         width : containerViewWidth,
  205.         height : viewHeight,
  206.         backgroundColor : "#FAFAFA",
  207.         borderWidth : 1,
  208.         borderColor : "#F0F0F0",
  209.         bottom : 0,
  210.         left : 0,
  211.         right : 0,
  212.         opacity : 1,
  213.         zIndex : 1002
  214.  
  215.     });
  216.  
  217.     var buttonOptions01_Comment = Titanium.UI.createView({
  218.         width : containerViewWidth - 50,
  219.         height : "100%",
  220.         left : -(containerViewWidth - 100),
  221.         backgroundColor : "#EB7F06",
  222.         zIndex : 1050
  223.  
  224.     });
  225.  
  226.     var frontView01 = Ti.UI.createView({
  227.         width : containerViewWidth - 50,
  228.         height : '100%',
  229.         left : -(containerViewWidth - 100),
  230.         optionView : buttonOptions01_Comment,
  231.         zIndex : 1052
  232.     });
  233.  
  234.     var dislikeHeartView = Titanium.UI.createImageView({
  235.         width : 20,
  236.         height : 20,
  237.         right : 25,
  238.         image : imagesPath + 'heart.png'
  239.     });
  240.  
  241.     var dislikesMessage = Ti.UI.createLabel({
  242.         text : dislike_msg_str,
  243.         color : '#fff',
  244.         font : {
  245.             fontSize : 10,
  246.             fontWeight : 'bold'
  247.         },
  248.         right : 50,
  249.         height : "auto",
  250.         width : "auto",
  251.         textAlign : 'center',
  252.         visible : false
  253.     });
  254.  
  255.     // Create a Label.
  256.     var buttonOptions01label_Comment = Ti.UI.createLabel({
  257.         text : '>',
  258.         color : '#ddd',
  259.         font : {
  260.             fontSize : 15,
  261.             fontWeight : 'bold'
  262.         },
  263.         right : 5,
  264.         height : "auto",
  265.         width : "auto",
  266.         textAlign : 'center',
  267.  
  268.     });
  269.  
  270.     var buttonOptions02_Comment = Titanium.UI.createView({
  271.         // width : containerViewWidth - 100,
  272.         height : "100%",
  273.         left : 50,
  274.         right : 50,
  275.         backgroundColor : "transparent",
  276.         // layout : 'horizontal',
  277.         zIndex : 1045
  278.  
  279.     });
  280.  
  281.     var buttonOptions03_Comment = Titanium.UI.createView({
  282.         width : containerViewWidth - 50,
  283.         height : "100%",
  284.         right : -(containerViewWidth - 100),
  285.         backgroundColor : "#9ACA40",
  286.         zIndex : 1050
  287.  
  288.     });
  289.  
  290.     var frontView03 = Ti.UI.createView({
  291.         width : containerViewWidth - 50,
  292.         height : '100%',
  293.         right : -(containerViewWidth - 100),
  294.         optionView : buttonOptions01_Comment,
  295.         zIndex : 1052
  296.     });
  297.  
  298.     var likeHeartView = Titanium.UI.createImageView({
  299.         width : 20,
  300.         height : 20,
  301.         left : 25,
  302.         image : imagesPath + 'heart.png'
  303.     });
  304.  
  305.     var likesMessage = Ti.UI.createLabel({
  306.         text : like_msg_str,
  307.         color : '#fff',
  308.         font : {
  309.             fontSize : 10,
  310.             fontWeight : 'bold'
  311.         },
  312.         left : 50,
  313.         height : "auto",
  314.         width : "auto",
  315.         textAlign : 'center',
  316.         visible : false
  317.     });
  318.  
  319.     var buttonOptions03label_Comment = Ti.UI.createLabel({
  320.         text : '<',
  321.         color : '#ddd',
  322.         font : {
  323.             fontSize : 15,
  324.             fontWeight : 'bold'
  325.         },
  326.         left : 5,
  327.         height : "auto",
  328.         width : "auto",
  329.         textAlign : 'center',
  330.     });
  331.  
  332.     if (likeness == true) {
  333.         Ti.API.info('liked');
  334.         // buttonOptions01label_Comment.applyProperties({
  335.             // color : '#007D0A'
  336.         // });
  337.         // buttonOptions03label_Comment.applyProperties({
  338.             // color : '#333333'
  339.         // });
  340.     }
  341.     if (likeness == false) {
  342.         Ti.API.info('unliked');
  343.         // buttonOptions01label_Comment.applyProperties({
  344.             // color : '#333333'
  345.         // });
  346.         // buttonOptions03label_Comment.applyProperties({
  347.             // color : '#7D0032'
  348.         // });
  349.     }
  350.  
  351.     var dislike_touched = false;
  352.     var like_touched = false;
  353.     var moved = false;
  354.     var initialViewX = 0;
  355.     var curX = 0;
  356.     var deltaX = 0;
  357.     var currentPosition = 0;
  358.     var prevPosition;
  359.     var rowWidth = containerViewWidth - 100;
  360.     var threshold = 0.5;
  361.    
  362.     function changeLikeness(side)
  363.     {
  364.         var likeness = 'like';
  365.         if(side > 0)
  366.         {
  367.             likeness = 'dislike';
  368.         }
  369.        
  370.         var networkArrayIndex = parseInt(getGenericConfig("activeNetworkArrayIndex"), 10);
  371.         var token = JSON.parse(Ti.App.Properties.getString('loggedUser', 'No existe')).networks[networkArrayIndex].token;
  372.         Ti.API.info('token ' + token);
  373.         var url = endpoint;
  374.         url = String.format(url, wall_post.veramiko_id, likeness);
  375.        
  376.         Ti.API.info('url ' + url);
  377.         var client = Ti.Network.createHTTPClient({
  378.             onload : function(e){
  379.                 Ti.API.info('Se recibio del likeness: ' + this.responseText);
  380.                
  381.             },
  382.             onerror : function(e){
  383.                 Ti.API.error('Error changing likeness: ' + this.responseText);
  384.             },
  385.             timeout : 10000
  386.         });
  387.         client.open('POST', url);
  388.         client.setRequestHeader('Authorization', 'Bearer ' + token);
  389.         // client.send();
  390.     }
  391.  
  392.     function touchStart(e) {
  393.         table.setScrollingEnabled(false);
  394.         prevPosition = containerViewWidth*2;
  395.         Ti.API.info('e ' + JSON.stringify(e));
  396.         Ti.API.info('source ' + JSON.stringify(e.source));
  397.         // Ti.API.info('parent ' + JSON.stringify(e.source.optionView));
  398.         // Ti.API.info(' containerViewWidth ' + containerViewWidth + ' rowWidth ' + rowWidth);
  399.         initialViewX = rowWidth;
  400.         curX = e.x;
  401.         // Ti.API.info('initialViewX ' + initialViewX + ' curX ' + curX);
  402.     }
  403.  
  404.     function touchMove(e, side) {
  405.         likesMessage.visible = false;
  406.         dislikesMessage.visible = false;
  407.         // Ti.API.info('e ' + JSON.stringify(e));
  408.         deltaX = (side) * (e.x - curX);
  409.         currentPosition = rowWidth - deltaX;
  410.         // Ti.API.info(' e.x ' + e.x + ' curX ' + curX + ' deltaX ' + deltaX + ' currentPosition ' + currentPosition + ' prevPosition ' + prevPosition);
  411.         var percentageDiscovered = (deltaX / rowWidth);
  412.         // Ti.API.info('percentageDiscovered ' + percentageDiscovered);
  413.         // Ti.API.info('');
  414.         if ((-currentPosition) > (-prevPosition)) {
  415.             if((-currentPosition) > 0)
  416.             {
  417.                 currentPosition = 0;
  418.             }
  419.             if (side == 1) {
  420.                 // Ti.API.info('dislike is moving');
  421.                 buttonOptions01_Comment.applyProperties({
  422.                     left : -currentPosition
  423.                 });
  424.                 if (like_touched) {
  425.                     // Ti.API.info('like has been touched, move it too');
  426.                     var otherRight = buttonOptions03_Comment.right;
  427.                     var newRight =  -deltaX;
  428.                     if(newRight < -(containerViewWidth - 100))
  429.                     {
  430.                         newRight = -(containerViewWidth - 100);
  431.                     }
  432.                     // Ti.API.info('other right ' + otherRight + ' newRight ' + newRight);
  433.                     buttonOptions03_Comment.applyProperties({
  434.                         right : newRight
  435.                     });
  436.                 }
  437.             } else {
  438.                 // Ti.API.info('like is moving');
  439.                 buttonOptions03_Comment.applyProperties({
  440.                     right : -currentPosition
  441.                 });
  442.                 if (dislike_touched) {
  443.                     // Ti.API.info('dislike has been touched, move it too');
  444.                     var otherLeft = buttonOptions01_Comment.left;
  445.                     var newLeft = -deltaX;
  446.                     if(newLeft < -(containerViewWidth - 100))
  447.                     {
  448.                         newLeft = -(containerViewWidth - 100);
  449.                     }
  450.                     // Ti.API.info('other left ' + otherLeft + ' newleft ' + newLeft);
  451.                     buttonOptions01_Comment.applyProperties({
  452.                         left : newLeft
  453.                     });
  454.                 }
  455.             }
  456.             if (percentageDiscovered >= threshold) {
  457.                 moved = true;
  458.             } else {
  459.                 moved = false;
  460.             }
  461.             prevPosition = currentPosition;
  462.         }
  463.  
  464.     }
  465.  
  466.     function touchEnd(e, side) {
  467.         table.setScrollingEnabled(true);
  468.         if (moved) {
  469.             if (side == 1) {
  470.                 // Ti.API.info('dislike did reach the end');
  471.                 buttonOptions01_Comment.applyProperties({
  472.                     left : 0
  473.                 });
  474.                 frontView01.applyProperties({
  475.                     left : 0
  476.                 });
  477.                 dislikesMessage.visible = true;
  478.                 dislike_touched = true;
  479.                 if(like_touched)
  480.                 {
  481.                     // Ti.API.info('like has been touched before, moving it to the back');
  482.                     buttonOptions03_Comment.applyProperties({right : -(containerViewWidth - 100)});
  483.                     frontView03.applyProperties({right : -(containerViewWidth - 100)});
  484.                     like_touched = false;
  485.                 }
  486.                 changeLikeness(side);
  487.             } else {
  488.                 // Ti.API.info('like did reach the end');
  489.                 buttonOptions03_Comment.applyProperties({
  490.                     right : 0
  491.                 });
  492.                 frontView03.applyProperties({right : 0});
  493.                 likesMessage.visible = true;
  494.                 like_touched = true;
  495.                 if(dislike_touched)
  496.                 {
  497.                     // Ti.API.info('dislike has been touched before, moving it to the back');
  498.                     buttonOptions01_Comment.applyProperties({left : -(containerViewWidth - 100)});
  499.                     frontView01.applyProperties({ left : -(containerViewWidth - 100)});
  500.                     dislike_touched = false;
  501.                 }
  502.                 changeLikeness(side);
  503.             }
  504.         } else {
  505.             if (side == 1) {
  506.                 // Ti.API.info('dislike did not reach the end');
  507.                 buttonOptions01_Comment.applyProperties({
  508.                     left : -(containerViewWidth - 100)
  509.                 });
  510.                 frontView01.applyProperties({left : -(containerViewWidth - 100)});
  511.                 dislikesMessage.visible = false;
  512.                 dislike_touched = false;
  513.                 if(like_touched)
  514.                 {
  515.                     // Ti.API.info('like has been touched before, moving it to the end');
  516.                     likesMessage.visible = true;
  517.                     buttonOptions03_Comment.applyProperties({right : 0});
  518.                     frontView03.applyProperties({right : 0});
  519.                 }
  520.             } else {
  521.                 // Ti.API.info('like did not reach the end');
  522.                 buttonOptions03_Comment.applyProperties({
  523.                     right : -(containerViewWidth - 100)
  524.                 });
  525.                 frontView03.applyProperties({right : -(containerViewWidth - 100)});
  526.                 likesMessage.visible = false;
  527.                 like_touched = false;
  528.                 if(dislike_touched)
  529.                 {
  530.                     // Ti.API.info('dislike has been touched before, moving it to the end');
  531.                     dislikesMessage.visible = true;
  532.                     buttonOptions01_Comment.applyProperties({left : 0});
  533.                     frontView01.applyProperties({left : 0});
  534.                 }
  535.             }
  536.         }
  537.         moved = false;
  538.     }
  539.    
  540.     function touchCancel(e, side)
  541.     {
  542.         Ti.API.info('cancelado');
  543.         touchEnd(e, side);
  544.     }
  545.  
  546.  
  547.     frontView01.addEventListener('touchstart', function(e) {
  548.         touchStart(e);
  549.     });
  550.  
  551.     frontView01.addEventListener('touchmove', function(e) {
  552.         touchMove(e, 1);
  553.     });
  554.  
  555.     frontView01.addEventListener('touchend', function(e) {
  556.         touchEnd(e, 1);
  557.     });
  558.    
  559.     frontView01.addEventListener('touchcancel', function(e){
  560.         touchCancel(e, 1);
  561.     });
  562.  
  563.     frontView03.addEventListener('touchstart', function(e) {
  564.         touchStart(e);
  565.     });
  566.  
  567.     frontView03.addEventListener('touchmove', function(e) {
  568.         touchMove(e, -1);
  569.     });
  570.  
  571.     frontView03.addEventListener('touchend', function(e) {
  572.         touchEnd(e, -1);
  573.     });
  574.    
  575.     frontView03.addEventListener('touchcancel', function(e){
  576.         touchCancel(e, -1);
  577.     });
  578.  
  579.     buttonOptions01_Comment.add(buttonOptions01label_Comment);
  580.     buttonOptions01_Comment.add(dislikeHeartView);
  581.     buttonOptions01_Comment.add(dislikesMessage);
  582.  
  583.     buttonOptions02_Comment.add(dislikesLabel);
  584.     buttonOptions02_Comment.add(separatorLabel);
  585.     buttonOptions02_Comment.add(likesLabel);
  586.  
  587.     buttonOptions03_Comment.add(buttonOptions03label_Comment);
  588.     buttonOptions03_Comment.add(likeHeartView);
  589.     buttonOptions03_Comment.add(likesMessage);
  590.  
  591.     viewOptionsLayout_Comment.add(buttonOptions01_Comment);
  592.     viewOptionsLayout_Comment.add(frontView01);
  593.     viewOptionsLayout_Comment.add(buttonOptions02_Comment);
  594.     viewOptionsLayout_Comment.add(buttonOptions03_Comment);
  595.     viewOptionsLayout_Comment.add(frontView03);
  596.  
  597.     //*******************
  598.  
  599.     containerView.add(viewOptionsLayout_Comment);
  600.  
  601.     that.comment = containerView;
  602.    
  603.     return that;
  604.  
  605. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement