source: Dev/branches/rest-dojo-ui/client/dojox/widget/FilePicker.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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