Commits

Tom Morton  committed 90b4759

convert API to coffee script, expand watchlist functionality

  • Participants
  • Parent commits cafa415

Comments (0)

Files changed (8)

File css/style.css

 footer { text-align: center;}
 
 
+.watchlist {
+	list-style: none;
+}
+.watchlist .comment { padding-left: 110px; font-style: italic;}
+.watchlist .userlinks { font-style: italic;}}
+.watchlist .time { font-weight: bold; }
 
 
 
 
 
 
-
-
         list: "watchlist",
         wllimit: 150,
         format: "json",
-        wlprop: "title|ids|user|comment|flags"
+        wlprop: "title|ids|user|comment|flags|timestamp|sizes|parsedcomment"
       }, function(data) {
         var list;
         list = new ListView();
+        _.each(data.query.watchlist, function(item) {
+          item.date = new Date(item.timestamp);
+          if (item.newlen > item.oldlen) {
+            item.changeClass = "badge-success";
+            return item.change = item.newlen - item.oldlen;
+          } else {
+            if (item.newlen === item.oldlen) {
+              item.changeClass = "";
+              return item.change = "0";
+            } else {
+              item.changeClass = "badge-important";
+              return item.change = item.oldlen - item.newlen;
+            }
+          }
+        });
+        console.log(data.query.watchlist);
         return list.render(data.query.watchlist);
       });
     };

File js/script.js

 /* Author:
 
 */
+
+// Hacks
+Date.prototype.monthNames = [
+    "January", "February", "March",
+    "April", "May", "June",
+    "July", "August", "September",
+    "October", "November", "December"
+];
+
+Date.prototype.getMonthName = function() {
+    return this.monthNames[this.getMonth()];
+};
+Date.prototype.getShortMonthName = function () {
+    return this.getMonthName().substr(0, 3);
+};
+
 require.config({
 	baseUrl: "js",
  	paths: {
   // Again, the other dependencies passed in are not "AMD" therefore don't pass a parameter to this function
   app = new App();
   Backbone.history.start();
-});
-
-/*
-
-
-
-var AppView = Backbone.View.extend({
-	el: "body",
-
-	events: {
-		"click a" : "link"
-	},
-
-	routes: {},
-	
-	route: function(route,callback)
-	{
-		this.routes[route] = callback;
-	},
-
-	link: function(event) {
-		event.preventDefault();
-		href = $(event.currentTarget).attr("href");
-		if(href.indexOf("#") == 0)
-		{
-			// reset active
-			this.routes[href]();
-		}
-	},
-
-	
-});
-
-app = new AppView();
-
-var Watchlist = {
-	index: function()
-	{
-		
-	}
-}
-
-app.route("#watchlist",Watchlist.index)*/
-
-
-
-
-
-
+});

File js/templates/watchlist/list.html

-<ul>
+<ul class="watchlist" >
 	{{#items}}
-		<li>{{title}}</li>
+		<li>
+			
+				{{date.getDay}} {{date.getMonthName}} <span class="time" >{{date.getHours}}:{{date.getMinutes}}</span> |
+			</span>
+			<a href="http://en.wikipedia.org/wiki/{{title}}" target="_blank" >{{title}}</a>
+			<span class="badge {{changeClass}}">{{change}}</span>
+			<span class="userlinks" >
+				<a href="http://en.wikipedia.org/wiki/User:{{user}}" target="_blank" >{{user}}</a>
+				(<a href="http://en.wikipedia.org/wiki/User_talk:{{user}}" target="_blank" >talk</a> 
+				| <a href="http://en.wikipedia.org/wiki/Special:Contributions/{{user}}" target="_blank" >contribs</a>)
+			</span>
+			<div class="comment" >{{{parsedcomment}}}</div>
+		</li>
 	{{/items}}
 </ul>

File js/views/auth/login.js

       });
       $(this.el).html("");
       $(this.el).modal("hide");
-      this.unbind();
-      return this.destroy();
+      return this.unbind();
     };
 
     return LoginModal;

File src/coffee/app.coffee

           "watchlist": "watchlist"
           "login": "login"
         watchlist: () ->       
-          MW.get({action:"query",list:"watchlist",wllimit:150,format:"json",wlprop:"title|ids|user|comment|flags"}, (data) ->
+          MW.get({action:"query",list:"watchlist",wllimit:150,format:"json",wlprop:"title|ids|user|comment|flags|timestamp|sizes|parsedcomment"}, (data) ->
             list = new ListView()
+            _.each(data.query.watchlist, (item) -> 
+              item.date = new Date(item.timestamp)
+              if item.newlen > item.oldlen
+                item.changeClass = "badge-success"
+                item.change = item.newlen - item.oldlen 
+              else
+                if item.newlen == item.oldlen
+                  item.changeClass = ""
+                  item.change = "0"
+                else
+                  item.changeClass = "badge-important"
+                  item.change = item.oldlen - item.newlen
+            )
+            console.log(data.query.watchlist)
             list.render(data.query.watchlist)
           )
         login: () ->

File src/coffee/libs/wiki.coffee

+define([],() ->
+  class MW
+    api: "http://en.wikipedia.org/w/api.php"
+
+    get: (data,success,fail) ->
+      $.ajax({url:@api,data:data,success:success})
+    checkLogin: (success,fail)->
+      $.ajax({
+        url   : @api
+        data  : 
+          action  : "query"
+          meta    : "userinfo"
+          format  : "json"
+        success: (data) => 
+          if data.query.userinfo.id == 0
+            fail()
+          else
+            success()
+        fail  : fail
+      })
+    loginWithToken: (user,pass,token,success,error) =>
+      $.ajax({
+	      type   : 'POST'
+	      url    : @api
+	      data:
+          action    : "login"
+          lgname    : user
+          lgpassword: pass
+          lgtoken   : token
+          format    : "json"
+	      success: (data) =>
+		      if data.login.result == "Success"
+            success(data.login)
+          else
+            error(data.login.result)
+		    })
+    doLogin: (user,pass,success,error) ->
+      $.ajax({
+      	type   : 'POST'
+      	url    : @api
+      	data:
+          action    : "login"
+          lgname    : user
+          lgpassword: "xx"
+          format    : "json"
+      	success: (data) => 
+          @loginWithToken(user,pass,data.login.token,success,error)
+        fail: (data) =>
+          error("Unknown Error")
+      })
+  new MW
+)

File src/coffee/views/auth/login.coffee

         $(@el).html("")
         $(@el).modal("hide")
         @unbind()
-        
-
 )