AlertDialog.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // wrapped by build app
  2. define("dojox/mobile/app/AlertDialog", ["dijit","dojo","dojox","dojo/require!dijit/_WidgetBase"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.mobile.app.AlertDialog");
  4. dojo.experimental("dojox.mobile.app.AlertDialog");
  5. dojo.require("dijit._WidgetBase");
  6. dojo.declare("dojox.mobile.app.AlertDialog", dijit._WidgetBase, {
  7. // title: String
  8. // The title of the AlertDialog
  9. title: "",
  10. // text: String
  11. // The text message displayed in the AlertDialog
  12. text: "",
  13. // controller: Object
  14. // The SceneController for the currently active scene
  15. controller: null,
  16. // buttons: Array
  17. buttons: null,
  18. defaultButtonLabel: "OK",
  19. // onChoose: Function
  20. // The callback function that is invoked when a button is tapped.
  21. // If the dialog is cancelled, no parameter is passed to this function.
  22. onChoose: null,
  23. constructor: function(){
  24. this.onClick = dojo.hitch(this, this.onClick);
  25. this._handleSelect = dojo.hitch(this, this._handleSelect);
  26. },
  27. buildRendering: function(){
  28. this.domNode = dojo.create("div",{
  29. "class": "alertDialog"
  30. });
  31. // Create the outer dialog body
  32. var dlgBody = dojo.create("div", {"class": "alertDialogBody"}, this.domNode);
  33. // Create the title
  34. dojo.create("div", {"class": "alertTitle", innerHTML: this.title || ""}, dlgBody);
  35. // Create the text
  36. dojo.create("div", {"class": "alertText", innerHTML: this.text || ""}, dlgBody);
  37. // Create the node that encapsulates all the buttons
  38. var btnContainer = dojo.create("div", {"class": "alertBtns"}, dlgBody);
  39. // If no buttons have been defined, default to a single button saying OK
  40. if(!this.buttons || this.buttons.length == 0){
  41. this.buttons = [{
  42. label: this.defaultButtonLabel,
  43. value: "ok",
  44. "class": "affirmative"
  45. }];
  46. }
  47. var _this = this;
  48. // Create each of the buttons
  49. dojo.forEach(this.buttons, function(btnInfo){
  50. var btn = new dojox.mobile.Button({
  51. btnClass: btnInfo["class"] || "",
  52. label: btnInfo.label
  53. });
  54. btn._dialogValue = btnInfo.value;
  55. dojo.place(btn.domNode, btnContainer);
  56. _this.connect(btn, "onClick", _this._handleSelect);
  57. });
  58. var viewportSize = this.controller.getWindowSize();
  59. // Create the mask that blocks out the rest of the screen
  60. this.mask = dojo.create("div", {"class": "dialogUnderlayWrapper",
  61. innerHTML: "<div class=\"dialogUnderlay\"></div>",
  62. style: {
  63. width: viewportSize.w + "px",
  64. height: viewportSize.h + "px"
  65. }
  66. }, this.controller.assistant.domNode);
  67. this.connect(this.mask, "onclick", function(){
  68. _this.onChoose && _this.onChoose();
  69. _this.hide();
  70. });
  71. },
  72. postCreate: function(){
  73. this.subscribe("/dojox/mobile/app/goback", this._handleSelect);
  74. },
  75. _handleSelect: function(event){
  76. // summary:
  77. // Handle the selection of a value
  78. var node;
  79. console.log("handleSelect");
  80. if(event && event.target){
  81. node = event.target;
  82. // Find the widget that was tapped.
  83. while(!dijit.byNode(node)){
  84. node - node.parentNode;
  85. }
  86. }
  87. // If an onChoose function was provided, tell it what button
  88. // value was chosen
  89. if(this.onChoose){
  90. this.onChoose(node ? dijit.byNode(node)._dialogValue: undefined);
  91. }
  92. // Hide the dialog
  93. this.hide();
  94. },
  95. show: function(){
  96. // summary:
  97. // Show the dialog
  98. this._doTransition(1);
  99. },
  100. hide: function(){
  101. // summary:
  102. // Hide the dialog
  103. this._doTransition(-1);
  104. },
  105. _doTransition: function(dir){
  106. // summary:
  107. // Either shows or hides the dialog.
  108. // dir:
  109. // An integer. If positive, the dialog is shown. If negative,
  110. // the dialog is hidden.
  111. // TODO: replace this with CSS transitions
  112. var anim;
  113. var h = dojo.marginBox(this.domNode.firstChild).h;
  114. var bodyHeight = this.controller.getWindowSize().h;
  115. console.log("dialog height = " + h, " body height = " + bodyHeight);
  116. var high = bodyHeight - h;
  117. var low = bodyHeight;
  118. var anim1 = dojo.fx.slideTo({
  119. node: this.domNode,
  120. duration: 400,
  121. top: {start: dir < 0 ? high : low, end: dir < 0 ? low: high}
  122. });
  123. var anim2 = dojo[dir < 0 ? "fadeOut" : "fadeIn"]({
  124. node: this.mask,
  125. duration: 400
  126. });
  127. var anim = dojo.fx.combine([anim1, anim2]);
  128. var _this = this;
  129. dojo.connect(anim, "onEnd", this, function(){
  130. if(dir < 0){
  131. _this.domNode.style.display = "none";
  132. dojo.destroy(_this.domNode);
  133. dojo.destroy(_this.mask);
  134. }
  135. });
  136. anim.play();
  137. },
  138. destroy: function(){
  139. this.inherited(arguments);
  140. dojo.destroy(this.mask);
  141. },
  142. onClick: function(){
  143. }
  144. });
  145. });