-
Notifications
You must be signed in to change notification settings - Fork 0
/
query-builder-subfield.js
355 lines (305 loc) · 12.1 KB
/
query-builder-subfield.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright 2016, Yahoo Inc.
* Licensed under the terms of the Apache License, Version 2.0.
* See the LICENSE file associated with the project for terms.
*/
/*
* jQuery QueryBuilder Subfield
* This plugin lets you define a free form field in addition to choosing a filter operator. It also
* works with the SQLPlugin.
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery', 'query-builder'], factory);
}
else {
factory(root.jQuery);
}
}(this, function($) {
"use strict";
var QueryBuilder = $.fn.queryBuilder;
QueryBuilder.prototype = $.fn.queryBuilder.constructor.prototype;
QueryBuilder.Rule = $.fn.queryBuilder.constructor.Rule;
QueryBuilder.templates = $.fn.queryBuilder.constructor.templates;
QueryBuilder.selectors = $.fn.queryBuilder.constructor.selectors;
// Unfortunately, the events system is not sufficient. Need to override these methods to implement adding a new text field.
var originalUpdateRuleFilter = QueryBuilder.prototype.updateRuleFilter;
var originalUpdateRuleOperator = QueryBuilder.prototype.updateRuleOperator;
var originalGetRules = QueryBuilder.prototype.getRules;
var originalSetRules = QueryBuilder.prototype.setRules;
var originalAddRule = QueryBuilder.prototype.addRule;
var SubfieldSelectors = {
subfield_container: '.rule-subfield-container',
rule_subfield: '.rule-subfield-container [name$=_subfield]'
};
QueryBuilder.defaults({
fieldSubfieldSeparator: '.',
fieldSuffixForSubfield: '.*',
subfield_template: {
subfieldInput: null
}
});
/*
* Derived from defineModelProperties
*/
function defineModelSubfieldProperty(obj, fields) {
fields.forEach(function(field) {
Object.defineProperty(obj.prototype, field, {
enumerable: true,
get: function() {
return this.__[field];
},
set: function(value) {
var oldValue = (this.__[field] !== null && typeof this.__[field] == 'object') ?
$.extend({}, this.__[field]) :
this.__[field];
this.__[field] = value;
if (this.model !== null) {
this.model.trigger('updateSubfield', this, field, value, oldValue);
}
}
});
});
}
/**
* Add a subfield property to the Rule defined in QueryBuilder
*/
defineModelSubfieldProperty(QueryBuilder.Rule, ['subfield']);
QueryBuilder.templates.subfieldInput = '\
<input class="form-control" name="{{= it.rule.id }}_subfield" placeholder="subfield"> \
</input>';
QueryBuilder.define('subfield', function() {
var that = this;
this.on('afterInit', function() {
that.subfield_template = that.settings.subfield_template;
// add subfield template
Object.keys(that.subfield_template).forEach(function(tpl) {
if (!that.templates[tpl]) {
that.templates[tpl] = QueryBuilder.templates[tpl];
}
if (typeof that.templates[tpl] == 'string') {
that.templates[tpl] = doT.template(that.templates[tpl]);
}
}, that);
// bind update subfield event
that.bindSubfieldEvent();
});
this.on('getRuleTemplate.filter', function(h) {
var $h = $(h.value);
$h.find(QueryBuilder.selectors.operator_container).before('<div class="rule-subfield-container"></div>');
h.value = $h.prop('outerHTML');
});
this.on('getSQLField.queryBuilder.filter', function(e, rule) {
if (rule.subfield) {
// Remove the suffix from the field and add on the subfield separator
var oldfield = e.value;
e.value = oldfield.substring(0, oldfield.lastIndexOf(that.settings.fieldSuffixForSubfield)) + that.settings.fieldSubfieldSeparator + rule.subfield;
}
});
this.on('sqlToRule.queryBuilder.filter', function(e) {
var rule = e.value;
if (rule.field) {
var split_field = rule.field.split(that.settings.fieldSubfieldSeparator);
var length = split_field.length;
if (length >= 2) {
// Search the filters to make sure this field doesn't exist exactly. If so, don't subfield it
var filter = e.builder.filters.find(function(filter) {
return filter.id === rule.field;
});
if (!filter) {
rule.field = split_field.slice(0, -1).join(that.settings.fieldSubfieldSeparator);
rule.subfield = split_field[length - 1];
}
}
}
e.value = rule;
});
});
QueryBuilder.extend({
bindSubfieldEvent : function() {
var that = this;
// model events
this.model.on({
'updateSubfield': function(e, node, field, value, oldValue) {
if (node instanceof QueryBuilder.Rule) {
switch (field) {
case 'error':
that.displayError(node);
break;
case 'subfield':
that.updateRuleSubfield(node);
break;
}
}
else {
switch (field) {
case 'error':
that.displayError(node);
break;
case 'flags':
that.applyGroupFlags(node);
break;
case 'condition':
that.updateGroupCondition(node);
break;
}
}
}
});
},
getSubfieldInput : function(rule) {
var h = this.templates.subfieldInput({
builder: this,
rule: rule
});
return this.change('getSubfieldInput', h, rule);
},
createRuleSubfield : function(rule) {
var $subfieldContainer = rule.$el.find(SubfieldSelectors.subfield_container);
$subfieldContainer.empty();
rule.__.subfield = undefined;
if (!rule.filter.show_subfield) {
return;
}
var $subfieldInput = $(this.getSubfieldInput(rule));
$subfieldContainer.html($subfieldInput);
var that = this, filter = rule.filter;
$subfieldInput.on('change ' + (filter.input_event || ''), function() {
that.status.updating_subfield = true;
rule.subfield = that.getRuleSubfield(rule);
that.status.updating_subfield = false;
});
this.trigger('afterCreateRuleSubfield', rule);
that.status.updating_subfield = true;
rule.subfield = that.getRuleSubfield(rule);
that.status.updating_subfield = false;
},
getRuleSubfield : function(rule) {
var filter = rule.filter,
operator = rule.operator,
value = [];
var $value = rule.$el.find(SubfieldSelectors.subfield_container);
var name = rule.id + '_subfield';
value.push($value.find('[name='+ name +']').val());
return this.change('getRuleSubfield', value, rule);
},
setRuleSubfield : function(rule, value) {
var $value = rule.$el.find(SubfieldSelectors.subfield_container);
var name = rule.id +'_subfield';
$value.find('[name='+ name +']').val(value).trigger('change');
},
updateRuleSubfield : function(rule) {
if (!this.status.updating_subfield) {
this.setRuleSubfield(rule, rule.subfield);
}
this.trigger('afterUpdateRuleSubfield', rule);
},
/*
* Extend original updateRuleFilter
*/
updateRuleFilter : function(rule) {
this.createRuleSubfield(rule);
originalUpdateRuleFilter.call(this, rule);
},
/*
* Extend original updateRuleOperator
*/
updateRuleOperator : function(rule, previousOperator) {
var previousOperatorOverride = {};
previousOperatorOverride.nb_inputs = -1;
originalUpdateRuleOperator.call(this, rule, previousOperatorOverride);
},
// ----------- For getting rules --------------------------
preSetSubfieldFromDataForRule: function(rule) {
if (rule.subfield) {
if (!rule.data) {
rule.data = {};
}
rule.data.subfield = rule.subfield;
} else {
if (rule.data && rule.data.subfield) {
delete rule.data.subfield;
}
}
},
preSetSubfieldFromDataForGroup: function(group) {
var that = this;
group.each(
function(rule) {
that.preSetSubfieldFromDataForRule(rule);
},
function(group) {
that.preSetSubfieldFromDataForGroup(group);
}
);
},
postSetSubfieldFromData : function(data) {
var that = this;
if (data.rules && data.rules.length > 0) {
data.rules.forEach(function(item) {
that.postSetSubfieldFromData(item);
});
} else {
var rule = data;
if (rule.data && rule.data.subfield) {
rule.subfield = rule.data.subfield;
delete rule.data.subfield;
}
}
},
/*
* Extend the original getRules
*/
getRules : function(options) {
this.preSetSubfieldFromDataForGroup(this.model.root);
var data = originalGetRules.call(this, options);
this.postSetSubfieldFromData(data);
return data;
},
// ------------ For setting rules ----------------------
preSetSubfieldToData : function(data) {
var that = this;
if (data.rules && data.rules.length > 0) {
data.rules.forEach(function(item) {
that.preSetSubfieldToData(item);
});
} else {
var rule = data;
if (rule.subfield) {
if (!rule.data) {
rule.data = {};
}
rule.data.subfield = rule.subfield;
}
}
},
postSetSubfieldToDataForRule : function(rule) {
if (rule.data && rule.data.subfield) {
var subfieldTmp = [];
subfieldTmp.push(rule.data.subfield);
rule.subfield = subfieldTmp;
}
},
postSetSubfieldToDataForGroup: function(group) {
var that = this;
group.each(
function(rule) {
that.postSetSubfieldToDataForRule(rule);
},
function(group) {
that.postSetSubfieldToDataForGroup(group);
}
);
},
/*
* Extend the original setRules
*/
setRules : function(data) {
// Add the subfield information to the data object
this.preSetSubfieldToData(data);
originalSetRules.call(this, data);
// Add the subfield information from the data objects to the model
this.postSetSubfieldToDataForGroup(this.model.root);
},
});
}));