Avatar.js 3.5 KB

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