index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var utils = require('../utils'),
  2. config = require('../config'),
  3. transition = require('../transition')
  4. module.exports = {
  5. on : require('./on'),
  6. repeat : require('./repeat'),
  7. model : require('./model'),
  8. 'if' : require('./if'),
  9. 'with' : require('./with'),
  10. html : require('./html'),
  11. style : require('./style'),
  12. partial : require('./partial'),
  13. attr: function (value) {
  14. if (value || value === 0) {
  15. this.el.setAttribute(this.arg, value)
  16. } else {
  17. this.el.removeAttribute(this.arg)
  18. }
  19. },
  20. text: {
  21. bind: function () {
  22. this.attr = this.el.nodeType === 3
  23. ? 'nodeValue'
  24. : 'textContent'
  25. },
  26. update: function (value) {
  27. this.el[this.attr] = utils.toText(value)
  28. }
  29. },
  30. show: function (value) {
  31. var el = this.el,
  32. target = value ? '' : 'none',
  33. change = function () {
  34. el.style.display = target
  35. }
  36. transition(el, value ? 1 : -1, change, this.compiler)
  37. },
  38. 'class': function (value) {
  39. if (this.arg) {
  40. utils[value ? 'addClass' : 'removeClass'](this.el, this.arg)
  41. } else {
  42. if (this.lastVal) {
  43. utils.removeClass(this.el, this.lastVal)
  44. }
  45. if (value) {
  46. utils.addClass(this.el, value)
  47. this.lastVal = value
  48. }
  49. }
  50. },
  51. cloak: {
  52. isEmpty: true,
  53. bind: function () {
  54. var el = this.el
  55. this.compiler.observer.once('hook:ready', function () {
  56. el.removeAttribute(config.prefix + '-cloak')
  57. })
  58. }
  59. },
  60. ref: {
  61. isLiteral: true,
  62. bind: function () {
  63. var id = this.expression
  64. if (id) {
  65. this.vm.$parent.$[id] = this.vm
  66. }
  67. },
  68. unbind: function () {
  69. var id = this.expression
  70. if (id) {
  71. delete this.vm.$parent.$[id]
  72. }
  73. }
  74. }
  75. }