Skip to content
Browse files

Options page for adding/showing people you follow

hooks up popup page to retrieve data from localStorage
  • Loading branch information...
1 parent bb07fd5 commit ae1270659ac6c0c0f172b97bd5845b836bc0ecd3 @weilu committed Apr 8, 2012
Showing with 199 additions and 56 deletions.
  1. +128 −0 js/options.js
  2. +39 −50 js/popup.js
  3. +6 −6 js/sites.js
  4. +26 −0 options.html
View
128 js/options.js
@@ -0,0 +1,128 @@
+$(function(){
+ Stacker = {};
+ Stacker.sites = {"Stack Overflow":"stackoverflow","Server Fault":"serverfault","Super User":"superuser","Meta Stack Overflow":"meta.stackoverflow","Web Applications":"webapps","Web Applications Meta":"meta.webapps","Gaming":"gaming","Gaming Meta":"meta.gaming","Webmasters":"webmasters","Webmasters Meta":"meta.webmasters","Cooking":"cooking","Cooking Meta":"meta.cooking","Game Development":"gamedev","Game Development Meta":"meta.gamedev","Photography":"photo","Photography Meta":"meta.photo","Statistical Analysis":"stats","Statistical Analysis Meta":"meta.stats","Mathematics":"math","Mathematics Meta":"meta.math","Home Improvement":"diy","Home Improvement Meta":"meta.diy","Meta Super User":"meta.superuser","Meta Server Fault":"meta.serverfault","GIS":"gis","GIS Meta":"meta.gis","TeX - LaTeX":"tex","TeX - LaTeX Meta":"meta.tex","Ask Ubuntu":"askubuntu","Ask Ubuntu Meta":"meta.askubuntu","Personal Finance and Money":"money","Personal Finance and Money Meta":"meta.money","English Language and Usage":"english","English Language and Usage Meta":"meta.english","Stack Apps":"stackapps","User Experience":"ux","User Experience Meta":"meta.ux","Unix and Linux":"unix","Unix and Linux Meta":"meta.unix","WordPress":"wordpress","WordPress Meta":"meta.wordpress","Theoretical Computer Science":"cstheory","Theoretical Computer Science Meta":"meta.cstheory","Apple":"apple","Apple Meta":"meta.apple","Role-playing Games":"rpg","Role-playing Games Meta":"meta.rpg","Bicycles":"bicycles","Bicycles Meta":"meta.bicycles","Programmers":"programmers","Programmers Meta":"meta.programmers","Electrical Engineering":"electronics","Electrical Engineering Meta":"meta.electronics","Android Enthusiasts":"android","Android Enthusiasts Meta":"meta.android","OnStartups":"answers.onstartups","OnStartups Meta":"meta.answers.onstartups","Board and Card Games":"boardgames","Board and Card Games Meta":"meta.boardgames","Physics":"physics","Physics Meta":"meta.physics","Homebrewing":"homebrew","Homebrewing Meta":"meta.homebrew","IT Security":"security","IT Security Meta":"meta.security","Writers":"writers","Writers Meta":"meta.writers","Audio-Video Production":"avp","Audio-Video Production Meta":"meta.avp","Graphic Design":"graphicdesign","Graphic Design Meta":"meta.graphicdesign","Database Administrators":"dba","Database Administrators Meta":"meta.dba","Science Fiction and Fantasy":"scifi","Science Fiction and Fantasy Meta":"meta.scifi","Code Review":"codereview","Code Review Meta":"meta.codereview","Code Golf":"codegolf","Code Golf Meta":"meta.codegolf","Quantitative Finance":"quant","Quantitative Finance Meta":"meta.quant","Project Management":"pm","Project Management Meta":"meta.pm","Skeptics":"skeptics","Skeptics Meta":"meta.skeptics","Fitness and Nutrition":"fitness","Fitness and Nutrition Meta":"meta.fitness","Drupal Answers":"drupal","Drupal Answers Meta":"meta.drupal","Motor Vehicle Maintenance and Repair":"mechanics","Motor Vehicle Maintenance and Repair Meta":"meta.mechanics","Parenting":"parenting","Parenting Meta":"meta.parenting","SharePoint":"sharepoint","SharePoint Meta":"meta.sharepoint","Musical Practice and Performance":"music","Musical Practice and Performance Meta":"meta.music","Software Quality Assurance and Testing":"sqa","Software Quality Assurance and Testing Meta":"meta.sqa","Jewish Life and Learning":"judaism","Jewish Life and Learning Meta":"meta.judaism","German Language and Usage":"german","German Language and Usage Meta":"meta.german","Japanese Language and Usage":"japanese","Japanese Language and Usage Meta":"meta.japanese","Astronomy":"astronomy","Astronomy Meta":"meta.astronomy","Philosophy":"philosophy","Philosophy Meta":"meta.philosophy","Gardening and Landscaping":"gardening","Gardening and Landscaping Meta":"meta.gardening","Travel":"travel","Travel Meta":"meta.travel","Personal Productivity":"productivity","Personal Productivity Meta":"meta.productivity","Cryptography":"crypto","Cryptography Meta":"meta.crypto","Literature":"literature","Literature Meta":"meta.literature","Signal Processing":"dsp","Signal Processing Meta":"meta.dsp","French Language and Usage":"french","French Language and Usage Meta":"meta.french","Christianity":"christianity","Christianity Meta":"meta.christianity","Bitcoin":"bitcoin","Bitcoin Meta":"meta.bitcoin","Linguistics":"linguistics","Linguistics Meta":"meta.linguistics","Theoretical Physics":"theoreticalphysics","Theoretical Physics Meta":"meta.theoreticalphysics","Biblical Hermeneutics":"hermeneutics","Biblical Hermeneutics Meta":"meta.hermeneutics","History":"history","History Meta":"meta.history","Economics":"economics","Economics Meta":"meta.economics","Healthcare IT":"healthcareit","Healthcare IT Meta":"meta.healthcareit","LEGO® Answers":"bricks","LEGO® Answers Meta":"meta.bricks","Firearms":"firearms","Firearms Meta":"meta.firearms","Spanish Language and Usage":"spanish","Spanish Language and Usage Meta":"meta.spanish","Computational Science":"scicomp","Computational Science Meta":"meta.scicomp","Movies and TV":"movies","Movies and TV Meta":"meta.movies","Chinese Language and Usage":"chinese","Chinese Language and Usage Meta":"meta.chinese","Biology":"biology","Biology Meta":"meta.biology","Poker":"poker","Poker Meta":"meta.poker","Mathematica":"mathematica","Mathematica Meta":"meta.mathematica","Cognitive Sciences":"cogsci","Cognitive Sciences Meta":"meta.cogsci","The Great Outdoors":"outdoors","The Great Outdoors Meta":"meta.outdoors","SmugMug":"smugmug","SmugMug Meta":"meta.smugmug","Martial Arts":"martialarts","Martial Arts Meta":"meta.martialarts","Sports":"sports","Sports Meta":"meta.sports","Academia":"academia","Academia Meta":"meta.academia","Computer Science":"cs","Computer Science Meta":"meta.cs"}
+
+ Stacker.OptView = Backbone.View.extend({
+ el: 'body',
+
+ events: {
+ 'click button': 'follow'
+ },
+
+ initialize: function(){
+ this.following = {};
+ this.sitesToUsers = {};
+
+ var stackerStorage = localStorage["stacker"];
+ if(!stackerStorage){
+ return;
+ }
+
+ var stackerOpts = JSON.parse(stackerStorage)
+ if(stackerOpts.following){
+ this.following = stackerOpts.following;
+ }
+ if(stackerOpts.sitesToUsers){
+ this.sitesToUsers = stackerOpts.sitesToUsers;
+ }
+ },
+
+ render: function(){
+ this.populateSeleteSites();
+ this.displayFollowing();
+
+ return this;
+ },
+
+ populateSeleteSites: function(){
+ var select = $('select#site');
+ select.empty();
+ for(var siteName in Stacker.sites){
+ select.append("<option value='" + this._getSite(siteName) + "'>" + siteName + "</optoins>");
+ }
+ },
+
+ displayFollowing: function(){
+ var ul = $('ul#following');
+ ul.empty();
+
+ for(var userId in this.following){
+ ul.append("<li>" + this.following[userId] + "</li>");
+ }
+ },
+
+ follow: function(){
+ var site = $('#site').val();
+ var userId = $('#user_id').val();
+
+ if(!site || !userId){
+ return;
+ }
+
+ this._getUser(site, userId);
+
+ return false;
+ },
+
+ _getUser: function(site, userId){
+ var url = "http://api.stackexchange.com/2.0/users/" + userId + "?site=" + site;
+ var req = new XMLHttpRequest();
+ req.open("GET", url, true);
+
+ view = this;
+ req.onload = function(){
+ var items = JSON.parse(req.response).items;
+ if(items && items.length==1){
+ view._addUser(items[0]);
+ }
+ }
+ req.send(null);
+ },
+
+ _addUser: function(user){
+ if(this.following[user.account_id]){
+ return; //already following
+ }
+
+ this.following[user.account_id] = user.display_name;
+
+ var ul = $('ul#following');
+ ul.append("<li>" + user.display_name + "</li>");
+
+ this._updateSitesToUsers(user.account_id);
+ },
+
+ _updateSitesToUsers: function(accountId){
+ var url = "http://api.stackexchange.com/2.0/users/" + accountId + "/associated";
+ var req = new XMLHttpRequest();
+ req.open("GET", url, true);
+
+ view = this;
+ req.onload = function(){
+ var items = JSON.parse(req.response).items;
+ for(var index in items){
+ var account = items[index];
+ var site = view._getSite(account.site_name);
+ if(!view.sitesToUsers[site]){
+ view.sitesToUsers[site] = [];
+ }
+ view.sitesToUsers[site].push(account.user_id)
+ view.sitesToUsers[site] = _.uniq(view.sitesToUsers[site]);
+ }
+
+ view._save();
+ }
+ req.send(null);
+ },
+
+ _getSite: function(siteName){
+ return Stacker.sites[siteName];
+ },
+
+ _save: function(){
+ localStorage["stacker"] = JSON.stringify({following: this.following, sitesToUsers: this.sitesToUsers});
+ }
+ });
+
+ window.view = new Stacker.OptView().render();
+});
View
89 js/popup.js
@@ -1,41 +1,6 @@
$(function(){
Stacker = {};
- Stacker.sites = ["stackoverflow", "serverfault", "superuser", "meta.stackoverflow", "webapps",
- "meta.webapps", "gaming", "meta.gaming", "webmasters", "meta.webmasters",
- "cooking", "meta.cooking", "gamedev", "meta.gamedev", "photo",
- "meta.photo", "stats", "meta.stats", "math", "meta.math",
- "diy", "meta.diy", "meta.superuser", "meta.serverfault", "gis",
- "meta.gis", "tex", "meta.tex", "askubuntu", "meta.askubuntu",
- "money", "meta.money", "english", "meta.english", "stackapps",
- "ux", "meta.ux", "unix", "meta.unix", "wordpress",
- "meta.wordpress", "cstheory", "meta.cstheory", "apple", "meta.apple",
- "rpg", "meta.rpg", "bicycles", "meta.bicycles", "programmers",
- "meta.programmers", "electronics", "meta.electronics", "android", "meta.android",
- "answers.onstartups", "meta.answers.onstartups", "boardgames", "meta.boardgames", "physics",
- "meta.physics", "homebrew", "meta.homebrew", "security", "meta.security",
- "writers", "meta.writers", "avp", "meta.avp", "graphicdesign",
- "meta.graphicdesign", "dba", "meta.dba", "scifi", "meta.scifi",
- "codereview", "meta.codereview", "codegolf", "meta.codegolf", "quant",
- "meta.quant", "pm", "meta.pm", "skeptics", "meta.skeptics",
- "fitness", "meta.fitness", "drupal", "meta.drupal", "mechanics",
- "meta.mechanics", "parenting", "meta.parenting", "sharepoint", "meta.sharepoint",
- "music", "meta.music", "sqa", "meta.sqa", "judaism",
- "meta.judaism", "german", "meta.german", "japanese", "meta.japanese",
- "astronomy", "meta.astronomy", "philosophy", "meta.philosophy", "gardening",
- "meta.gardening", "travel", "meta.travel", "productivity", "meta.productivity",
- "crypto", "meta.crypto", "literature", "meta.literature", "dsp",
- "meta.dsp", "french", "meta.french", "christianity", "meta.christianity",
- "bitcoin", "meta.bitcoin", "linguistics", "meta.linguistics", "theoreticalphysics",
- "meta.theoreticalphysics", "hermeneutics", "meta.hermeneutics", "history", "meta.history",
- "economics", "meta.economics", "healthcareit", "meta.healthcareit", "bricks",
- "meta.bricks", "firearms", "meta.firearms", "spanish", "meta.spanish",
- "scicomp", "meta.scicomp", "movies", "meta.movies", "chinese",
- "meta.chinese", "biology", "meta.biology", "poker", "meta.poker",
- "mathematica", "meta.mathematica", "cogsci", "meta.cogsci", "outdoors",
- "meta.outdoors", "smugmug", "meta.smugmug", "martialarts", "meta.martialarts",
- "sports", "meta.sports", "academia", "meta.academia", "cs",
- "meta.cs"];
Stacker.AppView = Backbone.View.extend({
@@ -65,17 +30,24 @@ $(function(){
Stacker.FeedView = Backbone.View.extend({
tagName: 'li',
render: function(){
- this.$el.html(this.model.get('title') + "[" + this.model.get('post_type') + "] by - " + this.model.get('user_id')); //TODO
+ console.debug("model", this.model);
+ this.$el.html("*" + this.model.get('timeline_type') + "*"
+ + this.model.get('title') + "[" + this.model.get('post_type')
+ + "] by - " + this.model.get('user_id')); //TODO
return this;
}
});
Stacker.Feed = Backbone.Model.extend({});
- Stacker.Feeds = Backbone.Collection.extend({
-
+ Stacker.BaseFeeds = Backbone.Collection.extend({
model: Stacker.Feed,
-
+ comparator: function(feed){
+ return -feed.get("creation_date");
+ }
+ });
+
+ Stacker.Feeds = Stacker.BaseFeeds.extend({
initialize: function(options){
this.site = options.site;
this.userIds = options.userIds;
@@ -85,25 +57,42 @@ $(function(){
return "https://api.stackexchange.com/2.0/users/"
+ encodeURIComponent(this.userIds.join(";"))
+ "/timeline?site="
- + this.site
+ + this.site;
},
parse: function(response){
// console.debug("response.items", response.items);
return response.items;
- },
-
- comparator: function(feed){
- return -feed.get("creation_date");
}
});
- window.feeds = new Stacker.Feeds({site: "stackoverflow", userIds: [429288,859536]});
- window.view = new Stacker.AppView({ feeds: window.feeds });
+ window.feeds = new Stacker.BaseFeeds();
+ var stackerStorage = localStorage['stacker'];
+ if(!stackerStorage){
+ return;
+ }
+
+ var sitesToUsers = JSON.parse(stackerStorage).sitesToUsers;
+ if(!sitesToUsers){
+ return;
+ }
+
+ var totalDone = 0;
+ for(var site in sitesToUsers){
+ var feeds = new Stacker.Feeds({site: site, userIds: sitesToUsers[site]});
+ feeds.fetch({
+ success: sortAndDisplay
+ });
+ }
- window.feeds.fetch({
- success: function(){
- window.view = window.view.render();
+ function sortAndDisplay(collection, response){
+ console.debug("feeds for site and users ", collection.site, collection.userIds);
+ window.feeds.add(collection.models, {silent: true});
+ totalDone++;
+
+ if(totalDone == Object.keys(sitesToUsers).length){
+ window.view = new Stacker.AppView({ feeds: window.feeds }).render();
}
- });
+ }
+
});
View
12 js/sites.js
@@ -8,19 +8,19 @@ function getSites(page, sites){
req.onload = function(){
response = JSON.parse(req.response);
- var newSites = _.pluck(response.items, "api_site_parameter");
- sites = sites.concat(newSites);
-
- // console.debug("2 sites", sites);
+ for(var item in response.items){
+ var site = response.items[item]
+ sites[site.name] = site.api_site_parameter;
+ };
if(response.has_more){
getSites(page+1, sites);
}else{
window.Stacker.sites = sites;
- // console.debug("all sites: ", window.Stacker.sites);
+ // console.debug("all sites: ", JSON.stringify(window.Stacker.sites));
}
};
req.send(null);
}
-// getSites(1, []);
+// getSites(1, {});
View
26 options.html
@@ -0,0 +1,26 @@
+<html>
+ <head>
+ <title>Stacker Options</title>
+ <script src="js/lib/zepto.js" type="text/javascript"></script>
+ <script src="js/lib/underscore.js" type="text/javascript"></script>
+ <script src="js/lib/backbone.js" type="text/javascript"></script>
+ <script src="js/options.js" type="text/javascript"></script>
+ </head>
+
+ <body>
+
+ <h2>Start Following</h2>
+ <label>StackExchange Site</label>
+ <select id="site">
+ </select>
+
+ <label>User ID: </label>
+ <input type="text" id="user_id">
+
+ <button>Follow</button>
+
+ <h2>Currently Following</h2>
+ <ul id="following"></ul>
+
+ </body>
+</html>

0 comments on commit ae12706

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