You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
6.7 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. var ImageChooser = function(config){
  16. this.config = config;
  17. }
  18. ImageChooser.prototype = {
  19. // cache data by image name for easy lookup
  20. lookup : {},
  21. show : function(el, callback){
  22. if(!this.win){
  23. this.initTemplates();
  24. this.store = new Ext.data.JsonStore({
  25. url: this.config.url,
  26. root: 'images',
  27. fields: [
  28. 'name', 'url',
  29. {name:'size', type: 'float'},
  30. {name:'lastmod', type:'date', dateFormat:'timestamp'}
  31. ],
  32. listeners: {
  33. 'load': {fn:function(){ this.view.select(0); }, scope:this, single:true}
  34. }
  35. });
  36. this.store.load();
  37. var formatSize = function(data){
  38. if(data.size < 1024) {
  39. return data.size + " bytes";
  40. } else {
  41. return (Math.round(((data.size*10) / 1024))/10) + " KB";
  42. }
  43. };
  44. var formatData = function(data){
  45. data.shortName = data.name.ellipse(15);
  46. data.sizeString = formatSize(data);
  47. data.dateString = new Date(data.lastmod).format("m/d/Y g:i a");
  48. this.lookup[data.name] = data;
  49. return data;
  50. };
  51. this.view = new Ext.DataView({
  52. tpl: this.thumbTemplate,
  53. singleSelect: true,
  54. overClass:'x-view-over',
  55. itemSelector: 'div.thumb-wrap',
  56. emptyText : '<div style="padding:10px;">No images match the specified filter</div>',
  57. store: this.store,
  58. listeners: {
  59. 'selectionchange': {fn:this.showDetails, scope:this, buffer:100},
  60. 'dblclick' : {fn:this.doCallback, scope:this},
  61. 'loadexception' : {fn:this.onLoadException, scope:this},
  62. 'beforeselect' : {fn:function(view){
  63. return view.store.getRange().length > 0;
  64. }}
  65. },
  66. prepareData: formatData.createDelegate(this)
  67. });
  68. var cfg = {
  69. title: 'Choose an Image',
  70. id: 'img-chooser-dlg',
  71. layout: 'border',
  72. minWidth: 500,
  73. minHeight: 300,
  74. modal: true,
  75. closeAction: 'hide',
  76. border: false,
  77. items:[{
  78. id: 'img-chooser-view',
  79. region: 'center',
  80. autoScroll: true,
  81. items: this.view,
  82. tbar:[{
  83. text: 'Filter:'
  84. },{
  85. xtype: 'textfield',
  86. id: 'filter',
  87. selectOnFocus: true,
  88. width: 100,
  89. listeners: {
  90. 'render': {fn:function(){
  91. Ext.getCmp('filter').getEl().on('keyup', function(){
  92. this.filter();
  93. }, this, {buffer:500});
  94. }, scope:this}
  95. }
  96. }, ' ', '-', {
  97. text: 'Sort By:'
  98. }, {
  99. id: 'sortSelect',
  100. xtype: 'combo',
  101. typeAhead: true,
  102. triggerAction: 'all',
  103. width: 100,
  104. editable: false,
  105. mode: 'local',
  106. displayField: 'desc',
  107. valueField: 'name',
  108. lazyInit: false,
  109. value: 'name',
  110. store: new Ext.data.ArrayStore({
  111. fields: ['name', 'desc'],
  112. data : [['name', 'Name'],['size', 'File Size'],['lastmod', 'Last Modified']]
  113. }),
  114. listeners: {
  115. 'select': {fn:this.sortImages, scope:this}
  116. }
  117. }]
  118. },{
  119. id: 'img-detail-panel',
  120. region: 'east',
  121. split: true,
  122. width: 150,
  123. minWidth: 150,
  124. maxWidth: 250
  125. }],
  126. buttons: [{
  127. id: 'ok-btn',
  128. text: 'OK',
  129. handler: this.doCallback,
  130. scope: this
  131. },{
  132. text: 'Cancel',
  133. handler: function(){ this.win.hide(); },
  134. scope: this
  135. }],
  136. keys: {
  137. key: 27, // Esc key
  138. handler: function(){ this.win.hide(); },
  139. scope: this
  140. }
  141. };
  142. Ext.apply(cfg, this.config);
  143. this.win = new Ext.Window(cfg);
  144. }
  145. this.reset();
  146. this.win.show(el);
  147. this.callback = callback;
  148. this.animateTarget = el;
  149. },
  150. initTemplates : function(){
  151. this.thumbTemplate = new Ext.XTemplate(
  152. '<tpl for=".">',
  153. '<div class="thumb-wrap" id="{name}">',
  154. '<div class="thumb"><img src="{url}" title="{name}"></div>',
  155. '<span>{shortName}</span></div>',
  156. '</tpl>'
  157. );
  158. this.thumbTemplate.compile();
  159. this.detailsTemplate = new Ext.XTemplate(
  160. '<div class="details">',
  161. '<tpl for=".">',
  162. '<img src="{url}"><div class="details-info">',
  163. '<b>Image Name:</b>',
  164. '<span>{name}</span>',
  165. '<b>Size:</b>',
  166. '<span>{sizeString}</span>',
  167. '<b>Last Modified:</b>',
  168. '<span>{dateString}</span></div>',
  169. '</tpl>',
  170. '</div>'
  171. );
  172. this.detailsTemplate.compile();
  173. },
  174. showDetails : function(){
  175. var selNode = this.view.getSelectedNodes();
  176. var detailEl = Ext.getCmp('img-detail-panel').body;
  177. if(selNode && selNode.length > 0){
  178. selNode = selNode[0];
  179. Ext.getCmp('ok-btn').enable();
  180. var data = this.lookup[selNode.id];
  181. detailEl.hide();
  182. this.detailsTemplate.overwrite(detailEl, data);
  183. detailEl.slideIn('l', {stopFx:true,duration:.2});
  184. }else{
  185. Ext.getCmp('ok-btn').disable();
  186. detailEl.update('');
  187. }
  188. },
  189. filter : function(){
  190. var filter = Ext.getCmp('filter');
  191. this.view.store.filter('name', filter.getValue());
  192. this.view.select(0);
  193. },
  194. sortImages : function(){
  195. var v = Ext.getCmp('sortSelect').getValue();
  196. this.view.store.sort(v, v == 'name' ? 'asc' : 'desc');
  197. this.view.select(0);
  198. },
  199. reset : function(){
  200. if(this.win.rendered){
  201. Ext.getCmp('filter').reset();
  202. this.view.getEl().dom.scrollTop = 0;
  203. }
  204. this.view.store.clearFilter();
  205. this.view.select(0);
  206. },
  207. doCallback : function(){
  208. var selNode = this.view.getSelectedNodes()[0];
  209. var callback = this.callback;
  210. var lookup = this.lookup;
  211. this.win.hide(this.animateTarget, function(){
  212. if(selNode && callback){
  213. var data = lookup[selNode.id];
  214. callback(data);
  215. }
  216. });
  217. },
  218. onLoadException : function(v,o){
  219. this.view.getEl().update('<div style="padding:10px;">Error loading images.</div>');
  220. }
  221. };
  222. String.prototype.ellipse = function(maxLength){
  223. if(this.length > maxLength){
  224. return this.substr(0, maxLength-3) + '...';
  225. }
  226. return this;
  227. };