-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-builder-placeholders.js
60 lines (55 loc) · 2.28 KB
/
query-builder-placeholders.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
/*
* 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 Placeholders
* Allows you to add custom placeholders per type of the filter operation by
* defining a placeholders object with keys being the type of the operator
* and value the placeholder string.
*/
(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;
var originalGetRuleInput = QueryBuilder.prototype.getRuleInput;
QueryBuilder.define('placeholders', function() { });
QueryBuilder.extend({
/*
* Extend original getRuleInput. If the filter has a custom placeholders object, with placeholders defined
* for a rule operator type, use those as the filter placeholder instead.
*/
getRuleInput : function(rule, value_id) {
var filter = rule.filter;
if (typeof filter.input !== 'function') {
switch (filter.input) {
case 'radio':
case 'checkbox':
case 'select':
break;
default:
// textarea and text
if (rule.operator && filter.placeholders && filter.placeholders[rule.operator.type]) {
var newPlaceholder = filter.placeholders[rule.operator.type];
// Save any existing placeholder if not saved before
if (filter.placeholder && !filter.placeholderHolder) {
filter.placeholderHolder = filter.placeholder;
}
filter.placeholder = newPlaceholder;
} else if (filter.placeholderHolder) {
filter.placeholder = filter.placeholderHolder;
}
}
}
return originalGetRuleInput.call(this, rule, value_id);
}
});
}));