Avatar.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. define("dojo/dnd/Avatar", ["../main", "./common"], function(dojo) {
  2. // module:
  3. // dojo/dnd/Avatar
  4. // summary:
  5. // TODOC
  6. dojo.declare("dojo.dnd.Avatar", null, {
  7. // summary:
  8. // Object that represents transferred DnD items visually
  9. // manager: Object
  10. // a DnD manager object
  11. constructor: function(manager){
  12. this.manager = manager;
  13. this.construct();
  14. },
  15. // methods
  16. construct: function(){
  17. // summary:
  18. // constructor function;
  19. // it is separate so it can be (dynamically) overwritten in case of need
  20. this.isA11y = dojo.hasClass(dojo.body(),"dijit_a11y");
  21. var a = dojo.create("table", {
  22. "class": "dojoDndAvatar",
  23. style: {
  24. position: "absolute",
  25. zIndex: "1999",
  26. margin: "0px"
  27. }
  28. }),
  29. source = this.manager.source, node,
  30. b = dojo.create("tbody", null, a),
  31. tr = dojo.create("tr", null, b),
  32. td = dojo.create("td", null, tr),
  33. icon = this.isA11y ? dojo.create("span", {
  34. id : "a11yIcon",
  35. innerHTML : this.manager.copy ? '+' : "<"
  36. }, td) : null,
  37. span = dojo.create("span", {
  38. innerHTML: source.generateText ? this._generateText() : ""
  39. }, td),
  40. k = Math.min(5, this.manager.nodes.length), i = 0;
  41. // we have to set the opacity on IE only after the node is live
  42. dojo.attr(tr, {
  43. "class": "dojoDndAvatarHeader",
  44. style: {opacity: 0.9}
  45. });
  46. for(; i < k; ++i){
  47. if(source.creator){
  48. // create an avatar representation of the node
  49. node = source._normalizedCreator(source.getItem(this.manager.nodes[i].id).data, "avatar").node;
  50. }else{
  51. // or just clone the node and hope it works
  52. node = this.manager.nodes[i].cloneNode(true);
  53. if(node.tagName.toLowerCase() == "tr"){
  54. // insert extra table nodes
  55. var table = dojo.create("table"),
  56. tbody = dojo.create("tbody", null, table);
  57. tbody.appendChild(node);
  58. node = table;
  59. }
  60. }
  61. node.id = "";
  62. tr = dojo.create("tr", null, b);
  63. td = dojo.create("td", null, tr);
  64. td.appendChild(node);
  65. dojo.attr(tr, {
  66. "class": "dojoDndAvatarItem",
  67. style: {opacity: (9 - i) / 10}
  68. });
  69. }
  70. this.node = a;
  71. },
  72. destroy: function(){
  73. // summary:
  74. // destructor for the avatar; called to remove all references so it can be garbage-collected
  75. dojo.destroy(this.node);
  76. this.node = false;
  77. },
  78. update: function(){
  79. // summary:
  80. // updates the avatar to reflect the current DnD state
  81. dojo[(this.manager.canDropFlag ? "add" : "remove") + "Class"](this.node, "dojoDndAvatarCanDrop");
  82. if (this.isA11y){
  83. var icon = dojo.byId("a11yIcon");
  84. var text = '+'; // assume canDrop && copy
  85. if (this.manager.canDropFlag && !this.manager.copy) {
  86. text = '< '; // canDrop && move
  87. }else if (!this.manager.canDropFlag && !this.manager.copy) {
  88. text = "o"; //!canDrop && move
  89. }else if(!this.manager.canDropFlag){
  90. text = 'x'; // !canDrop && copy
  91. }
  92. icon.innerHTML=text;
  93. }
  94. // replace text
  95. dojo.query(("tr.dojoDndAvatarHeader td span" +(this.isA11y ? " span" : "")), this.node).forEach(
  96. function(node){
  97. node.innerHTML = this._generateText();
  98. }, this);
  99. },
  100. _generateText: function(){
  101. // summary: generates a proper text to reflect copying or moving of items
  102. return this.manager.nodes.length.toString();
  103. }
  104. });
  105. return dojo.dnd.Avatar;
  106. });