global.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var _ = require('../util')
  2. var config = require('../config')
  3. /**
  4. * Expose useful internals
  5. */
  6. exports.util = _
  7. exports.config = config
  8. exports.nextTick = _.nextTick
  9. exports.compiler = require('../compiler')
  10. exports.parsers = {
  11. path: require('../parsers/path'),
  12. text: require('../parsers/text'),
  13. template: require('../parsers/template'),
  14. directive: require('../parsers/directive'),
  15. expression: require('../parsers/expression')
  16. }
  17. /**
  18. * Each instance constructor, including Vue, has a unique
  19. * cid. This enables us to create wrapped "child
  20. * constructors" for prototypal inheritance and cache them.
  21. */
  22. exports.cid = 0
  23. var cid = 1
  24. /**
  25. * Class inheritance
  26. *
  27. * @param {Object} extendOptions
  28. */
  29. exports.extend = function (extendOptions) {
  30. extendOptions = extendOptions || {}
  31. var Super = this
  32. var Sub = createClass(
  33. extendOptions.name ||
  34. Super.options.name ||
  35. 'VueComponent'
  36. )
  37. Sub.prototype = Object.create(Super.prototype)
  38. Sub.prototype.constructor = Sub
  39. Sub.cid = cid++
  40. Sub.options = _.mergeOptions(
  41. Super.options,
  42. extendOptions
  43. )
  44. Sub['super'] = Super
  45. // allow further extension
  46. Sub.extend = Super.extend
  47. // create asset registers, so extended classes
  48. // can have their private assets too.
  49. config._assetTypes.forEach(function (type) {
  50. Sub[type] = Super[type]
  51. })
  52. return Sub
  53. }
  54. /**
  55. * A function that returns a sub-class constructor with the
  56. * given name. This gives us much nicer output when
  57. * logging instances in the console.
  58. *
  59. * @param {String} name
  60. * @return {Function}
  61. */
  62. function createClass (name) {
  63. return new Function(
  64. 'return function ' + _.classify(name) +
  65. ' (options) { this._init(options) }'
  66. )()
  67. }
  68. /**
  69. * Plugin system
  70. *
  71. * @param {Object} plugin
  72. */
  73. exports.use = function (plugin) {
  74. // additional parameters
  75. var args = _.toArray(arguments, 1)
  76. args.unshift(this)
  77. if (typeof plugin.install === 'function') {
  78. plugin.install.apply(plugin, args)
  79. } else {
  80. plugin.apply(null, args)
  81. }
  82. return this
  83. }
  84. /**
  85. * Create asset registration methods with the following
  86. * signature:
  87. *
  88. * @param {String} id
  89. * @param {*} definition
  90. */
  91. config._assetTypes.forEach(function (type) {
  92. exports[type] = function (id, definition) {
  93. if (!definition) {
  94. return this.options[type + 's'][id]
  95. } else {
  96. if (
  97. type === 'component' &&
  98. _.isPlainObject(definition)
  99. ) {
  100. definition.name = id
  101. definition = _.Vue.extend(definition)
  102. }
  103. this.options[type + 's'][id] = definition
  104. }
  105. }
  106. })