Added access key check with server
authorDaniel Davis <ddavis@w3.org>
Wed, 04 Sep 2013 16:39:58 +0900
changeset 50 a6ebae92b1e9
parent 46 2adb46f4fa0e
child 51 6ca4bfdb5788
Added access key check with server
js/w3c_talks.js
--- a/js/w3c_talks.js	Wed Sep 04 05:53:48 2013 +0000
+++ b/js/w3c_talks.js	Wed Sep 04 16:39:58 2013 +0900
@@ -49,7 +49,7 @@
     };
     
     // Add start box to top-right of page
-    function makeUI() {
+    function init() {
         // Create CSS for start box
         var style = document.createElement('style');
         style.innerHTML = '#w3c_talks {background:rgba(0, 0, 0, 0.7); border-radius:0 0 0 1em; color:#fff; display:inline-block; opacity:1; padding:0.5em 1em; position:absolute; right:0; text-align:center; top:0; z-index:1000;} #w3c_talks.hidden {opacity:0; -webkit-transition:3s; -moz-transition:3s; -ms-transition:3s; -o-transition:3s; transition:3s;} #w3c_talks_start {display:block; font-weight:bold; margin:0.5em; padding:0.5em 1em; width:100%;} #w3c_talks_start:hover, #w3c_talks_start:focus {color:blue; cursor:pointer;} #w3c_talks a {color:#e3e7ff; text-decoration:underline;} #w3c_talks a:hover, #w3c_talks:focus {text-decoration:none;}';
@@ -67,14 +67,39 @@
             event = event || window.event;
             event.preventDefault();
             var target = event.target || event.srcElement;
-            // Disable form button
-            target.children['w3c_talks_start'].value = 'Ready';
-            target.children['w3c_talks_start'].disabled = true;
-            // Set access key and hide UI
-            window.sessionStorage['accesskey'] = target.children['w3c_talks_key'].value;
-            document.getElementById('w3c_talks').className = 'hidden';
+            // Set access key and check with server
+            if (target.children['w3c_talks_key'].value) {
+                window.sessionStorage['accesskey'] = target.children['w3c_talks_key'].value;
+                var data = 'accesskey=' + window.sessionStorage['accesskey'];
+                doPost('http://talk.keio.w3.org/checkkey', data, checkKey);
+            } else {
+                alert('Please enter the access key');
+                target.children['w3c_talks_key'].focus();
+            }
         };
     }
+    
+    function checkKey(data) {
+        try {
+            var check = JSON.parse(data);
+            if (check.accepted === true) {
+                // Disable form button
+                var start = document.getElementById('w3c_talks');
+                start.value = 'Ready';
+                start.disabled = true;
+                document.getElementById('w3c_talks').className = 'hidden';
+                // Run the main "talking" function
+                startTalking();
+            } else {
+                throw 'WrongKey';
+            }
+        } catch(e) {
+            alert('The access key is wrong. Please try again');
+            var key_input = document.getElementById('w3c_talks_key');
+            key_input.value = '';
+            key_input.focus();
+        }
+    }
 
     function getMetaTag(name) {
         var tag = document.querySelector('[name=' + name + ']');
@@ -88,7 +113,7 @@
     }
     
     // Use XHR to send data to specified URL
-    function doPost(url, data) {
+    function doPost(url, data, successCallback) {
         var httpRequest;
         
         if (window.XMLHttpRequest) { // Modern browsers
@@ -114,6 +139,7 @@
                 } else {
                     // For debugging
                     console.log(httpRequest.response);
+                    if (successCallback) successCallback(httpRequest.response);
                 }
             }
         };
@@ -132,7 +158,6 @@
                     data += '&author=' + encodeURIComponent(author);
                     data += '&title=' + encodeURIComponent(title);
                     data += '&accesskey=' + window.sessionStorage['accesskey'];
-                    console.log(data);
                     doPost(url, data);
                 }
             } catch(e) {
@@ -141,36 +166,39 @@
         }
     }
     
-    makeUI();
-    
-    // The following is for Slidy:
-    if (window.w3c_slidy) {
-        // Store original "show_slide" method for later
-        w3c_slidy.__show_slide = w3c_slidy.show_slide;
+    // Hijack methods in slide templates and add ability to post notes
+    function startTalking() {
+        // The following is for Slidy:
+        if (window.w3c_slidy) {
+            // Store original "show_slide" method for later
+            w3c_slidy.__show_slide = w3c_slidy.show_slide;
+            
+            // Hijack "show_slide" method and add action to send note
+            w3c_slidy.show_slide = function(slide) {
+                this.__show_slide(slide);
+                
+                sendNote(slide);
+            };
+        }
         
-        // Hijack "show_slide" method and add action to send note
-        w3c_slidy.show_slide = function(slide) {
-            this.__show_slide(slide);
+        // The following is for Shower:
+        if (window.shower) {
+            var slides = document.querySelectorAll('.slide');
             
-            sendNote(slide);
-        };
+            // Store original "go" method for later
+            shower.__go = shower.go;
+            
+            // Hijack "go" method and add action to send note
+            shower.go = function(slideNumber, callback) {
+                this.__go(slideNumber, callback);
+                
+                if (slides.length > slideNumber) {
+                    sendNote(slides[slideNumber]);
+                }
+            };
+        }
     }
     
-    // The following is for Shower:
-    if (window.shower) {
-        var slides = document.querySelectorAll('.slide');
-        
-        // Store original "go" method for later
-        shower.__go = shower.go;
-        
-        // Hijack "go" method and add action to send note
-        shower.go = function(slideNumber, callback) {
-            this.__go(slideNumber, callback);
-            
-            if (slides.length > slideNumber) {
-                sendNote(slides[slideNumber]);
-            }
-        };
-    }
+    init();
     
 })();