Commits

Adam Gomaa committed 89db8de

Reindent the main JS file, use a more RESTful pagination scheme, remove IE-targeting caret position code (I'm only targeting webkit, really)

Comments (0)

Files changed (2)

         return _.template($(selector).html().replace(/&lt;/g, "<").replace(/&gt;/g, ">"));
     }
 
-// https://gist.github.com/838460
-var PaginatedCollection = Backbone.Collection.extend({
-  initialize: function() {
-    _.bindAll(this, 'parse', 'url', 'pageInfo', 'nextPage', 'previousPage');
-    typeof(options) != 'undefined' || (options = {});
-    this.page = 1;
-    typeof(this.perPage) != 'undefined' || (this.perPage = 10);
-  },
-  fetch: function(options) {
-    typeof(options) != 'undefined' || (options = {});
-    this.trigger("fetching");
-    var self = this;
-    var success = options.success;
-    options.success = function(resp) {
-      self.trigger("fetched");
-      if(success) { success(self, resp); }
-    };
-    return Backbone.Collection.prototype.fetch.call(this, options);
-  },
-  parse: function(resp) {
-    this.page = resp.page;
-    this.perPage = resp.perPage;
-    this.total = resp.total;
-    return resp.models;
-  },
-  url: function() {
-      return this.baseUrl + '?' + $.param({col: "", page: this.page, perPage: this.perPage});
-  },
-  pageInfo: function() {
-    var info = {
-      total: this.total,
-      page: this.page,
-      perPage: this.perPage,
-      pages: Math.ceil(this.total / this.perPage),
-      prev: false,
-      next: false
-    };
+    // https://gist.github.com/838460
+    var PaginatedCollection = Backbone.Collection.extend({
+        initialize: function() {
+            _.bindAll(this, 'parse', 'url', 'pageInfo', 'nextPage', 'previousPage');
+            typeof(options) != 'undefined' || (options = {});
+            this.page = 1;
+            typeof(this.perPage) != 'undefined' || (this.perPage = 10);
+        },
+        fetch: function(options) {
+            typeof(options) != 'undefined' || (options = {});
+            this.trigger("fetching");
+            var self = this;
+            var success = options.success;
+            options.success = function(resp) {
+                self.trigger("fetched");
+                if(success) { success(self, resp); }
+            };
+            return Backbone.Collection.prototype.fetch.call(this, options);
+        },
+        parse: function(resp) {
+            this.page = resp.page;
+            this.perPage = resp.perPage;
+            this.total = resp.total;
+            return resp.models;
+        },
+        url: function() {
+            return this.baseUrl + '?' + $.param({page: this.page, perPage: this.perPage});
+        },
+        pageInfo: function() {
+            var info = {
+                total: this.total,
+                page: this.page,
+                perPage: this.perPage,
+                pages: Math.ceil(this.total / this.perPage),
+                prev: false,
+                next: false
+            };
 
-    var max = Math.min(this.total, this.page * this.perPage);
+            var max = Math.min(this.total, this.page * this.perPage);
 
-    if (this.total == this.pages * this.perPage) {
-      max = this.total;
-    }
+            if (this.total == this.pages * this.perPage) {
+                max = this.total;
+            }
 
-    info.range = [(this.page - 1) * this.perPage + 1, max];
+            info.range = [(this.page - 1) * this.perPage + 1, max];
 
-    if (this.page > 1) {
-      info.prev = this.page - 1;
-    }
+            if (this.page > 1) {
+                info.prev = this.page - 1;
+            }
 
-    if (this.page < info.pages) {
-      info.next = this.page + 1;
-    }
+            if (this.page < info.pages) {
+                info.next = this.page + 1;
+            }
 
-    return info;
-  },
-  nextPage: function() {
-    if (!this.pageInfo().next) {
-      return false;
-    }
-    this.page = this.page + 1;
-    return this.fetch();
-  },
-  previousPage: function() {
-    if (!this.pageInfo().prev) {
-      return false;
-    }
-    this.page = this.page - 1;
-    return this.fetch();
-  }
+            return info;
+        },
+        nextPage: function() {
+            if (!this.pageInfo().next) {
+                return false;
+            }
+            this.page = this.page + 1;
+            return this.fetch();
+        },
+        previousPage: function() {
+            if (!this.pageInfo().prev) {
+                return false;
+            }
+            this.page = this.page - 1;
+            return this.fetch();
+        }
 
-});
+    });
 
 
-var Note = Backbone.Model.extend({
-    timestamp_display: function(){
-        var ts = this.get("timestamp");
-        return ts.substr(0, 10);
-    },
-    url: function(){
-        return "/notes/note/" + this.id;
-    }
-});
+    var Note = Backbone.Model.extend({
+        timestamp_display: function(){
+            var ts = this.get("timestamp");
+            return ts.substr(0, 10);
+        },
+        url: function(){
+            return "/notes/note/" + this.id;
+        }
+    });
 
-    var NoteCol = PaginatedCollection.extend({model: Note, baseUrl: '/notes/', perPage: 10});
+    var NoteCol = PaginatedCollection.extend({model: Note, baseUrl: '/notes/note', perPage: 10});
 
