forked from ReDEnergy/SessionSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-filtering.js
More file actions
139 lines (106 loc) · 3.7 KB
/
Copy pathsession-filtering.js
File metadata and controls
139 lines (106 loc) · 3.7 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
define(function(require, exports) {
'use strict';
// *****************************************************************************
// Custom Modules
const { AppConfig } = require('../config');
const { WindowEvents, GlobalEvents } = require('../utils/global-events');
const { HTMLCreator } = require('../utils/dom');
// *****************************************************************************
// API
/*
* SessionFiltering
*/
function SessionFiltering()
{
var DomElem = HTMLCreator();
// ------------------------------------------------------------------------
// Create UI
var panel = DomElem('div', {class: 'search-bar', content: 'false', match: 'true'});
var createSession = DomElem('div', {class: 'create-session' });
panel.appendChild(createSession);
var close = DomElem('div', {class: 'close' });
panel.appendChild(close);
var searchInput = DomElem('input', {type: 'text', class: 'search', placeholder:'Search'});
panel.appendChild(searchInput);
// ------------------------------------------------------------------------
// Methods
var timeOut = null;
var prevValue = '';
var dismiss = function dismiss()
{
clearTimeout(timeOut);
if (searchInput.value.length) {
prevValue = '';
searchInput.value = prevValue;
WindowEvents.emit(document, 'FilterSessions', searchInput.value);
}
searchInput.blur();
onBlur();
panel.setAttribute('content', searchInput.value != '');
WindowEvents.emit(document, 'SetUIState', {'search-box-content': searchInput.value != ''});
};
var onKeyDown = function()
{
if (prevValue == searchInput.value)
return;
prevValue = searchInput.value;
panel.setAttribute('match', 'false');
panel.setAttribute('content', searchInput.value != '');
WindowEvents.emit(document, 'SetUIState', {'search-box-content': searchInput.value != ''});
clearTimeout(timeOut);
timeOut = setTimeout(function () {
WindowEvents.emit(document, 'FilterSessions', searchInput.value);
}, 200);
};
var onFocus = function onFocus() {
panel.setAttribute('active', true);
document.addEventListener('keyup', onKeyDown);
};
var onBlur = function onBlur() {
if (searchInput.value == '') {
panel.setAttribute('active', false);
}
document.removeEventListener('keyup', onKeyDown);
};
// ------------------------------------------------------------------------
// Events
// Tooltip events
createSession.addEventListener('mouseover', function(e) {
WindowEvents.emit(document, 'ShowTooltip', {
node: e.target,
message: 'Create session',
});
});
createSession.addEventListener('mouseleave', function() {
WindowEvents.emit(document, 'HideTooltip');
});
createSession.addEventListener('click', function() {
GlobalEvents.emit('CreateNewSession', searchInput.value);
WindowEvents.emit(document, 'HideTooltip');
});
close.addEventListener('click', dismiss);
searchInput.addEventListener('focus', onFocus);
searchInput.addEventListener('blur', onBlur);
WindowEvents.on(document, 'FilterSessionsExactMatch', function(isMatch) {
panel.setAttribute('match', isMatch);
});
WindowEvents.on(document, 'FocusSessionFilter', function () {
searchInput.focus();
});
// ------------------------------------------------------------------------
// Init state
var settingsKey = 'session.active.filter';
AppConfig.onChange(settingsKey, function(value) {
if (value && value.length > 0) {
searchInput.value = value;
panel.setAttribute('active', true);
onKeyDown();
}
});
// ------------------------------------------------------------------------
// Public properties
this.DOMRoot = panel;
}
// Public API
exports.SessionFiltering = SessionFiltering;
});