1+ /*!
2+ * jQuery QueryBuilder Loopback Support
3+ * Copyright 2014-2015 Fabien Franzen (https://github.com/fabien)
4+ * Licensed under MIT (http://opensource.org/licenses/MIT)
5+ */
6+
7+ ( function ( $ ) {
8+
9+ // DEFAULT CONFIG
10+ // ===============================
11+ $ . fn . queryBuilder . defaults . set ( {
12+ loopbackOperators : {
13+ equal : function ( v ) { return v [ 0 ] ; } ,
14+ not_equal : function ( v ) { return { 'neq' : v [ 0 ] } ; } ,
15+ in : function ( v ) { return { 'inq' : v } ; } ,
16+ not_in : function ( v ) { return { 'nin' : v } ; } ,
17+ less : function ( v ) { return { 'lt' : v [ 0 ] } ; } ,
18+ less_or_equal : function ( v ) { return { 'lte' : v [ 0 ] } ; } ,
19+ greater : function ( v ) { return { 'gt' : v [ 0 ] } ; } ,
20+ greater_or_equal : function ( v ) { return { 'gte' : v [ 0 ] } ; } ,
21+ between : function ( v ) { return { 'between' : v } ; } ,
22+ begins_with : function ( v ) { return { 'like' : '^' + escapeRegExp ( v [ 0 ] ) } ; } ,
23+ not_begins_with : function ( v ) { return { 'nlike' : '^' + escapeRegExp ( v [ 0 ] ) } ; } ,
24+ contains : function ( v ) { return { 'like' : escapeRegExp ( v [ 0 ] ) } ; } ,
25+ not_contains : function ( v ) { return { 'nlike' : escapeRegExp ( v [ 0 ] ) } ; } ,
26+ ends_with : function ( v ) { return { 'like' : escapeRegExp ( v [ 0 ] ) + '$' } ; } ,
27+ not_ends_with : function ( v ) { return { 'nlike' : escapeRegExp ( v [ 0 ] ) + '$' } ; } ,
28+ is_empty : function ( v ) { return '' ; } ,
29+ is_not_empty : function ( v ) { return { 'neq' : '' } ; } ,
30+ is_null : function ( v ) { return null ; } ,
31+ is_not_null : function ( v ) { return { 'neq' : null } ; }
32+ }
33+ } ) ;
34+
35+
36+ // PUBLIC METHODS
37+ // ===============================
38+ $ . fn . queryBuilder . extend ( {
39+ /**
40+ * Get rules as Loopback query
41+ * @param data {object} (optional) rules
42+ * @return {object }
43+ */
44+ getLoopback : function ( data ) {
45+ data = ( data === undefined ) ? this . getRules ( ) : data ;
46+
47+ var that = this ;
48+
49+ return ( function parse ( data ) {
50+ if ( ! data . condition ) {
51+ data . condition = that . settings . default_condition ;
52+ }
53+ if ( [ 'AND' , 'OR' ] . indexOf ( data . condition . toUpperCase ( ) ) === - 1 ) {
54+ $ . error ( 'Unable to build Loopback query with ' + data . condition + ' condition' ) ;
55+ }
56+
57+ if ( ! data . rules ) {
58+ return { } ;
59+ }
60+
61+ var parts = [ ] ;
62+
63+ $ . each ( data . rules , function ( i , rule ) {
64+ if ( rule . rules && rule . rules . length > 0 ) {
65+ parts . push ( parse ( rule ) ) ;
66+ }
67+ else {
68+ var mdb = that . settings . loopbackOperators [ rule . operator ] ,
69+ ope = that . getOperatorByType ( rule . operator ) ,
70+ values = [ ] ;
71+
72+ if ( mdb === undefined ) {
73+ $ . error ( 'Loopback operation unknown for operator ' + rule . operator ) ;
74+ }
75+
76+ if ( ope . accept_values ) {
77+ if ( ! ( rule . value instanceof Array ) ) {
78+ rule . value = [ rule . value ] ;
79+ }
80+
81+ rule . value . forEach ( function ( v , i ) {
82+ values . push ( changeType ( v , rule . type ) ) ;
83+ } ) ;
84+ }
85+
86+ var part = { } ;
87+ part [ rule . field ] = mdb . call ( that , values ) ;
88+ parts . push ( part ) ;
89+ }
90+ } ) ;
91+
92+ var res = { } ;
93+ if ( parts . length > 0 ) {
94+ res [ data . condition . toLowerCase ( ) ] = parts ;
95+ }
96+ return res ;
97+ } ( data ) ) ;
98+ }
99+ } ) ;
100+
101+
102+ // UTILITIES
103+ // ===============================
104+ /**
105+ * Change type of a value to int, float or boolean
106+ * @param value {mixed}
107+ * @param type {string}
108+ * @return {mixed }
109+ */
110+ function changeType ( value , type , db ) {
111+ switch ( type ) {
112+ case 'integer' : return parseInt ( value ) ;
113+ case 'double' : return parseFloat ( value ) ;
114+ case 'boolean' :
115+ var bool = value . trim ( ) . toLowerCase ( ) === 'true' || value . trim ( ) === '1' || value === 1 ;
116+ if ( db === 'sql' ) {
117+ return bool ? 1 : 0 ;
118+ }
119+ else {
120+ return bool ;
121+ }
122+ break ;
123+ default : return value ;
124+ }
125+ }
126+
127+ /**
128+ * Escape value for use in regex
129+ * @param value {string}
130+ * @return {string }
131+ */
132+ function escapeRegExp ( str ) {
133+ return str . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, "\\$&" ) ;
134+ }
135+
136+ } ( jQuery ) ) ;
0 commit comments