ListSelector.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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["dojox.mobile.app.ListSelector"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.mobile.app.ListSelector"] = true;
  8. dojo.provide("dojox.mobile.app.ListSelector");
  9. dojo.experimental("dojox.mobile.app.ListSelector");
  10. dojo.require("dojox.mobile.app._Widget");
  11. dojo.require("dojo.fx");
  12. dojo.declare("dojox.mobile.app.ListSelector", dojox.mobile.app._Widget, {
  13. // data: Array
  14. // The array of items to display. Each element in the array
  15. // should have both a label and value attribute, e.g.
  16. // [{label: "Open", value: 1} , {label: "Delete", value: 2}]
  17. data: null,
  18. // controller: Object
  19. // The current SceneController widget.
  20. controller: null,
  21. // onChoose: Function
  22. // The callback function for when an item is selected
  23. onChoose: null,
  24. destroyOnHide: false,
  25. _setDataAttr: function(data){
  26. this.data = data;
  27. if(this.data){
  28. this.render();
  29. }
  30. },
  31. postCreate: function(){
  32. dojo.addClass(this.domNode, "listSelector");
  33. var _this = this;
  34. this.connect(this.domNode, "onclick", function(event){
  35. if(!dojo.hasClass(event.target, "listSelectorRow")){
  36. return;
  37. }
  38. if(_this.onChoose){
  39. _this.onChoose(_this.data[event.target._idx].value);
  40. }
  41. _this.hide();
  42. });
  43. this.connect(this.domNode, "onmousedown", function(event){
  44. if(!dojo.hasClass(event.target, "listSelectorRow")){
  45. return;
  46. }
  47. dojo.addClass(event.target, "listSelectorRow-selected");
  48. });
  49. this.connect(this.domNode, "onmouseup", function(event){
  50. if(!dojo.hasClass(event.target, "listSelectorRow")){
  51. return;
  52. }
  53. dojo.removeClass(event.target, "listSelectorRow-selected");
  54. });
  55. this.connect(this.domNode, "onmouseout", function(event){
  56. if(!dojo.hasClass(event.target, "listSelectorRow")){
  57. return;
  58. }
  59. dojo.removeClass(event.target, "listSelectorRow-selected");
  60. });
  61. var viewportSize = this.controller.getWindowSize();
  62. this.mask = dojo.create("div", {"class": "dialogUnderlayWrapper",
  63. innerHTML: "<div class=\"dialogUnderlay\"></div>"
  64. }, this.controller.assistant.domNode);
  65. this.connect(this.mask, "onclick", function(){
  66. _this.onChoose && _this.onChoose();
  67. _this.hide();
  68. });
  69. },
  70. show: function(fromNode){
  71. // Using dojo.fx here. Must figure out how to do this with CSS animations!!
  72. var startPos;
  73. var windowSize = this.controller.getWindowSize();
  74. var fromNodePos;
  75. if(fromNode){
  76. fromNodePos = dojo._abs(fromNode);
  77. startPos = fromNodePos;
  78. }else{
  79. startPos.x = windowSize.w / 2;
  80. startPos.y = 200;
  81. }
  82. console.log("startPos = ", startPos);
  83. dojo.style(this.domNode, {
  84. opacity: 0,
  85. display: "",
  86. width: Math.floor(windowSize.w * 0.8) + "px"
  87. });
  88. var maxWidth = 0;
  89. dojo.query(">", this.domNode).forEach(function(node){
  90. dojo.style(node, {
  91. "float": "left"
  92. });
  93. maxWidth = Math.max(maxWidth, dojo.marginBox(node).w);
  94. dojo.style(node, {
  95. "float": "none"
  96. });
  97. });
  98. maxWidth = Math.min(maxWidth, Math.round(windowSize.w * 0.8))
  99. + dojo.style(this.domNode, "paddingLeft")
  100. + dojo.style(this.domNode, "paddingRight")
  101. + 1;
  102. dojo.style(this.domNode, "width", maxWidth + "px");
  103. var targetHeight = dojo.marginBox(this.domNode).h;
  104. var _this = this;
  105. var targetY = fromNodePos ?
  106. Math.max(30, fromNodePos.y - targetHeight - 10) :
  107. this.getScroll().y + 30;
  108. console.log("fromNodePos = ", fromNodePos, " targetHeight = ", targetHeight,
  109. " targetY = " + targetY, " startPos ", startPos);
  110. var anim1 = dojo.animateProperty({
  111. node: this.domNode,
  112. duration: 400,
  113. properties: {
  114. width: {start: 1, end: maxWidth},
  115. height: {start: 1, end: targetHeight},
  116. top: {start: startPos.y, end: targetY},
  117. left: {start: startPos.x, end: (windowSize.w/2 - maxWidth/2)},
  118. opacity: {start: 0, end: 1},
  119. fontSize: {start: 1}
  120. },
  121. onEnd: function(){
  122. dojo.style(_this.domNode, "width", "inherit");
  123. }
  124. });
  125. var anim2 = dojo.fadeIn({
  126. node: this.mask,
  127. duration: 400
  128. });
  129. dojo.fx.combine([anim1, anim2]).play();
  130. },
  131. hide: function(){
  132. // Using dojo.fx here. Must figure out how to do this with CSS animations!!
  133. var _this = this;
  134. var anim1 = dojo.animateProperty({
  135. node: this.domNode,
  136. duration: 500,
  137. properties: {
  138. width: {end: 1},
  139. height: {end: 1},
  140. opacity: {end: 0},
  141. fontSize: {end: 1}
  142. },
  143. onEnd: function(){
  144. if(_this.get("destroyOnHide")){
  145. _this.destroy();
  146. }
  147. }
  148. });
  149. var anim2 = dojo.fadeOut({
  150. node: this.mask,
  151. duration: 400
  152. });
  153. dojo.fx.combine([anim1, anim2]).play();
  154. },
  155. render: function(){
  156. // summary:
  157. // Renders
  158. dojo.empty(this.domNode);
  159. dojo.style(this.domNode, "opacity", 0);
  160. var row;
  161. for(var i = 0; i < this.data.length; i++){
  162. // Create each row and add any custom classes. Also set the _idx property.
  163. row = dojo.create("div", {
  164. "class": "listSelectorRow " + (this.data[i].className || ""),
  165. innerHTML: this.data[i].label
  166. }, this.domNode);
  167. row._idx = i;
  168. if(i == 0){
  169. dojo.addClass(row, "first");
  170. }
  171. if(i == this.data.length - 1){
  172. dojo.addClass(row, "last");
  173. }
  174. }
  175. },
  176. destroy: function(){
  177. this.inherited(arguments);
  178. dojo.destroy(this.mask);
  179. }
  180. });
  181. }