Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #7 from DinoChiesa/master

Fix prettyDate().  Also, provide a index.php file to do CORS.
  • Loading branch information...
commit 77ce760c48a7677076cc7ffa0151c6926d2eb507 2 parents f25d647 + a942523
Rod Simpson authored September 19, 2012
14  index.html
@@ -10,10 +10,14 @@
10 10
         <script src="js/app.js" type="text/javascript"></script>
11 11
         <script src="js/usergrid.appSDK.js" type="text/javascript"></script>
12 12
         <script type="text/javascript">
13  
-
14  
-            //first set the org / app path
15  
-            Usergrid.ApiClient.init('Apigee', 'messageeapp'); //<== update this line (organization name, application name)
16  
-
  13
+          try {
  14
+            // first, set the org and app path.
  15
+            Usergrid.ApiClient.init('Apigee', 'messageeapp');
  16
+          }
  17
+          catch (e) {
  18
+            $('#login-section-error')
  19
+              .html('There was an error initializing: ' + JSON.stringify(e));
  20
+          }
17 21
         </script>
18 22
     </head>
19 23
     <body>
@@ -26,7 +30,7 @@
26 30
             <h3>Messagee is a sample messaging app, like Twitter</h3>
27 31
             <p>
28 32
               <h4>Log in using your Messagee account.</h4>
29  
-              Don't have an account? use our test account (username: testuser / password: testuser), or create a new one!
  33
+              Don&apos;t have an account? use our test account (username: testuser / password: testuser), or create a new one!
30 34
             </p>
31 35
             <span id="login-section-error"></span>
32 36
             <form name="form-login" id="form-login">
41  index.php
... ...
@@ -0,0 +1,41 @@
  1
+<?php
  2
+//
  3
+// CORS allows HTML5 apps running in modern browsers
  4
+// to make valid cross-origin requests.
  5
+//
  6
+// For more on CORS, see
  7
+// http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
  8
+//
  9
+// This PHP module sets the appropriate headers in the webserver
  10
+// response, to tell browsers that it's ok to connect to
  11
+// api.usergrid.com directly from script.
  12
+//
  13
+// Another way to add this header is to do so via Webserver
  14
+// configuration.  You can use one or the other, but you do not need
  15
+// both.
  16
+//
  17
+// NB: This file will work only on webservers that have PHP enabled.
  18
+//
  19
+
  20
+// Specify domains from which requests are allowed
  21
+header('Access-Control-Allow-Origin: https://api.usergrid.com');
  22
+
  23
+// Specify which request methods are allowed
  24
+header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  25
+
  26
+// Additional headers which may be sent along with the CORS request
  27
+// The X-Requested-With header allows jQuery requests to go through
  28
+header('Access-Control-Allow-Headers: X-Requested-With');
  29
+
  30
+// Set the age to 1 day to improve speed/caching.
  31
+header('Access-Control-Max-Age: 86400');
  32
+
  33
+// Exit early so the page isn't fully loaded for options requests
  34
+if (strtolower($_SERVER['REQUEST_METHOD']) == 'options') {
  35
+    exit();
  36
+}
  37
+
  38
+// delegate to the view
  39
+include './index.html';
  40
+
  41
