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.

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