source: trunk/web/addons/job_monarch/lib/extjs/examples/view/chooser.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

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