+?>
104  js/app.js
@@ -43,25 +43,11 @@ $(document).ready(function () {
43 43
   userFeed.setQueryParams({"ql":"order by created desc"});
44 44
 
45 45
   //bind the various click events
46  
-  $('#btn-login').bind('click', function() {
47  
-    login();
48  
-  });
49  
-
50  
-  $('#btn-show-page-update-account').bind('click', function() {
51  
-     pageUpdateAccount();
52  
-  });
53  
-
54  
-  $('#btn-logout').bind('click', function() {
55  
-    logout();
56  
-  });
57  
-
58  
-  $('#btn-create-new-account').bind('click', function() {
59  
-    createNewUser();
60  
-  });
61  
-
62  
-  $('#btn-update-account').bind('click', function() {
63  
-    updateUser();
64  
-  });
  46
+  $('#btn-login').bind('click', login);
  47
+  $('#btn-show-page-update-account').bind('click', pageUpdateAccount);
  48
+  $('#btn-logout').bind('click', logout);
  49
+  $('#btn-create-new-account').bind('click', createNewUser);
  50
+  $('#btn-update-account').bind('click', updateUser);
65 51
 
66 52
   $('#btn-close').bind('click', function() {
67 53
     //turn the reload timer on
@@ -84,13 +70,8 @@ $(document).ready(function () {
84 70
     }
85 71
   });
86 72
 
87  
-  $('#btn-show-my-feed').bind('click', function() {
88  
-    showMyFeed();
89  
-  });
90  
-
91  
-  $('#btn-show-full-feed').bind('click', function() {
92  
-    showFullFeed();
93  
-  });
  73
+  $('#btn-show-my-feed').bind('click', showMyFeed);
  74
+  $('#btn-show-full-feed').bind('click', showFullFeed);
94 75
 
95 76
   $('#btn-show-create-message').bind('click', function() {;
96 77
     $("#content").val('');
@@ -99,9 +80,7 @@ $(document).ready(function () {
99 80
     window.clearInterval( feedReloadTimer );
100 81
   });
101 82
 
102  
-  $('#post-message').bind('click', function() {
103  
-    postMessage();
104  
-  });
  83
+  $('#post-message').bind('click', postMessage);
105 84
 
106 85
   /**
107 86
    *  function to log in the app user.  The API returns a token,
@@ -162,7 +141,7 @@ $(document).ready(function () {
162 141
   function pageUpdateAccount(){
163 142
     //turn the reload timer off so we don't get interrupted during the update
164 143
     window.clearInterval( feedReloadTimer );
165  
-    
  144
+
166 145
     $("#update-name").val(appUser.get('name'));
167 146
     $("#update-email").val(appUser.get('email'));
168 147
     $("#update-username").val(appUser.get('username'));
@@ -186,10 +165,10 @@ $(document).ready(function () {
186 165
     $("#new-username").removeClass('error');
187 166
     $("#new-password").removeClass('error');
188 167
 
189  
-    var name     = $("#new-name").val();
190  
-    var email    = $("#new-email").val();
191  
-    var username = $("#new-username").val();
192  
-    var password = $("#new-password").val();
  168
+    var name     = $("#new-name").val(),
  169
+        email    = $("#new-email").val(),
  170
+        username = $("#new-username").val(),
  171
+        password = $("#new-password").val();
193 172
 
194 173
     if (Usergrid.validation.validateName(name, function (){
195 174
           $("#new-name").focus();
@@ -202,8 +181,9 @@ $(document).ready(function () {
202 181
           $("#new-username").addClass('error');})  &&
203 182
          Usergrid.validation.validatePassword(password, function (){
204 183
           $("#new-password").focus();
205  
-          $("#new-password").addClass('error');})  ) {
206  
-      appUser = new Usergrid.Entity('users'); //make sure we have a clean user, and then add the data
  184
+           $("#new-password").addClass('error');})  ) {
  185
+      // make sure we have a clean user, and then add the data
  186
+      appUser = new Usergrid.Entity('users');
207 187
       appUser.set({"name":name,"username":username,"email":email,"password":password});
208 188
       appUser.save(
209 189
         function () {
@@ -217,7 +197,8 @@ $(document).ready(function () {
217 197
           $('#login-section-error').html('There was an error creating the new user.');
218 198
         }
219 199
       );
220  
-    } else {
  200
+    }
  201
+      else {
221 202
 
222 203
     }
223 204
   }
@@ -274,7 +255,7 @@ $(document).ready(function () {
274 255
            $('#user-message-update-account').html('<strong>There was an error updating your account</strong>');
275 256
         }
276 257
       );
277  
-    } 
  258
+    }
278 259
   }
279 260
 
280 261
   /**
@@ -326,7 +307,7 @@ $(document).ready(function () {
326 307
           $("#next-btn-container").hide();
327 308
         }
328 309
       },
329  
-      function(){ 
  310
+      function(){
330 311
         $("#messages-list").html("There was an error getting the messages!");
331 312
       }
332 313
     );
@@ -358,14 +339,14 @@ $(document).ready(function () {
358 339
     }
359 340
     //make sure we are on the messages page
360 341
     window.location = "#page-messages-list";
361  
-    
  342
+
362 343
     fullFeedView = true;
363 344
     $('#btn-show-full-feed').addClass('ui-btn-up-c');
364 345
     $('#btn-show-my-feed').removeClass('ui-btn-up-c');
365 346
 
366 347
     fullActivityFeed.get(
367 348
       function(){
368  
-        drawMessages(fullActivityFeed);        
  349
+        drawMessages(fullActivityFeed);
369 350
         if (fullActivityFeed.hasPreviousPage()) {
370 351
           $("#previous-btn-container").show();
371 352
         } else {
@@ -400,16 +381,15 @@ $(document).ready(function () {
400 381
     var usersToBind = [];
401 382
     feed.resetEntityPointer();
402 383
     while(feed.hasNextEntity()) {
403  
-      var message = feed.getNextEntity();
404  
-      var created = message.get('created');
405  
-      var content = message.get('content');
406  
-      var actor = message.get('actor');
407  
-      var name = actor.displayName;
408  
-      if (!name) { name = 'Anonymous'; }
409  
-      var username = actor.displayName;
410  
-
411  
-      var email = '';
412  
-      var imageUrl = '';
  384
+      var message = feed.getNextEntity(),
  385
+        created = message.get('created'),
  386
+        content = message.get('content'),
  387
+        email = '',
  388
+        imageUrl = '',
  389
+        actor = message.get('actor'),
  390
+        name = actor.displayName || 'Anonymous',
  391
+        username = actor.displayName;
  392
+
413 393
       if ('email' in actor) {
414 394
         email = actor.email;
415 395
         imageUrl = 'http://www.gravatar.com/avatar/' + MD5(email.toLowerCase()) + '?s=' + 50;
@@ -423,12 +403,7 @@ $(document).ready(function () {
423 403
         imageUrl = 'http://www.gravatar.com/avatar/' + MD5('rod@apigee.com') + '?s=' + 50;
424 404
       }
425 405
 
426  
-      var initialDate = new Date(created);
427  
-      var offset = initialDate.getTimezoneOffset() * 60000;
428  
-      var utcDate = created - offset;
429  
-      var date = new Date(utcDate);
430  
-      var isotime = date.toISOString();
431  
-      formattedTime = prettyDate(isotime);
  406
+      formattedTime = prettyDate(created);
432 407
 
433 408
       html += '<div style="border-bottom: 1px solid #444; padding: 5px; min-height: 60px;"><img src="' + imageUrl + '" style="border none; height: 50px; width: 50px; float: left;padding-right: 10px"> ';
434 409
       html += '<span style="float: right">'+formattedTime+'</span>';
@@ -543,7 +518,7 @@ $(document).ready(function () {
543 518
           //reset the feed object so when we view it again, we will get the latest feed
544 519
           userFeed.clearQuery();
545 520
           showMyFeed();
546  
-        }        
  521
+        }
547 522
         window.location = "#page-messages-list";
548 523
       },
549 524
       function () {
@@ -563,12 +538,11 @@ $(document).ready(function () {
563 538
    * Licensed under the MIT and GPL licenses.
564 539
    */
565 540
 
566  
-  // Takes an ISO time and returns a string representing how
567  
-  // long ago the date represents.
568  
-  function prettyDate(time){
569  
-    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," "));
570  
-    var diff = (((new Date()).getTime() - date.getTime()) / 1000);
571  
-    var day_diff = Math.floor(diff / 86400);
  541
+  // Takes a numeric date value (in seconds) and returns a string
  542
+  // representing how long ago the date represents.
  543
+  function prettyDate(createdDateValue) {
  544
+    var diff = (((new Date()).getTime() - createdDateValue) / 1000),
  545
+      day_diff = Math.floor(diff / 86400);
572 546
 
573 547
     if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
574 548
       return 'just now';
@@ -613,4 +587,4 @@ $(document).ready(function () {
613 587
 
614 588
 });
615 589
 
616  
-//abudda abudda abudda that's all folks!
  590
+//abudda abudda abudda that's all folks!

0 notes on commit 77ce760

Please sign in to comment.
Something went wrong with that request. Please try again.