utils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. var config = require('./config'),
  2. attrs = config.attrs,
  3. toString = Object.prototype.toString,
  4. join = Array.prototype.join,
  5. console = window.console,
  6. ViewModel // late def
  7. /**
  8. * Create a prototype-less object
  9. * which is a better hash/map
  10. */
  11. function makeHash () {
  12. return Object.create(null)
  13. }
  14. /**
  15. * trim for map
  16. */
  17. function trim (str) {
  18. return str.trim()
  19. }
  20. var utils = module.exports = {
  21. hash: makeHash,
  22. // global storage for user-registered
  23. // vms, partials and transitions
  24. components : makeHash(),
  25. partials : makeHash(),
  26. transitions : makeHash(),
  27. /**
  28. * get an attribute and remove it.
  29. */
  30. attr: function (el, type) {
  31. var attr = attrs[type],
  32. val = el.getAttribute(attr)
  33. if (val) el.removeAttribute(attr)
  34. return val
  35. },
  36. /**
  37. * Define an ienumerable property
  38. * This avoids it being included in JSON.stringify
  39. * or for...in loops.
  40. */
  41. defProtected: function (obj, key, val, enumerable, configurable) {
  42. if (obj.hasOwnProperty(key)) return
  43. Object.defineProperty(obj, key, {
  44. value : val,
  45. enumerable : !!enumerable,
  46. configurable : !!configurable
  47. })
  48. },
  49. /**
  50. * split a transition class string into array
  51. */
  52. split: function (classString) {
  53. if (classString) {
  54. return classString.split(',').map(trim)
  55. }
  56. },
  57. /**
  58. * Accurate type check
  59. * internal use only, so no need to check for NaN
  60. */
  61. typeOf: function (obj) {
  62. return toString.call(obj).slice(8, -1)
  63. },
  64. /**
  65. * Make sure only strings and numbers are output to html
  66. * output empty string is value is not string or number
  67. */
  68. toText: function (value) {
  69. /* jshint eqeqeq: false */
  70. return (typeof value === 'string' ||
  71. typeof value === 'boolean' ||
  72. (typeof value === 'number' && value == value)) // deal with NaN
  73. ? value
  74. : ''
  75. },
  76. /**
  77. * simple extend
  78. */
  79. extend: function (obj, ext, protective) {
  80. for (var key in ext) {
  81. if (protective && obj[key]) continue
  82. obj[key] = ext[key]
  83. }
  84. },
  85. /**
  86. * Convert a string template to a dom fragment
  87. */
  88. toFragment: function (template) {
  89. if (typeof template !== 'string') {
  90. return template
  91. }
  92. if (template.charAt(0) === '#') {
  93. var templateNode = document.getElementById(template.slice(1))
  94. if (!templateNode) return
  95. template = templateNode.innerHTML
  96. }
  97. var node = document.createElement('div'),
  98. frag = document.createDocumentFragment(),
  99. child
  100. node.innerHTML = template.trim()
  101. /* jshint boss: true */
  102. while (child = node.firstChild) {
  103. frag.appendChild(child)
  104. }
  105. return frag
  106. },
  107. /**
  108. * Convert the object to a ViewModel constructor
  109. * if it is not already one
  110. */
  111. toConstructor: function (obj) {
  112. ViewModel = ViewModel || require('./viewmodel')
  113. return obj.prototype instanceof ViewModel || obj === ViewModel
  114. ? obj
  115. : ViewModel.extend(obj)
  116. },
  117. /**
  118. * convert certain option values to the desired format.
  119. */
  120. processOptions: function (options) {
  121. var components = options.components,
  122. partials = options.partials,
  123. template = options.template,
  124. key
  125. if (components) {
  126. for (key in components) {
  127. components[key] = utils.toConstructor(components[key])
  128. }
  129. }
  130. if (partials) {
  131. for (key in partials) {
  132. partials[key] = utils.toFragment(partials[key])
  133. }
  134. }
  135. if (template) {
  136. options.template = utils.toFragment(template)
  137. }
  138. },
  139. /**
  140. * log for debugging
  141. */
  142. log: function () {
  143. if (config.debug && console) {
  144. console.log(join.call(arguments, ' '))
  145. }
  146. },
  147. /**
  148. * warnings, thrown in all cases
  149. */
  150. warn: function() {
  151. if (!config.silent && console) {
  152. console.warn(join.call(arguments, ' '))
  153. }
  154. }
  155. }