-PaginatedView = Backbone.View.extend({
-  initialize: function() {
-    _.bindAll(this, 'previous', 'next', 'render');
-      this.collection.bind('reset', this.render, this);
-  },
-  events: {
-    'click a.prev': 'previous',
-    'click a.next': 'next'
-  },
-  render: function() {
-      this.$el.find(".notes-pagination").html(get_template("#pagination-template")(this.collection.pageInfo()));
-  },
+    PaginatedView = Backbone.View.extend({
+        initialize: function() {
+            _.bindAll(this, 'previous', 'next', 'render');
+            this.collection.bind('reset', this.render, this);
+        },
+        events: {
+            'click a.prev': 'previous',
+            'click a.next': 'next'
+        },
+        render: function() {
+            this.$el.find(".notes-pagination").html(get_template("#pagination-template")(this.collection.pageInfo()));
+        },
 
-  previous: function() {
-    this.collection.previousPage();
-    return false;
-  },
-  next: function() {
-    this.collection.nextPage();
-    return false;
-  }
-});
+        previous: function() {
+            this.collection.previousPage();
+            return false;
+        },
+        next: function() {
+            this.collection.nextPage();
+            return false;
+        }
+    });
 
-// http://stackoverflow.com/a/499158/16361
-
-function setSelectionRange(input, selectionStart, selectionEnd) {
-  if (input.setSelectionRange) {
-    input.focus();
-    input.setSelectionRange(selectionStart, selectionEnd);
-  }
-  else if (input.createTextRange) {
-    var range = input.createTextRange();
-    range.collapse(true);
-    range.moveEnd('character', selectionEnd);
-    range.moveStart('character', selectionStart);
-    range.select();
-  }
-}
-
-function setCaretToPos (input, pos) {
-  setSelectionRange(input, pos, pos);
-}
-
-var NoteView = Backbone.View.extend({
-    tagName: "div",
-    template: get_template("#note-template"),
-    events: {
-        "click .edit": "startEdit",
-    },
-    initialize: function(){
-        _.bindAll(this, "startEdit");
-        this.edit_view = null;
-        this.model.bind("sync", function(){
-            this.render();
-        }, this);
-    },
-    render: function(){
-        $(this.el).html(this.template(this.template_context()));
-        return this;
-    },
-    template_context: function(){
-        return this.model.attributes;
-    },
-    startEdit: function(){
-        if(!this.edit_view){
-            this.edit_view = new EditableNoteView({model: this.model, el: this.el, view: this});
+    var NoteView = Backbone.View.extend({
+        tagName: "div",
+        template: get_template("#note-template"),
+        events: {
+            "click .edit": "startEdit",
+        },
+        initialize: function(){
+            _.bindAll(this, "startEdit");
+            this.edit_view = null;
+            this.model.bind("sync", function(){
+                this.render();
+            }, this);
+        },
+        render: function(){
+            $(this.el).html(this.template(this.template_context()));
+            return this;
+        },
+        template_context: function(){
+            return this.model.attributes;
+        },
+        startEdit: function(){
+            if(!this.edit_view){
+                this.edit_view = new EditableNoteView({model: this.model, el: this.el, view: this});
+            }
+            this.edit_view.render();
         }
-        this.edit_view.render();
-    }
-});
+    });
 
 
     var EditableNoteView = Backbone.View.extend({
             var height = this.$(".note-text").height();
             $(this.el).html(this.template(this.model.attributes));
             this.$("textarea").width(width).height(height).focus();
-            setCaretToPos(this.$("textarea")[0], this.$("textarea").val().length);
+            var len = this.$("textarea").val().length
+            this.$("textarea").get(0).setSelectionRange(len, len);
         },
         stopEdit: function(){
             var val = this.$("textarea").val()
         }
     });
 
-var ImageNoteView = Backbone.View.extend({
-    tagName: "div",
-    template: get_template("#note-image-template"),
-    render: function(){
-        $(this.el).html(this.template(this.template_context()));
-        this.$("img").attr("src", this.model.url());
-        return this;
-    },
-    template_context: function(){
-        return this.model.attributes;
-    }
-});
+    var ImageNoteView = Backbone.View.extend({
+        tagName: "div",
+        template: get_template("#note-image-template"),
+        render: function(){
+            $(this.el).html(this.template(this.template_context()));
+            this.$("img").attr("src", this.model.url());
+            return this;
+        },
+        template_context: function(){
+            return this.model.attributes;
+        }
+    });
 
 
 
-var AppView = PaginatedView.extend({
-    el: $("#notes-view"),
-    initialize: function(options){
-        this.collection = new NoteCol();
-        this.constructor.__super__.initialize.apply(this, [options])
-        //this.notes.bind("add", this.addOne, this)
-        this.collection.bind("reset", this.addAll, this)
-        this.collection.fetch();
-    },
-    addOne: function(note){
-        if(note.get("text")){
-            var view = new NoteView({model: note});
-        }else{
-            var view = new ImageNoteView({model: note});
+    var AppView = PaginatedView.extend({
+        el: $("#notes-view"),
+        initialize: function(options){
+            this.collection = new NoteCol();
+            this.constructor.__super__.initialize.apply(this, [options])
+            //this.notes.bind("add", this.addOne, this)
+            this.collection.bind("reset", this.addAll, this)
+            this.collection.fetch();
+        },
+        addOne: function(note){
+            if(note.get("text")){
+                var view = new NoteView({model: note});
+            }else{
+                var view = new ImageNoteView({model: note});
+            }
+            this.$(".notes-container").append(view.render().el);
+        },
+        addAll: function(){
+            this.$(".notes-container").empty();
+            this.collection.each(this.addOne);
         }
-        this.$(".notes-container").append(view.render().el);
-    },
-    addAll: function(){
-        this.$(".notes-container").empty();
-        this.collection.each(this.addOne)
-    }
-})
+    });
 
     window.app = new AppView();
 });
         return fs_file("notes.js", content_type="text/javascript")
     if path == "css":
         return fs_file("notes.css", content_type="text/css")
-    if "col" in request.GET:
+    if path.startswith("note") and "page" in request.GET:
         return notes_data()
     elif path.startswith("note/"):
         return _Note(path[len("note/"):]).GET()