vml-element.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Wrapper for VML element.
  3. * @constructor
  4. * @extends jvm.AbstractElement
  5. * @param {String} name Tag name of the element
  6. * @param {Object} config Set of parameters to initialize element with
  7. */
  8. jvm.VMLElement = function(name, config){
  9. if (!jvm.VMLElement.VMLInitialized) {
  10. jvm.VMLElement.initializeVML();
  11. }
  12. jvm.VMLElement.parentClass.apply(this, arguments);
  13. };
  14. jvm.inherits(jvm.VMLElement, jvm.AbstractElement);
  15. /**
  16. * Shows if VML was already initialized for the current document or not.
  17. * @static
  18. * @private
  19. * @type {Boolean}
  20. */
  21. jvm.VMLElement.VMLInitialized = false;
  22. /**
  23. * Initializes VML handling before creating the first element
  24. * (adds CSS class and creates namespace). Adds one of two forms
  25. * of createElement method depending of support by browser.
  26. * @static
  27. * @private
  28. */
  29. // The following method of VML handling is borrowed from the
  30. // Raphael library by Dmitry Baranovsky.
  31. jvm.VMLElement.initializeVML = function(){
  32. try {
  33. if (!document.namespaces.rvml) {
  34. document.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
  35. }
  36. /**
  37. * Creates DOM element.
  38. * @param {String} tagName Name of element
  39. * @private
  40. * @returns DOMElement
  41. */
  42. jvm.VMLElement.prototype.createElement = function (tagName) {
  43. return document.createElement('<rvml:' + tagName + ' class="rvml">');
  44. };
  45. } catch (e) {
  46. /**
  47. * @private
  48. */
  49. jvm.VMLElement.prototype.createElement = function (tagName) {
  50. return document.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
  51. };
  52. }
  53. document.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
  54. jvm.VMLElement.VMLInitialized = true;
  55. };
  56. /**
  57. * Returns constructor for element by name prefixed with 'VML'.
  58. * @param {String} ctr Name of basic constructor to return
  59. * proper implementation for.
  60. * @returns Function
  61. * @private
  62. */
  63. jvm.VMLElement.prototype.getElementCtr = function( ctr ){
  64. return jvm['VML'+ctr];
  65. };
  66. /**
  67. * Adds CSS class for underlying DOM element.
  68. * @param {String} className Name of CSS class name
  69. */
  70. jvm.VMLElement.prototype.addClass = function( className ){
  71. jvm.$(this.node).addClass(className);
  72. };
  73. /**
  74. * Applies attribute value to the underlying DOM element.
  75. * @param {String} name Name of attribute
  76. * @param {Number|String} config Value of attribute to apply
  77. * @private
  78. */
  79. jvm.VMLElement.prototype.applyAttr = function( attr, value ){
  80. this.node[attr] = value;
  81. };
  82. /**
  83. * Returns boundary box for the element.
  84. * @returns {Object} Boundary box with numeric fields: x, y, width, height
  85. * @override
  86. */
  87. jvm.VMLElement.prototype.getBBox = function(){
  88. var node = jvm.$(this.node);
  89. return {
  90. x: node.position().left / this.canvas.scale,
  91. y: node.position().top / this.canvas.scale,
  92. width: node.width() / this.canvas.scale,
  93. height: node.height() / this.canvas.scale
  94. };
  95. };