Commit f2fdaf1d authored by Augier's avatar Augier Committed by Benjamin Neff
Browse files

Use typeahead on conversations

parent 5269a0d3
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -79,6 +79,9 @@ app.pages.Contacts = Backbone.View.extend({
  },

  showMessageModal: function(){
    $("#conversationModal").on("modal:loaded", function() {
      new app.views.ConversationsForm({prefill: gon.conversationPrefill});
    });
    app.helpers.showModal("#conversationModal");
  },

+63 −20
Original line number Diff line number Diff line
@@ -5,32 +5,63 @@ app.views.ConversationsForm = Backbone.View.extend({

  events: {
    "keydown .conversation-message-text": "keyDown",
    "click .conversation-recipient-tag .remove": "removeRecipient"
  },

  initialize: function(opts) {
    this.contacts = _.has(opts, "contacts") ? opts.contacts : null;
    this.prefill = [];
    if (_.has(opts, "prefillName") && _.has(opts, "prefillValue")) {
      this.prefill = [{name: opts.prefillName, value: opts.prefillValue}];
    opts = opts || {};
    this.conversationRecipients = [];

    this.typeaheadElement = this.$el.find("#contacts-search-input");
    this.contactsIdsListInput = this.$el.find("#contact-ids");
    this.tagListElement = this.$("#recipients-tag-list");

    this.search = new app.views.SearchBase({
      el: this.$el.find("#new-conversation"),
      typeaheadInput: this.typeaheadElement,
      customSearch: true,
      autoselect: true,
      remoteRoute: {url: "/contacts", extraParameters: "mutual=true"}
    });

    this.bindTypeaheadEvents();

    this.tagListElement.empty();
    if (opts.prefill) {
      this.prefill(opts.prefill);
    }
    this.prepareAutocomplete(this.contacts);

    this.$("form#new-conversation").on("ajax:success", this.conversationCreateSuccess);
    this.$("form#new-conversation").on("ajax:error", this.conversationCreateError);
  },

  prepareAutocomplete: function(data){
    this.$("#contact-autocomplete").autoSuggest(data, {
      selectedItemProp: "name",
      searchObjProps: "name",
      asHtmlID: "contact_ids",
      retrieveLimit: 10,
      minChars: 1,
      keyDelay: 0,
      startText: '',
      emptyText: Diaspora.I18n.t("no_results"),
      preFill: this.prefill
    });
    $("#contact_ids").attr("aria-labelledby", "toLabel").focus();
  addRecipient: function(person) {
    this.conversationRecipients.push(person);
    this.updateContactIdsListInput();
    /* eslint-disable camelcase */
    this.tagListElement.append(HandlebarsTemplates.conversation_recipient_tag_tpl(person));
    /* eslint-enable camelcase */
  },

  prefill: function(handles) {
    handles.forEach(this.addRecipient.bind(this));
  },

  updateContactIdsListInput: function() {
    this.contactsIdsListInput.val(_(this.conversationRecipients).pluck("id").join(","));
    this.search.ignoreDiasporaIds.length = 0;
    this.conversationRecipients.forEach(this.search.ignorePersonForSuggestions.bind(this.search));
  },

  bindTypeaheadEvents: function() {
    this.typeaheadElement.on("typeahead:select", function(evt, person) {
      this.onSuggestionSelection(person);
    }.bind(this));
  },

  onSuggestionSelection: function(person) {
    this.addRecipient(person);
    this.typeaheadElement.typeahead("val", "");
  },

  keyDown: function(evt) {
@@ -39,6 +70,18 @@ app.views.ConversationsForm = Backbone.View.extend({
    }
  },

  removeRecipient: function(evt) {
    var $recipientTagEl = $(evt.target).parents(".conversation-recipient-tag");
    var diasporaHandle = $recipientTagEl.data("diaspora-handle");

    this.conversationRecipients = this.conversationRecipients.filter(function(person) {
      return diasporaHandle.localeCompare(person.handle) !== 0;
    });

    this.updateContactIdsListInput();
    $recipientTagEl.remove();
  },

  conversationCreateSuccess: function(evt, data) {
    app._changeLocation(Routes.conversation(data.id));
  },
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ app.views.ConversationsInbox = Backbone.View.extend({
  },

  initialize: function() {
    new app.views.ConversationsForm({contacts: gon.contacts});
    new app.views.ConversationsForm();
    this.setupConversation();
  },

+4 −1
Original line number Diff line number Diff line
@@ -79,8 +79,11 @@ app.views.ProfileHeader = app.views.Base.extend({
  },

  showMessageModal: function(){
    $("#conversationModal").on("modal:loaded", function() {
      new app.views.ConversationsForm({prefill: gon.conversationPrefill});
    });
    app.helpers.showModal("#conversationModal");
  },
  }
});
// @license-end
+1 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ app.views.PublisherMention = app.views.SearchBase.extend({
      typeaheadInput: this.typeaheadInput,
      customSearch: true,
      autoselect: true,
      remoteRoute: "/contacts"
      remoteRoute: {url: "/contacts"}
    });
  },

Loading