Commit 11a8ac61 authored by Jonne Haß's avatar Jonne Haß
Browse files

Merge pull request #6240 from svbergerem/contact-page-update-counts

Update number of contacts on contact page when adding/removing contacts
parents 97c9561a 04191996
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ With the port to Bootstrap 3, app/views/terms/default.haml has a new structure.
* Optionally make your extended profile details public [#6162](https://github.com/diaspora/diaspora/pull/6162)
* Add admin dashboard showing latest diaspora\* version [#6216](https://github.com/diaspora/diaspora/pull/6216)
* Display poll & location on mobile [#6238](https://github.com/diaspora/diaspora/pull/6238)
* Update counts on contacts page dynamically [#6240](https://github.com/diaspora/diaspora/pull/6240)

# 0.5.3.0

+3 −3
Original line number Diff line number Diff line
@@ -2,12 +2,12 @@

app.models.Contact = Backbone.Model.extend({
  initialize : function() {
    this.aspect_memberships = new app.collections.AspectMemberships(this.get('aspect_memberships'));
    if( this.get('person') ) this.person = new app.models.Person(this.get('person'));
    this.aspectMemberships = new app.collections.AspectMemberships(this.get("aspect_memberships"));
    if(this.get("person")) { this.person = new app.models.Person(this.get("person")); }
  },

  inAspect : function(id) {
    return this.aspect_memberships.any(function(membership){ return membership.get('aspect').id === id; });
    return this.aspectMemberships.any(function(membership){ return membership.get("aspect").id === id; });
  }
});
// @license-end
+47 −0
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ app.pages.Contacts = Backbone.View.extend({
    $("#people_stream.contacts .header i").tooltip({"placement": "bottom"});
    $(document).on("ajax:success", "form.edit_aspect", this.updateAspectName);
    app.events.on("aspect:create", function(){ window.location.reload() });
    app.events.on("aspect_membership:create", this.addAspectMembership, this);
    app.events.on("aspect_membership:destroy", this.removeAspectMembership, this);

    this.aspectCreateView = new app.views.AspectCreate({ el: $("#newAspectContainer") });
    this.aspectCreateView.render();
@@ -95,6 +97,51 @@ app.pages.Contacts = Backbone.View.extend({
      revert: true,
      helper: "clone"
    });
  },

  updateBadgeCount: function(selector, change) {
    var count = parseInt($("#aspect_nav " + selector + " .badge").text(), 10);
    $("#aspect_nav " + selector + " .badge").text(count + change);
  },

  addAspectMembership: function(data) {
    if(data.startSharing) {
      this.updateBadgeCount(".all_aspects", 1);

      var contact = this.stream.collection.find(function(c) {
        return c.get("person").id === data.membership.personId;
      });

      if(contact && contact.person.get("relationship") === "sharing") {
        contact.person.set({relationship: "mutual"});
        this.updateBadgeCount(".only_sharing", -1);
      }
      else if(contact && contact.person.get("relationship") === "not_sharing") {
        contact.person.set({relationship: "receiving"});
        this.updateBadgeCount(".all_contacts", 1);
      }
    }
    this.updateBadgeCount("[data-aspect-id='" + data.membership.aspectId + "']", 1);
  },

  removeAspectMembership: function(data) {
    if(data.stopSharing) {
      this.updateBadgeCount(".all_aspects", -1);

      var contact = this.stream.collection.find(function(c) {
        return c.get("person").id === data.membership.personId;
      });

      if(contact && contact.person.get("relationship") === "mutual") {
        contact.person.set({relationship: "sharing"});
        this.updateBadgeCount(".only_sharing", 1);
      }
      else if(contact && contact.person.get("relationship") === "receiving") {
        contact.person.set({relationship: "not_sharing"});
        this.updateBadgeCount(".all_contacts", -1);
      }
    }
    this.updateBadgeCount("[data-aspect-id='" + data.membership.aspectId + "']", -1);
  }
});
// @license-end
+34 −17
Original line number Diff line number Diff line
@@ -77,20 +77,28 @@ app.views.AspectMembership = app.views.AspectsDropdown.extend({
    return aspect_membership.save();
  },

  _successSaveCb: function(aspect_membership) {
    var aspect_id = aspect_membership.get('aspect_id');
    var membership_id = aspect_membership.get('id');
    var li = this.dropdown.find('li[data-aspect_id="'+aspect_id+'"]');
  _successSaveCb: function(aspectMembership) {
    var aspectId = aspectMembership.get("aspect_id"),
        membershipId = aspectMembership.get("id"),
        li = this.dropdown.find("li[data-aspect_id='" + aspectId + "']"),
        personId = li.closest("ul.dropdown-menu").data("person_id"),
        startSharing = false;

    // the user didn't have this person in any aspects before, congratulate them
    // on their newly found friendship ;)
    if( this.dropdown.find('li.selected').length === 0 ) {
      var msg = Diaspora.I18n.t('aspect_dropdown.started_sharing_with', { 'name': this._name() });
      Diaspora.page.flashMessages.render({ 'success':true, 'notice':msg });
    if( this.dropdown.find("li.selected").length === 0 ) {
      var msg = Diaspora.I18n.t("aspect_dropdown.started_sharing_with", { "name": this._name() });
      startSharing = true;
      Diaspora.page.flashMessages.render({ "success": true, "notice": msg });
    }

    li.attr('data-membership_id', membership_id) // just to be sure...
      .data('membership_id', membership_id);
    app.events.trigger("aspect_membership:create", {
      membership: { aspectId: aspectId, personId: personId },
      startSharing: startSharing
    });

    li.attr("data-membership_id", membershipId) // just to be sure...
      .data("membership_id", membershipId);

    this.updateSummary(li);
    this._done();
@@ -119,21 +127,30 @@ app.views.AspectMembership = app.views.AspectsDropdown.extend({
    return aspect_membership.destroy();
  },

  _successDestroyCb: function(aspect_membership) {
    var membership_id = aspect_membership.get('id');
    var li = this.dropdown.find('li[data-membership_id="'+membership_id+'"]');
  _successDestroyCb: function(aspectMembership) {
    var membershipId = aspectMembership.get("id"),
        li = this.dropdown.find("li[data-membership_id='" + membershipId + "']"),
        aspectId = li.data("aspect_id"),
        personId = li.closest("ul.dropdown-menu").data("person_id"),
        stopSharing = false;

    li.removeAttr('data-membership_id')
      .removeData('membership_id');
    li.removeAttr("data-membership_id")
      .removeData("membership_id");
    this.updateSummary(li);

    // we just removed the last aspect, inform the user with a flash message
    // that he is no longer sharing with that person
    if( this.dropdown.find('li.selected').length === 0 ) {
      var msg = Diaspora.I18n.t('aspect_dropdown.stopped_sharing_with', { 'name': this._name() });
      Diaspora.page.flashMessages.render({ 'success':true, 'notice':msg });
    if( this.dropdown.find("li.selected").length === 0 ) {
      var msg = Diaspora.I18n.t("aspect_dropdown.stopped_sharing_with", { "name": this._name() });
      stopSharing = true;
      Diaspora.page.flashMessages.render({ "success": true, "notice": msg });
    }

    app.events.trigger("aspect_membership:destroy", {
      membership: { aspectId: aspectId, personId: personId },
      stopSharing: stopSharing
    });

    this._done();
  },

+27 −9
Original line number Diff line number Diff line
@@ -36,31 +36,49 @@ app.views.Contact = app.views.Base.extend({

  addContactToAspect: function(){
    var self = this;
    this.model.aspect_memberships.create({
      'person_id': this.model.get('person_id'),
      'aspect_id': app.aspect.get('id')
    // do we create the first aspect membership for this person?
    var startSharing = this.model.aspectMemberships.length === 0;
    this.model.aspectMemberships.create({
      "person_id": this.model.get("person_id"),
      "aspect_id": app.aspect.get("id")
    },{
      success: function(){
        app.events.trigger("aspect_membership:create", {
          membership: {
            aspectId: app.aspect.get("id"),
            personId: self.model.get("person_id")
          },
          startSharing: startSharing
        });
        self.render();
      },
      error: function(){
        var msg = Diaspora.I18n.t('contacts.error_add', { 'name': self.model.get('person').name });
        Diaspora.page.flashMessages.render({ 'success':false, 'notice':msg });
        var msg = Diaspora.I18n.t("contacts.error_add", { "name": self.model.get("person").name });
        Diaspora.page.flashMessages.render({ "success": false, "notice": msg });
      }
    });
  },

  removeContactFromAspect: function(){
    var self = this;
    this.model.aspect_memberships
      .find(function(membership){ return membership.get('aspect').id === app.aspect.id; })
    // do we destroy the last aspect membership for this person?
    var stopSharing = this.model.aspectMemberships.length <= 1;
    this.model.aspectMemberships
      .find(function(membership){ return membership.get("aspect").id === app.aspect.id; })
      .destroy({
        success: function(){
          app.events.trigger("aspect_membership:destroy", {
            membership: {
              aspectId: app.aspect.get("id"),
              personId: self.model.get("person_id")
            },
            stopSharing: stopSharing
          });
          self.render();
        },
        error: function(){
          var msg = Diaspora.I18n.t('contacts.error_remove', { 'name': self.model.get('person').name });
          Diaspora.page.flashMessages.render({ 'success':false, 'notice':msg });
          var msg = Diaspora.I18n.t("contacts.error_remove", { "name": self.model.get("person").name });
          Diaspora.page.flashMessages.render({ "success": false, "notice": msg });
        }
      });
  }
Loading