FilePicker.js 7.7 KB

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