[Bedework-commit] carddav r123 - trunk/clients/javascript/bwAddrbookClient

svnadmin at bedework.org svnadmin at bedework.org
Wed Sep 29 17:14:25 EDT 2010


Author: bleibson
Date: 2010-09-29 17:14:23 -0400 (Wed, 29 Sep 2010)
New Revision: 123

Added:
   trunk/clients/javascript/bwAddrbookClient/test2.html
Log:
checkpoint of some javascript that parses vcards from the server

Added: trunk/clients/javascript/bwAddrbookClient/test2.html
===================================================================
--- trunk/clients/javascript/bwAddrbookClient/test2.html	                        (rev 0)
+++ trunk/clients/javascript/bwAddrbookClient/test2.html	2010-09-29 21:14:23 UTC (rev 123)
@@ -0,0 +1,341 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<!-- 
+    Licensed to Jasig under one or more contributor license
+    agreements. See the NOTICE file distributed with this work
+    for additional information regarding copyright ownership.
+    Jasig licenses this file to you under the Apache License,
+    Version 2.0 (the "License"); you may not use this file
+    except in compliance with the License. You may obtain a
+    copy of the License at:
+    
+    http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on
+    an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied. See the License for the
+    specific language governing permissions and limitations
+    under the License.
+-->
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+  <head>
+    <title>Bedework Address Book Client</title>    
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+    <link href="resources/reset.css" rel="stylesheet" type="text/css"/>
+    <script type="text/javascript" src="resources/jquery/jquery-1.4.2.min.js"></script>
+    <script type="text/javascript" src="resources/jquery/jquery-ui-1.8.2.custom.min.js"></script>
+    <script type="text/javascript" src="resources/Math.uuid.js"></script>
+    <script type="text/javascript">
+      $(document).ready(function() {
+
+        var carddavUrl = "/ucarddav";
+        var userpath = "/user/";
+        var bookName = "/addressbook/"; 
+
+        // set on entry:
+        $("#visitListing").attr("href",carddavUrl + userpath + $("#userid").val() + bookName);
+        // change if the userid is changed:
+        $("#userid").change(function() {
+          $("#visitListing").attr("href",carddavUrl + userpath + $("#userid").val() + bookName);
+        });
+
+        $("#auth").click(function() {
+          var addrBookUrl = carddavUrl + userpath + $("#userid").val() + bookName;
+          $.ajax({
+            type: "get",
+            url: addrBookUrl,
+            dataType: "html",
+            success: function(responseData){
+              alert(responseData);            
+            },
+            error: function(msg) {
+              // there was a problem
+              alert(msg.statusText);
+            }
+          });
+        });  
+
+        $("#report").click(function() {
+          var addrBookUrl = carddavUrl + userpath + $("#userid").val() + bookName;
+          var content = '<?xml version="1.0" encoding="utf-8" ?><C:addressbook-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav"><D:prop><D:getetag/><C:address-data/></D:prop><C:filter></C:filter></C:addressbook-query>';
+          $.ajax({
+            type: "post",
+            url: addrBookUrl,
+            dataType: "xml",
+            processData: false,
+            data: content,
+            beforeSend: function(xhrobj) {
+              xhrobj.setRequestHeader("X-HTTP-Method-Override", "REPORT");
+              xhrobj.setRequestHeader("Depth", "1");
+              xhrobj.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
+              xhrobj.setRequestHeader("Authorization", "Basic");
+            },
+            success: parsexml,
+            error: function(msg) {
+              // there was a problem
+              alert(msg.statusText);
+            }
+          });
+        });
+
+       function parsexml(xml) {
+           $(xml).find("response").each(function() {
+              $(this).find("propstat").each(function() {
+                 $(this).find("prop").each(function() {
+                    $(this).find("[nodeName=C:address-data]").each(function() {
+                       parseVCardBlobIntoJson($(this).text());
+                    });
+                 });
+              });
+	   });
+         }
+
+
+        function attributeSpecifics (attribute) {
+           var returnArray=new Array();
+	   switch (attribute) {
+                   case "BEGIN":
+                   case "END":
+                    returnArray[0]=0;
+                    break;
+                  case "ADR":
+		    returnArray[0]=2;
+                    returnArray[1]="po-box,extended-address,street-address,locality,state,postal-code,country";
+                    break;
+                  default: 
+                    returnArray[0]=1;
+           }
+           return returnArray;
+         }
+	     
+
+        function parseVCardBlobIntoJson(blob) {
+           //each line ends in '\n'
+            var jsonObj = "{";
+            var lines =  blob.split('\n');
+            for (var i=0;i<lines.sort().length;i++) {
+              //each line is in the form of a key[;param;param]:value.  Sometimes the value contains colons, too.
+              if (lines[i] != "") {
+                var colonSplit = lines[i].split(':');
+
+                //split out the key and the paramaters
+                var semiColonSplit = colonSplit[0].split(';');
+                var attribute = semiColonSplit[0];
+                var attributeInfo = new Array();
+                attributeInfo = attributeSpecifics(attribute);
+                var attributeType = attributeInfo[0];
+
+                // This takes care of the VCARD:BEGIN and VCARD:END
+                if (attributeType == 0) {
+	          continue;
+                }
+
+                
+                //write out key and open array
+                jsonObj += '"' + attribute + '": [ {';
+
+                //locate any parameters in the key and write out the parameter array
+                jsonObj += '"params": [' 
+                for (var n=1;n<semiColonSplit.length;n++) {
+                    jsonObj += '{';
+                    var equalsSplit = semiColonSplit[n].split('=');
+
+		    // THIS ISN'T COMPLETE -- NEED to split on comma, too.
+                    jsonObj += '"parameter-name": "' + equalsSplit[0] + '",'
+                    jsonObj += '"parameter-value": "' + equalsSplit[1] + '"'
+                    jsonObj += '}';
+                    
+                    //add a comma between parameters (avoid adding at end)
+                    if (n != semiColonSplit.length - 1) {
+                       jsonObj += ',';
+                    }
+                }
+               
+                jsonObj += '],'
+
+                if (attributeType == 1) {
+
+                    //a single value
+                
+                   jsonObj += '"value": ';
+
+                   //write out part of value before the first colon -- generally all of it.
+                   jsonObj += '"' + colonSplit[1]
+
+                   //put back colon(s) and write out what's past the first colon
+                   for (k=2;k<colonSplit.length;k++) { 
+		    jsonObj += ':' + colonSplit[k];
+                  }
+                }
+                if (attributeType == 2) {
+
+                  //multiple named values
+
+                  //Will need to deal with the possibility of colons in the individual values.
+
+                  var attributeFieldNames = attributeInfo[1].split(',');
+                  var attributeFieldValues = colonSplit[1].split(';');
+                  jsonObj += '"values": [';
+                  for (y=0;y<attributeFieldNames.length;y++) {
+                     jsonObj += '"' + attributeFieldNames[y] + '": ';
+	             jsonObj += '"' + attributeFieldValues[y]  + '"';
+
+                     //add a comma between fields (avoid adding at end)
+                     if (y != attributeFieldNames.length - 1) {
+                       jsonObj += ',';
+                     }
+		  }
+              
+                  jsonObj += ']';
+                }
+                //close quote
+		jsonObj += '"}]';
+                ///////////////////////////////////	
+                  
+                //except for last key, value pair, add a comma.
+                if ((i != lines.length - 1) && (lines[i+1] != "")) {
+                  jsonObj += ',';
+                }
+              }
+            }
+            jsonObj += "}";
+            $("#listOut").append(jsonObj);
+            $("#listOut").append("<hr />");
+        }
+
+        
+       
+        $("#add").click(function() {
+          //var vcData = "BEGIN:VCARD\nFN:Venerable Bede\nUID:myveryhandcodeduid at mysite.edu6754841\nCLASS:PRIVATE\nCATEGORIES:Services\nREV:20100211T041750Z\nEMAIL;TYPE=PREF;TYPE=INTERNET:vbede at mysite.edu\nTEL;TYPE=HOME:+1 555 555-5555\nADR;TYPE=HOME:;;23 Toadstool Ln;Troy;NY;12180;\nN:Bede;Venerable;;;\nVERSION:4.0\nEND:VCARD";
+          //alert($("#FN").val() + "\n" + $("#UID").val() + "\n" + $("#EMAIL").val() + "\n" + $("#HOMEPHONE").val() + "\n" + $("#ADRHOMESTREET").val());
+          
+          var addrBookUrl = carddavUrl + userpath + $("#userid").val() + bookName;
+          var newUUID = "BwABC-" + Math.uuid();
+          
+          var vcData = "BEGIN:VCARD\n"
+          vcData += "FN:" + $("#FIRSTNAME").val() + " " + $("#LASTNAME").val() + "\n";
+          vcData += "UID:" + newUUID + "\n";
+          vcData += "CLASS:PRIVATE\n";
+          vcData += "REV:20100914T041750Z\n";
+          vcData += "EMAIL;TYPE=PREF;TYPE=INTERNET:" + $("#EMAIL").val() + "\n";
+          vcData += "TEL;TYPE=HOME:" + $("#HOMEPHONE").val() + "\n";  
+          vcData += "ADR;TYPE=HOME:;;" + $("#ADRHOMESTREET").val() + ";" + $("#ADRHOMECITY").val() + ";" +  $("#ADRHOMESTATE").val() + ";" + $("#ADRHOMEPOSTAL").val() + ";\n";
+          vcData += "N:" + $("#LASTNAME").val() + ";" + $("#FIRSTNAME").val() + ";;;\n"; 
+          vcData += "VERSION:4.0\nEND:VCARD";
+              
+          $.ajax({
+            type: "put",
+            url: addrBookUrl + newUUID + ".vcf",
+            data: vcData,
+            dataType: "text",
+            processData: false,
+            beforeSend: function(xhrobj) {
+              xhrobj.setRequestHeader("X-HTTP-Method-Override", "PUT");
+              xhrobj.setRequestHeader("If-None-Match", "*");
+              xhrobj.setRequestHeader("Authorization", "Basic");
+              xhrobj.setRequestHeader("Content-Type", "text/vcard");
+            },
+            success: function(responseData){
+              alert(responseData);            
+            },
+            error: function(msg) {
+              // there was a problem
+              alert(msg.statusText);
+            }
+          });
+        });
+
+
+        $("#delete").click(function() {
+          var addrBookUrl = carddavUrl + userpath + $("#userid").val() + bookName;
+          $.ajax({
+            type: "delete",
+            url: addrBookUrl + $("#DUID").val() + ".vcf",
+            dataType: "text",
+            beforeSend: function(xhrobj) {
+              xhrobj.setRequestHeader("X-HTTP-Method-Override", "DELETE");
+              xhrobj.setRequestHeader("Authorization", "Basic");
+            },
+            success: function(responseData){
+              alert(responseData);            
+            },
+            error: function(msg) {
+              // there was a problem
+              alert(msg.statusText);
+            }
+          });
+        });       
+        
+      });
+    </script>
+    <style type="text/css">
+      body {
+        font-family: Arial, Helvetica, sans-serif;
+      }
+      #header {
+        margin: 0;
+        padding: 0.5em;
+      }
+      #leftnav {
+        
+      }
+      #content {
+        
+      }
+      h1 {
+        font-size: 1.2em;
+      }
+      h3 {
+        margin: 1em 0;
+        font-size: 1em;
+        font-weight: bold;
+      }
+      p {
+        margin: 0 0 1em 0;
+      }
+      ul {
+        margin-top: 2em;
+      }
+      em {
+        font-style: italic;
+        font-size: 0.9em;
+      }
+    </style>
+  </head>
+  <body>
+    <div id="header">
+      <h1>Bedework Address Book</h1>
+      <p>
+        Test page for user <input type="text" id="userid" size="20" value="vbede"/> <a href="#" id="visitListing" target="_blank">Visit Listing</a><br/>
+        <em>Note: you must clear your authenticated sessions to switch between users.</em>
+      </p>
+      <p>
+        <button id="auth" type="button">Authenticate/Test</button>
+        <button id="report" type="button">Report</button>
+      </p>
+      <hr />
+      <ol id="listOut"></ol>
+      <hr/>
+      <h3>Add Contact</h3>
+      <div id="addForm">
+        First Name: <input type="text" size="60" value="" id="FIRSTNAME"/><br/>
+        Last Name: <input type="text" size="60" value="" id="LASTNAME"/><br/>
+        Email: <input type="text" size="60" value="" id="EMAIL"/><br/>
+        Home Phone: <input type="text" size="60" value="" id="HOMEPHONE"/><br/>
+        Home Address Street: <input type="text" size="60" value="" id="ADRHOMESTREET"/><br/>
+        Home Address City: <input type="text" size="60" value="" id="ADRHOMECITY"/><br/>
+        Home Address State: <input type="text" size="60" value="" id="ADRHOMESTATE"/><br/>
+        Home Address Postal Code: <input type="text" size="60" value="" id="ADRHOMEPOSTAL"/><br/>
+        <button id="add" type="button">Add Contact</button>
+      </div>
+      <hr/>
+      <h3>Delete Contact (by UID)</h3>
+      <div id="deleteForm">
+        UID: <input type="text" size="60" value="" id="DUID"/><br/>
+        <button id="delete" type="button">Delete Contact</button>
+      </div>
+    </div>
+  </body>
+</html>



More information about the Bedework-commit mailing list