-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathautocomplete.js
More file actions
95 lines (78 loc) · 2.65 KB
/
autocomplete.js
File metadata and controls
95 lines (78 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
/* globals define */
define('composer/autocomplete', ['composer/preview'], function(preview) {
var autocomplete = {
_active: {},
};
$(window).on('action:composer.discard', function (evt, data) {
if (autocomplete._active.hasOwnProperty(data.post_uuid)) {
autocomplete._active[data.post_uuid].destroy();
delete autocomplete._active[data.post_uuid];
}
});
autocomplete.init = function(postContainer, post_uuid) {
var element = postContainer.find('.write');
var dropdownClass = 'composer-autocomplete-dropdown-' + post_uuid;
if (!element.length) {
/**
* Some composers do their own thing before calling autocomplete.init() again.
* One reason is because they want to override the textarea with their own element.
* In those scenarios, they don't specify the "write" class, and this conditional
* looks for that and stops the autocomplete init process.
**/
return;
}
var data = {
element: element,
strategies: [],
options: {
placement: 'auto',
style: {
'z-index': 20000,
},
className: dropdownClass + ' dropdown-menu textcomplete-dropdown',
}
};
$(window).trigger('composer:autocomplete:init', data);
autocomplete._active[post_uuid] = autocomplete.setup(data);
data.element.on('textComplete:select', function() {
preview.render(postContainer);
});
};
// This is a generic method that is also used by the chat
autocomplete.setup = function (data) {
var element = data.element.get(0);
if (!element) {
return;
}
var editor;
if (element.nodeName === 'TEXTAREA') {
var Textarea = window.Textcomplete.editors.Textarea;
editor = new Textarea(element);
} else if (element.nodeName === 'DIV' && element.getAttribute('contenteditable') === 'true') {
var ContentEditable = window.Textcomplete.editors.ContentEditable;
editor = new ContentEditable(element);
}
// yuku-t/textcomplete inherits directionality from target element itself
element.setAttribute('dir', document.querySelector('html').getAttribute('data-dir'));
var textcomplete = new window.Textcomplete(editor, {
dropdown: data.options,
});
// hack till https://github.com/yuku/textcomplete/issues/166
var _getCursorOffset = editor.getCursorOffset;
editor.getCursorOffset = function () {
var offset = _getCursorOffset.apply(editor, arguments);
offset.clientTop = offset.top;
return offset;
};
textcomplete.register(data.strategies);
textcomplete.on('rendered', function () {
if (textcomplete.dropdown.items.length) {
// Activate the first item by default.
textcomplete.dropdown.items[0].activate();
}
});
return textcomplete;
};
return autocomplete;
});