Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / chooser.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js">var ImageChooser = function(config){\r
9         this.config = config;\r
10 }\r
11 \r
12 ImageChooser.prototype = {\r
13     // cache data by image name for easy lookup\r
14     lookup : {},\r
15 \r
16         show : function(el, callback){\r
17                 if(!this.win){\r
18                         this.initTemplates();\r
19 \r
20                         this.store = new Ext.data.JsonStore({\r
21                             url: this.config.url,\r
22                             root: 'images',\r
23                             fields: [\r
24                                 'name', 'url',\r
25                                 {name:'size', type: 'float'},\r
26                                 {name:'lastmod', type:'date', dateFormat:'timestamp'}\r
27                             ],\r
28                             listeners: {\r
29                                 'load': {fn:function(){ this.view.select(0); }, scope:this, single:true}\r
30                             }\r
31                         });\r
32                         this.store.load();\r
33 \r
34                         var formatSize = function(data){\r
35                         if(data.size < 1024) {\r
36                             return data.size + " bytes";\r
37                         } else {\r
38                             return (Math.round(((data.size*10) / 1024))/10) + " KB";\r
39                         }\r
40                     };\r
41 \r
42                         var formatData = function(data){\r
43                         data.shortName = data.name.ellipse(15);\r
44                         data.sizeString = formatSize(data);\r
45                         data.dateString = new Date(data.lastmod).format("m/d/Y g:i a");\r
46                         this.lookup[data.name] = data;\r
47                         return data;\r
48                     };\r
49 \r
50                     this.view = new Ext.DataView({\r
51                                 tpl: this.thumbTemplate,\r
52                                 singleSelect: true,\r
53                                 overClass:'x-view-over',\r
54                                 itemSelector: 'div.thumb-wrap',\r
55                                 emptyText : '<div style="padding:10px;">No images match the specified filter</div>',\r
56                                 store: this.store,\r
57                                 listeners: {\r
58                                         'selectionchange': {fn:this.showDetails, scope:this, buffer:100},\r
59                                         'dblclick'       : {fn:this.doCallback, scope:this},\r
60                                         'loadexception'  : {fn:this.onLoadException, scope:this},\r
61                                         'beforeselect'   : {fn:function(view){\r
62                                         return view.store.getRange().length > 0;\r
63                                     }}\r
64                                 },\r
65                                 prepareData: formatData.createDelegate(this)\r
66                         });\r
67 \r
68                         var cfg = {\r
69                         title: 'Choose an Image',\r
70                         id: 'img-chooser-dlg',\r
71                         layout: 'border',\r
72                                 minWidth: 500,\r
73                                 minHeight: 300,\r
74                                 modal: true,\r
75                                 closeAction: 'hide',\r
76                                 border: false,\r
77                                 items:[{\r
78                                         id: 'img-chooser-view',\r
79                                         region: 'center',\r
80                                         autoScroll: true,\r
81                                         items: this.view,\r
82                     tbar:[{\r
83                         text: 'Filter:'\r
84                     },{\r
85                         xtype: 'textfield',\r
86                         id: 'filter',\r
87                         selectOnFocus: true,\r
88                         width: 100,\r
89                         listeners: {\r
90                                 'render': {fn:function(){\r
91                                                         Ext.getCmp('filter').getEl().on('keyup', function(){\r
92                                                                 this.filter();\r
93                                                         }, this, {buffer:500});\r
94                                 }, scope:this}\r
95                         }\r
96                     }, ' ', '-', {\r
97                         text: 'Sort By:'\r
98                     }, {\r
99                         id: 'sortSelect',\r
100                         xtype: 'combo',\r
101                                         typeAhead: true,\r
102                                         triggerAction: 'all',\r
103                                         width: 100,\r
104                                         editable: false,\r
105                                         mode: 'local',\r
106                                         displayField: 'desc',\r
107                                         valueField: 'name',\r
108                                         lazyInit: false,\r
109                                         value: 'name',\r
110                                         store: new Ext.data.ArrayStore({\r
111                                                 fields: ['name', 'desc'],\r
112                                                 data : [['name', 'Name'],['size', 'File Size'],['lastmod', 'Last Modified']]\r
113                                             }),\r
114                                             listeners: {\r
115                                                         'select': {fn:this.sortImages, scope:this}\r
116                                             }\r
117                                     }]\r
118                                 },{\r
119                                         id: 'img-detail-panel',\r
120                                         region: 'east',\r
121                                         split: true,\r
122                                         width: 150,\r
123                                         minWidth: 150,\r
124                                         maxWidth: 250\r
125                                 }],\r
126                                 buttons: [{\r
127                                         id: 'ok-btn',\r
128                                         text: 'OK',\r
129                                         handler: this.doCallback,\r
130                                         scope: this\r
131                                 },{\r
132                                         text: 'Cancel',\r
133                                         handler: function(){ this.win.hide(); },\r
134                                         scope: this\r
135                                 }],\r
136                                 keys: {\r
137                                         key: 27, // Esc key\r
138                                         handler: function(){ this.win.hide(); },\r
139                                         scope: this\r
140                                 }\r
141                         };\r
142                         Ext.apply(cfg, this.config);\r
143                     this.win = new Ext.Window(cfg);\r
144                 }\r
145 \r
146                 this.reset();\r
147             this.win.show(el);\r
148                 this.callback = callback;\r
149                 this.animateTarget = el;\r
150         },\r
151 \r
152         initTemplates : function(){\r
153                 this.thumbTemplate = new Ext.XTemplate(\r
154                         '<tpl for=".">',\r
155                                 '<div class="thumb-wrap" id="{name}">',\r
156                                 '<div class="thumb"><img src="{url}" title="{name}"></div>',\r
157                                 '<span>{shortName}</span></div>',\r
158                         '</tpl>'\r
159                 );\r
160                 this.thumbTemplate.compile();\r
161 \r
162                 this.detailsTemplate = new Ext.XTemplate(\r
163                         '<div class="details">',\r
164                                 '<tpl for=".">',\r
165                                         '<img src="{url}"><div class="details-info">',\r
166                                         '<b>Image Name:</b>',\r
167                                         '<span>{name}</span>',\r
168                                         '<b>Size:</b>',\r
169                                         '<span>{sizeString}</span>',\r
170                                         '<b>Last Modified:</b>',\r
171                                         '<span>{dateString}</span></div>',\r
172                                 '</tpl>',\r
173                         '</div>'\r
174                 );\r
175                 this.detailsTemplate.compile();\r
176         },\r
177 \r
178         showDetails : function(){\r
179             var selNode = this.view.getSelectedNodes();\r
180             var detailEl = Ext.getCmp('img-detail-panel').body;\r
181                 if(selNode && selNode.length > 0){\r
182                         selNode = selNode[0];\r
183                         Ext.getCmp('ok-btn').enable();\r
184                     var data = this.lookup[selNode.id];\r
185             detailEl.hide();\r
186             this.detailsTemplate.overwrite(detailEl, data);\r
187             detailEl.slideIn('l', {stopFx:true,duration:.2});\r
188                 }else{\r
189                     Ext.getCmp('ok-btn').disable();\r
190                     detailEl.update('');\r
191                 }\r
192         },\r
193 \r
194         filter : function(){\r
195                 var filter = Ext.getCmp('filter');\r
196                 this.view.store.filter('name', filter.getValue());\r
197                 this.view.select(0);\r
198         },\r
199 \r
200         sortImages : function(){\r
201                 var v = Ext.getCmp('sortSelect').getValue();\r
202         this.view.store.sort(v, v == 'name' ? 'asc' : 'desc');\r
203         this.view.select(0);\r
204     },\r
205 \r
206         reset : function(){\r
207                 if(this.win.rendered){\r
208                         Ext.getCmp('filter').reset();\r
209                         this.view.getEl().dom.scrollTop = 0;\r
210                 }\r
211             this.view.store.clearFilter();\r
212                 this.view.select(0);\r
213         },\r
214 \r
215         doCallback : function(){\r
216         var selNode = this.view.getSelectedNodes()[0];\r
217                 var callback = this.callback;\r
218                 var lookup = this.lookup;\r
219                 this.win.hide(this.animateTarget, function(){\r
220             if(selNode && callback){\r
221                                 var data = lookup[selNode.id];\r
222                                 callback(data);\r
223                         }\r
224                 });\r
225     },\r
226 \r
227         onLoadException : function(v,o){\r
228             this.view.getEl().update('<div style="padding:10px;">Error loading images.</div>');\r
229         }\r
230 };\r
231 \r
232 String.prototype.ellipse = function(maxLength){\r
233     if(this.length > maxLength){\r
234         return this.substr(0, maxLength-3) + '...';\r
235     }\r
236     return this;\r
237 };\r
238 </pre>    \r
239 </body>\r
240 </html>