1+ ( function ( ) {
2+ 'use strict' ;
3+
4+ var module = angular . module ( 'letsAngular' ) ;
5+
6+ module . controller ( 'AutoCompleteController' , function ( $scope , locale , $q , $timeout ) {
7+
8+ $scope . autocompleteModels = { } ;
9+ $scope . doafterAutoCompleteSelect = { } ;
10+
11+ $scope . _autocomplete = function ( field , val , detail ) {
12+
13+ var data = $scope . filterdata || $scope . data || { } ;
14+
15+ var queries = [ ] ;
16+ var deferred = $q . defer ( ) ;
17+
18+ // Autocomplete Dependencies
19+ if ( field . autocomplete_dependencies . length > 0 ) {
20+ var deps = field . autocomplete_dependencies ;
21+ for ( var x in deps ) {
22+ var dep = deps [ x ] ;
23+ if ( data [ dep . field ] == undefined || data [ dep . field ] == null || data [ dep . field ] == "null" ) {
24+
25+ var text =
26+ locale . translate ( 'letsfw.select_before' )
27+ . replace ( '%name%' , dep . label . toLocaleLowerCase ( ) )
28+ . replace ( '%gender%' , ( dep . gender ? dep . gender : 'o(a)' ) ) ;
29+
30+ var data = [ ] ;
31+ data . push ( { id : null , label : text } ) ;
32+
33+ deferred . resolve ( data ) ;
34+
35+ return deferred . promise ;
36+ } else {
37+ queries [ dep . field ] = data [ dep . field ] ;
38+ }
39+ }
40+ }
41+
42+ // Check Value
43+ val = val . trim ( ) ;
44+ if ( val . length == 0 || field . customOptions . select == true ) {
45+ val = '[blank]' ;
46+ }
47+
48+ // Callback Continue
49+ var callback = function ( options ) {
50+
51+ var exs = options . length > 0 ;
52+ if ( options . length == 0 ) {
53+ options . unshift ( { id : null , label : locale . translate ( 'letsfw.no_record_found' ) } ) ;
54+ }
55+
56+ if ( field . customOptions . select == true && exs ) {
57+ if ( field . customOptions . searchBlank ) {
58+ options . unshift ( { id : "null" , label : locale . translate ( 'letsfw.is_blank' ) } ) ;
59+ }
60+ if ( ! field . customOptions . required ) {
61+ options . unshift ( { id : null , label : '--- ' + locale . translate ( 'letsfw.select' ) + ' ---' } ) ;
62+ }
63+ }
64+
65+ if ( field . quickAdd === true && val != '[blank]' ) {
66+ options . push ( { id : - 1 , label : 'Adicionar novo: ' + val } ) ;
67+ }
68+
69+ deferred . resolve ( options ) ;
70+ }
71+
72+ // Check field type
73+ if ( field . customOptions . list == undefined ) {
74+
75+ if ( field . customOptions . general !== undefined ) {
76+ var route = 'general/autocomplete/' + field . customOptions . general + '/' + val ;
77+
78+ } else if ( detail ) {
79+ var route = 'details/' + detail + '/autocomplete/' + field . name + '/' + val ;
80+
81+ } else {
82+ var route = 'autocomplete/' + field . name + '/' + val ;
83+ }
84+
85+ if ( field . customOptions . select == true ) {
86+ queries [ "limit" ] = 0 ;
87+ } else {
88+ queries [ "limit" ] = 20 ;
89+ }
90+
91+ $scope . resource . customGET ( route , queries ) . then ( function ( options ) {
92+ callback ( options ) ;
93+ } , function errorCallback ( ) {
94+ return deferred . reject ( ) ;
95+ } ) ;
96+
97+ } else {
98+ var options = angular . copy ( field . customOptions . list ) || [ ] ;
99+ callback ( options ) ;
100+ }
101+
102+ return deferred . promise ;
103+ }
104+
105+ $scope . _autocompleteSelect = function ( $item , $model , $label , detail ) {
106+
107+ var _data = this . detail_data || this . data || { } ;
108+
109+ if ( $item . id != null && typeof $item . id != 'integer' || ( typeof $item . id == 'integer' && $item . id > 0 ) ) {
110+ _data [ this . field . name ] = $item . id ;
111+ }
112+ else if ( $item . id == null ) {
113+ _data [ this . field . name ] = _data [ this . field . name + '.label' ] = null ;
114+ }
115+ else {
116+ _data [ this . field . name + '.label' ] = null ;
117+ return false ;
118+ }
119+
120+ if ( detail ) {
121+ this . detail_data = _data ;
122+ } else {
123+ this . data = _data ;
124+ }
125+
126+ $scope . $emit ( 'autocomplete-select-' + this . field . name , { scope :$scope , value :$item } ) ;
127+
128+ if ( typeof $scope . doafterAutoCompleteSelect [ this . field . name ] == "function" ) {
129+ $scope . doafterAutoCompleteSelect [ this . field . name ] . call ( this , _data , $item , $model , $label ) ;
130+ }
131+
132+ var field = this . field ;
133+ $timeout ( function ( ) {
134+ jQuery ( '#' + field . name ) . trigger ( 'keyup' ) ;
135+ } ) ;
136+ }
137+
138+ $scope . autocompleteAdd = function ( query ) {
139+ // console.log(query);
140+ }
141+
142+ $scope . autocomplete = function ( field , val ) {
143+ return this . _autocomplete ( field , val , null ) ;
144+ }
145+
146+ $scope . autocompleteDetail = function ( detail , field , val ) {
147+ return this . _autocomplete ( field , val , detail ) ;
148+ }
149+
150+ $scope . autocompleteSelect = function ( detail , $item , $model , $label ) {
151+ this . _autocompleteSelect ( $item , $model , $label , null ) ;
152+ } ;
153+
154+ $scope . autocompleteDetailSelect = function ( detail , $item , $model , $label ) {
155+ this . _autocompleteSelect ( $item , $model , $label , detail ) ;
156+ }
157+
158+ } ) ;
159+
160+ } ) ( ) ;
0 commit comments