# HG changeset patch
# User Miguel de Juana <miguel.dejuana@openbravo.com>
# Date 1480678140 -3600
#      Fri Dec 02 12:29:00 2016 +0100
# Node ID aa9045661e64d49cd88d294b2e7a12788fc85f1f
# Parent  68384a1ed9d52af5d908cc2abaaf8627ad554e56
Fixed issue 0034648: [UX] Ugly line breaks in the 'Receipt' with high prices

- Add a new function to resize font of text
- Call this function whenever is needed to resize
- Add a new class to have flex container to let items inside this container to jump or not to next line

diff --git a/web/org.openbravo.mobile.core/assets/css/ob-standard.css b/web/org.openbravo.mobile.core/assets/css/ob-standard.css
--- a/web/org.openbravo.mobile.core/assets/css/ob-standard.css
+++ b/web/org.openbravo.mobile.core/assets/css/ob-standard.css
@@ -1455,13 +1455,13 @@
 }
 
 body.indev-background {
-    background: darkgray url(./../img/BACKGROUND-InDevelopment.png) top left;
-    background-position: -5px -10px;
-    height: 100%;
+  background: darkgray url(./../img/BACKGROUND-InDevelopment.png) top left;
+  background-position: -5px -10px;
+  height: 100%;
 }
 .filter-panel-enabled {
-	border:2px solid #ccc; 
-	background-color: yellow;
+  border:2px solid #ccc; 
+  background-color: yellow;
 }
 
 .filter-panel-disabled {
@@ -1477,6 +1477,15 @@
 .obflex {
   padding: 5px;
 }
+
+.standardFlexContainer {
+  padding: 0;
+  margin: 0;
+  display: flex!important;
+  flex-direction: row;
+  justify-content: space-between;
+  flex-wrap: nowrap;
+}
  
 .listcontextmenu-icon {
   background-image: url('./../img/iconContextMenu.png');
diff --git a/web/org.openbravo.mobile.core/source/component/ob-commonbuttons.js b/web/org.openbravo.mobile.core/source/component/ob-commonbuttons.js
--- a/web/org.openbravo.mobile.core/source/component/ob-commonbuttons.js
+++ b/web/org.openbravo.mobile.core/source/component/ob-commonbuttons.js
@@ -940,4 +940,11 @@
     this.inherited(arguments);
 
   }
+});
+
+enyo.kind({
+  name: 'OB.UI.FitText',
+  resizeHandler: function () {
+    OB.UTIL.calculateFontSizes();
+  }
 });
\ No newline at end of file
diff --git a/web/org.openbravo.mobile.core/source/component/ob-terminal-component.js b/web/org.openbravo.mobile.core/source/component/ob-terminal-component.js
--- a/web/org.openbravo.mobile.core/source/component/ob-terminal-component.js
+++ b/web/org.openbravo.mobile.core/source/component/ob-terminal-component.js
@@ -310,7 +310,9 @@
   rendered: function () {
     var keeper;
     this.inherited(arguments);
-
+    if (OB.UTIL.calculateFontSizes) {
+      OB.UTIL.calculateFontSizes();
+    }
     //Do not allow showing context menu (mouse right click)
     window.oncontextmenu = function () {
       if (!OB.UTIL.Debug.isDebug()) {
diff --git a/web/org.openbravo.mobile.core/source/utils/ob-utilitiesui.js b/web/org.openbravo.mobile.core/source/utils/ob-utilitiesui.js
--- a/web/org.openbravo.mobile.core/source/utils/ob-utilitiesui.js
+++ b/web/org.openbravo.mobile.core/source/utils/ob-utilitiesui.js
@@ -697,4 +697,50 @@
       return;
     }
   }
+};
+
+OB.UTIL.calculateFontSizes = function () {
+
+  var maxWidth = false,
+      maxHeight = 18,
+      currentWidth = false,
+      currentBox = false,
+      currentTextObj = false,
+      className = 'fitText',
+      maxFontSize = 16;
+
+  function fit(maxFontSize) {
+    //to number
+    var tmpFontSize = OB.DEC.add(currentTextObj.style.fontSize.replace('px', ''), 0, 0);
+    currentTextObj.style.fontSize = tmpFontSize + 1 + 'px';
+    var tmpWidth = currentTextObj.offsetWidth,
+        tmpHeight = currentTextObj.offsetHeight,
+        finalFontSize = 0;
+    if (tmpWidth >= currentWidth && tmpWidth < maxWidth && tmpHeight < maxHeight && tmpFontSize < 20) {
+      currentWidth = currentTextObj.offsetWidth;
+      fit(maxFontSize);
+    } else {
+      finalFontSize = OB.DEC.add(currentTextObj.style.fontSize.replace('px', ''), 0, 0) - OB.DEC.One;
+      if (maxFontSize && maxFontSize < finalFontSize) {
+        finalFontSize = maxFontSize;
+      }
+      currentTextObj.style.fontSize = finalFontSize + 'px';
+    }
+  }
+
+  var objs = document.getElementsByClassName(className);
+  if (objs.length === 0) {
+    return;
+  }
+  _.each(objs, function (obj) {
+    maxWidth = obj.offsetWidth;
+    currentBox = obj;
+    currentTextObj = obj.getElementsByTagName('SPAN')[0];
+    if (currentTextObj.offsetWidth !== 0 && currentTextObj.textContent.length >= 10) {
+      //start in 9px to reduce iterations (9px is the minimum font size)
+      currentTextObj.style.fontSize = '9px';
+      currentWidth = currentTextObj.offsetWidth;
+      fit(maxFontSize);
+    }
+  });
 };
\ No newline at end of file
