utils.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. if (!dojo._hasResource["dojox.gfx.utils"]) { // _hasResource checks added by
  2. // build. Do not use
  3. // _hasResource directly in your
  4. // code.
  5. dojo._hasResource["dojox.gfx.utils"] = true;
  6. dojo.provide("dojox.gfx.utils");
  7. dojo.require("dojox.gfx");
  8. dojox.gfx.utils.serialize = function(
  9. /* dojox.gfx.Surface || dojox.gfx.Shape */object) {
  10. var t = {}, v, isSurface = object instanceof dojox.gfx.Surface;
  11. if (isSurface || object instanceof dojox.gfx.Group) {
  12. t.children = [];
  13. for (var i = 0; i < object.children.length; ++i) {
  14. t.children.push(dojox.gfx.utils.serialize(object.children[i]));
  15. }
  16. if (isSurface) {
  17. return t.children; // Array
  18. }
  19. } else {
  20. t.shape = object.getShape();
  21. }
  22. if (object.getTransform) {
  23. v = object.getTransform();
  24. if (v) {
  25. t.transform = v;
  26. }
  27. }
  28. if (object.getStroke) {
  29. v = object.getStroke();
  30. if (v) {
  31. t.stroke = v;
  32. }
  33. }
  34. if (object.getFill) {
  35. v = object.getFill();
  36. if (v) {
  37. t.fill = v;
  38. }
  39. }
  40. if (object.getFont) {
  41. v = object.getFont();
  42. if (v) {
  43. t.font = v;
  44. }
  45. }
  46. return t; // Object
  47. };
  48. dojox.gfx.utils.toJson = function(
  49. /* dojox.gfx.Surface || dojox.gfx.Shape */object,
  50. /* Boolean? */prettyPrint) {
  51. return dojo.toJson(dojox.gfx.utils.serialize(object), prettyPrint); // String
  52. };
  53. dojox.gfx.utils.deserialize = function(
  54. /* dojox.gfx.Surface || dojox.gfx.Shape */parent,
  55. /* dojox.gfx.Shape || Array */object) {
  56. if (object instanceof Array) {
  57. var t = [];
  58. for (var i = 0; i < object.length; ++i) {
  59. t.push(dojox.gfx.utils.deserialize(parent, object[i]));
  60. }
  61. return t; // Array
  62. }
  63. var shape = ("shape" in object)
  64. ? parent.createShape(object.shape)
  65. : parent.createGroup();
  66. if ("transform" in object) {
  67. shape.setTransform(object.transform);
  68. }
  69. if ("stroke" in object) {
  70. shape.setStroke(object.stroke);
  71. }
  72. if ("fill" in object) {
  73. shape.setFill(object.fill);
  74. }
  75. if ("font" in object) {
  76. shape.setFont(object.font);
  77. }
  78. if ("children" in object) {
  79. for (var i = 0; i < object.children.length; ++i) {
  80. dojox.gfx.utils.deserialize(shape, object.children[i]);
  81. }
  82. }
  83. return shape; // dojox.gfx.Shape
  84. };
  85. dojox.gfx.utils.fromJson = function(
  86. /* dojox.gfx.Surface || dojox.gfx.Shape */parent,
  87. /* String */json) {
  88. return dojox.gfx.utils.deserialize(parent, dojo.fromJson(json)); // Array
  89. // ||
  90. // dojox.gfx.Shape
  91. };
  92. }