details:   /erp/devel/pi/rev/a685d230ff9a
changeset: 12435:a685d230ff9a
user:      Martin Taal <martin.taal <at> openbravo.com>
date:      Wed May 25 17:44:50 2011 +0200
summary:   Fixes issue 17358: Custom buttons are visible for new rows in the 
grid

details:   /erp/devel/pi/rev/19655cd36ebf
changeset: 12436:19655cd36ebf
user:      Martin Taal <martin.taal <at> openbravo.com>
date:      Wed May 25 17:45:52 2011 +0200
summary:   Prevent change of custom buttons when changing a form item value

details:   /erp/devel/pi/rev/972652c1e7c2
changeset: 12437:972652c1e7c2
user:      Martin Taal <martin.taal <at> openbravo.com>
date:      Wed May 25 17:46:18 2011 +0200
summary:   Fixes issue 16883: Flexible interaction with date fields

diffstat:

 
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-formitem-widgets.js
 |  375 ++++++---
 
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-toolbar.js
          |   23 +-
 
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-view-form.js
        |    2 +-
 3 files changed, 244 insertions(+), 156 deletions(-)

diffs (truncated from 493 to 300 lines):

diff -r 8d337344cc56 -r 972652c1e7c2 
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-formitem-widgets.js
--- 
a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-formitem-widgets.js
   Wed May 25 16:40:26 2011 +0200
+++ 
b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/ob-formitem-widgets.js
   Wed May 25 17:46:18 2011 +0200
@@ -771,9 +771,228 @@
 // == OBDateItem ==
 // OBDateItem inherits from SmartClient DateItem
 // adds autocomplete and formatting based on the Openbravo date pattern
-isc.ClassFactory.defineClass('OBDateItem', DateItem);
+isc.ClassFactory.defineClass('OBDateItem', isc.DateItem);
 
