podio / jquery-mentions-input

MIT License
985 stars 333 forks source link

Add support for a default value on input #131

Closed fneves closed 9 years ago

fneves commented 9 years ago

This PR adds support for a default value on the textarea.

Usage example:

('textarea.mention').mentionsInput({
  defaultValue: 'This is the default value',
  onDataRequest: function (mode, query, callback) {
    var data = [
      { id:1, name:'Kenneth Auchenberg', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
      { id:2, name:'Jon Froda', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
      { id:3, name:'Anders Pollas', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
      { id:4, name:'Kasper Hulthin', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
      { id:5, name:'Andreas Haugstrup', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
      { id:6, name:'Pete Lacey', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' }
    ];

    data = _.filter(data, function(item) { return item.name.toLowerCase().indexOf(query.toLowerCase()) > -1 });

    callback.call(this, data);
  }
});
djhvscf commented 9 years ago

Thanks

grosser commented 9 years ago

@djhvscf can you tag / release this, I want to install via bower

lysender commented 9 years ago

Great patch! However, I'm having issues when trying to pre-populate mentions and single quotes, ex:

$('#task-comment').mentionsInput({
    defaultValue: "this is the default value @[Firstname Lastname](user:USR-340001431403696) what's going on?",
    minChars: 1,
    onDataRequest:function (mode, query, callback) {
        var data = window.usersToTag;
        data = _.filter(data, function(item) { return item.name.toLowerCase().indexOf(query.toLowerCase()) > -1 });
        callback.call(this, data);
    }
});

It encodes the single quote again, and a "@" symbol appears before the name. I'm trying to implement edit mode, ex: editing comments with mentions on it.

Thanks

lysender commented 9 years ago

What I did, to temporarily achieve what we want (ex: populate textarea for editing), I edited the code to:

         //Resets the text area
         function resetInput(currentVal) {
             mentionsCollection = [];
    -        var mentionText = utils.htmlEncode(currentVal);
    +        var mentionText = currentVal;
             var regex = new RegExp("(" + settings.triggerChar + ")\\[(.*?)\\]\\((.*?):(.*?)\\)", "gi");
             var match, newMentionText = mentionText;
             while ((match = regex.exec(mentionText)) != null) {
    -            newMentionText = newMentionText.replace(match[0], match[1] + match[2]);
    +            newMentionText = newMentionText.replace(match[0], match[2]);
                 mentionsCollection.push({ 'id': match[4], 'type': match[3], 'value': match[2], 'trigger': match[1] });
             }
             elmInputBox.val(newMentionText);

What do you think?

varun-raj commented 8 years ago

defaultValue is not working for me :/

grosser commented 8 years ago

possibly because it was never released ... @djhvscf can you tag a new version please ?