3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
7 var ImageChooser = function(config){
11 ImageChooser.prototype = {
12 // cache data by image name for easy lookup
15 show : function(el, callback){
19 this.store = new Ext.data.JsonStore({
24 {name:'size', type: 'float'},
25 {name:'lastmod', type:'date', dateFormat:'timestamp'}
28 'load': {fn:function(){ this.view.select(0); }, scope:this, single:true}
33 var formatSize = function(data){
34 if(data.size < 1024) {
35 return data.size + " bytes";
37 return (Math.round(((data.size*10) / 1024))/10) + " KB";
41 var formatData = function(data){
42 data.shortName = data.name.ellipse(15);
43 data.sizeString = formatSize(data);
44 data.dateString = new Date(data.lastmod).format("m/d/Y g:i a");
45 this.lookup[data.name] = data;
49 this.view = new Ext.DataView({
50 tpl: this.thumbTemplate,
52 overClass:'x-view-over',
53 itemSelector: 'div.thumb-wrap',
54 emptyText : '<div style="padding:10px;">No images match the specified filter</div>',
57 'selectionchange': {fn:this.showDetails, scope:this, buffer:100},
58 'dblclick' : {fn:this.doCallback, scope:this},
59 'loadexception' : {fn:this.onLoadException, scope:this},
60 'beforeselect' : {fn:function(view){
61 return view.store.getRange().length > 0;
64 prepareData: formatData.createDelegate(this)
68 title: 'Choose an Image',
69 id: 'img-chooser-dlg',
77 id: 'img-chooser-view',
89 'render': {fn:function(){
90 Ext.getCmp('filter').getEl().on('keyup', function(){
92 }, this, {buffer:500});
101 triggerAction: 'all',
105 displayField: 'desc',
109 store: new Ext.data.ArrayStore({
110 fields: ['name', 'desc'],
111 data : [['name', 'Name'],['size', 'File Size'],['lastmod', 'Last Modified']]
114 'select': {fn:this.sortImages, scope:this}
118 id: 'img-detail-panel',
128 handler: this.doCallback,
132 handler: function(){ this.win.hide(); },
137 handler: function(){ this.win.hide(); },
141 Ext.apply(cfg, this.config);
142 this.win = new Ext.Window(cfg);
147 this.callback = callback;
148 this.animateTarget = el;
151 initTemplates : function(){
152 this.thumbTemplate = new Ext.XTemplate(
154 '<div class="thumb-wrap" id="{name}">',
155 '<div class="thumb"><img src="{url}" title="{name}"></div>',
156 '<span>{shortName}</span></div>',
159 this.thumbTemplate.compile();
161 this.detailsTemplate = new Ext.XTemplate(
162 '<div class="details">',
164 '<img src="{url}"><div class="details-info">',
165 '<b>Image Name:</b>',
166 '<span>{name}</span>',
168 '<span>{sizeString}</span>',
169 '<b>Last Modified:</b>',
170 '<span>{dateString}</span></div>',
174 this.detailsTemplate.compile();
177 showDetails : function(){
178 var selNode = this.view.getSelectedNodes();
179 var detailEl = Ext.getCmp('img-detail-panel').body;
180 if(selNode && selNode.length > 0){
181 selNode = selNode[0];
182 Ext.getCmp('ok-btn').enable();
183 var data = this.lookup[selNode.id];
185 this.detailsTemplate.overwrite(detailEl, data);
186 detailEl.slideIn('l', {stopFx:true,duration:.2});
188 Ext.getCmp('ok-btn').disable();
194 var filter = Ext.getCmp('filter');
195 this.view.store.filter('name', filter.getValue());
199 sortImages : function(){
200 var v = Ext.getCmp('sortSelect').getValue();
201 this.view.store.sort(v, v == 'name' ? 'asc' : 'desc');
206 if(this.win.rendered){
207 Ext.getCmp('filter').reset();
208 this.view.getEl().dom.scrollTop = 0;
210 this.view.store.clearFilter();
214 doCallback : function(){
215 var selNode = this.view.getSelectedNodes()[0];
216 var callback = this.callback;
217 var lookup = this.lookup;
218 this.win.hide(this.animateTarget, function(){
219 if(selNode && callback){
220 var data = lookup[selNode.id];
226 onLoadException : function(v,o){
227 this.view.getEl().update('<div style="padding:10px;">Error loading images.</div>');
231 String.prototype.ellipse = function(maxLength){
232 if(this.length > maxLength){
233 return this.substr(0, maxLength-3) + '...';