-
Notifications
You must be signed in to change notification settings - Fork 556
Expand file tree
/
Copy pathmain.js
More file actions
223 lines (195 loc) · 5.67 KB
/
main.js
File metadata and controls
223 lines (195 loc) · 5.67 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
/**
* @typedef {object} Filter
* @memberof QueryBuilder
* @description See {@link http://querybuilder.js.org/index.html#filters}
*/
/**
* @typedef {object} Operator
* @memberof QueryBuilder
* @description See {@link http://querybuilder.js.org/index.html#operators}
*/
/**
* @param {jQuery} $el
* @param {object} options - see {@link http://querybuilder.js.org/#options}
* @constructor
*/
var QueryBuilder = function($el, options) {
$el[0].queryBuilder = this;
/**
* Element container
* @member {jQuery}
* @readonly
*/
this.$el = $el;
/**
* Configuration object
* @member {object}
* @readonly
*/
this.settings = $.extendext(true, 'replace', {}, QueryBuilder.DEFAULTS, options);
/**
* Internal model
* @member {Model}
* @readonly
*/
this.model = new Model();
/**
* Internal status
* @member {object}
* @property {string} id - id of the container
* @property {boolean} generated_id - if the container id has been generated
* @property {int} group_id - current group id
* @property {int} rule_id - current rule id
* @property {boolean} has_optgroup - if filters have optgroups
* @property {boolean} has_operator_optgroup - if operators have optgroups
* @readonly
* @private
*/
this.status = {
id: null,
generated_id: false,
group_id: 0,
rule_id: 0,
has_optgroup: false,
has_operator_optgroup: false
};
/**
* List of filters
* @member {QueryBuilder.Filter[]}
* @readonly
*/
this.filters = this.settings.filters;
/**
* List of icons
* @member {object.<string, string>}
* @readonly
*/
this.icons = this.settings.icons;
/**
* List of operators
* @member {QueryBuilder.Operator[]}
* @readonly
*/
this.operators = this.settings.operators;
/**
* List of templates
* @member {object.<string, function>}
* @readonly
*/
this.templates = this.settings.templates;
/**
* Plugins configuration
* @member {object.<string, object>}
* @readonly
*/
this.plugins = this.settings.plugins;
/**
* Translations object
* @member {object}
* @readonly
*/
this.lang = null;
// translations : english << 'lang_code' << custom
if (QueryBuilder.regional['en'] === undefined) {
Utils.error('Config', '"i18n/en.js" not loaded.');
}
this.lang = $.extendext(true, 'replace', {}, QueryBuilder.regional['en'], QueryBuilder.regional[this.settings.lang_code], this.settings.lang);
// "allow_groups" can be boolean or int
if (this.settings.allow_groups === false) {
this.settings.allow_groups = 0;
}
else if (this.settings.allow_groups === true) {
this.settings.allow_groups = -1;
}
// init templates
Object.keys(this.templates).forEach(function(tpl) {
if (!this.templates[tpl]) {
this.templates[tpl] = QueryBuilder.templates[tpl];
}
if (typeof this.templates[tpl] !== 'function') {
throw new Error(`Template ${tpl} must be a function`);
}
}, this);
// ensure we have a container id
if (!this.$el.attr('id')) {
this.$el.attr('id', 'qb_' + Math.floor(Math.random() * 99999));
this.status.generated_id = true;
}
this.status.id = this.$el.attr('id');
// INIT
this.$el.addClass('query-builder');
this.filters = this.checkFilters(this.filters);
this.operators = this.checkOperators(this.operators);
this.bindEvents();
this.initPlugins();
};
$.extend(QueryBuilder.prototype, /** @lends QueryBuilder.prototype */ {
/**
* Triggers an event on the builder container
* @param {string} type
* @returns {$.Event}
*/
trigger: function(type) {
var event = new $.Event(this._tojQueryEvent(type), {
builder: this
});
this.$el.triggerHandler(event, Array.prototype.slice.call(arguments, 1));
return event;
},
/**
* Triggers an event on the builder container and returns the modified value
* @param {string} type
* @param {*} value
* @returns {*}
*/
change: function(type, value) {
var event = new $.Event(this._tojQueryEvent(type, true), {
builder: this,
value: value
});
this.$el.triggerHandler(event, Array.prototype.slice.call(arguments, 2));
return event.value;
},
/**
* Attaches an event listener on the builder container
* @param {string} type
* @param {function} cb
* @returns {QueryBuilder}
*/
on: function(type, cb) {
this.$el.on(this._tojQueryEvent(type), cb);
return this;
},
/**
* Removes an event listener from the builder container
* @param {string} type
* @param {function} [cb]
* @returns {QueryBuilder}
*/
off: function(type, cb) {
this.$el.off(this._tojQueryEvent(type), cb);
return this;
},
/**
* Attaches an event listener called once on the builder container
* @param {string} type
* @param {function} cb
* @returns {QueryBuilder}
*/
once: function(type, cb) {
this.$el.one(this._tojQueryEvent(type), cb);
return this;
},
/**
* Appends `.queryBuilder` and optionally `.filter` to the events names
* @param {string} name
* @param {boolean} [filter=false]
* @returns {string}
* @private
*/
_tojQueryEvent: function(name, filter) {
return name.split(' ').map(function(type) {
return type + '.queryBuilder' + (filter ? '.filter' : '');
}).join(' ');
}
});