11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_civar stringWidth = require('string-width')
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cimodule.exports = TemplateItem
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cifunction isPercent (num) {
71cb0ef41Sopenharmony_ci  if (typeof num !== 'string') {
81cb0ef41Sopenharmony_ci    return false
91cb0ef41Sopenharmony_ci  }
101cb0ef41Sopenharmony_ci  return num.slice(-1) === '%'
111cb0ef41Sopenharmony_ci}
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction percent (num) {
141cb0ef41Sopenharmony_ci  return Number(num.slice(0, -1)) / 100
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifunction TemplateItem (values, outputLength) {
181cb0ef41Sopenharmony_ci  this.overallOutputLength = outputLength
191cb0ef41Sopenharmony_ci  this.finished = false
201cb0ef41Sopenharmony_ci  this.type = null
211cb0ef41Sopenharmony_ci  this.value = null
221cb0ef41Sopenharmony_ci  this.length = null
231cb0ef41Sopenharmony_ci  this.maxLength = null
241cb0ef41Sopenharmony_ci  this.minLength = null
251cb0ef41Sopenharmony_ci  this.kerning = null
261cb0ef41Sopenharmony_ci  this.align = 'left'
271cb0ef41Sopenharmony_ci  this.padLeft = 0
281cb0ef41Sopenharmony_ci  this.padRight = 0
291cb0ef41Sopenharmony_ci  this.index = null
301cb0ef41Sopenharmony_ci  this.first = null
311cb0ef41Sopenharmony_ci  this.last = null
321cb0ef41Sopenharmony_ci  if (typeof values === 'string') {
331cb0ef41Sopenharmony_ci    this.value = values
341cb0ef41Sopenharmony_ci  } else {
351cb0ef41Sopenharmony_ci    for (var prop in values) {
361cb0ef41Sopenharmony_ci      this[prop] = values[prop]
371cb0ef41Sopenharmony_ci    }
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci  // Realize percents
401cb0ef41Sopenharmony_ci  if (isPercent(this.length)) {
411cb0ef41Sopenharmony_ci    this.length = Math.round(this.overallOutputLength * percent(this.length))
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci  if (isPercent(this.minLength)) {
441cb0ef41Sopenharmony_ci    this.minLength = Math.round(this.overallOutputLength * percent(this.minLength))
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci  if (isPercent(this.maxLength)) {
471cb0ef41Sopenharmony_ci    this.maxLength = Math.round(this.overallOutputLength * percent(this.maxLength))
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci  return this
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciTemplateItem.prototype = {}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciTemplateItem.prototype.getBaseLength = function () {
551cb0ef41Sopenharmony_ci  var length = this.length
561cb0ef41Sopenharmony_ci  if (
571cb0ef41Sopenharmony_ci    length == null &&
581cb0ef41Sopenharmony_ci    typeof this.value === 'string' &&
591cb0ef41Sopenharmony_ci    this.maxLength == null &&
601cb0ef41Sopenharmony_ci    this.minLength == null
611cb0ef41Sopenharmony_ci  ) {
621cb0ef41Sopenharmony_ci    length = stringWidth(this.value)
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci  return length
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciTemplateItem.prototype.getLength = function () {
681cb0ef41Sopenharmony_ci  var length = this.getBaseLength()
691cb0ef41Sopenharmony_ci  if (length == null) {
701cb0ef41Sopenharmony_ci    return null
711cb0ef41Sopenharmony_ci  }
721cb0ef41Sopenharmony_ci  return length + this.padLeft + this.padRight
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciTemplateItem.prototype.getMaxLength = function () {
761cb0ef41Sopenharmony_ci  if (this.maxLength == null) {
771cb0ef41Sopenharmony_ci    return null
781cb0ef41Sopenharmony_ci  }
791cb0ef41Sopenharmony_ci  return this.maxLength + this.padLeft + this.padRight
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ciTemplateItem.prototype.getMinLength = function () {
831cb0ef41Sopenharmony_ci  if (this.minLength == null) {
841cb0ef41Sopenharmony_ci    return null
851cb0ef41Sopenharmony_ci  }
861cb0ef41Sopenharmony_ci  return this.minLength + this.padLeft + this.padRight
871cb0ef41Sopenharmony_ci}
88