-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathquerywidget.js
More file actions
387 lines (346 loc) · 15.5 KB
/
querywidget.js
File metadata and controls
387 lines (346 loc) · 15.5 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
;(function ($) {
// Define querywidget namespace if it doesn't exist
if (typeof($.querywidget) === "undefined") {
$.querywidget = {
config: {},
initialized: false
};
}
// Create a select menu
$.querywidget.createSelect = function (values, selectedvalue, className, name) {
// Create select
var select = $(document.createElement('select'))
.addClass(className)
.attr('name', name);
$.each(values, function (i, val) {
if ((typeof(val.enabled) === "undefined") || (val.enabled)) {
var option = $(document.createElement('option'))
.attr('value', i)
.html(val.title);
if (i === selectedvalue) {
option.prop('selected', 'selected');
}
if (typeof(val.group) !== "undefined") {
var optgroup = select.find("optgroup[label=" + val.group + "]");
if (optgroup.length === 0) {
optgroup = $(document.createElement('optgroup'))
.attr('label', val.group);
optgroup.append(option);
select.append(optgroup);
} else {
optgroup.append(option);
}
} else {
select.append(option);
}
}
});
return select;
};
// Create a queryindex select menu
$.querywidget.createQueryIndex = function (value) {
return $.querywidget.createSelect($.querywidget.config.indexes,
value,
'queryindex',
'query.i:records');
};
// Create a queryoperator select menu
$.querywidget.createQueryOperator = function (index, value) {
return $.querywidget.createSelect($.querywidget.config.indexes[index].operators,
value,
'queryoperator',
'query.o:records');
};
$.querywidget.createWidget = function (type, index) {
wrapper = $(document.createElement('div'));
switch (type) {
case 'StringWidget':
wrapper.load(portal_url + '/@@archetypes-querywidget-stringwidget');
break;
case 'RelativeDateWidget':
wrapper.load(portal_url + '/@@archetypes-querywidget-relativedatewidget');
break;
case 'DateWidget':
wrapper.load(portal_url + '/@@archetypes-querywidget-datewidget',
function () {
$(this).find('input')
.dateinput().removeClass('date')
.change(function (e) {
$.querywidget.updateSearch();
});
});
break;
case 'DateRangeWidget':
wrapper.load(portal_url + '/@@archetypes-querywidget-daterangewidget',
function () {
$(this).find('input')
.dateinput().removeClass('date')
.change(function (e) {
$.querywidget.updateSearch();
});
});
break;
case 'ReferenceWidget':
wrapper.load(portal_url + '/@@archetypes-querywidget-referencewidget');
break;
case 'RelativePathWidget':
wrapper.load(portal_url + '/@@archetypes-querywidget-relativepathwidget');
break;
case 'SelectionWidget':
wrapper.load(portal_url + '/@@archetypes-querywidget-selectionwidget',
{'index': index});
break;
case 'MultipleSelectionWidget':
wrapper.load(portal_url + '/@@archetypes-querywidget-multipleselectionwidget',
{'index': index});
break;
default:
wrapper.load(portal_url + '/@@archetypes-querywidget-emptywidget');
break;
}
return wrapper;
};
$.querywidget.getCurrentWidget = function (node) {
if (!node.length) {
return;
}
var classes = node.attr('class').split(' ');
for (var i in classes) {
if (classes[i].indexOf('Widget') !== -1) {
var classname = classes[i];
return classname.slice(0, 1).toUpperCase() + classname.slice(1);
}
}
};
/* Should livesearch update, looking at the last changed element? */
$.querywidget.shouldUpdate = function (node, e) {
var criteria = $(node).parents('.criteria');
var operator = criteria.children('.queryoperator').val();
var values = criteria.children('.queryvalue');
var index = criteria.children('.queryindex').val();
var querywidget = criteria.children('.querywidget');
var widgetname = $.querywidget.config.indexes[index].operators[operator].widget;
switch (widgetname) {
case 'SelectionWidget':
return true;
case 'MultipleSelectionWidget':
return true;
case 'DateRangeWidget':
/* fall through */
case 'DateWidget':
/* We use a datepicker which has it's own change handler. That
one does the refresh. */
return false;
case 'RelativeDateWidget':
return true;
default:
/* Backspace and delete should force update */
if (e.keyCode === 8 || e.keyCode === 46) {
return true;
}
if ($(node).val().length >= 3) {
return true;
}
break;
}
return false;
};
$.querywidget.updateSearch = function () {
var base_url = $("base").attr("href");
base_url = base_url.replace(/\/[a-z_]*edit$/, '');
if (base_url.indexOf("portal_factory") !== -1) {
base_url = base_url.split("/").slice(0, -2).join("/");
}
var query = base_url + "/@@querybuilder_html_results?";
var querylist = [];
var items = $('.ArchetypesQueryWidget .queryindex');
if (!items.length) {
return;
}
items.each(function () {
var results = $(this).parents('.criteria').children('.queryresults');
var index = $(this).val();
var operator = $(this).parents('.criteria').children('.queryoperator').val();
var widget = $.querywidget.config.indexes[index].operators[operator].widget;
var querywidget = $(this).parents('.criteria').find('.querywidget');
querylist.push('query.i:records=' + index);
querylist.push('query.o:records=' + operator);
function getDateWidgetValue(obj) {
// Get values from the date widget instead of the field itself, as the value
// of the field isn't yet updated at this point in the "change" event.
value = (obj.data('dateinput') && obj.data('dateinput').getValue('mm/dd/yyyy')) || '';
return value;
}
switch (widget) {
case 'DateWidget':
value = getDateWidgetValue($(this).parents('.criteria').find('.queryvalue'));
if (value) {
querylist.push('query.v:records=' + value);
}
break;
case 'DateRangeWidget':
start_date = getDateWidgetValue($(querywidget.children('input')[0]));
end_date = getDateWidgetValue($(querywidget.children('input')[1]));
querylist.push('query.v:records:list=' + start_date);
querylist.push('query.v:records:list=' + end_date);
break;
case 'MultipleSelectionWidget':
querywidget.find('input:checked').each(function () {
querylist.push('query.v:records:list=' + $(this).val());
});
break;
default:
value = $(this).parents('.criteria').find('.queryvalue').val();
push_string = 'query.v:records=';
if (value) {
push_string = push_string + value;
}
querylist.push(push_string);
break;
}
if (querylist.length) {
$.get(portal_url + '/@@querybuildernumberofresults?' + querylist.join('&'),
{},
function (data) { results.html(data); });
}
});
query += querylist.join('&');
query += '&sort_on=' + $('#sort_on').val();
if ($('#sort_order:checked').length > 0) {
query += '&sort_order=reverse';
}
$.get(query, {}, function (data) { $('.ArchetypesQueryWidget .previewresults').html(data); });
};
// Enhance for javascript browsers
$(document).ready(function () {
// Check if ArchetypesQueryWidget exists on page
if ($(".ArchetypesQueryWidget").length !== 0) {
// Init
$.querywidget.init();
}
});
// Init widget
$.querywidget.init = function () {
// Check if already initialized
if ($.querywidget.initialized === true) {
// Return nothing done
return false;
}
// Set initialized
$.querywidget.initialized = true;
// Get configuration
$.getJSON(portal_url + '/@@querybuilderjsonconfig', function (data) {
$.querywidget.config = data;
// Find querywidgets
$(".ArchetypesQueryWidget").each(function () {
// Get object
var obj = $(this);
// Hide controls used for non-javascript only
obj.find(".addIndexButton").hide();
obj.find(".multipleSelectionWidget dt").show();
obj.find(".multipleSelectionWidget dd").addClass('widgetPulldownMenu').hide();
$('div.queryindex').each(function () {
$(this).before(
$(document.createElement('div'))
.addClass('queryresults discreet')
.html('')
);
$(this).replaceWith($.querywidget.createQueryIndex($(this).children('input').val()));
});
$('div.queryoperator').each(function () {
$(this).replaceWith($.querywidget.createQueryOperator($(this).parents('.criteria').children('.queryindex').val(),
$(this).children('input').val()));
});
$.querywidget.updateSearch();
});
});
$(document).on('click', '.multipleSelectionWidget dt', function () {
$(this).parent().children('dd').toggle();
});
/* Clicking outside a multipleSelectionWidget will close all open
multipleSelectionWidgets */
$(window).click(function (event) {
if ($(event.target).parents('.multipleSelectionWidget').length) {
return;
}
$('.multipleSelectionWidget dd').hide();
});
$(document).on('change', '.queryindex', function () {
var index = $(this).find(':selected')[0].value;
$(this).parents(".criteria").children('.queryoperator')
.replaceWith($.querywidget.createQueryOperator(index, ''));
var operatorvalue = $(this).parents('.criteria').children('.queryoperator').val();
var widget = $.querywidget.config.indexes[index].operators[operatorvalue].widget;
var querywidget = $(this).parent(".criteria").find('.querywidget');
if ((widget !== $.querywidget.getCurrentWidget(querywidget)) || (widget === 'MultipleSelectionWidget')) {
querywidget.replaceWith($.querywidget.createWidget(widget, index));
}
$.querywidget.updateSearch();
});
$(document).on('change', '.queryoperator', function () {
var index = $(this).parents('.criteria').children('.queryindex').val();
var operatorvalue = $(this).children(':selected')[0].value;
var widget = $.querywidget.config.indexes[index].operators[operatorvalue].widget;
var querywidget = $(this).parent().find('.querywidget');
if (widget !== $.querywidget.getCurrentWidget(querywidget)) {
querywidget.replaceWith($.querywidget.createWidget(widget, index));
}
$.querywidget.updateSearch();
});
$(document).on('change', '#sort_on, #sort_order', function () {
$.querywidget.updateSearch();
});
$(document).on('change', '.multipleSelectionWidget input', function () {
var widget = $(this).parents('.multipleSelectionWidget');
var selected_values = [];
widget.find('input:checked').each(function () {
selected_values.push($(this).parent().children('span').html());
});
widget.find('.multipleSelectionWidgetTitle')
.attr('title', selected_values.join(', '))
.html(selected_values.join(', '));
$.querywidget.updateSearch();
});
$(document).on('keyup', '.queryvalue', function (e) {
if ($.querywidget.shouldUpdate(this, e)) {
$.querywidget.updateSearch();
}
});
$(document).on('change', '.selectionWidget', function (e) {
if ($.querywidget.shouldUpdate(this, e)) {
$.querywidget.updateSearch();
}
});
$(document).on('keydown', '.queryvalue', function (e) {
if (e.keyCode === 13) {
return false;
}
});
$(document).on('change', '.addIndex', function () {
var index = $(this).find(':selected')[0].value;
var criteria = $(this).parents('.criteria');
var newcriteria = $(document.createElement('div'))
.addClass('criteria');
newcriteria.append(
$(document.createElement('div'))
.addClass('queryresults discreet')
.html('')
);
newcriteria.append($.querywidget.createQueryIndex(index));
var operator = $.querywidget.createQueryOperator(index, '');
newcriteria.append(operator);
var operatorvalue = $(operator.children()[0]).attr('value');
newcriteria.append($.querywidget.createWidget($.querywidget.config.indexes[index].operators[operatorvalue].widget, index));
$.get(portal_url + '/@@archetypes-querywidget-removecriterialink',
{}, function (data) { newcriteria.append(data); });
criteria.before(newcriteria);
$(this).val('');
$.querywidget.updateSearch();
});
$(document).on('click', '.removecriteria', function () {
$(this).parents('.criteria').remove();
$.querywidget.updateSearch();
return false;
});
};
})(jQuery);