forked from brendo/textboxfield
-
Notifications
You must be signed in to change notification settings - Fork 5
/
textboxfield.datasources.js
157 lines (120 loc) · 4.06 KB
/
textboxfield.datasources.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*-----------------------------------------------------------------------------
Text Box Interface
-----------------------------------------------------------------------------*/
jQuery.fn.filterTextBox = function(custom_settings) {
/*-------------------------------------------------------------------------
Initialise
-------------------------------------------------------------------------*/
var objects = jQuery(this);
var settings = {
input: '> label > input, > .content > label > input',
help: '> p, > .content > p',
filters: '.filter-prefix-suggestions > li'
};
jQuery.extend(settings, custom_settings);
/*-------------------------------------------------------------------------
Objects
-------------------------------------------------------------------------*/
objects = objects.map(function() {
var object = jQuery(this);
var message = '';
var widgets = {
container: null,
input: null,
help: null,
filters: null
};
var methods = {
filters: function() {
var filter = jQuery(this);
var input = widgets.input.get(0);
var value = widgets.input.val();
var match = new RegExp('^' + filter.attr('title') + '\\s*');
var selection_start = input.selectionStart;
var selection_diff = input.selectionEnd - selection_start;
var selection_track = true;
if (match.test(value)) {
var matched = match.exec(value)[0];
value = value.replace(match, '');
if (selection_start < matched.length) {
selection_track = false;
}
else {
selection_start = selection_start - matched.length;
}
}
else {
widgets.filters.each(function() {
var current_filter = jQuery(this);
var current_match = new RegExp('^' + current_filter.attr('title') + '\\s*');
if (filter != current_filter && current_match.test(value)) {
var matched = current_match.exec(value)[0];
value = value.replace(current_match, '');
current_filter.removeClass('filter-selected');
if (selection_start < matched.length) {
selection_track = false;
}
else {
selection_start = selection_start - matched.length;
}
}
});
value = filter.attr('title') + ' ' + value;
if (selection_track) {
selection_start = selection_start + filter.attr('title').length + 1;
}
}
widgets.input.val(value).focus();
if (selection_track) {
input.selectionStart = selection_start;
input.selectionEnd = selection_start + selection_diff;
}
else {
input.selectionStart = 0;
input.selectionEnd = 0;
}
methods.refresh();
return false;
},
refresh: function() {
var value = widgets.input.val();
var matched = false;
widgets.filters.each(function() {
var filter = jQuery(this);
var match = new RegExp('^' + filter.attr('title') + '\\s*');
if (match.test(value)) {
matched = true;
filter.addClass('filter-selected');
widgets.help.html(filter.attr('alt'));
}
else {
filter.removeClass('filter-selected');
}
});
if (!matched) widgets.help.html(message);
},
silence: function() { return false; }
};
// Initialize objects:
widgets.container = object.addClass('initialised');
widgets.input = object.find(settings.input);
widgets.input.keyup(methods.refresh);
// Has help?
widgets.help = object.find(settings.help);
message = widgets.help.html();
// Has filters?
widgets.filters = object.find(settings.filters);
widgets.filters.mousedown(methods.silence);
widgets.filters.click(methods.filters);
methods.refresh();
return object;
});
return objects;
};
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function() {
jQuery('.field-textbox').filterTextBox();
jQuery('.filters-duplicator').on('click.duplicator', '.constructor:not(.disabled)', function() {
jQuery('.field-textbox:not(.initialised)').filterTextBox();
})
});