Google-Maps-Karten-Symbol auch anzeigen, wenn Land leer ist
[kivitendo-erp.git] / js / kivi.CustomerVendor.js
index 371434c..e8f412a 100644 (file)
@@ -1,6 +1,6 @@
-namespace('kivi.CustomerVendor', function() {
+namespace('kivi.CustomerVendor', function(ns) {
 
-  this.selectShipto = function() {
+  this.selectShipto = function(params) {
     var shiptoId = $('#shipto_shipto_id').val();
 
     if( shiptoId ) {
@@ -11,12 +11,18 @@ namespace('kivi.CustomerVendor', function() {
           $(document.getElementById('shipto_'+ key)).val(data[key]);
 
         $('#action_delete_shipto').show();
+
+        if( params.onFormSet )
+          params.onFormSet();
       });
     }
     else {
       $('#shipto :input').not(':button, :submit, :reset, :hidden').val('');
 
       $('#action_delete_shipto').hide();
+
+      if( params.onFormSet )
+        params.onFormSet();
     }
   };
 
@@ -35,7 +41,7 @@ namespace('kivi.CustomerVendor', function() {
     }
   };
 
-  this.selectContact = function() {
+  this.selectContact = function(params) {
     var contactId = $('#contact_cp_id').val();
 
     if( contactId ) {
@@ -44,53 +50,114 @@ namespace('kivi.CustomerVendor', function() {
       $.getJSON(url, function(data) {
         var contact = data.contact;
         for(var key in contact)
-          $(document.getElementById('contact_'+ key)).val(contact[key]);
+          $(document.getElementById('contact_'+ key)).val(contact[key])
 
         var cvars = data.contact_cvars;
         for(var key in cvars)
           $(document.getElementById('contact_cvar_'+ key)).val(cvars[key]);
 
         $('#action_delete_contact').show();
+
+        if( params.onFormSet )
+          params.onFormSet();
       });
     }
     else {
       $('#contacts :input').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
 
       $('#action_delete_contact').hide();
+
+      if( params.onFormSet )
+        params.onFormSet();
     }
 
     $('#contact_cp_title_select, #contact_cp_abteilung_select').val('');
   };
 
-
-  this.showMap = function(prefix) {
-    var searchStmts = [
-      '#street',
-      ', ',
-      '#zipcode',
-      ' ',
-      '#city',
-      ', ',
-      '#country'
-    ];
-
-    var searchString = "";
-
-    for(var i in searchStmts) {
-      var stmt = searchStmts[i];
-      if( stmt.charAt(0) == '#' ) {
-        var val = $('#'+ prefix + stmt.substring(1)).val();
-        if( val )
-          searchString += val;
+  var mapSearchStmts = [
+    '#street',
+    ', ',
+    '#zipcode',
+    ' ',
+    '#city',
+    ', ',
+    '#country'
+  ];
+
+  this.MapWidget = function(prefix)
+  {
+    var $mapSearchElements = [];
+    var $widgetWrapper;
+
+    var init = function() {
+      if( $mapSearchElements.length > 0 )
+        return;
+
+      for(var i in mapSearchStmts) {
+        var stmt = mapSearchStmts[i];
+        if( stmt.charAt(0) == '#' ) {
+          var $elem = $('#'+ prefix + stmt.substring(1));
+          if( $elem )
+            $mapSearchElements.push($elem);
+        }
+      }
+    };
+
+    var isNotEmpty = function() {
+      for(var i in $mapSearchElements)
+        if( ($mapSearchElements[i].attr('id') != prefix + 'country') && ($mapSearchElements[i].val() == '') )
+          return false;
+      return true;
+    };
+
+    var showMap = function() {
+      var searchString = "";
+
+      for(var i in mapSearchStmts) {
+        var stmt = mapSearchStmts[i];
+        if( stmt.charAt(0) == '#' ) {
+          var val = $('#'+ prefix + stmt.substring(1)).val();
+          if( val )
+            searchString += val;
+        }
+        else
+          searchString += stmt;
       }
-      else
-        searchString += stmt;
-    }
 
-    var url = 'https://maps.google.com/maps?q='+ encodeURIComponent(searchString);
+      var url = 'https://maps.google.com/maps?q='+ encodeURIComponent(searchString);
+
+      window.open(url, '_blank');
+      window.focus();
+    };
+
+    var render = function(widgetWrapper) {
+      init();
+
+      $widgetWrapper = $(widgetWrapper);
+
+      $widgetWrapper
+        .html('<img src="image/map.png" alt="'+ kivi.t8("Map") +'" title="'+ kivi.t8("Map") +'" />')
+        .click(function() {
+          showMap();
+        });
+      for(var i in $mapSearchElements)
+        $mapSearchElements[i].keyup(function() {
+          testInputs();
+        });
+      this.testInputs();
+    };
+
+    var testInputs = function() {
+      init();
+
+      if( isNotEmpty() )
+        $widgetWrapper.show();
+      else
+        $widgetWrapper.hide();
+    };
 
-    window.open(url, '_blank');
-    window.focus();
+    this.render = render;
+    this.testInputs = testInputs;
   };
 
   this.showHistoryWindow = function(id) {