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
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js">var ImageChooser = function(config){
\r
9 this.config = config;
\r
12 ImageChooser.prototype = {
\r
13 // cache data by image name for easy lookup
\r
16 show : function(el, callback){
\r
18 this.initTemplates();
\r
20 this.store = new Ext.data.JsonStore({
\r
21 url: this.config.url,
\r
25 {name:'size', type: 'float'},
\r
26 {name:'lastmod', type:'date', dateFormat:'timestamp'}
\r
29 'load': {fn:function(){ this.view.select(0); }, scope:this, single:true}
\r
34 var formatSize = function(data){
\r
35 if(data.size < 1024) {
\r
36 return data.size + " bytes";
\r
38 return (Math.round(((data.size*10) / 1024))/10) + " KB";
\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
50 this.view = new Ext.DataView({
\r
51 tpl: this.thumbTemplate,
\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
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
65 prepareData: formatData.createDelegate(this)
\r
69 title: 'Choose an Image',
\r
70 id: 'img-chooser-dlg',
\r
75 closeAction: 'hide',
\r
78 id: 'img-chooser-view',
\r
87 selectOnFocus: true,
\r
90 'render': {fn:function(){
\r
91 Ext.getCmp('filter').getEl().on('keyup', function(){
\r
93 }, this, {buffer:500});
\r
102 triggerAction: 'all',
\r
106 displayField: 'desc',
\r
107 valueField: '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
115 'select': {fn:this.sortImages, scope:this}
\r
119 id: 'img-detail-panel',
\r
129 handler: this.doCallback,
\r
133 handler: function(){ this.win.hide(); },
\r
137 key: 27, // Esc key
\r
138 handler: function(){ this.win.hide(); },
\r
142 Ext.apply(cfg, this.config);
\r
143 this.win = new Ext.Window(cfg);
\r
148 this.callback = callback;
\r
149 this.animateTarget = el;
\r
152 initTemplates : function(){
\r
153 this.thumbTemplate = new Ext.XTemplate(
\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
160 this.thumbTemplate.compile();
\r
162 this.detailsTemplate = new Ext.XTemplate(
\r
163 '<div class="details">',
\r
165 '<img src="{url}"><div class="details-info">',
\r
166 '<b>Image Name:</b>',
\r
167 '<span>{name}</span>',
\r
169 '<span>{sizeString}</span>',
\r
170 '<b>Last Modified:</b>',
\r
171 '<span>{dateString}</span></div>',
\r
175 this.detailsTemplate.compile();
\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
186 this.detailsTemplate.overwrite(detailEl, data);
\r
187 detailEl.slideIn('l', {stopFx:true,duration:.2});
\r
189 Ext.getCmp('ok-btn').disable();
\r
190 detailEl.update('');
\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
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
206 reset : function(){
\r
207 if(this.win.rendered){
\r
208 Ext.getCmp('filter').reset();
\r
209 this.view.getEl().dom.scrollTop = 0;
\r
211 this.view.store.clearFilter();
\r
212 this.view.select(0);
\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
227 onLoadException : function(v,o){
\r
228 this.view.getEl().update('<div style="padding:10px;">Error loading images.</div>');
\r
232 String.prototype.ellipse = function(maxLength){
\r
233 if(this.length > maxLength){
\r
234 return this.substr(0, maxLength-3) + '...';
\r