Advertisement
erupnu

Enyo 1.0 URL/Email Kind

Feb 6th, 2012
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enyo.kind({
  2.     name: "FC.UrlEmail",
  3.     kind: enyo.Component,
  4.    
  5.     published: {
  6.         message: ""
  7.     },
  8.    
  9.     events: {
  10.         onConfirm: ""
  11.     },
  12.    
  13.     components: [
  14.         {kind: "PalmService", service: "palm://com.palm.applicationManager/", components: [
  15.             {name: "launchApp", method: "launch"},
  16.             {name: "openApp", method: "open"}
  17.         ]}
  18.     ],
  19.    
  20.     create: function( ){
  21.         this.inherited(arguments);
  22.     },
  23.    
  24.     gotoUrl: function(url){
  25.         console.log("Going to URL: " + url);
  26.        
  27.         //--> Data checks
  28.         if (!url){
  29.             console.log("No URL to go to...");
  30.             return false;
  31.         }
  32.        
  33.         //--> Determine which OS and which method to use
  34.         if (FlashCards.fetchAppInfo.os == "ios" || FlashCards.fetchAppInfo.os == "android"){
  35.             //--> iOS & Android Child Browser
  36.             window.plugins.childBrowser.showWebPage(url);
  37.            
  38.             //--> Close app store launches after 3 seconds
  39.             if (Left(url, 23) == "http://itunes.apple.com"){
  40.                 setTimeout(function(){
  41.                     window.plugins.childBrowser.close();
  42.                 }, 3000);
  43.             }
  44.         }else if (FlashCards.fetchAppInfo.os == "chrome" || FlashCards.fetchAppInfo.os == "web" || FlashCards.fetchAppInfo.os == "intel"){
  45.             //--> For web apps
  46.             window.open(url,'_newtab','');
  47.         }else{
  48.             //--> webOS method
  49.             this.$.openApp.call(
  50.                 {target: url}
  51.             );
  52.         }
  53.     },
  54.    
  55.     gotoEmail: function(subject, message, to, cc, bcc, html){
  56.         console.log("Sending Email to: " + to + ", " + html);
  57.         //--> Data checks
  58.         if (!to){
  59.             to = "";   
  60.         }
  61.         if (!cc){
  62.             cc = "";   
  63.         }
  64.         if (!bcc){
  65.             bcc = "";  
  66.         }
  67.         if (!html){
  68.             html = false;  
  69.         }
  70.        
  71.         //--> Determine which OS and which method to use
  72.         if (FlashCards.fetchAppInfo.os == "ios"){
  73.             //--> iOS & Android Child Browser
  74.             window.plugins.emailComposer.showEmailComposer(subject, message, to, cc, bcc, html);
  75.         }else if (FlashCards.fetchAppInfo.os == "chrome" || FlashCards.fetchAppInfo.os == "web" || FlashCards.fetchAppInfo.os == "intel" || FlashCards.fetchAppInfo.os == "android"){
  76.             //--> For web apps
  77.             window.location = "mailto:" + to + "?subject=" + subject + "&body=" + message;
  78.         }else{
  79.             //--> webOS method
  80.             this.$.openApp.call({
  81.                 id: "com.palm.app.email",
  82.                 params: {
  83.                     summary: subject,
  84.                     text: message
  85.                 }
  86.             });
  87.         }
  88.     },
  89.  
  90.     gotoText: function(to, message){
  91.         console.log("Sending Text Message: " + message);
  92.         //--> Data checks
  93.         if (!to){
  94.             to = "";   
  95.         }
  96.        
  97.         //--> Determine which OS and which method to use
  98.         if (FlashCards.fetchAppInfo.os == "ios"){
  99.             //--> iOS & Android Child Browser
  100.             window.plugins.smsComposer.showSMSComposer(to, message);
  101.         }else if (FlashCards.fetchAppInfo.os == "android"){
  102.             //--> iOS & Android Child Browser
  103.             window.plugins.sms.send(to, message);
  104.         }else if (FlashCards.fetchAppInfo.os == "chrome" || FlashCards.fetchAppInfo.os == "web" || FlashCards.fetchAppInfo.os == "intel"){
  105.             //--> For web apps
  106.             //window.open(url,'','');
  107.             alert("Sorry, messaging is not enabled for your platform");
  108.         }else{
  109.             //--> webOS method
  110.             this.$.openApp.call({
  111.                 id: "com.palm.app.messaging",
  112.                 params: {
  113.                     messageText: message
  114.                 }
  115.             });
  116.         }
  117.     },
  118.  
  119.  
  120.     gotoFacebookShare: function(url, message){
  121.         console.log("Sharing on Facebook: " + url + ", " + message);
  122.         //--> Determine which OS and which method to use
  123.         if (FlashCards.fetchAppInfo.os == "webos"){
  124.             //--> webOS method
  125.             this.$.launchApp.call({
  126.                 id: "com.palm.app.enyo-facebook",
  127.                 params: {
  128.                     type: "status", statusText: message
  129.                 }
  130.             });
  131.         }else{
  132.             //--> Other method
  133.             this.gotoUrl("http://www.facebook.com/sharer.php?u=" + Url.encode(url) + "&t=" + Url.encode(message));
  134.         }
  135.     },
  136.  
  137.     //--> Processes a click on any object and looks for http:// and mailto:
  138.     processClick: function(inSender, inEvent){
  139.         //--> No need to do this on webOS
  140.         if (FlashCards.fetchAppInfo.os != "webos"){
  141.             var emailEvent = this.extractEmailFromEvent(inEvent);
  142.             if (emailEvent.to != ""){
  143.                 //console.log("Found an Email: " + emailEvent.to);
  144.                 this.gotoEmail(emailEvent.subject, emailEvent.body, emailEvent.to, emailEvent.cc, emailEvent.bcc, false);
  145.                 return;
  146.             }
  147.             if (this.extractLinkFromEvent(inEvent) != ""){
  148.                 //console.log("Found a URL: " + this.extractLinkFromEvent(inEvent));
  149.                 this.gotoUrl(this.extractLinkFromEvent(inEvent));
  150.                 return;
  151.             }
  152.         }
  153.     },
  154.    
  155.     //--> Extracts a link from a click on a target
  156.     extractLinkFromEvent: function(obj){
  157.         if (!obj || isUndefined(obj)){
  158.             return "";
  159.         }
  160.        
  161.         if (obj && obj.target && obj.target.parentNode && Left(obj.target.parentNode, 4).toLowerCase() == "http"){
  162.             console.log("extractLinkFromEvent 1");
  163.             return String(obj.target.parentNode);
  164.         }else if (obj && obj.target && obj.target.href){
  165.             console.log("extractLinkFromEvent 2");
  166.             return obj.target.href;
  167.         //}else if (obj && obj.srcElement){
  168.         //  console.log(obj.srcElement);
  169.         //  console.log("extractLinkFromEvent 3");
  170.         //  return obj.srcElement;
  171.         //}else if (obj.target && obj.target.innerHTML){
  172.         //  return getLinks(obj.target.innerHTML);
  173.         }else{
  174.             console.log("extractLinkFromEvent 4");
  175.             return ""; 
  176.         }
  177.     },
  178.    
  179.     //--> Extracts a link from a click on a target
  180.     extractEmailFromEvent: function(obj){
  181.         try{
  182.             if (!obj || isUndefined(obj)){
  183.                 return this.extractPartsFromMailto("");
  184.             }
  185.            
  186.             if (obj && isString(obj) && Left(obj, 6).toLowerCase() == "mailto"){
  187.                 //console.log("****** extractEmailFromEvent 1: " + String(obj));
  188.                 return this.extractPartsFromMailto(String(obj));
  189.             }else if (obj && obj.target && obj.target.parentNode && Left(obj.target.parentNode, 6).toLowerCase() == "mailto"){
  190.                 //console.log("****** extractEmailFromEvent 1: " + String(obj.target.parentNode));
  191.                 return this.extractPartsFromMailto(String(obj.target.parentNode));
  192.             }else if (obj && obj.target && obj.target.parentElement && this.findEmailAddresses(obj.target.parentElement) != ""){
  193.                 //console.log("****** extractEmailFromEvent 2");
  194.                 return this.extractPartsFromMailto(obj.target.parentElement)
  195.             }else if (obj.target && obj.target.parentElement.href && this.findEmailAddresses(obj.target.parentElement.href) != ""){
  196.                 //console.log("****** extractEmailFromEvent 3");
  197.                 return this.extractPartsFromMailto(obj.target.parentElement.href)
  198.             }else if (obj && obj.target && obj.target.href && this.findEmailAddresses(obj.target.href) != ""){
  199.                 //console.log("****** extractEmailFromEvent 4");
  200.                 return this.extractPartsFromMailto(obj.target.href)
  201.             }else if (obj && obj.srcElement && this.findEmailAddresses(obj.srcElement) != ""){
  202.                 //console.log("****** extractEmailFromEvent 5");
  203.                 return this.extractPartsFromMailto(obj.srcElement)
  204.             //}else if (obj.target && obj.target.innerHTML){
  205.             //  return getLinks(obj.target.innerHTML);
  206.             //}else if (obj.target && obj.target.innerHTML){
  207.             //  return getLinks(obj.target.innerHTML);
  208.             }else{
  209.                 //console.log("****** extractEmailFromEvent 6");
  210.                 return this.extractPartsFromMailto("");
  211.             }
  212.         }catch(e){
  213.             console.log("extractEmailFromEvent Error: " + e);
  214.             return this.extractPartsFromMailto("");
  215.         }
  216.     },
  217.    
  218.     //--> Extracts a link from a click on a target
  219.     extractPartsFromMailto: function(inText){
  220.         try{
  221.             if (!inText || inText == null || isUndefined(inText)){
  222.                 inText = "";
  223.             }
  224.             inText = inText.toString();
  225.            
  226.             var textObj = inText.split("?");
  227.             var outObj = {"to": "", "subject": "", "body": "", "cc": "", "bcc": "", "inText": inText}
  228.             outObj.to = this.findEmailAddresses(inText);
  229.    
  230.    
  231.             if (textObj.length > 0 && !isUndefined(textObj[1])){
  232.                 var partsObj = textObj[1].split("&")
  233.                 for (i=0; i<partsObj.length; i++){
  234.                     var thisObj = partsObj[i].split("=");
  235.                     if (thisObj.length > 0 && !isUndefined(thisObj[0])){
  236.                         if (thisObj[0].toLowerCase() == "subject"){
  237.                             outObj.subject = Url.decode(thisObj[1]);
  238.                         }else if (thisObj[0].toLowerCase() == "body"){
  239.                             outObj.body = Url.decode(thisObj[1]);
  240.                         }else if (thisObj[0].toLowerCase() == "cc"){
  241.                             outObj.cc = Url.decode(thisObj[1]);
  242.                         }else if (thisObj[0].toLowerCase() == "bcc"){
  243.                             outObj.bcc = Url.decode(thisObj[1]);
  244.                         }
  245.                     }
  246.                 }
  247.             }
  248.            
  249.             return outObj;
  250.         }catch(e){
  251.             console.log("extractPartsFromMailto Error: " + e);
  252.             return outObj;
  253.         }
  254.     },
  255.    
  256.     findEmailAddresses: function(StrObj) {
  257.         try{
  258.             if (!StrObj || StrObj == null || isUndefined(StrObj)){
  259.                 return "";
  260.             }
  261.            
  262.             var separateEmailsBy = ", ";
  263.             var email = ""; // if no match, use this
  264.             var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
  265.             if (emailsArray){
  266.                 for (var i = 0; i < emailsArray.length; i++) {
  267.                     //if (i != 0){
  268.                     //  email += separateEmailsBy;
  269.                     //}
  270.                     //email += emailsArray[i];
  271.                     email = emailsArray[i];
  272.                 }
  273.             }
  274.             return email;
  275.         }catch(e){
  276.             return "";
  277.         }
  278.     }
  279. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement