UNPKG

8.34 kBJavaScriptView Raw
1/**
2 * Sync load of language file once QUnit and Blanket are ready
3 * Otherwise the language file is loaded before instrumented files
4 */
5QUnit.begin(function() {
6 $.ajax({
7 async: false,
8 url: '../dist/i18n/query-builder.en.js',
9 dataType: 'script'
10 });
11});
12
13/**
14 * Add GitHub link in header
15 */
16QUnit.begin(function(){
17 $('#qunit-header').append(
18 '<div class="pull-right" style="margin:-5px 10px 0 0">' +
19 '<a href="https://github.com/mistic100/jQuery-QueryBuilder">' +
20 '<img src="https://assets.github.com/images/icons/emoji/octocat.png" width=32px height=32px>' +
21 '</a>' +
22 '</div>'
23 );
24});
25
26/**
27 * Modify Blanket results display
28 */
29QUnit.done(function(){
30 $('#blanket-main')
31 .css('marginTop', '10px')
32 .addClass('col-lg-8 col-lg-push-2')
33 .find('.bl-file a').each(function(){
34 this.innerHTML = this.innerHTML.replace(/(.*)\/src\/(.*)$/, '$2');
35 });
36});
37
38
39/**
40 * Custom assert to compare rules objects
41 */
42QUnit.assert.rulesMatch = function(actual, expected, message) {
43 var ok = (function match(a, b){
44 var ok = true;
45
46 if (a.hasOwnProperty('valid') && b.hasOwnProperty('valid')) {
47 ok = QUnit.equiv(a.valid, b.valid);
48 }
49
50 if (b.hasOwnProperty('data')) {
51 if (!a.hasOwnProperty('data')) {
52 ok = false;
53 }
54 else {
55 ok = QUnit.equiv(a.data, b.data);
56 }
57 }
58
59 if (b.hasOwnProperty('flags')) {
60 if (!a.hasOwnProperty('flags')) {
61 ok = false;
62 }
63 else {
64 ok = QUnit.equiv(a.flags, b.flags);
65 }
66 }
67
68 if (b.hasOwnProperty('rules')) {
69 if (!a.hasOwnProperty('rules')) {
70 ok = false;
71 }
72 else {
73 for (var i=0, l=a.rules.length; i<l; i++) {
74 if (b.rules[i]===undefined || !match(a.rules[i], b.rules[i])) {
75 ok = false;
76 break;
77 }
78 }
79
80 for (var i=0, l=b.rules.length; i<l; i++) {
81 if (a.rules[i]===undefined || !match(a.rules[i], b.rules[i])) {
82 ok = false;
83 break;
84 }
85 }
86 }
87
88 ok&= a.condition == b.condition;
89 }
90 else if (a.hasOwnProperty('rules') && !b.hasOwnProperty('rules')) {
91 ok = false;
92 }
93 else {
94 if ($.isArray(a.value)) {
95 ok&= $(a.value).not(b.value).length == 0 && $(b.value).not(a.value).length == 0;
96 }
97 else {
98 ok&= a.value==b.value;
99 }
100
101 ok&= a.id==b.id && a.operator==b.operator;
102 }
103
104 return ok;
105 }(actual, expected));
106
107 this.push(ok, actual, expected, message);
108};
109
110/**
111 * Custom assert for init errors
112 */
113QUnit.assert.initError = function($b, options, error) {
114 this.throws(
115 function() { $b.queryBuilder(options); },
116 error,
117 'Should throw "' + error + '" error'
118 );
119};
120
121/**
122 * Custom assert for validation errors
123 */
124QUnit.assert.validationError = function($b, rule, code) {
125 if (rule !== null) {
126 $b.queryBuilder('setRules', {
127 rules: [rule]
128 });
129 }
130
131 $b.on('validationError.queryBuilder', function(e, node, error) {
132 throw error[0];
133 });
134
135 this.throws(
136 function() { $b.queryBuilder('validate'); },
137 code,
138 'Should throw "' + code + '" error'
139 );
140
141 $b.off('validationError.queryBuilder');
142};
143
144/**
145 * Custom assert to test option or inputs list (in order)
146 */
147QUnit.assert.optionsMatch = function($target, expected, message) {
148 var options = [];
149
150 $target.each(function(){
151 options.push($(this).val());
152 });
153
154 this.deepEqual(options, expected, message);
155};
156
157/**
158 * Custom assert to test a regex
159 */
160QUnit.assert.match = function(actual, regex, message) {
161 this.push(regex.test(actual), actual, regex, message);
162};
163
164
165/**
166 * Drag & Drop simulation
167 * https://gist.github.com/mistic100/37c95fab77b5626c5623
168 */
169(function($) {
170 $.fn.simulateDragDrop = function(options) {
171 return this.each(function() {
172 new $.simulateDragDrop(this, options);
173 });
174 };
175
176 $.simulateDragDrop = function(elem, options) {
177 var that = this;
178
179 this.options = options;
180 this.elem = elem;
181
182 if (this.options.start) {
183 this.options.start.call(this.elem);
184 }
185
186 setTimeout(function() {
187 that.dragstart();
188 }, this.options.dragStartDelay || 0);
189 };
190
191 $.extend($.simulateDragDrop.prototype, {
192 dragstart: function() {
193 var that = this;
194
195 var event = this.createEvent('dragstart');
196 this.dispatchEvent(this.elem, 'dragstart', event);
197
198 setTimeout(function() {
199 that.drop(event);
200 }, this.options.dropDelay || 0);
201 },
202 drop: function(event) {
203 var that = this;
204
205 var dropEvent = this.createEvent('drop');
206 dropEvent.dataTransfer = event.dataTransfer;
207 this.dispatchEvent($(this.options.dropTarget)[0], 'drop', dropEvent);
208
209 setTimeout(function() {
210 that.dragend(event);
211 }, this.options.dragEndDelay || 0);
212 },
213 dragend: function(event) {
214 var dragEndEvent = this.createEvent('dragend');
215 dragEndEvent.dataTransfer = event.dataTransfer;
216 this.dispatchEvent(this.elem, 'dragend', dragEndEvent);
217
218 if (this.options.done) {
219 this.options.done.call(this.elem);
220 }
221 },
222 createEvent: function(type) {
223 var event = document.createEvent('CustomEvent');
224 event.initCustomEvent(type, true, true, null);
225 event.dataTransfer = {
226 data: {},
227 setData: function(type, val) {
228 this.data[type] = val;
229 },
230 getData: function(type) {
231 return this.data[type];
232 }
233 };
234 return event;
235 },
236 dispatchEvent: function(elem, type, event) {
237 if (elem.dispatchEvent) {
238 elem.dispatchEvent(event);
239 }
240 else if (elem.fireEvent) {
241 elem.fireEvent('on' + type, event);
242 }
243 }
244 });
245})(jQuery);
246
247
248var basic_filters = [{
249 id: 'name',
250 label: 'Name',
251 type: 'string',
252 value_separator: ','
253}, {
254 id: 'category',
255 label: 'Category',
256 type: 'string',
257 input: 'select',
258 multiple: true,
259 values: {
260 'bk': 'Books',
261 'mo': 'Movies',
262 'mu': 'Music',
263 'to': 'Tools',
264 'go': 'Goodies',
265 'cl': 'Clothes'
266 },
267 operators: ['in', 'not_in', 'equal', 'not_equal', 'is_null', 'is_not_null']
268}, {
269 id: 'in_stock',
270 label: 'In stock',
271 type: 'integer',
272 input: 'radio',
273 values: {
274 1: 'Yes',
275 0: 'No'
276 },
277 operators: ['equal']
278}, {
279 id: 'price',
280 label: 'Price',
281 type: 'double',
282 validation: {
283 min: 0,
284 step: 0.01
285 },
286 description: 'Lorem ipsum sit amet'
287}, {
288 id: 'id',
289 label: 'Identifier',
290 type: 'string',
291 placeholder: '____-____-____',
292 operators: ['equal', 'not_equal'],
293 validation: {
294 format: /^.{4}-.{4}-.{4}$/,
295 messages: {
296 format: 'Custom format error message'
297 }
298 }
299}, {
300 id: 'age',
301 label: 'Age',
302 type: 'integer',
303 input: 'text',
304 value_separator: '|'
305}];
306
307var basic_rules = {
308 condition: 'AND',
309 rules: [{
310 id: 'price',
311 field: 'price',
312 operator: 'less',
313 value: 10.25
314 }, {
315 id: 'name',
316 field: 'name',
317 operator: 'is_null',
318 value: null
319 }, {
320 condition: 'OR',
321 rules: [{
322 id: 'category',
323 field: 'category',
324 operator: 'in',
325 value: ['mo', 'mu']
326 }, {
327 id: 'id',
328 field: 'id',
329 operator: 'not_equal',
330 value: '1234-azer-5678'
331 }]
332 }]
333};