FileInput.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.form.FileInput"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.form.FileInput"] = true;
  8. dojo.provide("dojox.form.FileInput");
  9. dojo.experimental("dojox.form.FileInput");
  10. dojo.require("dijit.form._FormWidget");
  11. dojo.require("dijit._Templated");
  12. dojo.declare("dojox.form.FileInput",
  13. dijit.form._FormWidget,
  14. {
  15. // summary: A styled input type="file"
  16. //
  17. // description: A input type="file" form widget, with a button for uploading to be styled via css,
  18. // a cancel button to clear selection, and FormWidget mixin to provide standard dijit.form.Form
  19. // support (FIXME: maybe not fully implemented)
  20. // label: String
  21. // the title text of the "Browse" button
  22. label: "Browse ...",
  23. // cancelText: String
  24. // the title of the "Cancel" button
  25. cancelText: "Cancel",
  26. // name: String
  27. // ugh, this should be pulled from this.domNode
  28. name: "uploadFile",
  29. templateString: dojo.cache("dojox.form", "resources/FileInput.html", "<div class=\"dijitFileInput\">\n\t<input id=\"${id}\" class=\"dijitFileInputReal\" type=\"file\" dojoAttachPoint=\"fileInput\" name=\"${name}\" />\n\t<div class=\"dijitFakeInput\">\n\t\t<input class=\"dijitFileInputVisible\" type=\"text\" dojoAttachPoint=\"focusNode, inputNode\" />\n\t\t<div class=\"dijitInline dijitFileInputText\" dojoAttachPoint=\"titleNode\">${label}</div>\n\t\t<div class=\"dijitInline dijitFileInputButton\" dojoAttachPoint=\"cancelNode\" \n\t\t\tdojoAttachEvent=\"onclick:reset\">${cancelText}</div>\n\t</div>\n</div>\n"),
  30. startup: function(){
  31. // summary: listen for changes on our real file input
  32. this._listener = this.connect(this.fileInput,"onchange","_matchValue");
  33. this._keyListener = this.connect(this.fileInput,"onkeyup","_matchValue");
  34. },
  35. //get rid of the this.connect in _FormWidget.postCreate to allow IE to show
  36. //the file picker dialog properly
  37. postCreate: function(){},
  38. _matchValue: function(){
  39. // summary: set the content of the upper input based on the semi-hidden file input
  40. this.inputNode.value = this.fileInput.value;
  41. if(this.inputNode.value){
  42. this.cancelNode.style.visibility = "visible";
  43. dojo.fadeIn({ node: this.cancelNode, duration:275 }).play();
  44. }
  45. },
  46. setLabel: function(/* String */label,/* String? */cssClass){
  47. // summary: method to allow use to change button label
  48. this.titleNode.innerHTML = label;
  49. },
  50. reset: function(/* Event */e){
  51. // summary: on click of cancel button, since we can't clear the input because of
  52. // security reasons, we destroy it, and add a new one in it's place.
  53. this.disconnect(this._listener);
  54. this.disconnect(this._keyListener);
  55. if(this.fileInput){
  56. this.domNode.removeChild(this.fileInput);
  57. }
  58. dojo.fadeOut({ node: this.cancelNode, duration:275 }).play();
  59. // should we use cloneNode()? can we?
  60. this.fileInput = document.createElement('input');
  61. // dojo.attr(this.fileInput,{
  62. // "type":"file", "id":this.id, "name": this.name
  63. //});
  64. this.fileInput.setAttribute("type","file");
  65. this.fileInput.setAttribute("id", this.id);
  66. this.fileInput.setAttribute("name", this.name);
  67. dojo.addClass(this.fileInput,"dijitFileInputReal");
  68. this.domNode.appendChild(this.fileInput);
  69. this._keyListener = this.connect(this.fileInput, "onkeyup", "_matchValue");
  70. this._listener = this.connect(this.fileInput, "onchange", "_matchValue");
  71. this.inputNode.value = "";
  72. }
  73. });
  74. }