AlertDialog.js 4.6 KB

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