You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by mw...@apache.org on 2012/08/30 20:13:04 UTC

[2/3] git commit: [app] Refactor report function for clarity.

[app] Refactor report function for clarity.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-app-hello-world/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-app-hello-world/commit/46d47900
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-app-hello-world/tree/46d47900
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-app-hello-world/diff/46d47900

Branch: refs/heads/master
Commit: 46d47900639995d83d3463ec2073646a63046294
Parents: 7e8ddbc
Author: Michael Brooks <mi...@michaelbrooks.ca>
Authored: Thu Aug 30 10:49:06 2012 -0700
Committer: Michael Brooks <mi...@michaelbrooks.ca>
Committed: Thu Aug 30 10:49:06 2012 -0700

----------------------------------------------------------------------
 www/css/index.css  |   11 ++++++-----
 www/index.html     |    6 +++---
 www/js/index.js    |   19 +++++++++----------
 www/spec/helper.js |    4 ++++
 www/spec/index.js  |   26 +++++++++++++-------------
 5 files changed, 35 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-app-hello-world/blob/46d47900/www/css/index.css
----------------------------------------------------------------------
diff --git a/www/css/index.css b/www/css/index.css
index bacd6e3..f1f9d76 100644
--- a/www/css/index.css
+++ b/www/css/index.css
@@ -78,8 +78,7 @@ h1 {
     text-align:center;
 }
 
-.status {
-    background-color:#333333;
+.event {
     border-radius:4px;
     -webkit-border-radius:4px;
     color:#FFFFFF;
@@ -88,11 +87,13 @@ h1 {
     padding:2px 0px;
 }
 
-.status.complete {
-    background-color:#4B946A;
+.event.listening {
+    background-color:#333333;
+    display:block;
 }
 
-.hide {
+.event.received {
+    background-color:#4B946A;
     display:none;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-app-hello-world/blob/46d47900/www/index.html
----------------------------------------------------------------------
diff --git a/www/index.html b/www/index.html
index 9bc12df..92484ad 100644
--- a/www/index.html
+++ b/www/index.html
@@ -28,9 +28,9 @@
     <body>
         <div class="app">
             <h1>Apache Cordova</h1>
-            <div id="deviceready">
-                <p class="status pending blink">Connecting to Device</p>
-                <p class="status complete blink hide">Device is Ready</p>
+            <div id="deviceready" class="blink">
+                <p class="event listening">Connecting to Device</p>
+                <p class="event received">Device is Ready</p>
             </div>
         </div>
         <script type="text/javascript" src="cordova-2.1.0.js"></script>

http://git-wip-us.apache.org/repos/asf/incubator-cordova-app-hello-world/blob/46d47900/www/js/index.js
----------------------------------------------------------------------
diff --git a/www/js/index.js b/www/js/index.js
index e62b652..0a0e8c6 100644
--- a/www/js/index.js
+++ b/www/js/index.js
@@ -26,17 +26,16 @@ var app = {
     onDeviceReady: function() {
         // This is an event handler function, which means the scope is the event.
         // So, we must explicitly called `app.report()` instead of `this.report()`.
-        app.report('deviceready');
+        app.receivedEvent('deviceready');
     },
-    report: function(id) {
-        // Report the event in the console
-        console.log("Report: " + id);
+    receivedEvent: function(id) {
+        var parentElement = document.getElementById(id);
+        var listeningElement = parentElement.querySelector('.listening');
+        var receivedElement = parentElement.querySelector('.received');
 
-        // Toggle the state from "pending" to "complete" for the reported ID.
-        // Accomplished by adding .hide to the pending element and removing
-        // .hide from the complete element.
-        document.querySelector('#' + id + ' .pending').className += ' hide';
-        var completeElem = document.querySelector('#' + id + ' .complete');
-        completeElem.className = completeElem.className.split('hide').join('');
+        listeningElement.setAttribute('style', 'display:none;');
+        receivedElement.setAttribute('style', 'display:block;');
+
+        console.log('Received Event: ' + id);
     }
 };

http://git-wip-us.apache.org/repos/asf/incubator-cordova-app-hello-world/blob/46d47900/www/spec/helper.js
----------------------------------------------------------------------
diff --git a/www/spec/helper.js b/www/spec/helper.js
index 821604b..929f776 100644
--- a/www/spec/helper.js
+++ b/www/spec/helper.js
@@ -25,5 +25,9 @@ var helper = {
         var e = document.createEvent('Event');
         e.initEvent(name, true, true);
         obj.dispatchEvent(e);
+    },
+    getComputedStyle: function(querySelector, property) {
+        var element = document.querySelector(querySelector);
+        return window.getComputedStyle(element).getPropertyValue(property);
     }
 };

http://git-wip-us.apache.org/repos/asf/incubator-cordova-app-hello-world/blob/46d47900/www/spec/index.js
----------------------------------------------------------------------
diff --git a/www/spec/index.js b/www/spec/index.js
index 3693d6b..20f8be5 100644
--- a/www/spec/index.js
+++ b/www/spec/index.js
@@ -37,31 +37,31 @@ describe('app', function() {
 
     describe('onDeviceReady', function() {
         it('should report that it fired', function() {
-            spyOn(app, 'report');
+            spyOn(app, 'receivedEvent');
             app.onDeviceReady();
-            expect(app.report).toHaveBeenCalledWith('deviceready');
+            expect(app.receivedEvent).toHaveBeenCalledWith('deviceready');
         });
     });
 
-    describe('report', function() {
+    describe('receivedEvent', function() {
         beforeEach(function() {
             var el = document.getElementById('stage');
             el.innerHTML = ['<div id="deviceready">',
-                            '    <p class="status pending">Pending</p>',
-                            '    <p class="status complete hide">Complete</p>',
+                            '    <p class="event listening">Listening</p>',
+                            '    <p class="event received">Received</p>',
                             '</div>'].join('\n');
         });
 
-        it('should show the completion state', function() {
-            app.report('deviceready');
-            var el = document.querySelector('#deviceready .complete:not(.hide)');
-            expect(el).toBeTruthy();
+        it('should hide the listening element', function() {
+            app.receivedEvent('deviceready');
+            var displayStyle = helper.getComputedStyle('#deviceready .listening', 'display');
+            expect(displayStyle).toEqual('none');
         });
 
-        it('should hide the pending state', function() {
-            app.report('deviceready');
-            var el = document.querySelector('#deviceready .pending.hide');
-            expect(el).toBeTruthy();
+        it('should show the received element', function() {
+            app.receivedEvent('deviceready');
+            var displayStyle = helper.getComputedStyle('#deviceready .received', 'display');
+            expect(displayStyle).toEqual('block');
         });
     });
 });