Badge.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. define("dojox/image/Badge", ["dojo", "dijit", "dojox/main", "dijit/_Widget", "dijit/_TemplatedMixin", "dojo/fx/easing"], function(dojo, dijit, dojox){
  2. dojo.experimental("dojox.image.Badge");
  3. dojo.getObject("image", true, dojox);
  4. dojo.declare("dojox.image.Badge", [dijit._Widget, dijit._TemplatedMixin], {
  5. // summary: A simple grid of Images that loops through thumbnails
  6. //
  7. baseClass: "dojoxBadge",
  8. templateString:'<div class="dojoxBadge" dojoAttachPoint="containerNode"></div>',
  9. // children: String
  10. // A CSS3 Selector that determines the node to become a child
  11. children: "div.dojoxBadgeImage",
  12. // rows: Integer
  13. // Number of Rows to display
  14. rows: 4,
  15. // cols: Integer
  16. // Number of Columns to display
  17. cols: 5,
  18. // cellSize: Integer
  19. // Size in PX of each thumbnail
  20. cellSize: 50,
  21. // cellMargin: Integer
  22. // Size in PX to adjust for cell margins
  23. cellMargin: 1,
  24. // delay: Integer
  25. // Time (in ms) to show the image before sizing down again
  26. delay: 2000,
  27. // threads: Integer
  28. // how many cycles will be going "simultaneously" (>2 not reccommended)
  29. threads: 1,
  30. // easing: Function|String
  31. // An easing function to use when showing the node (does not apply to shrinking)
  32. easing: "dojo.fx.easing.backOut",
  33. startup: function(){
  34. if(this._started){ return; }
  35. if(dojo.isString(this.easing)){
  36. this.easing = dojo.getObject(this.easing);
  37. }
  38. this.inherited(arguments);
  39. this._init();
  40. },
  41. _init: function(){
  42. // summary: Setup and layout the images
  43. var _row = 0,
  44. _w = this.cellSize;
  45. dojo.style(this.domNode, {
  46. width: _w * this.cols + "px",
  47. height: _w * this.rows + "px"
  48. });
  49. this._nl = dojo.query(this.children, this.containerNode)
  50. .forEach(function(n, _idx){
  51. var _col = _idx % this.cols,
  52. t = _row * _w,
  53. l = _col * _w,
  54. m = this.cellMargin * 2;
  55. dojo.style(n, {
  56. top: t + "px",
  57. left: l + "px",
  58. width: _w - m + "px",
  59. height: _w - m + "px"
  60. });
  61. if(_col == this.cols - 1){ _row++; }
  62. dojo.addClass(n, this.baseClass + "Image");
  63. }, this)
  64. ;
  65. var l = this._nl.length;
  66. while(this.threads--){
  67. var s = Math.floor(Math.random() * l);
  68. setTimeout(dojo.hitch(this, "_enbiggen", {
  69. target: this._nl[s]
  70. }), this.delay * this.threads);
  71. }
  72. },
  73. _getCell: function(/* DomNode */ n){
  74. // summary: Return information about the position for a given node
  75. var _pos = this._nl.indexOf(n);
  76. if(_pos >= 0){
  77. var _col = _pos % this.cols;
  78. var _row = Math.floor(_pos / this.cols);
  79. return { x: _col, y: _row, n: this._nl[_pos], io: _pos };
  80. }else{
  81. return undefined;
  82. }
  83. },
  84. _getImage: function(){
  85. // summary: Returns the next image in the list, or the first one if not available
  86. return "url('')";
  87. },
  88. _enbiggen: function(/* Event|DomNode */ e){
  89. // summary: Show the passed node in the picker
  90. var _pos = this._getCell(e.target || e);
  91. if (_pos){
  92. // we have a node, and know where it is
  93. var m = this.cellMargin,
  94. _cc = (this.cellSize * 2) - (m * 2),
  95. props = {
  96. height: _cc,
  97. width: _cc
  98. }
  99. ;
  100. var _tehDecider = function(){
  101. // if we have room, we'll want to decide which direction to go
  102. // let "teh decider" decide.
  103. return Math.round(Math.random());
  104. };
  105. if(_pos.x == this.cols - 1 || (_pos.x > 0 && _tehDecider() )){
  106. // we have to go left, at right edge (or we want to and not on left edge)
  107. props.left = this.cellSize * (_pos.x - m);
  108. }
  109. if(_pos.y == this.rows - 1 || (_pos.y > 0 && _tehDecider() )){
  110. // we have to go up, at bottom edge (or we want to and not at top)
  111. props.top = this.cellSize * (_pos.y - m);
  112. }
  113. var bc = this.baseClass;
  114. dojo.addClass(_pos.n, bc + "Top");
  115. dojo.addClass(_pos.n, bc + "Seen");
  116. dojo.animateProperty({ node: _pos.n, properties: props,
  117. onEnd: dojo.hitch(this, "_loadUnder", _pos, props),
  118. easing: this.easing
  119. }).play();
  120. }
  121. },
  122. _loadUnder: function(info, props){
  123. // summary: figure out which three images are being covered, and
  124. // determine if they need loaded or not
  125. var idx = info.io;
  126. var nodes = [];
  127. var isLeft = (props.left >= 0);
  128. var isUp = (props.top >= 0);
  129. var c = this.cols,
  130. // the three node index's we're allegedly over:
  131. e = idx + (isLeft ? -1 : 1),
  132. f = idx + (isUp ? -c : c),
  133. // don't ask:
  134. g = (isUp ? (isLeft ? e - c : f + 1) : (isLeft ? f - 1 : e + c)),
  135. bc = this.baseClass;
  136. dojo.forEach([e, f, g], function(x){
  137. var n = this._nl[x];
  138. if(n){
  139. if(dojo.hasClass(n, bc + "Seen")){
  140. // change the background image out?
  141. dojo.removeClass(n, bc + "Seen");
  142. }
  143. }
  144. },this);
  145. setTimeout(dojo.hitch(this, "_disenbiggen", info, props), this.delay * 1.25);
  146. },
  147. _disenbiggen: function(info, props){
  148. // summary: Hide the passed node (info.n), passing along properties
  149. // received.
  150. if(props.top >= 0){
  151. props.top += this.cellSize;
  152. }
  153. if(props.left >= 0){
  154. props.left += this.cellSize;
  155. }
  156. var _cc = this.cellSize - (this.cellMargin * 2);
  157. dojo.animateProperty({
  158. node: info.n,
  159. properties: dojo.mixin(props, {
  160. width:_cc,
  161. height:_cc
  162. }),
  163. onEnd: dojo.hitch(this, "_cycle", info, props)
  164. }).play(5);
  165. },
  166. _cycle: function(info, props){
  167. // summary: Select an un-viewed image from the list, and show it
  168. var bc = this.baseClass;
  169. dojo.removeClass(info.n, bc + "Top");
  170. var ns = this._nl.filter(function(n){
  171. return !dojo.hasClass(n, bc + "Seen")
  172. });
  173. var c = ns[Math.floor(Math.random() * ns.length)];
  174. setTimeout(dojo.hitch(this,"_enbiggen", { target: c }), this.delay / 2)
  175. }
  176. });
  177. return dojox.image.Badge;
  178. })