Requires

Provides

Attributes.js

A mixin that adds support for setting attributes, adding and removing classes and pseudos

License:
Public domain (http://unlicense.org).
Authors:
Yaroslaff Fedin
  1. 23
  2. 24
  3. 25
  4. 26
  5. 27
  6. 28
  7. 29
  8. 30
  9. 31
  10. 32
  11. 33
  12. 34
  13. 35
  14. 36
  15. 37
  16. 38
  17. 39
  18. 40
  19. 41
  20. 42
  21. 43
  22. 44
  23. 45
  24. 46
  25. 47
  26. 48
  27. 49
  28. 50
  29. 51
  30. 52
  31. 53
  32. 54
  33. 55
  34. 56
  35. 57
  36. 58
  37. 59
  38. 60
  39. 61
  40. 62
  41. 63
  42. 64
  43. 65
  44. 66
  45. 67
  46. 68
  47. 69
  48. 70
  49. 71
  50. 72
  51. 73
  52. 74
  53. 75
  54. 76
  55. 77
  56. 78
  57. 79
  58. 80
  59. 81
  60. 82
  61. 83
  62. 84
  63. 85
  64. 86
  65. 87
  66. 88
  67. 89
  68. 90
  69. 91
  70. 92
  71. 93
  72. 94
  73. 95
  74. 96
  75. 97
  76. 98
  77. 99
  78. 100
  79. 101
  80. 102
  81. 103
  82. 104
  83. 105
  84. 106
  85. 107
  86. 108
  87. 109
  88. 110
  89. 111
  90. 112
  91. 113
  92. 114
  93. 115
  94. 116
  95. 117
  96. 118
  97. 119
  98. 120
  99. 121
  100. 122
  101. 123
  102. 124
  103. 125
  104. 126
  105. 127
  106. 128
  107. 129
  108. 130
  109. 131
  110. 132
  111. 133
  112. 134
  113. 135
  114. 136
  115. 137
  116. 138
  117. 139
  118. 140
  119. 141
  120. 142
  121. 143
LSD.Module.Attributes = new Class({ initialize: function() { this.classes = new FastArray this.pseudos = new FastArray this.attributes = {} this.parent.apply(this, arguments); if (this.options.id) this.id = this.options.id; var attributes = this.options.attributes; if (attributes) for (var name in attributes) if (!LSD.Attributes.Ignore[name]) this.attributes[name] = attributes[name]; this.classes.concat(this.options.classes || []).each(function(kls) { if (LSD.States.Classes[kls]) this.pseudos.push(kls); else this.addClass(kls); }, this); this.pseudos.concat(this.options.pseudos || []).each(function(value) { if (this.$states[value]) this.setStateTo(value, true); else this.addPseudo(value); }, this); }, getAttribute: function(attribute) { switch (attribute) { case "id": return this.id; case "class": return this.classes.join(' '); default: return this.attributes[attribute] || this.pseudos[attribute] } }, removeAttribute: function(attribute) { delete this.attributes[attribute]; if (this.element) this.element.removeProperty(attribute); }, setAttribute: function(attribute, value) { if (LSD.Attributes.Ignore[attribute]) return; if (LSD.Attributes.Numeric[attribute]) value = value.toInt(); else { var logic = LSD.Attributes.Setter[attribute]; if (logic) logic.call(this, value) } if (value === attribute) value = true; if (typeof value != 'string') value = value.toString() //Slick compat this.attributes[attribute] = value; if (attribute != 'slick-uniqueid') if (this.element) this.element.setProperty(attribute, value); }, addPseudo: function(pseudo){ this.pseudos.include(pseudo); }, removePseudo: function(pseudo){ this.pseudos.erase(pseudo); }, addClass: function(name) { this.classes.include(name); if (this.element) this.element.addClass(name); }, removeClass: function(name) { var state = LSD.States.Classes[name]; if (state) this.pseudos.erase(state) this.classes.erase(name); if (this.element) this.element.removeClass(name); }, hasClass: function(name) { return this.classes[name] }, setState: function(state) { if (LSD.States.Attributes[state]) { this.setAttribute(state, true) } else { this.addClass(LSD.States.Classes[state] ? state : 'is-' + state); } this.addPseudo(state); }, unsetState: function(state) { if (LSD.States.Attributes[state]) { this.removeAttribute(state) } else { this.removeClass(LSD.States.Classes[state] ? state : 'is-' + state); } this.removePseudo(state); }, getSelector: function(){ var parent = this.parentNode; var selector = (parent && parent.getSelector) ? parent.getSelector() + ' ' : ''; selector += this.options.tag; if (this.options.id) selector += '#' + this.options.id; for (var klass in this.classes) if (this.classes.hasOwnProperty(klass)) selector += '.' + klass; for (var pseudo in this.pseudos) if (this.pseudos.hasOwnProperty(pseudo)) selector += ':' + pseudo; if (this.attributes) for (var name in this.attributes) selector += '[' + name + '=' + this.attributes[name] + ']'; return selector; }, onStateChange: function(state, value, args) { var args = Array.prototype.slice.call(arguments, 0); args.slice(1, 2); //state + args this[value ? 'setState' : 'unsetState'].apply(this, args); this.fireEvent('stateChange', [state, args]) return true; }, }); LSD.Attributes.Setter = { 'class': function(value) { value.split(' ').each(this.addClass.bind(this)); }, 'style': function(value) { value.split(/\s*;\s*/).each(function(definition) { var bits = definition.split(/\s*:\s*/) if (!bits[1]) return; bits[0] = bits[0].camelCase(); var integer = bits[1].toInt(); if (bits[1].indexOf('px') > -1 || (integer == bits[1])) bits[1] = integer

this.setStyle.apply(this, bits);

  1. 145
  2. 146
  3. 147
  4. 148
  5. 149
  6. 150
  7. 151
}, this); }, 'disabled': function(value) { if (value == false) this.enable() else this.disable(); } };