-
Notifications
You must be signed in to change notification settings - Fork 56
/
autocomplete.js
68 lines (56 loc) · 1.8 KB
/
autocomplete.js
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
'use strict';
define('composer/autocomplete', [
'composer/preview', 'autocomplete',
], function (preview, Autocomplete) {
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;
var timer;
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: {
style: {
'z-index': 20000,
},
className: dropdownClass + ' dropdown-menu textcomplete-dropdown',
},
};
element.on('keyup', function () {
clearTimeout(timer);
timer = setTimeout(function () {
var dropdown = document.querySelector('.' + dropdownClass);
if (dropdown) {
var pos = dropdown.getBoundingClientRect();
var margin = parseFloat(dropdown.style.marginTop, 10) || 0;
var offset = window.innerHeight + margin - 10 - pos.bottom;
dropdown.style.marginTop = Math.min(offset, 0) + 'px';
}
}, 0);
});
$(window).trigger('composer:autocomplete:init', data);
autocomplete._active[post_uuid] = Autocomplete.setup(data);
data.element.on('textComplete:select', function () {
preview.render(postContainer);
});
};
return autocomplete;
});