Add subscription management features
authorChip Black <bytex64@bytex64.net>
Mon, 23 Apr 2012 23:14:54 +0000 (16:14 -0700)
committerChip Black <bytex64@bytex64.net>
Mon, 23 Apr 2012 23:18:32 +0000 (16:18 -0700)
www/js/blerg.js
www/jssrc/blerg/API.js
www/jssrc/blerg/User.js

index 8da3872..fac5371 100644 (file)
@@ -241,130 +241,6 @@ Pager.prototype.showRecord = function(r) {
 
 Pager.prototype.loadItems = function(from, to, continuation) { }
 
-
-// Object to render user pages
-function User(m) {
-    Pager.call(this);
-    this.username = m[1];
-    this.baseFrag = m[1];
-    this.permalink = (m[2] != 'p');
-    this.pageStart = parseInt(m[3]);
-}
-User.prototype = new Pager();
-User.prototype.constructor = User;
-
-User.prototype.updateState = function(m) {
-    if (m[1] != this.username)
-        return false;
-    
-    this.permalink = (m[2] != 'p');
-    this.pageStart = parseInt(m[3]);
-    this.show();
-
-    return true;
-}
-
-User.prototype.show = function() {
-    Pager.prototype.show.call(this);
-
-    $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this));
-    $('rss').show();
-    $('rsslink').href = '/rss/' + this.username;
-    $$('[name=user.reflink]').each(function(e) {
-        e.href = baseURL + '/#/ref/' + this.username;
-    }.bind(this));
-    $('usercontrols').show();
-
-    if (this.permalink && this.pageStart >= 0) {
-        this.showRecord(this.pageStart);
-    } else if (this.pageStart >= 0) {
-        this.showPageAt(this.pageStart);
-    } else {
-        this.reload();
-    }
-}
-
-User.prototype.hide = function() {
-    Pager.prototype.hide.call(this);
-    $('signup').hide();
-    $('rss').hide();
-    $('usercontrols').hide();
-}
-
-User.prototype.reload = function() {
-    this.pageStart = null;
-
-    $$('[name=user.subscribelink]').each(Element.hide);
-    $$('[name=user.unsubscribelink]').each(Element.hide);
-
-    if (loginStatus.loggedIn) {
-        new Ajax.Request(baseURL + '/feedinfo/' + this.username, {
-            method: 'post',
-            parameters: {
-                username: loginStatus.username
-            },
-            onSuccess: function(r) {
-                var json = r.responseText.evalJSON();
-                if (json.subscribed) {
-                    $$('[name=user.subscribelink]').each(Element.hide);
-                    $$('[name=user.unsubscribelink]').each(Element.show);
-                } else {
-                    $$('[name=user.subscribelink]').each(Element.show);
-                    $$('[name=user.unsubscribelink]').each(Element.hide);
-                }
-            }
-        });
-    }
-
-    new Ajax.Request(baseURL + '/info/' + this.username, {
-        method: 'get',
-        onSuccess: function(r) {
-            var response = r.responseText.evalJSON();
-            if (response) {
-                this.itemCount = parseInt(response.record_count);
-                this.showPageAt(this.itemCount - 1);
-            }
-        }.bind(this)
-    });
-}
-
-User.prototype.loadItems = function(from, to, continuation) {
-    if (to < 0)
-        return;
-
-    var url;
-    if (from != undefined && to != undefined) {
-        url = baseURL + '/get/' + this.username + '/' + from + '-' + to;
-        this.pageStart = to;
-    } else {
-        url = baseURL + '/get/' + this.username;
-    }
-
-    new Ajax.Request(url, {
-        method: 'get',
-        onSuccess: function(r) {
-            var records = r.responseText.evalJSON();
-            if (records && records.length > 0) {
-                records.each(function(v) {
-                    v.id = v.record;
-                    v.author = this.username;
-                    mangleRecord(v, recordTemplate);
-                }.bind(this));
-                this.addItems(records);
-                if (!this.pageStart)
-                    this.pageStart = records[0].recInt;
-            }
-            continuation();
-        }.bind(this),
-        onFailure: function(r) {
-            this.displayItems();
-        }.bind(this),
-        on404: function(r) {
-            displayError('User not found');
-        }
-    });
-}
-
 function displayError(msg) {
     items.innerText = msg;
 }
