]> git.r.bdr.sh - rbdr/r.bdr.sh/blobdiff - jekyll/js/unlimited_pizza.js
Remove unused files
[rbdr/r.bdr.sh] / jekyll / js / unlimited_pizza.js
index 2dc7a7c0001e6cc8bf446208a56abba182bbdce1..7517f6440d286d9198cf84f2d76cb799506dc468 100644 (file)
@@ -1,23 +1,35 @@
 'use strict';
 
-Class("UnlimitedPizza").inherits(Widget)({
+const UnlimitedPizza = {
 
-  /**
-   * Gets instance, creates it if not available.
-   */
-  instance : function getInstance(config) {
-    if (!this._mainInstance) {
-      this._mainInstance = new this(config);
-    }
-    return this._mainInstance;
+  _loadPepperoni() {
+
+    const pepperoniElement = document.querySelector('[data-application="pepperoni"]');
+
+    new Pepperoni({
+      element: pepperoniElement
+    });
   },
 
-  prototype : {
-    _loaded : false,
+  _loadGuestbook() {
+  
+  },
+
+  _loadGuestbookEntries() {
+  
+  }
+};
+
+window.addEventListener('load', UnlimitedPizza._onLoad.bind(UnlimitedPizza));
+
+Class("UnlimitedPizza").inherits(Widget)({
+    _fb : null,
 
     init : function (config) {
       Widget.prototype.init.call(this, config)
 
+      this._fb = new Firebase("https://guestbook-nsovocal.firebaseio.com");
+
       this._bindInternalEvents();
     },
 
@@ -25,32 +37,89 @@ Class("UnlimitedPizza").inherits(Widget)({
       this.bind('activate', this._onActivate.bind(this));
     },
 
-    _onActivate : function _activate() {
-      // If this is the first time activating it... then load
-      if (!this._loaded) {
-        this._load();
-      }
-    },
-
     /*
      * Loads everything.
      */
     _load : function _load() {
 
-      // Melty cheese is our header image widget.
-      this._loadMeltyCheese();
+      // Pepperoni is our recording widget.
+      this._loadPepperoni();
+
+      // Simple guestbook functionality
+      this._loadGuestbook();
+      this._loadPosts();
+    },
+
+    _loadGuestbook : function () {
+      var form = this.element.find('.guestbook-form form');
+      form.on('submit', function submitPost(ev) {
+        ev.preventDefault();
+
+        var formArray = form.serializeArray();
+        var recorder = this['recorder-0'];
+
+        recorder.finalize(function (buffer) {
+          var fb, arrayBuffer, fileReader;
+
+          if (buffer.size <= 44) {
+            alert("You need to record something.");
+            return;
+          }
+          if (formArray[0].value.length === 0) {
+            alert("You need a name.");
+            return;
+          }
+
+          fb = this._fb;
+
+          fileReader = new FileReader();
+          fileReader.onload = function() {
+            var binary, bytes, length, i;
+
+            binary = '';
+            bytes = new Uint8Array( this.result );
+            length = bytes.byteLength;
+            for (i = 0; i < length; i++) {
+              binary += String.fromCharCode( bytes[ i ] );
+            }
+
+            fb.push({
+              buffer: btoa(binary),
+              name: formArray[0].value
+            });
+            recorder.clear();
+          };
+          fileReader.readAsArrayBuffer(buffer);
+        }.bind(this));
+        return false;
+      }.bind(this))
     },
 
-    _loadMeltyCheese : function() {
-      this.element.find('.post-image').each(function (i, headerElement) {
+    _loadPosts : function () {
+      var feed = this.element.find('.guestbook-feed');
+
+      console.log("Loadin", feed.length);
+      if (feed.length > 0) {
+        this._fb.on('value', function (data) {
+          var posts, property, post;
 
-        // Create and activate
-        this.appendChild(new UnlimitedPizza.MeltyCheese({
-          element : $(headerElement),
-          name : 'header-' + i
-        }));
-        this['header-' + i].activate();
-      }.bind(this));
+          // Clear feed
+          feed.empty();
+          posts = data.val();
+
+          for (property in posts) {
+            if (posts.hasOwnProperty(property)) {
+              post = posts[property];
+
+              feed.append($('<li>\
+                    <div class="author">FROM: ' + post.name + '</div>\
+                    <div class="content">\
+                    <audio src="data:audio/wav;base64,' + post.buffer + '" controls></audio>\
+                    </div></li>'))
+            }
+          }
+        });
+      }
     }
   }
 });