/**
 * This file contains the source code for the module `lib/ui/helpers`
 * @file
 * @author "Rémi Becheras <rbecheras@sirap.fr>"
 * @copyright 2016 © Groupe SIRAP, tout droits réservés
 * @see module:lib/ui/helpers
 */

/**
 * This module exports some useful UI helplers to create UI components
 * @module lib/ui/helpers
 */

'use strict'

module.exports = {
  createTextBox: createTextBox,
  createUnitSelectBox: createUnitSelectBox,
  createTab: createTab,
  createFieldset: createFieldset,
  createForm: createForm,
  createListBox: createListBox,
  createListBoxItem: createListBoxItem,
  createColorPicker: createColorPicker
}

/**
 * Create a simple text box
 * @method
 * @param {string} label The textBox label
 * @param {string} name The textBox name
 * @returns {TextBox} textBox The new textBox
 */
function createTextBox (label, name) {
  return {
    label: label,
    name: name
  }
}

/**
/**
 * @function
 * @param
 * @returns
 */
function createUnitSelectBox (inputName) {
  return {
    label: 'Unit',
    name: inputName,
    type: 'listbox',
    text: 'None',
    minWidth: 90,
    maxWidth: null,
    values: [
      {text: 'pt', value: 'pt'},
      {text: 'cm', value: 'cm'},
      {text: 'mm', value: 'mm'}
    ]
  }
}

/**
 * @function
 * @param
 * @returns
 */
function createTab (title, fieldsets, direction) {
  return {
    title: title,
    type: 'form',
    items: {
      type: 'form',
      // layout: 'flex',
      direction: direction || 'collumn',
      labelGapCalc: 'children',
      padding: 0,
      items: fieldsets
    }
  }
}

/**
 * @function
 * @param
 * @returns
 */
function createFieldset (title, items) {
  return {
    type: 'fieldset',
    title: title,
    items: items
  }
}

/**
 * @function
 * @param
 * @returns
 */
function createForm (items) {
  return {
    type: 'form',
    labelGapCalc: false,
    padding: 0,
    layout: 'grid',
    columns: 2,
    defaults: {
      type: 'textbox',
      maxWidth: 100
    },
    items: items
  }
}

/**
 * @function
 * @param
 * @returns
 */
function createListBox (label, name, values) {
  return {
    label: label,
    name: name,
    type: 'listbox',
    text: 'None',
    minWidth: 90,
    maxWidth: null,
    values: values
  }
}

/**
 * Create an item for createListBox() values array
 * @param {string} text The text shown for the item
 * @param {string|number} value A value for the item
 * @return {ListBoxItem}
 */
function createListBoxItem (text, value) {
  if (value === undefined) {
    value = text
  }
  var item = {
    text: text,
    value: value
  }
  return item
}

/**
 * Create a color picker
 * @method
 * @param {string} label The label for the color picker
 * @param {string} name The name to identify the color picker in the form set
 * @returns {ColorPicker} colorPicker The new color picker
 */
function createColorPicker (label, name, callback) {
  return {
    type: 'colorbox',
    label: label,
    name: name,
    onaction: callback
  }
}

@@ -903,93 +903,375 @@
 /**%0A
-/**%0A * @function%0A * @param%0A * @returns%0A */%0Afunction createUnitSelectBox (inputName) %7B
+ * Create a select box to select a length unit%0A * @method%0A * @param %7Bstring%7D inputName - A name to identify the input in the form%0A * @param %7Bstring%7D %5BdefaultUnit=pt%5D - A default unit in (%60pt%60, %60mm%60, or %60cm%60).%0A * @returns %7BSelectBox%7D unitSelectBox The new unit select box.%0A */%0Afunction createUnitSelectBox (inputName, defaultUnit) %7B%0A  defaultUnit = defaultUnit %7C%7C 'pt'
 %0A  r
@@ -1339,34 +1339,16 @@
 stbox',%0A
-    text: 'None',%0A
     minW
@@ -1363,36 +1363,34 @@
 ,%0A    maxWidth: 
-null
+90
 ,%0A    values: %5B%0A
@@ -1492,16 +1492,63 @@
 '%7D%0A    %5D
+,%0A    text: defaultUnit,%0A    value: defaultUnit
 %0A  %7D%0A%7D%0A%0A
@@ -2309,34 +2309,303 @@
 /**%0A * @
-function%0A * @param
+method%0A * @static%0A * @param %7Bstring%7D label The label for the list box%0A * @param %7Bstring%7D name The name of the list box to identify it in the form%0A * @param %7Barray%3CListBoxItem%3E%7D values An array of list box items%0A * @param %7BListBoxItem%7D %5BdefaultItem=N/A%5D An item to select as default value
 %0A * @ret
@@ -2656,16 +2656,29 @@
 , values
+, defaultItem
 ) %7B%0A  re

add defaultValue arg to uiHelpers.createListBox()