53067cf3ec60890b452497ac537daf0379893c81.svn-base 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. if (!dojo._hasResource["dojox.widget.FileInputAuto"]) { // _hasResource checks
  2. // added by build. Do
  3. // not use _hasResource
  4. // directly in your
  5. // code.
  6. dojo._hasResource["dojox.widget.FileInputAuto"] = true;
  7. dojo.provide("dojox.widget.FileInputAuto");
  8. dojo.require("dojox.widget.FileInput");
  9. dojo.require("dojo.io.iframe");
  10. dojo.declare("dojox.widget.FileInputAuto", dojox.widget.FileInput, {
  11. // summary: An extension on dojox.widget.FileInput providing background
  12. // upload progress
  13. //
  14. // description: An extended version of FileInput - when the user focuses
  15. // away from the input
  16. // the selected file is posted via dojo.io.iframe to the url. example
  17. // implementation
  18. // comes with PHP solution for handling upload, and returning required
  19. // data.
  20. //
  21. // notes: the return data from the io.iframe is used to populate the
  22. // input element with
  23. // data regarding the results. it will be a JSON object, like:
  24. //
  25. // results = { size: "1024", filename: "file.txt" }
  26. //
  27. // all the parameters allowed to dojox.widget.FileInput apply
  28. // url: String
  29. // the URL where our background FileUpload will be sent
  30. url : "",
  31. // blurDelay: Integer
  32. // time in ms before an un-focused widget will wait before uploading the
  33. // file to the url="" specified
  34. // default: 2 seconds
  35. blurDelay : 2000,
  36. // duration: Integer
  37. // The time in ms to use as the generic timing mechanism for the
  38. // animations
  39. // set to 1 or 0 for "immediate respose"
  40. duration : 500,
  41. // uploadMessage: String
  42. //
  43. // FIXME: i18n somehow?
  44. uploadMessage : "Uploading ...",
  45. _sent : false,
  46. // small template changes, new attachpoint: overlay
  47. templateString : "<div class=\"dijitFileInput\">\n\t<input class=\"dijitFileInputReal\" type=\"file\" dojoAttachPoint=\"fileInput\" />\n\t<div class=\"dijitFakeInput\" dojoAttachPoint=\"fakeNodeHolder\">\n\t\t<input class=\"dijitFileInputVisible\" type=\"text\" dojoAttachPoint=\"focusNode, inputNode\" />\n\t\t<span class=\"dijitInline dijitFileInputText\" dojoAttachPoint=\"titleNode\">${label}</span>\n\t\t<span class=\"dijitInline dijitFileInputButton\" dojoAttachPoint=\"cancelNode\" dojoAttachEvent=\"onclick:_onClick\">${cancelText}</span>\n\t</div>\n\t<div class=\"dijitProgressOverlay\" dojoAttachPoint=\"overlay\">&nbsp;</div>\n</div>\n",
  48. startup : function() {
  49. // summary: add our extra blur listeners
  50. this._blurListener = dojo.connect(this.fileInput, "onblur", this,
  51. "_onBlur");
  52. this._focusListener = dojo.connect(this.fileInput, "onfocus", this,
  53. "_onFocus");
  54. this.inherited("startup", arguments);
  55. },
  56. _onFocus : function() {
  57. // summary: clear the upload timer
  58. if (this._blurTimer) {
  59. clearTimeout(this._blurTimer);
  60. }
  61. },
  62. _onBlur : function() {
  63. // summary: start the upload timer
  64. if (this._blurTimer) {
  65. clearTimeout(this._blurTimer);
  66. }
  67. if (!this._sent) {
  68. this._blurTimer = setTimeout(dojo.hitch(this, "_sendFile"),
  69. this.blurDelay);
  70. }
  71. },
  72. setMessage : function(/* String */title) {
  73. // summary: set the text of the progressbar
  74. // FIXME: this throws errors in IE?!?!?!? egads.
  75. if (!dojo.isIE) {
  76. this.overlay.innerHTML = title;
  77. }
  78. },
  79. _sendFile : function(/* Event */e) {
  80. // summary: triggers the chain of events needed to upload a file in
  81. // the background.
  82. if (!this.fileInput.value || this._sent) {
  83. return;
  84. }
  85. dojo.style(this.fakeNodeHolder, "display", "none");
  86. dojo.style(this.overlay, "opacity", "0");
  87. dojo.style(this.overlay, "display", "block");
  88. this.setMessage(this.uploadMessage);
  89. dojo.fadeIn({
  90. node : this.overlay,
  91. duration : this.duration
  92. }).play();
  93. var _newForm = document.createElement('form');
  94. _newForm.setAttribute("enctype", "multipart/form-data");
  95. var node = dojo.clone(this.fileInput);
  96. _newForm.appendChild(this.fileInput);
  97. dojo.body().appendChild(_newForm);
  98. dojo.io.iframe.send({
  99. url : this.url + "?name=" + this.name,
  100. form : _newForm,
  101. handleAs : "text",
  102. handle : dojo.hitch(this, "_handleSend")
  103. });
  104. },
  105. _handleSend : function(data, ioArgs) {
  106. // summary: The callback to toggle the progressbar, and fire the
  107. // user-defined callback
  108. if (!dojo.isIE) {
  109. // otherwise, this throws errors in ie? FIXME:
  110. this.overlay.innerHTML = "";
  111. }
  112. this._sent = true;
  113. dojo.style(this.overlay, "opacity", "0");
  114. dojo.style(this.overlay, "border", "none");
  115. dojo.style(this.overlay, "background", "none");
  116. this.overlay.style.backgroundImage = "none";
  117. this.fileInput.style.display = "none";
  118. this.fakeNodeHolder.style.display = "none";
  119. dojo.fadeIn({
  120. node : this.overlay,
  121. duration : this.duration
  122. }).play(250);
  123. dojo.disconnect(this._blurListener);
  124. dojo.disconnect(this._focusListener);
  125. this.onComplete(data, ioArgs, this);
  126. },
  127. _onClick : function(e) {
  128. // summary: accomodate our extra focusListeners
  129. if (this._blurTimer) {
  130. clearTimeout(this._blurTimer);
  131. }
  132. dojo.disconnect(this._blurListener);
  133. dojo.disconnect(this._focusListener);
  134. this.inherited("_onClick", arguments);
  135. this._blurListener = dojo.connect(this.fileInput, "onblur", this,
  136. "_onBlur");
  137. this._focusListener = dojo.connect(this.fileInput, "onfocus", this,
  138. "_onFocus");
  139. },
  140. onComplete : function(/* Object */data, /* dojo.Deferred._ioArgs */
  141. ioArgs, /* this */widgetRef) {
  142. // summary: stub function fired when an upload has finished.
  143. // data: the raw data found in the first [TEXTAREA] tag of the post
  144. // url
  145. // ioArgs: the dojo.Deferred data being passed from the handle:
  146. // callback
  147. }
  148. });
  149. dojo.declare("dojox.widget.FileInputBlind", dojox.widget.FileInputAuto, {
  150. // summary: An extended version of dojox.widget.FileInputAuto
  151. // that does not display an input node, but rather only a button
  152. // and otherwise behaves just like FileInputAuto
  153. startup : function() {
  154. // summary: hide our fileInput input field
  155. this.inherited("startup", arguments);
  156. this._off = dojo.style(this.inputNode, "width");
  157. this.inputNode.style.display = "none";
  158. this._fixPosition();
  159. },
  160. _fixPosition : function() {
  161. // summary: in this case, set the button under where the
  162. // visible button is
  163. if (dojo.isIE) {
  164. dojo.style(this.fileInput, "width", "1px");
  165. // dojo.style(this.fileInput,"height",this.overlay.scrollHeight+"px")
  166. } else {
  167. dojo.style(this.fileInput, "left", "-" + (this._off)
  168. + "px");
  169. }
  170. },
  171. _onClick : function(e) {
  172. // summary: onclick, we need to reposition our newly created
  173. // input type="file"
  174. this.inherited("_onClick", arguments);
  175. this._fixPosition();
  176. }
  177. });
  178. }