-isc.OBDateItem.addClassProperties({
+isc.OBDateItem.addProperties({
+  operator: 'equals',
+  // ** {{{ pickerConstructor }}} **
+  // Picker constructor class
+  pickerConstructor: 'OBDateChooser',
+  useSharedPicker: true,
+  
+  // ** {{{ dateFormat }}} **
+  // Dateformat function
+  dateFormat: OB.Format.date,
+  
+  // ** {{{ useTextField }}} **
+  // use text field for date entry
+  useTextField: true,
+  
+  // ** {{{ changeOnKeypress }}} **
+  // Fire change event on key press.
+  changeOnKeypress: true,
+  
+  // is done by the blur event defined here
+  validateOnExit: false,
+  validateOnChange: false,
+  
+  textAlign: 'left',
+  
+  // to prevent infinite looping as setFormErrors will also blur
+  inBlur: false,
+
+  dateParts : [],
+
+  init: function() {
+    var i, dateFormatUpper, index = 0, currentTime;
+    dateFormatUpper = this.dateFormat.toUpperCase();
+    this.dateSeparator = this.dateFormat.toUpperCase().replace(/D/g, '')
+        .replace(/M/g, '').replace(/Y/g, '').substr(0, 1);
+    for (i = 0; i < dateFormatUpper.length; i++) {
+      if (this.isSeparator(dateFormatUpper, i)) {
+        index++;
+      } else {
+        this.dateParts[index] = dateFormatUpper.charAt(i);
+      }
+    }
+    currentTime = new Date();
+    this.currentMonth = String(currentTime.getMonth() + 1);
+    if (this.currentMonth.length === 1) {
+      this.currentMonth = '0' + this.currentMonth;
+    }
+    this.currentDay = String(currentTime.getDate());
+    if (this.currentDay.length === 1) {
+      this.currentDay = '0' + this.currentDay;
+    }
+    this.currentYear = String(currentTime.getFullYear());
+
+    this.Super('init', arguments);
+  },
+
+  // compare while ignoring milli difference
+  compareValues: function (value1, value2) {
+    // not a date let the super class do it
+    if (!isc.isA.Date(value1) || !isc.isA.Date(value2)) {
+      return this.Super('compareValues', arguments);
+    }
+    var difference = value1.getTime() - value2.getTime();
+    if (difference < -1000) {
+      return true;
+    } else if (difference > 1000) {
+      return false;
+    } else {
+      return true;
+    }
+  },
+  
+  // ** {{{ blur }}} **
+  // Called when the focus leaves the field (sets value and validates)
+  blur: function(){
+    var newValue = this.blurValue(), oldValue = this.getValue(), editRow;
+    
+    if (oldValue !== newValue) {
+      this.storeValue(OB.Utilities.Date.OBToJS(newValue, this.dateFormat));
+    }
+    if (!this.inBlur) {
+      this.inBlur = true;
+      this.checkOBDateItemValue();
+      this.inBlur = false;
+    }
+    return this.Super('blur', arguments);
+  },
+  
+  blurValue: function() {
+    return this.parseValue();
+  },
+
+  parseValue: function() {
+    var i, str = this.getValue(), parts = [ '', '', '' ], partIndex = 0, 
result;
+    if (!str || isc.isA.Date(str)) {
+      return str;
+    }
+    for (i = 0; i < str.length; i++) {
+      if (this.isNumber(str, i)) {
+        if (this.reachedLength(parts[partIndex], partIndex)) {
+          partIndex++;
+        }
+        if (partIndex === 3) {
+          break;
+        }
+        parts[partIndex] = parts[partIndex] + str.charAt(i);
+      } else if (this.isSeparator(str, i)) {
+        partIndex++;
+      } else {
+        // invalid date
+        return str;
+      }
+      if (partIndex === 3) {
+        break;
+      }
+    }
+    for (i = 0; i < 3; i++) {
+      parts[i] = this.expandPart(parts[i], i);
+    }
+    return parts[0] + this.dateSeparator + parts[1] + this.dateSeparator
+        + parts[2];
+  },
+
+  expandPart : function(part, index) {
+    var year;
+    if (this.reachedLength(part, index)) {
+      return part;
+    }
+    if (part === '') {
+      if (this.dateParts[index] === 'D') {
+        return this.currentDay;
+      } else if (this.dateParts[index] === 'M') {
+        return this.currentMonth;
+      } else {
+        return this.currentYear;
+      }
+    } else if (this.dateParts[index] === 'Y') {
+      year = parseInt(part, 10);
+      if (year <= 50) {
+        return String(2000 + year);
+      } else if (year < 100) {
+        return String(1900 + year);
+      } else {
+        return '2' + part;
+      }
+    } else if (part.length === 1) {
+      return '0' + part;
+    }
+    return part;
+  },
+
+  reachedLength : function(part, index) {
+    var maxLength;
+    if (this.dateParts[index] === 'D' || this.dateParts[index] === 'M') {
+      maxLength = 2;
+    } else {
+      maxLength = 4;
+    }
+    return part.length >= maxLength;
+  },
+
+  isNumber : function(str, position) {
+    return str.charAt(position) >= '0' && str.charAt(position) <= '9';
+  },
+
+  isSeparator : function(str, position) {
+    return str.charAt(position) === '-' || str.charAt(position) === '\\'
+        || str.charAt(position) === '/';
+  },
+
+  // ** {{{ checkOBDateItemValue }}} **
+  // Validate the entered date and add a form error, is called onblur
+  checkOBDateItemValue: function(){
+    var value = this.getValue();
+    var validatorLength = this.validators.length;
+    var isValid = this.validators[validatorLength - 1].condition(this, 
this.form, value);
+    var isRequired = this.required;
+    if (typeof this.name === 'undefined') {
+      this.name = 'isc_' + this.getRandomString(this.getRandomInteger(6, 12));
+    }
+    if (isValid === false) {
+      this.form.addFieldErrors(this.name, isc.OBDateItem.invalidValueLabel, 
false);
+      this.form.markForRedraw();
+    } else if (isRequired === true &&
+    (value === null || value === '' || typeof value === 'undefined')) {
+      this.form.addFieldErrors(this.name, isc.OBDateItem.requiredValueLabel, 
false);
+      this.form.markForRedraw();
+    } else {
+      this.form.clearFieldErrors(this.name, false);
+      this.form.markForRedraw();
+    }
+  },
+  
+  validateOBDateItem: function(value){
+    var dateValue = OB.Utilities.Date.OBToJS(value, this.dateFormat);
+    var isValid = true;
+    if (this.getValue() && dateValue === null) {
+      isValid = false;
+    }
+    var isRequired = this.required;
+    if (isValid === false) {
+      return false;
+    } else if (isRequired === true && value === null) {
+      return false;
+    }
+    return true;
+  },
+  
+  validators: [{
+    type: 'custom',
+    condition: function(item, validator, value){
+      return item.validateOBDateItem(value);
+    }
+  }]
+ 
+});
+
+isc.ClassFactory.defineClass('OBDateTimeItem', OBDateItem);
+
+isc.OBDateTimeItem.addClassProperties({
   
   // ** {{{ autoCompleteData }}} **
   //
@@ -955,30 +1174,12 @@
 });
 
 // == OBDateItem properties ==
-isc.OBDateItem.addProperties({
-  operator: 'equals',
-  // ** {{{ pickerConstructor }}} **
-  // Picker constructor class
-  pickerConstructor: 'OBDateChooser',
-  useSharedPicker: true,
+isc.OBDateTimeItem.addProperties({
   
-  // ** {{{ dateFormat }}} **
-  // Dateformat function
-  dateFormat: OB.Format.date,
-  
-  // ** {{{ useTextField }}} **
-  // use text field for date entry
-  useTextField: true,
-  
-  // ** {{{ changeOnKeypress }}} **
-  // Fire change event on key press.
-  changeOnKeypress: true,
-  
-  // is done by the blur event defined here
-  validateOnExit: false,
-  
-  textAlign: 'left',
-  
+  blurValue: function() {
+    return OBDateTimeItem.expandDateYear(this.dateFormat, this.getValue());
+  },
+
   // ** {{{ change }}} **
   // Called when changing a value.
   change: function(form, item, value, oldValue){ /* transformInput */
@@ -988,136 +1189,16 @@
       return;
     }
     // prevent change events from happening
-    var completedDate = OBDateItem.autoCompleteDate(item.dateFormat, value, 
this);
+    var completedDate = OBDateTimeItem.autoCompleteDate(item.dateFormat, 
value, this);
     if (completedDate !== oldValue) {
       item.setValue(completedDate);
     }
-  },
-  
-  // to prevent infinite looping as setFormErrors will also blur
-  inBlur: false,
-
-  // compare while ignoring milli difference
-  compareValues: function (value1, value2) {
-    // not a date let the super class do it
-    if (!isc.isA.Date(value1) || !isc.isA.Date(value2)) {
-      return this.Super('compareValues', arguments);
-    }
-    var difference = value1.getTime() - value2.getTime();
-    if (difference < -1000) {
-      return true;
-    } else if (difference > 1000) {
-      return false;
-    } else {
-      return true;
-    }
-  },
-  

------------------------------------------------------------------------------
vRanger cuts backup time in half-while increasing security.
With the market-leading solution for virtual backup and recovery, 
you get blazing-fast, flexible, and affordable data protection.
Download your free trial now. 
http://p.sf.net/sfu/quest-d2dcopy1
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to