abstract-element.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Basic wrapper for DOM element.
  3. * @constructor
  4. * @param {String} name Tag name of the element
  5. * @param {Object} config Set of parameters to initialize element with
  6. */
  7. jvm.AbstractElement = function(name, config){
  8. /**
  9. * Underlying DOM element
  10. * @type {DOMElement}
  11. * @private
  12. */
  13. this.node = this.createElement(name);
  14. /**
  15. * Name of underlying element
  16. * @type {String}
  17. * @private
  18. */
  19. this.name = name;
  20. /**
  21. * Internal store of attributes
  22. * @type {Object}
  23. * @private
  24. */
  25. this.properties = {};
  26. if (config) {
  27. this.set(config);
  28. }
  29. };
  30. /**
  31. * Set attribute of the underlying DOM element.
  32. * @param {String} name Name of attribute
  33. * @param {Number|String} config Set of parameters to initialize element with
  34. */
  35. jvm.AbstractElement.prototype.set = function(property, value){
  36. var key;
  37. if (typeof property === 'object') {
  38. for (key in property) {
  39. this.properties[key] = property[key];
  40. this.applyAttr(key, property[key]);
  41. }
  42. } else {
  43. this.properties[property] = value;
  44. this.applyAttr(property, value);
  45. }
  46. };
  47. /**
  48. * Returns value of attribute.
  49. * @param {String} name Name of attribute
  50. */
  51. jvm.AbstractElement.prototype.get = function(property){
  52. return this.properties[property];
  53. };
  54. /**
  55. * Applies attribute value to the underlying DOM element.
  56. * @param {String} name Name of attribute
  57. * @param {Number|String} config Value of attribute to apply
  58. * @private
  59. */
  60. jvm.AbstractElement.prototype.applyAttr = function(property, value){
  61. this.node.setAttribute(property, value);
  62. };
  63. jvm.AbstractElement.prototype.remove = function(){
  64. jvm.$(this.node).remove();
  65. };