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.

468 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. /**
  8. * @class Ext.grid.PropertyRecord
  9. * A specific {@link Ext.data.Record} type that represents a name/value pair and is made to work with the
  10. * {@link Ext.grid.PropertyGrid}. Typically, PropertyRecords do not need to be created directly as they can be
  11. * created implicitly by simply using the appropriate data configs either via the {@link Ext.grid.PropertyGrid#source}
  12. * config property or by calling {@link Ext.grid.PropertyGrid#setSource}. However, if the need arises, these records
  13. * can also be created explicitly as shwon below. Example usage:
  14. * <pre><code>
  15. var rec = new Ext.grid.PropertyRecord({
  16. name: 'Birthday',
  17. value: new Date(Date.parse('05/26/1972'))
  18. });
  19. // Add record to an already populated grid
  20. grid.store.addSorted(rec);
  21. </code></pre>
  22. * @constructor
  23. * @param {Object} config A data object in the format: {name: [name], value: [value]}. The specified value's type
  24. * will be read automatically by the grid to determine the type of editor to use when displaying it.
  25. */
  26. Ext.grid.PropertyRecord = Ext.data.Record.create([
  27. {name:'name',type:'string'}, 'value'
  28. ]);
  29. /**
  30. * @class Ext.grid.PropertyStore
  31. * @extends Ext.util.Observable
  32. * A custom wrapper for the {@link Ext.grid.PropertyGrid}'s {@link Ext.data.Store}. This class handles the mapping
  33. * between the custom data source objects supported by the grid and the {@link Ext.grid.PropertyRecord} format
  34. * required for compatibility with the underlying store. Generally this class should not need to be used directly --
  35. * the grid's data should be accessed from the underlying store via the {@link #store} property.
  36. * @constructor
  37. * @param {Ext.grid.Grid} grid The grid this store will be bound to
  38. * @param {Object} source The source data config object
  39. */
  40. Ext.grid.PropertyStore = Ext.extend(Ext.util.Observable, {
  41. constructor : function(grid, source){
  42. this.grid = grid;
  43. this.store = new Ext.data.Store({
  44. recordType : Ext.grid.PropertyRecord
  45. });
  46. this.store.on('update', this.onUpdate, this);
  47. if(source){
  48. this.setSource(source);
  49. }
  50. Ext.grid.PropertyStore.superclass.constructor.call(this);
  51. },
  52. // protected - should only be called by the grid. Use grid.setSource instead.
  53. setSource : function(o){
  54. this.source = o;
  55. this.store.removeAll();
  56. var data = [];
  57. for(var k in o){
  58. if(this.isEditableValue(o[k])){
  59. data.push(new Ext.grid.PropertyRecord({name: k, value: o[k]}, k));
  60. }
  61. }
  62. this.store.loadRecords({records: data}, {}, true);
  63. },
  64. // private
  65. onUpdate : function(ds, record, type){
  66. if(type == Ext.data.Record.EDIT){
  67. var v = record.data.value;
  68. var oldValue = record.modified.value;
  69. if(this.grid.fireEvent('beforepropertychange', this.source, record.id, v, oldValue) !== false){
  70. this.source[record.id] = v;
  71. record.commit();
  72. this.grid.fireEvent('propertychange', this.source, record.id, v, oldValue);
  73. }else{
  74. record.reject();
  75. }
  76. }
  77. },
  78. // private
  79. getProperty : function(row){
  80. return this.store.getAt(row);
  81. },
  82. // private
  83. isEditableValue: function(val){
  84. return Ext.isPrimitive(val) || Ext.isDate(val);
  85. },
  86. // private
  87. setValue : function(prop, value, create){
  88. var r = this.getRec(prop);
  89. if(r){
  90. r.set('value', value);
  91. this.source[prop] = value;
  92. }else if(create){
  93. // only create if specified.
  94. this.source[prop] = value;
  95. r = new Ext.grid.PropertyRecord({name: prop, value: value}, prop);
  96. this.store.add(r);
  97. }
  98. },
  99. // private
  100. remove : function(prop){
  101. var r = this.getRec(prop);
  102. if(r){
  103. this.store.remove(r);
  104. delete this.source[prop];
  105. }
  106. },
  107. // private
  108. getRec : function(prop){
  109. return this.store.getById(prop);
  110. },
  111. // protected - should only be called by the grid. Use grid.getSource instead.
  112. getSource : function(){
  113. return this.source;
  114. }
  115. });
  116. /**
  117. * @class Ext.grid.PropertyColumnModel
  118. * @extends Ext.grid.ColumnModel
  119. * A custom column model for the {@link Ext.grid.PropertyGrid}. Generally it should not need to be used directly.
  120. * @constructor
  121. * @param {Ext.grid.Grid} grid The grid this store will be bound to
  122. * @param {Object} source The source data config object
  123. */
  124. Ext.grid.PropertyColumnModel = Ext.extend(Ext.grid.ColumnModel, {
  125. // private - strings used for locale support
  126. nameText : 'Name',
  127. valueText : 'Value',
  128. dateFormat : 'm/j/Y',
  129. trueText: 'true',
  130. falseText: 'false',
  131. constructor : function(grid, store){
  132. var g = Ext.grid,
  133. f = Ext.form;
  134. this.grid = grid;
  135. g.PropertyColumnModel.superclass.constructor.call(this, [
  136. {header: this.nameText, width:50, sortable: true, dataIndex:'name', id: 'name', menuDisabled:true},
  137. {header: this.valueText, width:50, resizable:false, dataIndex: 'value', id: 'value', menuDisabled:true}
  138. ]);
  139. this.store = store;
  140. var bfield = new f.Field({
  141. autoCreate: {tag: 'select', children: [
  142. {tag: 'option', value: 'true', html: this.trueText},
  143. {tag: 'option', value: 'false', html: this.falseText}
  144. ]},
  145. getValue : function(){
  146. return this.el.dom.value == 'true';
  147. }
  148. });
  149. this.editors = {
  150. 'date' : new g.GridEditor(new f.DateField({selectOnFocus:true})),
  151. 'string' : new g.GridEditor(new f.TextField({selectOnFocus:true})),
  152. 'number' : new g.GridEditor(new f.NumberField({selectOnFocus:true, style:'text-align:left;'})),
  153. 'boolean' : new g.GridEditor(bfield, {
  154. autoSize: 'both'
  155. })
  156. };
  157. this.renderCellDelegate = this.renderCell.createDelegate(this);
  158. this.renderPropDelegate = this.renderProp.createDelegate(this);
  159. },
  160. // private
  161. renderDate : function(dateVal){
  162. return dateVal.dateFormat(this.dateFormat);
  163. },
  164. // private
  165. renderBool : function(bVal){
  166. return this[bVal ? 'trueText' : 'falseText'];
  167. },
  168. // private
  169. isCellEditable : function(colIndex, rowIndex){
  170. return colIndex == 1;
  171. },
  172. // private
  173. getRenderer : function(col){
  174. return col == 1 ?
  175. this.renderCellDelegate : this.renderPropDelegate;
  176. },
  177. // private
  178. renderProp : function(v){
  179. return this.getPropertyName(v);
  180. },
  181. // private
  182. renderCell : function(val, meta, rec){
  183. var renderer = this.grid.customRenderers[rec.get('name')];
  184. if(renderer){
  185. return renderer.apply(this, arguments);
  186. }
  187. var rv = val;
  188. if(Ext.isDate(val)){
  189. rv = this.renderDate(val);
  190. }else if(typeof val == 'boolean'){
  191. rv = this.renderBool(val);
  192. }
  193. return Ext.util.Format.htmlEncode(rv);
  194. },
  195. // private
  196. getPropertyName : function(name){
  197. var pn = this.grid.propertyNames;
  198. return pn && pn[name] ? pn[name] : name;
  199. },
  200. // private
  201. getCellEditor : function(colIndex, rowIndex){
  202. var p = this.store.getProperty(rowIndex),
  203. n = p.data.name,
  204. val = p.data.value;
  205. if(this.grid.customEditors[n]){
  206. return this.grid.customEditors[n];
  207. }
  208. if(Ext.isDate(val)){
  209. return this.editors.date;
  210. }else if(typeof val == 'number'){
  211. return this.editors.number;
  212. }else if(typeof val == 'boolean'){
  213. return this.editors['boolean'];
  214. }else{
  215. return this.editors.string;
  216. }
  217. },
  218. // inherit docs
  219. destroy : function(){
  220. Ext.grid.PropertyColumnModel.superclass.destroy.call(this);
  221. this.destroyEditors(this.editors);
  222. this.destroyEditors(this.grid.customEditors);
  223. },
  224. destroyEditors: function(editors){
  225. for(var ed in editors){
  226. Ext.destroy(editors[ed]);
  227. }
  228. }
  229. });
  230. /**
  231. * @class Ext.grid.PropertyGrid
  232. * @extends Ext.grid.EditorGridPanel
  233. * A specialized grid implementation intended to mimic the traditional property grid as typically seen in
  234. * development IDEs. Each row in the grid represents a property of some object, and the data is stored
  235. * as a set of name/value pairs in {@link Ext.grid.PropertyRecord}s. Example usage:
  236. * <pre><code>
  237. var grid = new Ext.grid.PropertyGrid({
  238. title: 'Properties Grid',
  239. autoHeight: true,
  240. width: 300,
  241. renderTo: 'grid-ct',
  242. source: {
  243. "(name)": "My Object",
  244. "Created": new Date(Date.parse('10/15/2006')),
  245. "Available": false,
  246. "Version": .01,
  247. "Description": "A test object"
  248. }
  249. });
  250. </code></pre>
  251. * @constructor
  252. * @param {Object} config The grid config object
  253. */
  254. Ext.grid.PropertyGrid = Ext.extend(Ext.grid.EditorGridPanel, {
  255. /**
  256. * @cfg {Object} propertyNames An object containing property name/display name pairs.
  257. * If specified, the display name will be shown in the name column instead of the property name.
  258. */
  259. /**
  260. * @cfg {Object} source A data object to use as the data source of the grid (see {@link #setSource} for details).
  261. */
  262. /**
  263. * @cfg {Object} customEditors An object containing name/value pairs of custom editor type definitions that allow
  264. * the grid to support additional types of editable fields. By default, the grid supports strongly-typed editing
  265. * of strings, dates, numbers and booleans using built-in form editors, but any custom type can be supported and
  266. * associated with a custom input control by specifying a custom editor. The name of the editor
  267. * type should correspond with the name of the property that will use the editor. Example usage:
  268. * <pre><code>
  269. var grid = new Ext.grid.PropertyGrid({
  270. ...
  271. customEditors: {
  272. 'Start Time': new Ext.grid.GridEditor(new Ext.form.TimeField({selectOnFocus:true}))
  273. },
  274. source: {
  275. 'Start Time': '10:00 AM'
  276. }
  277. });
  278. </code></pre>
  279. */
  280. /**
  281. * @cfg {Object} source A data object to use as the data source of the grid (see {@link #setSource} for details).
  282. */
  283. /**
  284. * @cfg {Object} customRenderers An object containing name/value pairs of custom renderer type definitions that allow
  285. * the grid to support custom rendering of fields. By default, the grid supports strongly-typed rendering
  286. * of strings, dates, numbers and booleans using built-in form editors, but any custom type can be supported and
  287. * associated with the type of the value. The name of the renderer type should correspond with the name of the property
  288. * that it will render. Example usage:
  289. * <pre><code>
  290. var grid = new Ext.grid.PropertyGrid({
  291. ...
  292. customRenderers: {
  293. Available: function(v){
  294. if(v){
  295. return '<span style="color: green;">Yes</span>';
  296. }else{
  297. return '<span style="color: red;">No</span>';
  298. }
  299. }
  300. },
  301. source: {
  302. Available: true
  303. }
  304. });
  305. </code></pre>
  306. */
  307. // private config overrides
  308. enableColumnMove:false,
  309. stripeRows:false,
  310. trackMouseOver: false,
  311. clicksToEdit:1,
  312. enableHdMenu : false,
  313. viewConfig : {
  314. forceFit:true
  315. },
  316. // private
  317. initComponent : function(){
  318. this.customRenderers = this.customRenderers || {};
  319. this.customEditors = this.customEditors || {};
  320. this.lastEditRow = null;
  321. var store = new Ext.grid.PropertyStore(this);
  322. this.propStore = store;
  323. var cm = new Ext.grid.PropertyColumnModel(this, store);
  324. store.store.sort('name', 'ASC');
  325. this.addEvents(
  326. /**
  327. * @event beforepropertychange
  328. * Fires before a property value changes. Handlers can return false to cancel the property change
  329. * (this will internally call {@link Ext.data.Record#reject} on the property's record).
  330. * @param {Object} source The source data object for the grid (corresponds to the same object passed in
  331. * as the {@link #source} config property).
  332. * @param {String} recordId The record's id in the data store
  333. * @param {Mixed} value The current edited property value
  334. * @param {Mixed} oldValue The original property value prior to editing
  335. */
  336. 'beforepropertychange',
  337. /**
  338. * @event propertychange
  339. * Fires after a property value has changed.
  340. * @param {Object} source The source data object for the grid (corresponds to the same object passed in
  341. * as the {@link #source} config property).
  342. * @param {String} recordId The record's id in the data store
  343. * @param {Mixed} value The current edited property value
  344. * @param {Mixed} oldValue The original property value prior to editing
  345. */
  346. 'propertychange'
  347. );
  348. this.cm = cm;
  349. this.ds = store.store;
  350. Ext.grid.PropertyGrid.superclass.initComponent.call(this);
  351. this.mon(this.selModel, 'beforecellselect', function(sm, rowIndex, colIndex){
  352. if(colIndex === 0){
  353. this.startEditing.defer(200, this, [rowIndex, 1]);
  354. return false;
  355. }
  356. }, this);
  357. },
  358. // private
  359. onRender : function(){
  360. Ext.grid.PropertyGrid.superclass.onRender.apply(this, arguments);
  361. this.getGridEl().addClass('x-props-grid');
  362. },
  363. // private
  364. afterRender: function(){
  365. Ext.grid.PropertyGrid.superclass.afterRender.apply(this, arguments);
  366. if(this.source){
  367. this.setSource(this.source);
  368. }
  369. },
  370. /**
  371. * Sets the source data object containing the property data. The data object can contain one or more name/value
  372. * pairs representing all of the properties of an object to display in the grid, and this data will automatically
  373. * be loaded into the grid's {@link #store}. The values should be supplied in the proper data type if needed,
  374. * otherwise string type will be assumed. If the grid already contains data, this method will replace any
  375. * existing data. See also the {@link #source} config value. Example usage:
  376. * <pre><code>
  377. grid.setSource({
  378. "(name)": "My Object",
  379. "Created": new Date(Date.parse('10/15/2006')), // date type
  380. "Available": false, // boolean type
  381. "Version": .01, // decimal type
  382. "Description": "A test object"
  383. });
  384. </code></pre>
  385. * @param {Object} source The data object
  386. */
  387. setSource : function(source){
  388. this.propStore.setSource(source);
  389. },
  390. /**
  391. * Gets the source data object containing the property data. See {@link #setSource} for details regarding the
  392. * format of the data object.
  393. * @return {Object} The data object
  394. */
  395. getSource : function(){
  396. return this.propStore.getSource();
  397. },
  398. /**
  399. * Sets the value of a property.
  400. * @param {String} prop The name of the property to set
  401. * @param {Mixed} value The value to test
  402. * @param {Boolean} create (Optional) True to create the property if it doesn't already exist. Defaults to <tt>false</tt>.
  403. */
  404. setProperty : function(prop, value, create){
  405. this.propStore.setValue(prop, value, create);
  406. },
  407. /**
  408. * Removes a property from the grid.
  409. * @param {String} prop The name of the property to remove
  410. */
  411. removeProperty : function(prop){
  412. this.propStore.remove(prop);
  413. }
  414. /**
  415. * @cfg store
  416. * @hide
  417. */
  418. /**
  419. * @cfg colModel
  420. * @hide
  421. */
  422. /**
  423. * @cfg cm
  424. * @hide
  425. */
  426. /**
  427. * @cfg columns
  428. * @hide
  429. */
  430. });
  431. Ext.reg("propertygrid", Ext.grid.PropertyGrid);