JAVASCRIPT   67

functions.js

Guest on 30th July 2021 02:47:44 PM

  1. export function addWrapper (htmlData, params) {
  2.   let bodyStyle = 'font-family:' + params.font + ' !important; font-size: ' + params.font_size + ' !important; width:100%;'
  3.   return '<div style="' + bodyStyle + '">' + htmlData + '</div>'
  4. }
  5.  
  6. export function capitalizePrint (string) {
  7.   return string.charAt(0).toUpperCase() + string.slice(1)
  8. }
  9.  
  10. export function collectStyles (element, params) {
  11.   let win = document.defaultView || window
  12.  
  13.   let style = []
  14.  
  15.   // String variable to hold styling for each element
  16.   let elementStyle = ''
  17.  
  18.   if (win.getComputedStyle) { // Modern browsers
  19.     style = win.getComputedStyle(element, '')
  20.  
  21.     // Styles including
  22.     let targetStyles = ['border', 'box', 'break', 'text-decoration']
  23.  
  24.     // Exact match
  25.     let targetStyle = ['clear', 'display', 'width', 'min-width', 'height', 'min-height', 'max-height']
  26.  
  27.     // Optional - include margin and padding
  28.     if (params.honorMarginPadding) {
  29.       targetStyles.push('margin', 'padding')
  30.     }
  31.  
  32.     // Optional - include color
  33.     if (params.honorColor) {
  34.       targetStyles.push('color')
  35.     }
  36.  
  37.     for (let i = 0; i < style.length; i++) {
  38.       for (let s = 0; s < targetStyles.length; s++) {
  39.         if (style[i].indexOf(targetStyles[s]) !== -1 || targetStyle.indexOf(style[i]) !== -1) {
  40.           elementStyle += style[i] + ':' + style.getPropertyValue(style[i]) + ';'
  41.         }
  42.       }
  43.     }
  44.   } else if (element.currentStyle) { // IE
  45.     style = element.currentStyle
  46.  
  47.     for (let name in style) {
  48.       if (style.indexOf('border') !== -1 && style.indexOf('color') !== -1) {
  49.         elementStyle += name + ':' + style[name] + ';'
  50.       }
  51.     }
  52.   }
  53.  
  54.   // Print friendly defaults
  55.   elementStyle += 'max-width: ' + params.maxWidth + 'px !important;' + params.font_size + ' !important;'
  56.  
  57.   return elementStyle
  58. }
  59.  
  60. export function loopNodesCollectStyles (elements, params) {
  61.   for (let n = 0; n < elements.length; n++) {
  62.     let currentElement = elements[n]
  63.  
  64.     // Form Printing - check if is element Input
  65.     let tag = currentElement.tagName
  66.     if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') {
  67.       // Save style to variable
  68.       let textStyle = collectStyles(currentElement, params)
  69.  
  70.       // Remove INPUT element and insert a text node
  71.       let parent = currentElement.parentNode
  72.  
  73.       // Get text value
  74.       let textNode = tag === 'SELECT'
  75.                 ? document.createTextNode(currentElement.options[currentElement.selectedIndex].text)
  76.                 : document.createTextNode(currentElement.value)
  77.  
  78.       // Create text element
  79.       let textElement = document.createElement('div')
  80.       textElement.appendChild(textNode)
  81.  
  82.       // Add style to text
  83.       textElement.setAttribute('style', textStyle)
  84.  
  85.       // Add text
  86.       parent.appendChild(textElement)
  87.  
  88.       // Remove input
  89.       parent.removeChild(currentElement)
  90.     } else {
  91.       // Get all styling for print element
  92.       currentElement.setAttribute('style', collectStyles(currentElement, params))
  93.     }
  94.  
  95.     // Check if more elements in tree
  96.     let children = currentElement.children
  97.  
  98.     if (children && children.length) {
  99.       loopNodesCollectStyles(children, params)
  100.     }
  101.   }
  102. }
  103.  
  104. export function addHeader (printElement, header, headerStyle) {
  105.   // Create header element
  106.   let headerElement = document.createElement('h1')
  107.  
  108.   // Create header text node
  109.   let headerNode = document.createTextNode(header)
  110.  
  111.   // Build and style
  112.   headerElement.appendChild(headerNode)
  113.   headerElement.setAttribute('style', headerStyle)
  114.  
  115.   printElement.insertBefore(headerElement, printElement.childNodes[0])
  116. }

Raw Paste


Login or Register to edit or fork this paste. It's free.