JS: weniger getElementById nutzen
[kivitendo-erp.git] / js / kivi.CustomerVendor.js
1 namespace('kivi.CustomerVendor', function(ns) {
2
3   this.selectShipto = function(params) {
4     var shiptoId = $('#shipto_shipto_id').val();
5
6     if( shiptoId ) {
7       var url = 'controller.pl?action=CustomerVendor/ajaj_get_shipto&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ shiptoId;
8
9       $.getJSON(url, function(data) {
10         for(var key in data)
11           $('#shipto_'+ key).val(data[key]);
12
13         $('#action_delete_shipto').show();
14
15         if( params.onFormSet )
16           params.onFormSet();
17       });
18     }
19     else {
20       $('#shipto :input').not(':button, :submit, :reset, :hidden').val('');
21
22       $('#action_delete_shipto').hide();
23
24       if( params.onFormSet )
25         params.onFormSet();
26     }
27   };
28
29   this.selectDelivery = function(fromDate, toDate) {
30     var deliveryId = $('#delivery_id').val();
31
32     if( !deliveryId )
33       $("#delivery").empty();
34     else {
35       var url = 'controller.pl?action=CustomerVendor/get_delivery&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ $('#delivery_id').val();
36
37       if( fromDate && toDate )
38         url += '&delivery_from='+ fromDate +'&delivery_to='+ toDate;
39
40       $('#delivery').load(url);
41     }
42   };
43
44   this.setCustomVariablesFromAJAJ = function(cvars) {
45     for (var key in cvars) {
46       var cvar  = cvars[key];
47       var $ctrl = $('#contact_cvars_'+ key);
48
49       console.log($ctrl, cvar);
50
51       if (cvar.type == 'bool')
52         $ctrl.prop('checked', cvar.value == 1 ? 'checked' : '');
53
54       else if ((cvar.type == 'customer') || (cvar.type == 'vendor'))
55         kivi.CustomerVendorPicker($ctrl).set_item({ id: cvar.id, name: cvar.value });
56
57       else if (cvar.type == 'part')
58         kivi.PartPicker($ctrl).set_item({ id: cvar.id, name: cvar.value });
59
60       else
61         $ctrl.val(cvar.value);
62     }
63   };
64
65   this.selectContact = function(params) {
66     var contactId = $('#contact_cp_id').val();
67
68           var url = 'controller.pl?action=CustomerVendor/ajaj_get_contact&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&contact_id='+ contactId;
69
70     $.getJSON(url, function(data) {
71       var contact = data.contact;
72       for(var key in contact)
73         $('#contact_'+ key).val(contact[key])
74
75       kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.contact_cvars);
76
77       if ( contactId )
78         $('#action_delete_contact').show();
79       else
80         $('#action_delete_contact').hide();
81
82       if ( params.onFormSet )
83         params.onFormSet();
84     });
85
86     $('#contact_cp_title_select, #contact_cp_abteilung_select').val('');
87   };
88
89   var mapSearchStmts = [
90     '#street',
91     ', ',
92     '#zipcode',
93     ' ',
94     '#city',
95     ', ',
96     '#country'
97   ];
98
99   this.MapWidget = function(prefix, source_address)
100   {
101     var $mapSearchElements = [];
102     var $widgetWrapper;
103
104     var init = function() {
105       if( $mapSearchElements.length > 0 )
106         return;
107
108       for(var i in mapSearchStmts) {
109         var stmt = mapSearchStmts[i];
110         if( stmt.charAt(0) == '#' ) {
111           var $elem = $('#'+ prefix + stmt.substring(1));
112           if( $elem )
113             $mapSearchElements.push($elem);
114         }
115       }
116     };
117
118     var isNotEmpty = function() {
119       for(var i in $mapSearchElements)
120         if( ($mapSearchElements[i].attr('id') != prefix + 'country') && ($mapSearchElements[i].val() === '') )
121           return false;
122       return true;
123     };
124
125     var showMap = function() {
126       var searchString = "";
127
128       for(var i in mapSearchStmts) {
129         var stmt = mapSearchStmts[i];
130         if( stmt.charAt(0) == '#' ) {
131           var val = $('#'+ prefix + stmt.substring(1)).val();
132           if( val )
133             searchString += val;
134         }
135         else
136           searchString += stmt;
137       }
138
139       source_address = source_address || '';
140       var query      = source_address !== '' ? 'saddr=' + encodeURIComponent(source_address) + '&daddr=' : 'q=';
141       var url        = 'https://maps.google.com/maps?' + query + encodeURIComponent(searchString);
142
143       window.open(url, '_blank');
144       window.focus();
145     };
146
147     var render = function(widgetWrapper) {
148       init();
149
150       $widgetWrapper = $(widgetWrapper);
151
152       $widgetWrapper
153         .html('<img src="image/map.png" alt="'+ kivi.t8("Map") +'" title="'+ kivi.t8("Map") +'" />')
154         .click(function() {
155           showMap();
156         });
157       for(var i in $mapSearchElements)
158         $mapSearchElements[i].keyup(testInputs);
159       this.testInputs();
160     };
161
162     var testInputs = function() {
163       init();
164
165       if( isNotEmpty() )
166         $widgetWrapper.show();
167       else
168         $widgetWrapper.hide();
169     };
170
171     this.render = render;
172     this.testInputs = testInputs;
173   };
174
175   this.showHistoryWindow = function(id) {
176     var xPos = (screen.width - 800) / 2;
177     var yPos = (screen.height - 500) / 2;
178     var parm = "left="+ xPos +",top="+ yPos +",width=800,height=500,status=yes,scrollbars=yes";
179     var url = "common.pl?INPUT_ENCODING=UTF-8&action=show_history&longdescription=&input_name="+ encodeURIComponent(id);
180     window.open(url, "_new_generic", parm);
181   };
182
183   this.update_dial_action = function($input) {
184     var $action = $('#' + $input.prop('id') + '-dial-action');
185
186     if (!$action)
187       return true;
188
189     var number = $input.val().replace(/\s+/g, '');
190     if (number === '')
191       $action.hide();
192     else
193       $action.prop('href', 'controller.pl?action=CTI/call&number=' + encodeURIComponent(number)).show();
194
195     return true;
196   };
197
198   this.init_dial_action = function(input) {
199     if ($('#_cti_enabled').val() != 1)
200       return false;
201
202     var $input    = $(input);
203     var action_id = $input.prop('id') + '-dial-action';
204
205     if (!$('#' + action_id).size()) {
206       var $action = $('<a href="" id="' + action_id + '" class="cti_call_action" target="_blank" tabindex="-1"></a>');
207       $input.wrap('<span nobr></span>').after($action);
208
209       $input.change(function() { kivi.CustomerVendor.update_dial_action($input); });
210     }
211
212     kivi.CustomerVendor.update_dial_action($input);
213
214     return true;
215   };
216
217   this.inline_report = function(target, source, data){
218     $.ajax({
219       url:        source,
220       success:    function (rsp) {
221         $(target).html(rsp);
222         $(target).find('.paginate').find('a').click(function(event){ ns.redirect_event(event, target) });
223         $(target).find('a.report-generator-header-link').click(function(event){ ns.redirect_event(event, target) });
224       },
225       data:       data,
226     });
227   };
228   this.redirect_event = function(event, target){
229     event.preventDefault();
230     ns.inline_report(target, event.target + '', {});
231   };
232 });
233
234 function local_reinit_widgets() {
235   $('#cv_phone,#shipto_shiptophone,#contact_cp_phone1,#contact_cp_phone2,#contact_cp_mobile1,#contact_cp_mobile2').each(function(idx, elt) {
236     kivi.CustomerVendor.init_dial_action($(elt));
237   });
238 }