gitlab-org--gitlab-foss/app/assets/javascripts/gfm_auto_complete.js.es6

401 lines
13 KiB
JavaScript
Raw Normal View History

/* eslint-disable func-names, space-before-function-paren, no-template-curly-in-string, comma-dangle, object-shorthand, quotes, dot-notation, no-else-return, one-var, no-var, no-underscore-dangle, one-var-declaration-per-line, no-param-reassign, no-useless-escape, prefer-template, consistent-return, wrap-iife, prefer-arrow-callback, camelcase, no-unused-vars, no-useless-return, padded-blocks, vars-on-top, indent, no-extra-semi, no-multi-spaces, semi, max-len */
// Creates the variables for setting up GFM auto-completion
2016-07-24 20:45:11 +00:00
(function() {
if (window.GitLab == null) {
window.GitLab = {};
}
2016-11-07 00:16:39 +00:00
function sanitize(str) {
return str.replace(/<(?:.|\n)*?>/gm, '');
}
window.GitLab.GfmAutoComplete = {
2016-07-24 20:45:11 +00:00
dataLoading: false,
dataLoaded: false,
cachedData: {},
dataSource: '',
// Emoji
2016-07-24 20:45:11 +00:00
Emoji: {
template: '<li>${name} <img alt="${name}" height="20" src="${path}" width="20" /></li>'
},
// Team Members
2016-07-24 20:45:11 +00:00
Members: {
template: '<li>${avatarTag} ${username} <small>${title}</small></li>'
2016-07-24 20:45:11 +00:00
},
Labels: {
template: '<li><span class="dropdown-label-box" style="background: ${color}"></span> ${title}</li>'
},
// Issues and MergeRequests
2016-07-24 20:45:11 +00:00
Issues: {
template: '<li><small>${id}</small> ${title}</li>'
},
// Milestones
2016-07-24 20:45:11 +00:00
Milestones: {
template: '<li>${title}</li>'
},
Loading: {
template: '<li><i class="fa fa-refresh fa-spin"></i> Loading...</li>'
},
DefaultOptions: {
sorter: function(query, items, searchKey) {
2016-10-26 21:02:36 +00:00
// Highlight first item only if at least one char was typed
this.setting.highlightFirst = this.setting.alwaysHighlightFirst || query.length > 0;
2016-07-24 20:45:11 +00:00
if ((items[0].name != null) && items[0].name === 'loading') {
return items;
}
return $.fn.atwho["default"].callbacks.sorter(query, items, searchKey);
},
filter: function(query, data, searchKey) {
if (data[0] === 'loading') {
return data;
}
return $.fn.atwho["default"].callbacks.filter(query, data, searchKey);
},
beforeInsert: function(value) {
if (value && !this.setting.skipSpecialCharacterTest) {
var withoutAt = value.substring(1);
if (withoutAt && /[^\w\d]/.test(withoutAt)) value = value.charAt() + '"' + withoutAt + '"';
}
if (!window.GitLab.GfmAutoComplete.dataLoaded) {
2016-07-24 20:45:11 +00:00
return this.at;
} else {
return value;
}
},
matcher: function (flag, subtext) {
// The below is taken from At.js source
// Tweaked to commands to start without a space only if char before is a non-word character
// https://github.com/ichord/At.js
var _a, _y, regexp, match;
subtext = subtext.split(' ').pop();
flag = flag.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
_a = decodeURI("%C3%80");
_y = decodeURI("%C3%BF");
regexp = new RegExp("(?:\\B|\\W|\\s)" + flag + "(?!\\W)([A-Za-z" + _a + "-" + _y + "0-9_\'\.\+\-]*)|([^\\x00-\\xff]*)$", 'gi');
match = regexp.exec(subtext);
if (match) {
return match[2] || match[1];
} else {
return null;
}
2016-07-24 20:45:11 +00:00
}
},
setup: _.debounce(function(input) {
// Add GFM auto-completion to all input fields, that accept GFM input.
this.input = input || $('.js-gfm-input');
// destroy previous instances
2016-07-24 20:45:11 +00:00
this.destroyAtWho();
// set up instances
2016-07-24 20:45:11 +00:00
this.setupAtWho();
if (this.dataSource && !this.dataLoading && !this.cachedData) {
this.dataLoading = true;
return this.fetchData(this.dataSource)
.done((data) => {
this.dataLoading = false;
this.loadData(data);
});
};
if (this.cachedData != null) {
return this.loadData(this.cachedData);
2016-07-24 20:45:11 +00:00
}
}, 1000),
2016-07-24 20:45:11 +00:00
setupAtWho: function() {
// Emoji
2016-07-24 20:45:11 +00:00
this.input.atwho({
at: ':',
displayTpl: (function(_this) {
return function(value) {
if (value.path != null) {
return _this.Emoji.template;
} else {
return _this.Loading.template;
}
};
})(this),
insertTpl: ':${name}:',
data: ['loading'],
startWithSpace: false,
skipSpecialCharacterTest: true,
2016-07-24 20:45:11 +00:00
callbacks: {
sorter: this.DefaultOptions.sorter,
filter: this.DefaultOptions.filter,
beforeInsert: this.DefaultOptions.beforeInsert,
matcher: this.DefaultOptions.matcher
2016-07-24 20:45:11 +00:00
}
});
// Team Members
2016-07-24 20:45:11 +00:00
this.input.atwho({
at: '@',
displayTpl: (function(_this) {
return function(value) {
if (value.username != null) {
return _this.Members.template;
} else {
return _this.Loading.template;
}
};
})(this),
insertTpl: '${atwho-at}${username}',
searchKey: 'search',
data: ['loading'],
startWithSpace: false,
alwaysHighlightFirst: true,
skipSpecialCharacterTest: true,
2016-07-24 20:45:11 +00:00
callbacks: {
sorter: this.DefaultOptions.sorter,
filter: this.DefaultOptions.filter,
beforeInsert: this.DefaultOptions.beforeInsert,
matcher: this.DefaultOptions.matcher,
2016-07-24 20:45:11 +00:00
beforeSave: function(members) {
return $.map(members, function(m) {
let title = '';
2016-07-24 20:45:11 +00:00
if (m.username == null) {
return m;
}
title = m.name;
if (m.count) {
title += " (" + m.count + ")";
}
const autoCompleteAvatar = m.avatar_url || m.username.charAt(0).toUpperCase();
const imgAvatar = `<img src="${m.avatar_url}" alt="${m.username}" class="avatar avatar-inline center s26"/>`;
const txtAvatar = `<div class="avatar center avatar-inline s26">${autoCompleteAvatar}</div>`;
2016-07-24 20:45:11 +00:00
return {
username: m.username,
avatarTag: autoCompleteAvatar.length === 1 ? txtAvatar : imgAvatar,
2016-11-07 00:16:39 +00:00
title: sanitize(title),
search: sanitize(m.username + " " + m.name)
2016-07-24 20:45:11 +00:00
};
});
}
}
});
this.input.atwho({
at: '#',
alias: 'issues',
searchKey: 'search',
displayTpl: (function(_this) {
return function(value) {
if (value.title != null) {
return _this.Issues.template;
} else {
return _this.Loading.template;
}
};
})(this),
data: ['loading'],
insertTpl: '${atwho-at}${id}',
startWithSpace: false,
2016-07-24 20:45:11 +00:00
callbacks: {
sorter: this.DefaultOptions.sorter,
filter: this.DefaultOptions.filter,
beforeInsert: this.DefaultOptions.beforeInsert,
matcher: this.DefaultOptions.matcher,
2016-07-24 20:45:11 +00:00
beforeSave: function(issues) {
return $.map(issues, function(i) {
if (i.title == null) {
return i;
}
return {
id: i.iid,
2016-11-07 00:16:39 +00:00
title: sanitize(i.title),
2016-07-24 20:45:11 +00:00
search: i.iid + " " + i.title
};
});
}
}
});
this.input.atwho({
at: '%',
alias: 'milestones',
searchKey: 'search',
displayTpl: (function(_this) {
return function(value) {
if (value.title != null) {
return _this.Milestones.template;
} else {
return _this.Loading.template;
}
};
})(this),
insertTpl: '${atwho-at}${title}',
2016-07-24 20:45:11 +00:00
data: ['loading'],
startWithSpace: false,
2016-07-24 20:45:11 +00:00
callbacks: {
matcher: this.DefaultOptions.matcher,
2016-10-26 21:02:36 +00:00
sorter: this.DefaultOptions.sorter,
beforeInsert: this.DefaultOptions.beforeInsert,
2016-07-24 20:45:11 +00:00
beforeSave: function(milestones) {
return $.map(milestones, function(m) {
if (m.title == null) {
return m;
}
return {
id: m.iid,
2016-11-07 00:16:39 +00:00
title: sanitize(m.title),
2016-07-24 20:45:11 +00:00
search: "" + m.title
};
});
}
}
});
this.input.atwho({
at: '!',
alias: 'mergerequests',
searchKey: 'search',
displayTpl: (function(_this) {
return function(value) {
if (value.title != null) {
return _this.Issues.template;
} else {
return _this.Loading.template;
}
};
})(this),
data: ['loading'],
startWithSpace: false,
2016-07-24 20:45:11 +00:00
insertTpl: '${atwho-at}${id}',
callbacks: {
sorter: this.DefaultOptions.sorter,
filter: this.DefaultOptions.filter,
beforeInsert: this.DefaultOptions.beforeInsert,
matcher: this.DefaultOptions.matcher,
2016-07-24 20:45:11 +00:00
beforeSave: function(merges) {
return $.map(merges, function(m) {
if (m.title == null) {
return m;
}
return {
id: m.iid,
2016-11-07 00:16:39 +00:00
title: sanitize(m.title),
2016-07-24 20:45:11 +00:00
search: m.iid + " " + m.title
};
});
}
}
});
this.input.atwho({
2016-07-24 20:45:11 +00:00
at: '~',
alias: 'labels',
searchKey: 'search',
displayTpl: this.Labels.template,
insertTpl: '${atwho-at}${title}',
startWithSpace: false,
2016-07-24 20:45:11 +00:00
callbacks: {
matcher: this.DefaultOptions.matcher,
2016-10-26 21:02:36 +00:00
sorter: this.DefaultOptions.sorter,
beforeInsert: this.DefaultOptions.beforeInsert,
2016-07-24 20:45:11 +00:00
beforeSave: function(merges) {
return $.map(merges, function(m) {
return {
title: sanitize(m.title),
2016-07-24 20:45:11 +00:00
color: m.color,
search: "" + m.title
};
});
}
}
});
2016-08-13 01:19:16 +00:00
// We don't instantiate the slash commands autocomplete for note and issue/MR edit forms
this.input.filter('[data-supports-slash-commands="true"]').atwho({
at: '/',
alias: 'commands',
searchKey: 'search',
skipSpecialCharacterTest: true,
displayTpl: function(value) {
var tpl = '<li>/${name}';
if (value.aliases.length > 0) {
tpl += ' <small>(or /<%- aliases.join(", /") %>)</small>';
}
if (value.params.length > 0) {
tpl += ' <small><%- params.join(" ") %></small>';
}
if (value.description !== '') {
tpl += '<small class="description"><i><%- description %></i></small>';
}
tpl += '</li>';
return _.template(tpl)(value);
},
insertTpl: function(value) {
var tpl = "/${name} ";
var reference_prefix = null;
if (value.params.length > 0) {
reference_prefix = value.params[0][0];
if (/^[@%~]/.test(reference_prefix)) {
tpl += '<%- reference_prefix %>';
}
}
return _.template(tpl)({ reference_prefix: reference_prefix });
},
suffix: '',
callbacks: {
sorter: this.DefaultOptions.sorter,
filter: this.DefaultOptions.filter,
beforeInsert: this.DefaultOptions.beforeInsert,
beforeSave: function(commands) {
return $.map(commands, function(c) {
var search = c.name;
if (c.aliases.length > 0) {
search = search + " " + c.aliases.join(" ");
}
return {
name: c.name,
aliases: c.aliases,
params: c.params,
description: c.description,
search: search
};
});
},
matcher: function(flag, subtext, should_startWithSpace, acceptSpaceBar) {
var regexp = /(?:^|\n)\/([A-Za-z_]*)$/gi
var match = regexp.exec(subtext);
if (match) {
return match[1];
} else {
return null;
}
}
}
});
return;
2016-07-24 20:45:11 +00:00
},
destroyAtWho: function() {
return this.input.atwho('destroy');
},
fetchData: function(dataSource) {
return $.getJSON(dataSource);
},
loadData: function(data) {
this.cachedData = data;
this.dataLoaded = true;
// load members
2016-07-24 20:45:11 +00:00
this.input.atwho('load', '@', data.members);
// load issues
2016-07-24 20:45:11 +00:00
this.input.atwho('load', 'issues', data.issues);
// load milestones
2016-07-24 20:45:11 +00:00
this.input.atwho('load', 'milestones', data.milestones);
// load merge requests
2016-07-24 20:45:11 +00:00
this.input.atwho('load', 'mergerequests', data.mergerequests);
// load emojis
2016-07-24 20:45:11 +00:00
this.input.atwho('load', ':', data.emojis);
// load labels
2016-07-24 20:45:11 +00:00
this.input.atwho('load', '~', data.labels);
// load commands
this.input.atwho('load', '/', data.commands);
// This trigger at.js again
// otherwise we would be stuck with loading until the user types
2016-07-24 20:45:11 +00:00
return $(':focus').trigger('keyup');
}
};
}).call(this);