source: Dev/trunk/src/client/dojox/widget/FilePicker.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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