FilePicker.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // wrapped by build app
  2. define("dojox/widget/FilePicker", ["dijit","dojo","dojox","dojo/i18n!dojox/widget/nls/FilePicker","dojo/require!dojox/widget/RollingList,dojo/i18n"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.widget.FilePicker");
  4. dojo.require("dojox.widget.RollingList");
  5. dojo.require("dojo.i18n");
  6. dojo.requireLocalization("dojox.widget", "FilePicker");
  7. dojo.declare("dojox.widget._FileInfoPane",
  8. [dojox.widget._RollingListPane], {
  9. // summary: a pane to display the information for the currently-selected
  10. // file
  11. // templateString: string
  12. // delete our template string
  13. templateString: "",
  14. // templateString: String
  15. // The template to be used to construct the widget.
  16. templateString: dojo.cache("dojox.widget", "FilePicker/_FileInfoPane.html", "<div class=\"dojoxFileInfoPane\">\n\t<table>\n\t\t<tbody>\n\t\t\t<tr>\n\t\t\t\t<td class=\"dojoxFileInfoLabel dojoxFileInfoNameLabel\">${_messages.name}</td>\n\t\t\t\t<td class=\"dojoxFileInfoName\" dojoAttachPoint=\"nameNode\"></td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td class=\"dojoxFileInfoLabel dojoxFileInfoPathLabel\">${_messages.path}</td>\n\t\t\t\t<td class=\"dojoxFileInfoPath\" dojoAttachPoint=\"pathNode\"></td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td class=\"dojoxFileInfoLabel dojoxFileInfoSizeLabel\">${_messages.size}</td>\n\t\t\t\t<td class=\"dojoxFileInfoSize\" dojoAttachPoint=\"sizeNode\"></td>\n\t\t\t</tr>\n\t\t</tbody>\n\t</table>\n\t<div dojoAttachPoint=\"containerNode\" style=\"display:none;\"></div>\n</div>"),
  17. postMixInProperties: function(){
  18. this._messages = dojo.i18n.getLocalization("dojox.widget", "FilePicker", this.lang);
  19. this.inherited(arguments);
  20. },
  21. onItems: function(){
  22. // summary:
  23. // called after a fetch or load - at this point, this.items should be
  24. // set and loaded.
  25. var store = this.store, item = this.items[0];
  26. if(!item){
  27. this._onError("Load", new Error("No item defined"));
  28. }else{
  29. this.nameNode.innerHTML = store.getLabel(item);
  30. this.pathNode.innerHTML = store.getIdentity(item);
  31. this.sizeNode.innerHTML = store.getValue(item, "size");
  32. this.parentWidget.scrollIntoView(this);
  33. this.inherited(arguments);
  34. }
  35. }
  36. });
  37. dojo.declare("dojox.widget.FilePicker", dojox.widget.RollingList, {
  38. // summary: a specialized version of RollingList that handles file information
  39. // in a store
  40. className: "dojoxFilePicker",
  41. // pathSeparator: string
  42. // Our file separator - it will be guessed if not set
  43. pathSeparator: "",
  44. // topDir: string
  45. // The top directory string - it will be guessed if not set
  46. topDir: "",
  47. // parentAttr: string
  48. // the attribute to read for finding our parent directory
  49. parentAttr: "parentDir",
  50. // pathAttr: string
  51. // the attribute to read for getting the full path of our file
  52. pathAttr: "path",
  53. // preloadItems: boolean or int
  54. // Set this to a sane number - since we expect to mostly be using the
  55. // dojox.data.FileStore - which doesn't like loading lots of items
  56. // all at once.
  57. preloadItems: 50,
  58. // selectDirectories: boolean
  59. // whether or not we allow selection of directories - that is, whether or
  60. // our value can be set to a directory.
  61. selectDirectories: true,
  62. // selectFiles: boolean
  63. // whether or not we allow selection of files - that is, we will disable
  64. // the file entries.
  65. selectFiles: true,
  66. _itemsMatch: function(/*item*/ item1, /*item*/ item2){
  67. // Summary: returns whether or not the two items match - checks ID if
  68. // they aren't the exact same object - ignoring trailing slashes
  69. if(!item1 && !item2){
  70. return true;
  71. }else if(!item1 || !item2){
  72. return false;
  73. }else if(item1 == item2){
  74. return true;
  75. }else if (this._isIdentity){
  76. var iArr = [ this.store.getIdentity(item1), this.store.getIdentity(item2) ];
  77. dojo.forEach(iArr, function(i, idx){
  78. if(i.lastIndexOf(this.pathSeparator) == (i.length - 1)){
  79. iArr[idx] = i.substring(0, i.length - 1);
  80. }else{
  81. }
  82. }, this);
  83. return (iArr[0] == iArr[1]);
  84. }
  85. return false;
  86. },
  87. startup: function(){
  88. if(this._started){ return; }
  89. this.inherited(arguments);
  90. // Figure out our file separator if we don't have it yet
  91. var conn, child = this.getChildren()[0];
  92. var setSeparator = dojo.hitch(this, function(){
  93. if(conn){
  94. this.disconnect(conn);
  95. }
  96. delete conn;
  97. var item = child.items[0];
  98. if(item){
  99. var store = this.store;
  100. var parent = store.getValue(item, this.parentAttr);
  101. var path = store.getValue(item, this.pathAttr);
  102. this.pathSeparator = this.pathSeparator || store.pathSeparator;
  103. if(!this.pathSeparator){
  104. this.pathSeparator = path.substring(parent.length, parent.length + 1);
  105. }
  106. if(!this.topDir){
  107. this.topDir = parent;
  108. if(this.topDir.lastIndexOf(this.pathSeparator) != (this.topDir.length - 1)){
  109. this.topDir += this.pathSeparator;
  110. }
  111. }
  112. }
  113. });
  114. if(!this.pathSeparator || !this.topDir){
  115. if(!child.items){
  116. conn = this.connect(child, "onItems", setSeparator);
  117. }else{
  118. setSeparator();
  119. }
  120. }
  121. },
  122. getChildItems: function(item){
  123. var ret = this.inherited(arguments);
  124. if(!ret && this.store.getValue(item, "directory")){
  125. // It's an empty directory - so pass through an empty array
  126. ret = [];
  127. }
  128. return ret;
  129. },
  130. getMenuItemForItem: function(/*item*/ item, /* dijit._Contained */ parentPane, /* item[]? */ children){
  131. var menuOptions = {iconClass: "dojoxDirectoryItemIcon"};
  132. if(!this.store.getValue(item, "directory")){
  133. menuOptions.iconClass = "dojoxFileItemIcon";
  134. var l = this.store.getLabel(item), idx = l.lastIndexOf(".");
  135. if(idx >= 0){
  136. menuOptions.iconClass += " dojoxFileItemIcon_" + l.substring(idx + 1);
  137. }
  138. if(!this.selectFiles){
  139. menuOptions.disabled = true;
  140. }
  141. }
  142. var ret = new dijit.MenuItem(menuOptions);
  143. return ret;
  144. },
  145. getPaneForItem: function(/*item*/ item, /* dijit._Contained */ parentPane, /* item[]? */ children){
  146. var ret = null;
  147. if(!item || (this.store.isItem(item) && this.store.getValue(item, "directory"))){
  148. ret = new dojox.widget._RollingListGroupPane({});
  149. }else if(this.store.isItem(item) && !this.store.getValue(item, "directory")){
  150. ret = new dojox.widget._FileInfoPane({});
  151. }
  152. return ret;
  153. },
  154. _setPathValueAttr: function(/*string*/ path, /*boolean?*/ resetLastExec, /*function?*/ onSet){
  155. // Summary: sets the value of this widget based off the given path
  156. if(!path){
  157. this.set("value", null);
  158. return;
  159. }
  160. if(path.lastIndexOf(this.pathSeparator) == (path.length - 1)){
  161. path = path.substring(0, path.length - 1);
  162. }
  163. this.store.fetchItemByIdentity({identity: path,
  164. onItem: function(v){
  165. if(resetLastExec){
  166. this._lastExecutedValue = v;
  167. }
  168. this.set("value", v);
  169. if(onSet){ onSet(); }
  170. },
  171. scope: this});
  172. },
  173. _getPathValueAttr: function(/*item?*/val){
  174. // summary: returns the path value of the given value (or current value
  175. // if not passed a value)
  176. if(!val){
  177. val = this.value;
  178. }
  179. if(val && this.store.isItem(val)){
  180. return this.store.getValue(val, this.pathAttr);
  181. }else{
  182. return "";
  183. }
  184. },
  185. _setValue: function(/* item */ value){
  186. // summary: internally sets the value and fires onchange
  187. delete this._setInProgress;
  188. var store = this.store;
  189. if(value && store.isItem(value)){
  190. var isDirectory = this.store.getValue(value, "directory");
  191. if((isDirectory && !this.selectDirectories) ||
  192. (!isDirectory && !this.selectFiles)){ return; }
  193. }else{
  194. value = null;
  195. }
  196. if(!this._itemsMatch(this.value, value)){
  197. this.value = value;
  198. this._onChange(value);
  199. }
  200. }
  201. });
  202. });