index 1a6b013..072eec5 100644 (file)
@@ -90,5 +90,50 @@ enyo.kind({
                        });
                }.bind(this));
                req.go();
-       }
+       },
+       getSubscriptionStatus: function(username) {
+               var req = new enyo.Ajax({
+                       url: baseURL + '/feedinfo/' + username,
+                       method: 'POST'
+               });
+               req.response(function(inSender, inResponse) {
+                       this.bubble('onSubscriptionStatus', {
+                               username: username,
+                               subscribed: inResponse.subscribed
+                       });
+               }.bind(this));
+               req.go({
+                       username: blerg.API.username
+               });
+       },
+       subscribe: function(username) {
+               var req = new enyo.Ajax({
+                       url: baseURL + '/subscribe/' + username,
+                       method: 'POST'
+               });
+               req.response(function(inSender, inResponse) {
+                       this.bubble('onSubscriptionStatus', {
+                               username: username,
+                               subscribed: inResponse.status == "success"
+                       });
+               }.bind(this));
+               req.go({
+                       username: blerg.API.username
+               });
+       },
+       unsubscribe: function(username) {
+               var req = new enyo.Ajax({
+                       url: baseURL + '/unsubscribe/' + username,
+                       method: 'POST'
+               });
+               req.response(function(inSender, inResponse) {
+                       this.bubble('onSubscriptionStatus', {
+                               username: username,
+                               subscribed: inResponse.status != "success"
+                       });
+               }.bind(this));
+               req.go({
+                       username: blerg.API.username
+               });
+       },
 });
index d2d0b83..5bcdbd0 100644 (file)
@@ -14,8 +14,8 @@ enyo.kind({
        components: [
                {classes: "blerg-user-controls", components: [
                        {name: "chatterLink", kind: "blerg.Link", content: "[chatter]"},
-                       {name: "stalkLink", kind: "blerg.Link", content: "[stalk]", showing: false, onclick: "startStalking"},
-                       {name: "unstalkLink", kind: "blerg.Link", content: "[stop stalking]", showing: false, onclick: "stopStalking"},
+                       {name: "stalkLink", kind: "blerg.Link", content: "[stalk]", showing: false, onNavigate: "startStalking"},
+                       {name: "unstalkLink", kind: "blerg.Link", content: "[stop stalking]", showing: false, onNavigate: "stopStalking"},
                        {name: "rssLink", kind: "blerg.Link", components: [
                                {noDom: true, content: "["},
                                {kind: "Image", src: "/images/rss.png", attributes: {width: 16, height: 16}},
@@ -25,7 +25,8 @@ enyo.kind({
                {name: "records"},
                {name: "loadMoreButton", kind: "onyx.Button", onclick: "loadMore", content: "Load More"},
                {name: "api", kind: "blerg.API",
-                onItemsLoaded: "itemsLoaded"}
+                onItemsLoaded: "itemsLoaded",
+                onSubscriptionStatus: "gotStalkStatus"}
        ],
        statics: {
                locationDetect: function(l) {
@@ -82,6 +83,21 @@ enyo.kind({
                        this.$.unstalkLink.hide();
                        return;
                }
-               // Make an API call to determine status
+               this.$.api.getSubscriptionStatus(this.username);
+       },
+       gotStalkStatus: function(inSender, inEvent) {
+               if (inEvent.subscribed) {
+                       this.$.stalkLink.hide();
+                       this.$.unstalkLink.show();
+               } else {
+                       this.$.stalkLink.show();
+                       this.$.unstalkLink.hide();
+               }
+       },
+       startStalking: function() {
+               this.$.api.subscribe(this.username);
+       },
+       stopStalking: function() {
+               this.$.api.unsubscribe(this.username);
        }
 });