Commit d486e374 authored by Faldrian's avatar Faldrian Committed by Steffen van Bergerem
Browse files

following tags now normalized and sorted in ui

closes #6454
parent a946251a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@

## Bug fixes
* Skip first getting started step if it looks done already [#6456](https://github.com/diaspora/diaspora/pull/6456)
* Normalize new followed tags and insert them alphabetically [#6454](https://github.com/diaspora/diaspora/pull/6454)

## Features
* Show spinner on initial stream load [#6384](https://github.com/diaspora/diaspora/pull/6384)
+28 −2
Original line number Diff line number Diff line
@@ -21,7 +21,12 @@ app.views.TagFollowingList = app.views.Base.extend({
  },

  postRenderTemplate : function() {
    this.collection.each(this.appendTagFollowing, this);
    // add the whole sorted collection without handling each item separately
    this.collection.each(function(tag) {
      this.$el.prepend(new app.views.TagFollowing({
        model: tag
      }).render().el);
    }, this);
  },

  setupAutoSuggest : function() {
@@ -63,12 +68,33 @@ app.views.TagFollowingList = app.views.Base.extend({
  createTagFollowing: function(evt) {
    if(evt){ evt.preventDefault(); }

    this.collection.create({"name":this.$(".tag_input").val()});
    var name = this.$(".tag_input").val();
    // compare tag_text_regexp in app/models/acts_as_taggable_on-tag.rb
    var normalizedName = (name === "<3" ? name : name.replace(
        new RegExp("[^" + PosixBracketExpressions.alnum + "_\\-]+", "gi"), "").toLowerCase());

    this.collection.create({"name":normalizedName});

    this.$(".tag_input").val("");
    return this;
  },

  appendTagFollowing: function(tag) {
    // insert new tag in the order of the collection
    var modelIndex = this.collection.indexOf(tag);
    var prevModel = this.collection.at(modelIndex + 1); // prev in alphabet, +1 (next) in reverse sorted list

    if (prevModel) {
      var prevModelDom = this.$("#tag-following-" + prevModel.get("name"));
      if (prevModelDom.length > 0) {
        prevModelDom.after(new app.views.TagFollowing({
          model: tag
        }).render().el);
        return;
      }
    }

    // we have no previous Model and no View, so just prepend to the list
    this.$el.prepend(new app.views.TagFollowing({
      model: tag
    }).render().el);
+60 −0
Original line number Diff line number Diff line
describe("app.views.TagFollowingList", function(){
  beforeEach(function () {
    this.tagsUnsorted = [
      {name: "ab"},
      {name: "cd"},
      {name: "bc"}
    ];

    this.tagsSorted = [
      {name: "ab"},
      {name: "bc"},
      {name: "cd"}
    ];

    app.tagFollowings = new app.collections.TagFollowings(this.tagsUnsorted);
    this.view = new app.views.TagFollowingList({collection: app.tagFollowings});
  });

  describe("render", function(){
    it("lists the tags alphabetically ascending", function(){
      var html = this.view.render();
      for(var i=0;i<this.tagsSorted.length;i++) {
        expect(html.el.children[i].id).toMatch("tag-following-" + this.tagsSorted[i].name);
      }
    });
  });

  describe("adding tags", function(){
    it("inserts a new tag at top if it comes before all others alphabetically", function(){
      app.tagFollowings.create({name: "aa"});

      var html = this.view.render();
      expect(html.el.children[0].id).toMatch("tag-following-aa");
      expect(html.el.children[1].id).toMatch("tag-following-ab");
    });

    it("inserts a new tag at the bottom if it comes after all others alphabetically", function(){
      app.tagFollowings.create({name: "zz"});

      var html = this.view.render();
      var lastItemIndex = html.el.childElementCount -2; // last element is the input box
      expect(html.el.children[lastItemIndex].id).toMatch("tag-following-zz");
    });

    it("inserts a new tag at second place if it comes after the first alphabetically", function(){
      app.tagFollowings.create({name: "ac"});

      var html = this.view.render();
      expect(html.el.children[1].id).toMatch("tag-following-ac");
    });

    it("inserts a new tag second to last if it comes before last tag alphabetically", function(){
      app.tagFollowings.create({name: "ca"});

      var html = this.view.render();
      var lastItemIndex = html.el.childElementCount -3; // last element is the input box. And one up, please.
      expect(html.el.children[lastItemIndex].id).toMatch("tag-following-ca");
    });
  });
});
+1 −1

File changed.

Contains only whitespace changes.